为 SystemC 模块定义自定义构造函数
Defining custom constructor for a SystemC module
我有一个如下所示的 SystemC 模块,我想将 "maps" 传递给构造函数。我该怎么做?
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];
}
}
};
例如,我想用 4 个不同的映射实例化此模块 4 次。
来自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.
因此,我认为您最好根本不使用 SC_CTOR
。我们公司的 SystemC 编码风格就是这样推荐的。
但有一个附带条件:如果您使用进程宏(SC_METHOD
或 SC_THREAD
),那么您还必须使用 SC_HAS_PROCESS
.
这是一个完整的示例,类似于您所追求的:
#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;
}
如果注释掉 SC_HAS_PROCESS
行,您将看到一串编译错误。
我有一个如下所示的 SystemC 模块,我想将 "maps" 传递给构造函数。我该怎么做?
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];
}
}
};
例如,我想用 4 个不同的映射实例化此模块 4 次。
来自SystemC Language Reference Manual:
The use of macro
SC_CTOR
is not obligatory. UsingSC_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.
因此,我认为您最好根本不使用 SC_CTOR
。我们公司的 SystemC 编码风格就是这样推荐的。
但有一个附带条件:如果您使用进程宏(SC_METHOD
或 SC_THREAD
),那么您还必须使用 SC_HAS_PROCESS
.
这是一个完整的示例,类似于您所追求的:
#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;
}
如果注释掉 SC_HAS_PROCESS
行,您将看到一串编译错误。