如何在 SystemC 中创建自定义频道?

How to create a custom channel in SystemC?

我正在尝试在系统 C 中创建自定义频道。频道数据结构如下

struct Command {

int cmdType;
int lba;
double timestamp;
int size;

Command() { }

Command(const int c, const int l, const double ts, const int sz) {
make(c, l, ts, sz);
}

void make(const int c, const int l, const double ts, const int sz) {
cmdType = c;
lba = l;
timestamp = ts;
size = sz;
}

inline bool operator ==(const Command & command) const {
return (command.cmdType == cmdType && command.lba == lba
    && command.timestamp == timestamp
    && command.size == size); }
};

此频道的 ostream 和 trace 函数定义如下

inline ostream & operator <<(ostream & os, const Command command)
{

   os << "CmdType " << command.cmdType << endl;
   os << "Lba " << command.lba << endl;
   os << "Time " << command.timestamp <<endl;
   os << "Data " << command.size << endl;

   return os;
}



inline void sc_trace(sc_trace_file * &tf, const Command & command, string &name)
{
    sc_trace(tf, command.cmdType, name + ".cmdType");
    sc_trace(tf, command.lba, name + ".lba");
    sc_trace(tf, command.timestamp, name + ".timestamp");
    sc_trace(tf, command.size, name + ".size");
}

但是我遇到了编译错误

报错信息很长,这里是最后一部分

/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:312:6: note:   candidate expects 4 arguments, 3 provided
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:317:6: note: void sc_core::sc_trace(sc_core::sc_trace_file*, const sc_core::sc_signal_in_if<short int>&, const string&, int)
 void sc_trace( sc_trace_file* tf,
      ^
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:317:6: note:   candidate expects 4 arguments, 3 provided
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:322:6: note: void sc_core::sc_trace(sc_core::sc_trace_file*, const sc_core::sc_signal_in_if<int>&, const string&, int)
 void sc_trace( sc_trace_file* tf,
      ^
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:322:6: note:   candidate expects 4 arguments, 3 provided
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:327:6: note: void sc_core::sc_trace(sc_core::sc_trace_file*, const sc_core::sc_signal_in_if<long int>&, const string&, int)
 void sc_trace( sc_trace_file* tf,
      ^
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:327:6: note:   candidate expects 4 arguments, 3 provided
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:343:1: note: void sc_core::sc_trace(sc_core::sc_trace_file*, const unsigned int&, const string&, const char**)
 sc_trace( sc_trace_file* tf,
 ^
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:343:1: note:   candidate expects 4 arguments, 3 provided
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:351:13: note: void sc_core::sc_trace(sc_core::sc_trace_file*, const void*, const string&)
 extern void sc_trace( sc_trace_file* tf,
             ^
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:351:13: note:   no known conversion for argument 2 from ‘const Command’ to ‘const void*’
In file included from /home/user/user/systemc-2.3.1//include/sysc/communication/sc_clock_ports.h:31:0,
                 from /home/user/user/systemc-2.3.1//include/systemc:79,
                 from /home/user/user/systemc-2.3.1//include/systemc.h:208,
                 from initiator.h:5,
                 from initiator.cpp:1:
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1808:1: note: template<class T> void sc_core::sc_trace(sc_core::sc_trace_file*, const sc_core::sc_inout<T>&, const string&)
 sc_trace( sc_trace_file* tf, const sc_inout<T>& port, 
 ^
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1808:1: note:   template argument deduction/substitution failed:
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1165:46: note:   ‘const Command’ is not derived from ‘const sc_core::sc_inout<T>’
      sc_trace( p->tf, iface->read(), p->name );
                                              ^
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1791:1: note: template<class T> void sc_core::sc_trace(sc_core::sc_trace_file*, const sc_core::sc_in<T>&, const string&)
 sc_trace(sc_trace_file* tf, const sc_in<T>& port, const std::string& name)
 ^
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1791:1: note:   template argument deduction/substitution failed:
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1165:46: note:   ‘const Command’ is not derived from ‘const sc_core::sc_in<T>’
      sc_trace( p->tf, iface->read(), p->name );
                                              ^

感谢有关如何解决此编译问题的帮助

这里是代码的简单版本---文件main.c

 #include <stdio.h>
 #include <csignal>
 #include "systemc.h"
 #include "producer.h"
 #include "consumer.h"
 #include "stdtype.h"
 #include <iomanip>
 #include <sstream>

 using namespace std;

 inline ostream & operator <<(ostream & os, const Command command)
 {
    os << "CmdType " << command.cmdType << endl;
    return os;
 }

 inline void sc_trace(sc_trace_file * tf, const Command & command, const string & name)
 {
    int* cmdType = (int*) &(command.cmdType);
    sc_trace(tf, cmdType, name + ".cmdType");
 }

 int sc_main(int arg_num, char *arg_vet[])
 {
    sc_clock clock("clock", 100, SC_PS);
    sc_signal <bool> reset;
    sc_signal <Command> cmd;
    Producer *prd;
    prd = new Producer("Producer");
    prd->clock(clock);
    prd->reset(reset);
    prd->cmd_tx(cmd);
    Consumer *con;
    con = new Consumer("Consumer");
    con->clock(clock);
    con->reset(reset);
    con->cmd_rx(cmd);
    sc_trace_file *tf = NULL;
    tf = sc_create_vcd_trace_file("trace");
    sc_trace(tf, reset, "reset");
    sc_trace(tf, clock, "clock");
    reset.write(1);
    sc_start(100, SC_NS);
    reset.write(0);
    sc_start(100, SC_NS);
    sc_close_vcd_trace_file(tf);
    return 0;
 }

文件consumer.h

 #ifndef __CONSUMER_H__
 #define __CONSUMER_H__

 #include <queue>
 #include <systemc.h>
 #include "stdtype.h"

 using namespace std;

 SC_MODULE(Consumer)
 {

    sc_in_clk clock;
    sc_in <bool> reset;

    sc_in <Command> cmd_rx;

    void ConsumerProcess();

    SC_CTOR(Consumer) {
    SC_METHOD(ConsumerProcess);
    sensitive << reset;
    sensitive << clock.pos();
    }

 };

#endif

文件producer.h

 #ifndef __PRODUCER_H__
 #define __PRODUCER_H__

 #include <queue>
 #include <systemc.h>
 #include "stdtype.h"

 using namespace std;

 SC_MODULE(Producer)
 {

    sc_in_clk clock;
    sc_in <bool> reset;

    sc_out <Command> cmd_tx;

    void ProducerProcess();

    SC_CTOR(Producer) {
    SC_METHOD(ProducerProcess);
    sensitive << reset;
    sensitive << clock.pos();
    }
 };

#endif

文件consumer.cpp

 #include "consumer.h"

 void Consumer:: ConsumerProcess(void)
 {
   cout << "con" << endl;
 }

文件producer.cpp

 #include "producer.h"

 void Producer:: ProducerProcess(void)
 {
   cout << "prd" << endl;
 }

和文件 stdtype.h

#ifndef __STDTYPE_H__
#define __STDTYPE_H__

#include <queue>
#include <systemc.h>
struct Command {

    int cmdType;

    inline bool operator ==(const Command & command) const {
    return (command.cmdType == cmdType); }
};

#endif

编译上面的代码这里是命令行:

g++ -I. -I$SYSTEMC_HOME/include -L. -L$SYSTEMC_HOME/lib-linux64 -Wl,-rpath=$SYSTEMC_HOME/lib-linux64 -I/usr/local/include -L/usr/local/lib -lyaml-cpp -o main main.cpp producer.cpp consumer.cpp -lsystemc -lm

您确定 sc_trace 功能正确吗? 为了调用成功,至少需要两种类型的重载函数。当然,我没想到隐式转换class。 无论如何,两个功能如下:

  1. sc_trace(sc_trace_file, int, string)
  2. sc_trace(sc_trace_文件,双精度,字符串)

`

    inline void sc_trace(sc_trace_file * &tf, const Command & command, string &name)
    {
        sc_trace(tf, command.cmdType, name + ".cmdType");     // 1
        sc_trace(tf, command.lba, name + ".lba");             // 1
        sc_trace(tf, command.timestamp, name + ".timestamp"); // 2
        sc_trace(tf, command.size, name + ".size");           // 1
    }

找到修复!!!

更改stdtype.h如下

#ifndef __STDTYPE_H__
#define __STDTYPE_H__

#include <queue>
#include <systemc.h>

#include <iomanip>
#include <sstream>

#include <map>
#include <utility>
#include <vector>
#include <string>

using namespace std;

struct Command {

    int cmdType;

    inline bool operator ==(const Command & command) const {
    return (command.cmdType == cmdType); }
};

inline ostream & operator <<(ostream & os, const Command & command)
{

    os << "CmdType " << command.cmdType << endl;

    return os;
}

inline void sc_trace(sc_trace_file * tf, const Command & command, const string & name)
{
    sc_trace(tf, command.cmdType, name + ".cmdType");
}

#endif

并从 main.cpp 中删除了这些功能,这解决了问题,我可以跟踪 vcd 文件中的频道,还添加了其他代码以通过频道进行通信