4

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?

yonigo
  • 987
  • 1
  • 15
  • 30

4 Answers4

4

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)

anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • thanks thats what i needed. BUT, is there any documentation on these functions? for example if i have some OS threads running in parallel to some sc_threads, and an OS thread calls these functions what will happen? – yonigo Aug 11 '13 at 06:07
  • I used [IEEE 1666](http://standards.ieee.org/getieee/1666/download/1666-2011.pdf) and documentation in code (`systemc-2.2.0/src/sysc/kernel/sc_simcontext.h`) – anatolyg Aug 11 '13 at 10:59
1

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

Yash Jain
  • 442
  • 7
  • 19
0

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
zehawk
  • 210
  • 1
  • 10
0

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
Xiang
  • 411
  • 4
  • 4