Reputation: 1087
When I create an instance if sc_module
I give it a string as a module name (sc_module_name
).
How can I get the name of the module that is currently running?
Upvotes: 5
Views: 7106
Reputation: 461
You can simply use name()
I used to use this to figure out which instance is doing what.
If that doesn't work it is because you need SC_HAS_PROCESS constructor instead of SC_CTOR
Upvotes: 1
Reputation: 411
This answer is based on the reply in this post.
You can simply use the method name()
provided by sc_object
(the base class of all sc_module) to get the hierarchical name of this module.
For example, I have a testbench tb_adder
which contains an Adder
as the submodule. Then in the sc_main()
function (or anywhere you can access the module), I can use the name()
method to get the name of each module.
The source code:
#include <systemc>
#include <iostream>
SC_MODULE(Adder) {
sc_core::sc_in<bool> clock;
// more ports definition here
void do_work() {
/*do some work here */
}
SC_CTOR(Adder) {
SC_CTHREAD(do_work, clock.pos());
}
};
SC_MODULE(tb_adder) {
sc_core::sc_in<bool> clock;
Adder *dut;
SC_CTOR(tb_adder) {
dut = new Adder("adder");
dut->clock(clock);
}
};
int sc_main(int argc, char* argv[]) {
sc_core::sc_clock clock ("my_clock", 1, 0.5);
tb_adder tb("tb_adder");
tb.clock(clock);
std::cout << "The name of the tb is: " << tb.name() << std::endl;
std::cout << "The name of the adder is: " << tb.dut->name() << std::endl;
return 0;
}
The output:
The name of the tb is: cool_tb_adder
The name of the adder is: cool_tb_adder.fun_adder
Upvotes: 0
Reputation: 28271
To get the name of the module that is currently running in systemc:
Use sc_get_current_process_b
to get the currently executing process (SC thread or method). Then use get_parent
to get its parent, which is going to be the module. Then use basename
or name
to get its name:
const char* name = sc_core::sc_get_current_process_b()->get_parent()->basename();
(omitted error handling for brevity)
Upvotes: 4
Reputation: 249
Don't use the built-in macro for the constructor. Use the following, assuming that module name is "counter":
counter(sc_module_name _name):sc_module(_name)
{
cout << "Creating object " << _name;
}
You can do various things with _name
after you include <string>
. You can use string()
, concatenate with the +
operator, etc.
Upvotes: 0