4

I have a SystemC module as below and I want to pass on "maps" to the constructor. How can I do it?

struct Detector: sc_module
{
    map <int,int> int_map;

    SC_CTOR(Detector)
    {
        for (int i = 0 ; i<10; i++)
        {
          int_map[i]= map[i][0];
        }
    }
};

For example, I want to instantiate this module 4 times with 4 different maps.

user3825711
  • 43
  • 1
  • 4
  • Would it be sufficient to add a parameter to the constructor? This seems the easiest solution, but it's too easy - does anything prevent you from doing this? – anatolyg Jan 27 '15 at 17:26
  • I would use a normal constructor and not the 'SC_CTOR'. And, as anatolyg said, pass the parameter to that constructor. I think you still have to add 'SC_HAS_PROCESS(Detector) to the code. – vermaete Jan 28 '15 at 07:16
  • Thanks guys. Yes using a normal constructor worked – user3825711 Jan 29 '15 at 10:03

1 Answers1

6

From the SystemC Language Reference Manual:

The use of macro SC_CTOR is not obligatory. Using SC_CTOR, it is not possible to add user-defined arguments to the constructor. If an application needs to pass additional arguments, the constructor shall be provided explicitly. This is a useful coding idiom.

Because of that, I think you're better off not using SC_CTOR at all. Our company's SystemC coding style recommends just that.

There is one proviso though: if you use the process macros (SC_METHOD or SC_THREAD) then you must also use SC_HAS_PROCESS.

Here's a complete example of something like what you are after:

#include <systemc>
#include <map>
#include <vector>

using namespace std;
using namespace sc_core;

struct Detector : public sc_module {

    typedef map<int, vector<int> > input_map_t;

    Detector(sc_module_name name, input_map_t& input_map)
        : sc_module(name)
    {
        for (int i = 0; i < input_map.size(); i++) {
            int_map[i] = input_map[i][0];
        }
        SC_METHOD(process);
    }

    void process() {}

    map<int, int> int_map;
    SC_HAS_PROCESS(Detector);
};

int sc_main(int argc, char *argv[]) {
    Detector::input_map_t input_map;
    for (int i = 0; i < 10; i++) {
        input_map[i] = vector<int>();
        input_map[i].push_back(i);
    }

    Detector d("d", input_map);
    return EXIT_SUCCESS;
}

If you comment out the SC_HAS_PROCESS line, you'll see a string of compilation errors.

DarrylLawson
  • 732
  • 3
  • 9
  • Thank you Darry ! It works. How can I do the same with a 2d array. Because I have 4 arrays of different dimensions,but the second dimension is fixed as 2. I am trying to take 4 different 2d arrays( of type uint64 )through the constructor and use it in a function during runtime.But I get an error saying that dimensions should be fixed and could not access the values from the arrays. – user3825711 Jan 29 '15 at 10:02
  • And I am using C++ 98. – user3825711 Jan 29 '15 at 10:27
  • I am not clear on what you are asking here. What data type are you using for the 2d arrays? I recommend using std::vector. If you like, perhaps post a separate question with an example code snippet. It sounds like a pure C++ question. – DarrylLawson Jan 29 '15 at 19:29