SC_FIFO 写:第一次机会异常

SC_FIFO write : First chance exception

我写了一个简单的程序,可以将二维数组中的数据从一个模块发送到另一个模块,但它似乎不起作用,我也不确定为什么。这是我的代码:

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

这是我的输出结果:

这是我从 VS2013 得到的错误:

程序似乎在尝试将 robotPath[3][1] 写入 fifo 时抛出异常,但我不确定为什么。我将我的 fifo 大小指定为 20,以便它可以一次存储 20 个值,但我发送的值不超过 20 个,当我尝试写入第 17 个值时会发生此异常,所以我可能误解了 sc_fifo_out 的使用.我可能忽略了一些明显的错误,但此时我有点筋疲力尽,无法弄清楚我搞砸了什么。

您是尝试使用 SystemC 对硬件设计进行建模还是尝试使用 SystemC 进行软件建模?
看来你把上下文搞混了

以下是您需要考虑的建议列表:

  • 在服务器模块中 Server_Main() 应该注册为 SC_THREAD:

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

    由于您正在使用 sc_fifo 的 write 方法,该方法在内部调用 wait(),因此在 SystemC 中,在 SC_METHOD 中使用 wait 是非法的。

  • 修改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_uint<20> 实例化一个 sc_fifo 作为数据类型,用于建模 20-位无符号整数,根据 SystemC 规范,fifo 的默认深度为 16。

    您可以实例化深度为 20 的 sc_fifo<>,如下所述:

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

    注意: 您不需要这样做,因为上面从 SC_METHOD 更改为 SC_THREAD 并且还更新了 Server_Main 将使此行为无效。