Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining custom constructor for a SystemC module

Tags:

c++

systemc

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.

like image 691
user3825711 Avatar asked Jan 27 '15 16:01

user3825711


1 Answers

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.

like image 162
DarrylLawson Avatar answered Sep 30 '22 16:09

DarrylLawson