SystemC学习(3)— APB_SRAM的建模与测试
一、前言
二、APB_SRAM建模
编写APB_SRAM模型文件apb_sram.h
文件如下所示:
#ifndef __APB_SRAM_H
#define __APB_SRAM_H
#include "systemc.h"
const int ADDR_SIZE = 32;
const int DATA_SIZE = 32;
SC_MODULE(apb_sram){
sc_in<bool> apb_pclk;
sc_in<bool> apb_presetn;
sc_in<bool> apb_psel;
sc_in<bool> apb_penable;
sc_in<bool> apb_pwrite;
sc_in<sc_uint<ADDR_SIZE> > apb_paddr;
sc_in<sc_uint<DATA_SIZE> > apb_pwdata;
sc_out<sc_uint<DATA_SIZE> > apb_prdata;
sc_out<bool> apb_pready;
sc_out<bool> apb_pslverr;
sc_bv<DATA_SIZE> mem[4096];
SC_CTOR(apb_sram){
SC_METHOD(prc_apb_sram);
sensitive << apb_pclk.pos();
sensitive << apb_presetn.neg();
}
void prc_apb_sram(){
if(!apb_presetn){
apb_prdata = 0;
apb_pready = 0;
apb_pslverr = 0;
}
else if((apb_psel.read()==1) && (apb_penable.read()==0) && (apb_pwrite.read()==0)) {
apb_prdata = mem[(apb_paddr.read()>>2)];
}
else if((apb_psel.read()==1) && (apb_penable.read()==1) && (apb_pwrite.read()==0)) {
apb_pready = 1;
}
else if((apb_psel.read()==1) && (apb_penable.read()==1) && (apb_pwrite.read()==1)) {
mem[(apb_paddr.read()>>2)] = apb_pwdata;
apb_pready = 1;
}
else {
apb_prdata = apb_prdata;
apb_pready = 0;
}
}
};
#endif
三、测试平台
编写“driver.h”如下所示:
#ifndef __DRIVER_H
#define __DRIVER_H
#include "apb_sram.h"
SC_MODULE(driver){
sc_in<bool> apb_pclk;
sc_out<bool> apb_presetn;
sc_out<bool> apb_psel;
sc_out<bool> apb_penable;
sc_out<bool> apb_pwrite;
sc_out<sc_uint<ADDR_SIZE> > apb_paddr;
sc_out<sc_uint<DATA_SIZE> > apb_pwdata;
sc_in<sc_uint<DATA_SIZE> > apb_prdata;
sc_in<bool> apb_pready;
sc_in<bool> apb_pslverr;
SC_CTOR(driver){
SC_THREAD(prc_test);
sensitive << apb_pclk.pos();
}
void prc_test(){
apb_presetn.write(0);
apb_psel.write(0);
apb_penable.write(0);
apb_pwrite.write(0);
apb_paddr.write(0);
apb_pwdata.write(0);
wait();
wait();
apb_presetn.write(1);
wait();
wait();
// write
for(int i=0; i<16; i++) {
apb_psel.write(1);
apb_penable.write(0);
apb_pwrite.write(1);
apb_paddr.write(i*4);
apb_pwdata.write(i);
wait();
apb_penable.write(1);
wait();
while(apb_pready.read() != 1){
wait();
}
apb_psel.write(0);
apb_penable.write(0);
wait();
wait();
}
// read
for(int i=0; i<16; i++) {
apb_psel.write(1);
apb_penable.write(0);
apb_pwrite.write(0);
apb_paddr.write(i*4);
wait();
apb_penable.write(1);
wait();
while(apb_pready.read() != 1){
wait();
}
apb_psel.write(0);
apb_penable.write(0);
wait();
wait();
}
sc_stop();
}
};
#endif
四、测试运行
使用如下命令进行编译:
g++ main.cpp -I${SYSTEMC_HOME}/include/ -L${SYSTEMC_HOME}/lib-linux64 -lsystemc -o run.x
运行命令:
./run.x
打开test.vcd
文件可看到波形如下所示: