2016-06-10 34 views
1

我試圖在SystemC中模擬一個帶有CABA(Cycle Accurate/Bit Accurate)模型的模塊,該模型添加了兩個數字。它有下列信號:在SystemC中的測試臺模塊中管理信號

模塊addition_CABA

  • a:用於添加輸入號碼。
  • b:添加的輸入號碼。
  • clk:時鐘輸入。
  • valid:當輸入ab可用時輸入信號變爲1。
  • result:輸出信號包含結果a + b
  • ready:當result準備就緒時,輸出信號變爲1。

爲了測試是否從該模塊的結果是正確的我已經創建了一個testbench模塊,其具有下列信號:

模塊testbench

  • result_tb:輸入信號接收來自addition_CABA模塊的結果信號。
  • ready_tb:接收來自addition_CABA模塊的就緒信號的輸入信號。
  • clk_tb:時鐘輸入信號。兩個模塊都是一樣的。
  • rst_tb:重置輸入信號。兩個模塊都是一樣的。
  • a_tb:向模塊發送數字a的輸出信號。
  • b_tb:將模塊b發送給addition_CABA模塊的輸出信號。
  • valid_tb:向addition_CABA模塊發送有效信號的輸出信號。

我正在做的試驗如下:

  • 內生成的隨機數對,得到值來ab模塊testbench
  • 模塊在某些時鐘週期後計算結果。
  • 測試臺直接執行加法操作並將其與模塊產生的結果進行比較。
  • 這些操作在重複五次的循環內。

我遇到的問題是,當我運行仿真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

#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++; 
     } 
    } 
} 

回答

0

的問題的根本原因是以下物質:

  1. main.cpp功能仿真時間過短(10納秒,我修改到500納秒)。
  2. 由於測試臺模塊中的測試功能是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++; 
     } 
    } 
}