我试图在SystemC中模拟一个带有CABA (循环精确/位精确)模型的模块,它增加了两个数字。它有以下信号:
addition_CABA 模块
a:添加的输入号。b:添加的输入号。clk:时钟输入。valid:当输入a和b可用时,输入信号变为1。result:包含a + b结果的输出信号。ready:当result准备就绪时,输出信号变为1。为了测试该模块的结果是否正确,我创建了一个具有以下信号的testbench模块:
testbench 模块
result_tb:从addition_CABA模块接收结果信号的输入信号。ready_tb:从addition_CABA模块接收就绪信号的输入信号。clk_tb:时钟输入信号。这两个模块都是一样的。rst_tb:复位输入信号。这两个模块都是一样的。a_tb:将数字a发送到addition_CABA模块的输出信号。b_tb:将数字b发送到addition_CABA模块的输出信号。valid_tb:向addition_CABA模块发送有效信号的输出信号。我正在做的测试如下:
testbench中生成一对随机数,为a和b提供值。我遇到的问题是,当我运行模拟时,testbench给出了正确的结果,addition_CABA显示了结果,但是在一些时钟周期之后,所以比较是在两个不同的数字之间。此外,当程序结束时,我会收到一条Segmentation fault (core dumped)消息。我试图找出的是如何指示testbench,直到结果准备好(用信号ready_tb),这样它就可以用正确的数字进行比较。在开始测试之前,我尝试了一个while(!ready_tb.read()) wait();条件,但是当这样做的时候,程序就结束了,模拟永远不会启动。
在main.cpp文件中,我只是在模块之间进行连接,生成时钟并将rst信号设置为0。下面是我的代码:
addition_CABA.h
#include <systemc.h>
//Module which adds two numbers
SC_MODULE(addition_CABA){
sc_in< sc_uint<8> > a;
sc_in< sc_uint<8> > b;
sc_in<bool> clk;
sc_in<bool> valid;
sc_in<bool> rst;
sc_out<bool> ready;
sc_out< sc_uint<8> > result;
int addcaba(int a, int b){
int c;
c = a+b;
wait(3);
return c;
}
void loop();
SC_CTOR(addition_CABA){
SC_CTHREAD(loop, clk.pos());
async_reset_signal_is(rst, true);
}
};addition_CABA.cpp
void addition_CABA::loop(){
ready.write(0);
result.write(0);
if(rst){
ready.write(0);
result.write(0);
}
else{
while(1){
while (!valid.read()) wait();
result.write(addcaba(a.read(),b.read()));
ready.write(1);
wait();
ready.write(0);
}
}
}testbench.h.h
#include <systemc.h>
SC_MODULE(testbench){
sc_in< sc_uint<8> > result_tb;
sc_in<bool> ready_tb;
sc_in<bool> clk_tb;
sc_in<bool> rst_tb;
sc_out< sc_uint<8> > a_tb;
sc_out< sc_uint<8> > b_tb;
sc_out<bool> valid_tb;
void test();
SC_CTOR(testbench){
SC_CTHREAD(test, clk_tb.pos());
async_reset_signal_is(rst_tb, true);
}
};testbench.cpp
void testbench::test(){
uint8_t c = 0;
int k = 0;
if (rst_tb){
c = 0;
k = 0;
cout << "\nReset on!\n" << endl;
}
else{
//while(!ready_tb.read()) wait(); //when using this condition the simulation never starts
while(k < 5){
a_tb.write( (1 + rand() % (128-1)) );
b_tb.write( (1 + rand() % (128-1)) );
valid_tb.write(1);
sc_start(10, SC_NS);
valid_tb.write(0);
sc_start(10, SC_NS);
cout << "\nTest number " << k+1 << endl;
cout << "\ta = " << a_tb.read() << " and b = " << b_tb.read() << endl;
cout << "\tAddition of " << a_tb.read() << " and " << b_tb.read();
cout << " = " << result_tb.read() << endl;
c = a_tb.read() + b_tb.read();
if ( result_tb.read() != c ){
cout << "Real result = " << a_tb.read() + b_tb.read();
cout << " and result with module = " << result_tb.read() << endl;
cout << "Wrong result\n" << endl;
// exit(1);
}
else cout << "Result OK\n" << endl;
k++;
}
}
}发布于 2016-06-10 23:46:03
造成这个问题的根本原因如下:
main.cpp函数的模拟时间太短(10 ns,我修改为500 ns)。SC_CTHREAD,所以它必须在一个无限循环中。以前的实现是完全错误的,我认为这也是Segmentation fault (core dumped)消息的根本原因。此外,重复测试五次的循环是不必要的,因为迭代次数与模拟时间有关(设置在main.cpp函数中)。下面是修正后的testbench.cpp代码:
void testbench::test(){
uint8_t c = 0;
int k = 0;
if (rst_tb){
c = 0;
k = 0;
cout << "\nReset on!\n" << endl;
}
else{
while(1){
a_tb.write( (1 + rand() % (128-1)) );
b_tb.write( (1 + rand() % (128-1)) );
valid_tb.write(1);
wait();
valid_tb.write(0);
wait();
while(!ready_tb.read()) wait();//This condition waits until ready_tb = true to continue the simulation
cout << "\nTest number " << k+1 << endl;
cout << "\ta = " << a_tb.read() << " and b = " << b_tb.read() << endl;
cout << "\tAddition of " << a_tb.read() << " and " << b_tb.read();
cout << " = " << result_tb.read() << endl;
c = a_tb.read() + b_tb.read();
if ( result_tb.read() != c ){
cout << "Real result = " << a_tb.read() + b_tb.read();
cout << " and result with module = " << result_tb.read() << endl;
cout << "Wrong result\n" << endl;
exit(1);
}
else cout << "Result OK\n" << endl;
k++;
}
}
}https://stackoverflow.com/questions/37754450
复制相似问题