我寫了一個簡單的程序,它將數據從一個模塊發送到另一個模塊,但它似乎不工作,我不知道爲什麼。這裏是我的代碼: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;
}
這裏是我的輸出是什麼樣子:
程序似乎當拋出一個異常它試圖寫入robotPath[3][1]
到fifo,但我不知道爲什麼。我指定我的fifo大小爲20,這樣它可以一次存儲20個值,但我不發送超過20個值,並且當我嘗試寫入第17個值時發生此異常,因此我可能會誤解使用sc_fifo_out
。這可能是一些顯而易見的錯誤,我可以忽略,但現在已經被燒燬了,並且無法弄清楚它在搞什麼。
任何幫助表示讚賞,謝謝。
當你說從SC_METHOD轉換到SC_THREAD會使行爲無效時,你是什麼意思?如果我有一個線程寫入和另一個線程讀取,但只有讀取一次,我已經寫了20個樣本到fifo,我不需要這種行爲? – Javia1492
@ Javia1492如果你需要這樣的行爲,那麼一旦fifo被寫入20個樣本,然後觸發Sensor :: loadPath()[thread]的執行,那麼你將不得不查看SystemC規範的靜態和動態靈敏度。 – AmeyaVS