How to get instance name using method acting on it in SystemVerilog?

Is there any way to get the name of the object in which it works in SystemVerilog?

How implementation

object1.printName() 

should print a line

 object1 
+4
source share
4 answers

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

For modules, you can use the %m format specifier to display a hierarchical name. But for classes, exiting using %m shows the class type name, not the instance name. (At least it was an observable behavior with Incisive and Questa.) Also note that %m will include the name of the function if called from within the 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 result from %m is useful, you can write it to a string using $sformatf , and then change or do something with it.

+8
source

Instances have no names. Consider this code:

 someObject a someObject b initial begin a = new(); b = a; a.printName(); b.printName(); end 

a and b point to the same instance of someObject . We just created it. Therefore, both calls will have to report the same name, but you want them to report the name of the handle we were accessing. It is simply not possible.


OVM / UVM objects contain a member variable that contains the instance name. It can be set during construction or using set_name() . It can be read using get_name() . Even if the objects you are talking about are not OVM / UVM, you can use a similar system.

+4
source

For something high level, I'm not sure if there is anything that you describe.

However, there is a $typename system task. However, I do not know how this works with class objects. I didn’t need this before.

Usually what I see (and what I do seems to be useful) is to create a string stored in a class that is assigned by the constructor, which is the "name" of the object. Then I can use it when registering, so I know where the different messages came from. Of course, it depends on whether you are creating new variables with useful names.

+2
source

If you use OVM / UVM, then get_full_name () / get_name () will return the name of the component in the testbench hierarchy.

Objects are dynamic, so the question you are implicitly asking is invalid.

When someone wants to implement the names of the objects, what they will do is pass the "string name" in the constructor of each object, so when the object is new'd, the parent will say what the name is.

If you look at each component in OVM / UVM, you will see the constructor signature: function new (string name = "", ovm_component parent = null)

they are used by base classes to implement get_full_name () / get_name ()

+2
source

Source: https://habr.com/ru/post/1436453/


All Articles