3

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

e19293001
  • 2,783
  • 9
  • 42
  • 54
  • What do you mean "name an array"? Isn't it "initialize an array"? – songyuanyao Feb 16 '16 at 06:39
  • If you want to initialize an array you can write `SC_CTOR(example) :clk("clk"), mysignals{some_value1, some_value2} {}`. – songyuanyao Feb 16 '16 at 07:03
  • Thanks for your suggestion. I tried it though still won't work. I've put an update to my question. Here is [a short intro to systemc](http://www.asic-world.com/systemc/first1.html#Hello_World_Program_Output) – e19293001 Feb 16 '16 at 07:35
  • There is no good solution for this in SystemC that I know of. You can potentially restructure and have arrays of pointers and new in the items one at a time with the appropriate name. – Ifor Feb 16 '16 at 12:19

3 Answers3

5

As soon as I posted 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
toolic
  • 57,801
  • 17
  • 75
  • 117
systemcpro
  • 856
  • 1
  • 7
  • 15
3

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
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
systemcpro
  • 856
  • 1
  • 7
  • 15
  • 1
    Actually it produces THIS output :) 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 – systemcpro Feb 21 '16 at 11:21
0

with C++17 also works:

sc_signal<bool> mysignals[2] {sc_signal<bool>{"sig1"},sc_signal<bool>{"sig2"}};

see also: C++ copy elision / RVO - return value optimization