编写D触发器模型文件dff.h文件如下所示:
#ifndef __DFF_H
#define __DFF_H
#include "systemc.h"
const int DSZIE = 8;
SC_MODULE(dff){
sc_in<bool> clk;
sc_in<bool> reset;
sc_in<sc_uint<DSIZE> > data_in;
sc_in<sc_uint<DSIZE> > data_out;
SC_CTOR(dff){
SC_METHOD(prc_dff);
sensitive << clk.pos << reset.pos;
}
void prc_dff(){
if(reset)
data_out = 0;
else
data_out = data_in;
}
};
#endif
编写“driver.h”如下所示:
#ifndef __DRIVER_H
#define __DRIVER_H
#include "dff.h"
SC_MODULE(driver){
sc_in<bool> clk;
sc_in<sc_uint<DSIZE> > data_out;
sc_out<bool> reset;
sc_out<sc_uint<DSIZE> > data_in;
SC_CTOR(driver){
SC_THREAD(prc_test);
sensitive << clk.pos();
SC_METHOD(prc_check)
sensitive << data_out;
}
void prc_test(){
reset.write(1);
wait();
wait();
reset.write(0);
data_in.write(1);
wait();
data_in.write(2);
wait();
data_in.write(3);
wait();
data_in.write(4);
wait();
data_in.write(5);
wait();
data_in.write(6);
wait();
data_in.write(7);
wait();
data_in.write(8);
wait();
data_in.write(9);
wait();
data_in.write(10);
wait();
sc_stop();
}
void prc_check(){
cout << "Output data is(@" << sc_time_stamp() << "): " << data_out.read() << endl;
}
};
#endif
编写“main.cpp”文件如下所示:
#include "driver.h"
int sc_main(int argc, char *argv[]){
sc_clock clk("clk", 2, SC_NS);
sc_signal<bool> reset;
sc_signal<sc_uint<DSIZE> > data_in, data_out;
driver drv("drv");
drv.clk(clk);
drv.reset(reset);
drv.data_out(data_out);
drv.data_in(data_in);
dff dut("dut");
dut.clk(clk);
dut.reset(reset);
dut.data_out(data_out);
dut.data_in(data_in);
sc_start();
return 0;
}
使用如下命令进行编译:
g++ main.cpp -I${SYSTEMC_HOME}/include/ -L${SYSTEMC_HOME}/lib-linux64 -lsystemc -o run.x
运行命令:
./run.x
运行结果如下所示:
