2017-03-21 40 views
0

我寫了一個簡單的程序,它將數據從一個模塊發送到另一個模塊,但它似乎不工作,我不知道爲什麼。這裏是我的代碼:SC_FIFO寫:第一次機會例外

Server.h

#include <iostream> 
#include "stdafx.h" 
using namespace std; 

SC_MODULE(Server){ 
    sc_in <bool> clock; 
    sc_fifo_out <sc_uint<20> > writePath; 

    bool init_flag; 
    int numRobots; 

    void Server_Main(); 

    SC_CTOR(Server){ 
     init_flag = 0; 
     numRobots = 4; 
     SC_METHOD(Server_Main){ sensitive << clock.pos(); } 
    } 
}; 

Server.cpp

#include "stdafx.h" 
#include "Server.h" 

void Server::Server_Main(){ 
    if (init_flag == 0){ 
     int robotPath[4][5] = { 
      { 1, 2, 3, 4, 1 }, 
      { 2, 1, 6, 3, 4 }, 
      { 3, 2, 9, 5, 1 }, 
      { 4, 1, 6, 8, 7 } 
     }; 

     //Write robot path to Sensor 
     for (int i = 0; i < numRobots; i++){ 
      for (int j = 0; j < 5; j++){ 
       cout << "SERVER MODULE: Write Robot Paths " << i << ": " << robotPath[i][j] << endl; 
       writePath.write(robotPath[i][j]); 
      } 
     } 
     init_flag = 1; 
    } 
    else{ sc_stop(); } 
} 

Sensor.h

#include <iostream> 
#include "stdafx.h" 
using namespace std; 

SC_MODULE(Sensor){ 
    //sc_in <bool> clock; 
    sc_fifo_in <sc_uint<20> > readPath; 

    int robotPath[4][5]; 

    void loadPath(); 
    //void Sensor_Main(); 

    SC_CTOR(Sensor){ 
     //SC_METHOD(Sensor_Main){ sensitive << clock.pos(); } 
     SC_THREAD(loadPath){} 
    } 
}; 

Sensor.cpp

#include "stdafx.h" 
#include "Sensor.h" 

void Sensor::loadPath(){ 
    int i = 0; 
    int j = 0; 
    while (1){ 
     robotPath[i][j] = readPath.read(); 
     cout << "SENSOR MODULE: Read Robot " << i << " Path " << j << " : " << robotPath[i][j] << endl; 
     if (j == 4){ 
      j = 0; //Set index to beginning of array 
      i++; //Move to next robot 
     } 
     else{ j++; } //Continue loading path for current robot 
    } 
} 

Main.cpp的

#include "stdafx.h" 
#include <iostream> 

#include "Sensor.h" 
#include "Server.h" 

using namespace std; 

int sc_main(int argc, char* argv[]) 
{ 
    sc_clock clock("sysclock", 50, SC_MS, .5); 
    sc_fifo <sc_uint<20> > robotPath; 

    Sensor sensor_mod("Sensor"); 
    sensor_mod.readPath(robotPath); 

    Server server_mod("Server"); 
    server_mod.clock(clock); 
    server_mod.writePath(robotPath); 

    sc_start(); 

    return 0; 
} 

這裏是我的輸出是什麼樣子:

enter image description here

這裏的錯誤,我從VS2013得到: enter image description here

程序似乎當拋出一個異常它試圖寫入robotPath[3][1]到fifo,但我不知道爲什麼。我指定我的fifo大小爲20,這樣它可以一次存儲20個值,但我不發送超過20個值,並且當我嘗試寫入第17個值時發生此異常,因此我可能會誤解使用sc_fifo_out。這可能是一些顯而易見的錯誤,我可以忽略,但現在已經被燒燬了,並且無法弄清楚它在搞什麼。

任何幫助表示讚賞,謝謝。

回答

1

您是否試圖使用SystemC建模硬件設計或嘗試使用SystemC進行軟件建模?
看來你混淆了上下文。

這裏是指針,你需要考慮的一個列表:

  • 在服務器模塊Server_Main()應該被註冊爲SC_THREAD:

    SC_THREAD(Server_Main); 
    sensitive << clk.pos(); 
    

    由於您使用sc_fifo的寫入方法,內部調用wait(),在SystemC中使用SC_METHOD中的等待是非法的。

  • 修改Server.cpp如下所述:

    void Server::Server_Main() { 
        int robotPath[4][5] = {{ 1, 2, 3, 4, 1 }, 
             { 2, 1, 6, 3, 4 }, 
             { 3, 2, 9, 5, 1 }, 
             { 4, 1, 6, 8, 7 }}; 
        //Write robot path to Sensor 
        for (int i = 0; i < numRobots; i++){ 
        for (int j = 0; j < 5; j++){ 
         cout << "SERVER MODULE: Write Robot Paths " << i << ": " << robotPath[i][j] << endl; 
         writePath.write(robotPath[i][j]); 
         wait(); //< Notice this wait statement. 
        } 
        } 
    } 
    
  • 在Sensor.cpp的while循環添加wait()的聲明。

  • 而且sc_fifo < sc_uint < 20>>沒有實例化一個sc_fifo與深度20像你想象的。
    它實際上是實例化sc_fifosc_uint < 20>作爲被用於模擬一個20位無符號整數的數據類型,和FIFO的默認深度爲16%的SystemC的規格。

    您可以實例化一個sc_fifo <>與深度20如下所述:

    sc_fifo<sc_uint<20> > robotPath("robotPath", 20); 
    

    注:你並不需要這樣做,因爲從SC_METHOD上述變化SC_THREAD也更新Server_Main將使此行爲失效。

+0

當你說從SC_METHOD轉換到SC_THREAD會使行爲無效時,你是什麼意思?如果我有一個線程寫入和另一個線程讀取,但只有讀取一次,我已經寫了20個樣本到fifo,我不需要這種行爲? – Javia1492

+0

@ Javia1492如果你需要這樣的行爲,那麼一旦fifo被寫入20個樣本,然後觸發Sensor :: loadPath()[thread]的執行,那麼你將不得不查看SystemC規範的靜態和動態靈敏度。 – AmeyaVS

相關問題