C++ SystemC中测试台模块内的信号管理
我试图在SystemC中模拟一个模块,该模块使用一个CABA(周期精确/位精确)模型,该模型添加了两个数字。它具有以下信号: 模块C++ SystemC中测试台模块内的信号管理,c++,segmentation-fault,systemc,test-bench,C++,Segmentation Fault,Systemc,Test Bench,我试图在SystemC中模拟一个模块,该模块使用一个CABA(周期精确/位精确)模型,该模型添加了两个数字。它具有以下信号: 模块添加\u CABA a:输入添加的编号 b:输入添加的编号 clk:时钟输入 valid:当输入a和b可用时变为1的输入信号 result:包含a+b结果的输出信号 就绪:当结果就绪时变为1的输出信号 为了测试此模块的结果是否正确,我创建了一个测试台模块,该模块具有以下信号: 模块测试台 result\u tb:从addition\u CABA模块接收结果信号的
添加\u CABA
:输入添加的编号a
:输入添加的编号b
:时钟输入clk
:当输入valid
和a
可用时变为1的输入信号b
:包含result
+a
结果的输出信号b
:当就绪
就绪时变为1的输出信号结果
测试台
模块,该模块具有以下信号:
模块测试台
:从result\u tb
模块接收结果信号的输入信号addition\u CABA
:从ready\u tb
模块接收就绪信号的输入信号添加\u CABA
:时钟输入信号。这两个模块都是一样的clk_tb
:重置输入信号。这两个模块都是一样的rst\u tb
:将数字a发送到a\u tb
模块的输出信号加法\u CABA
:将数字b发送到b_tb
模块的输出信号addition_CABA
:将有效信号发送到valid\u tb
模块的输出信号加法\u CABA
- 在模块
中,生成一对随机数,为testbench
和a
赋值b
- 模块在一些时钟周期后计算结果
- 测试台直接执行添加操作,并将其与模块生成的操作进行比较
- 这些动作在一个循环中重复五次
testbench
时,会给出正确的结果,addition\u CABA
会显示结果,但会延迟一些时钟周期,因此比较是在两个不同的数字之间进行的。此外,当程序结束时,我有一条分段错误(堆芯转储)
消息。我试图弄明白的是,如何指示testbench
等待结果准备就绪(使用信号ready_tb
),以便它可以使用正确的数字进行比较。我在(!ready_tb.read())wait()时尝试了开始测试前的代码>条件,但执行此操作时,程序结束,模拟从未开始
在main.cpp
文件中,我只是在进行模块之间的连接,生成时钟并将rst
信号设置为0
。下面是我的代码:
添加\u 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);
}
};
#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.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);
}
};
#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);
}
};
#包括
SC_模块(测试台){
结果tb中的SCU;
sc_in ready_tb;
以时钟为单位的sc_tb;
sc_在rst_tb中;
sc_outa_tb;
sc_outb_tb;
sc_输出有效的_tb;
无效试验();
高级工程师(试验台){
SC_CTHREAD(test,clk_tb.pos());
异步复位信号为(rst_tb,真);
}
};
testbench.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);
}
}
}
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++;
}
}
}
void testbench::test(){
uint8_t c=0;
int k=0;
如果(rst_tb){
c=0;
k=0;
cout问题的根本原因如下:
main.cpp
函数中的模拟时间太短(10 ns,我修改为500 ns)
由于testbench模块中的test函数是一个SC_CTHREAD
,它必须在一个无限循环中。以前的实现是完全错误的,我认为这也是分段错误(内核转储)
消息的根本原因
此外,不需要重复测试五次的循环,因为迭代次数与模拟时间有关(在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++;
}
}
}
void testbench::test(){
uint8_t c=0;
int k=0;
如果(rst_tb){
c=0;
k=0;
库特