Search code examples
verilogsystem-verilog

Is there a way to get the name a Verilog module was instantiated with?


I'd like to do some simulation debugging in Verilog and want to add $display to some modules. However, I want to be able to track the output a single module provides. I'd like to be able to get the name that the current module was instantiated with to add to the $display.

For example:

module adder (...)
    ...
    $display($magic_instance_name, " ", in0, " + ", in1);
    ...

adder memory_address (...);
adder fifo_nvals (...);

And then the output would look like:

memory_address 100 + 8
fifo_nvals 3 + 1
...

One way I could do this is to add an instance_name parameter to each module. Is there an easier way?


Solution

  • You can use %m to get the full path including the unique instance name:

    module tb;
        adder memory_address ();
        adder fifo_nvals ();
        initial #5 $finish;
    endmodule
    
    module adder;
        initial $display("   hello from ... %m");
    endmodule
    

    Prints:

       hello from ... tb.memory_address
       hello from ... tb.fifo_nvals