How do I get name of an instance using a method operating on it in SystemVerilog?

Jean picture Jean · Sep 26, 2012 · Viewed 18.6k times · Source

Is there any way a method can get name of the object it operates on in SystemVerilog ?

Like implementing

object1.printName()  

should print the string

object1

Answer

dwikle picture dwikle · Sep 27, 2012

No, there is no mechanism in the language for doing this for classes.

For modules, you can use the %m format specifier for displaying a hierarchical name. But for classes, the output using %m shows the class type name, not the instance name. (At least that was the observed behavior with Incisive and Questa.) Also note that %m will include the the function name if called from within a function.

Example:

module test;

  // Print %m in a module
  function void printName();
    $display("%m");
  endfunction

  class foo;

    // Print %m in a class
    virtual function void printName();
      $display("%m");
    endfunction

  endclass

  foo foo_inst = new;

endmodule

module top;
   test test_inst();

   initial begin
     test_inst.foo_inst.printName();
     test_inst.printName();
   end
endmodule

Output:

top.test_inst.foo.printName
top.test_inst.printName

If the output from %m is useful, you can capture it in a string using $sformatf and then modify or do whatever with it.