How to initialize a systemc port name which is an array?

强颜欢笑 提交于 2019-12-13 16:06:37

问题


I wanted to initialize a port name. The port is an array and my code does not work.

SC_MODULE(example) {
  sc_clock clk;
  sc_signal<bool> mysignals[2];

 public: 

 SC_CTOR(example)
   :clk("clk"),
    mysignals[0]("mysignals[0]"), // won't work
    mysignals[1]("mysignals[1]") // won't work
      {}

  ~example() {
  }
};

The code below would work by giving clk with a name "clk". However clk port is not an array:

SC_MODULE(example) {
  sc_clock clk;

 public: 

 SC_CTOR(example)
   :clk("clk")
      {}

  ~example() {
  }
};

How do I name an array of ports?

UPDATE:

Tried the comment suggested. Still won't work:

#include "systemc.h"

SC_MODULE(example) {
  sc_clock clk;
  sc_signal<bool> mysignals[2];

 public: 

  SC_CTOR(example)
    :clk("clk"),
    mysignals{"mysig1", "mysig2"}
  {}

  ~example() {
  }
};

int sc_main(int argc, char* argv[]) {
  example hello("HELLO");

  return(0);
}

Compiled with:

g++ -I. -I<SYSTEMC LIB>/include -L. -L<SYSTEMC LIB>/lib-linux64 -o sim example.cpp -lsystemc -lm -std=c++0x

Error:

example.cpp: In constructor ‘example::example(sc_core::sc_module_name)’: example.cpp:11: error: bad array initializer


回答1:


Two choices: 1) Create an array of signals and let systemc name them 2) An array of signal pointers name as we construct them

Example code:

SC_MODULE(M){

static const int SIZE = 4;

sc_signal<bool> S[SIZE];    //array of signals let sc name them
sc_signal<bool>* P[SIZE];   //array of pointers name on create

SC_CTOR(M){

    for(int i = 0; i < SIZE; ++i)   //new the signals and give them a name
        P[i] = new sc_signal<bool>(("my_sig_" + to_string(i)).c_str());
}

};

int sc_main(int, char**){

M m("m");

for(int i = 0; i < M::SIZE; ++i){
    cout << "S[" << i << "].name = " << m.S[i].basename() << '\n';
    cout << "P[" << i << "].name = " << m.P[i]->basename() << '\n';
}
return 0;

}

Produces the following output on my machine

P[0].name = signal_0
P[0].name = my_sig_0
S[1].name = signal_1
P[1].name = my_sig_1
S[0].name = signal_0
P[0].name = my_sig_0
S[1].name = signal_1
P[1].name = my_sig_1
S[2].name = signal_2
P[2].name = my_sig_2
S[3].name = signal_3
P[3].name = my_sig_3



回答2:


No sooner had i sent the answer i remembered option 3! Use sc_vector. For example:

SC_MODULE(M){

     static const int SIZE = 4;

     typedef sc_uint<16> DataType;
     typedef sc_in<DataType> PortType;
     typedef sc_vector<PortType> PortVectorType;

     PortVectorType port_vec;

     SC_CTOR(M) : port_vec("my_port", SIZE){
        for(int i = 0; i < SIZE; ++i)
           cout << port_vec[i].basename() << '\n';
    }       
};

int sc_main(int, char**){
   M("m");
   return 0;
}

Produces the following output my_port_0 my_port_1 my_port_2 my_port_3



来源:https://stackoverflow.com/questions/35425052/how-to-initialize-a-systemc-port-name-which-is-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!