# How to get type name on systemVerilog

**URL:** <https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, system-verilog-interface-generate\
**Created:** [June 23, 2023, 3:00pm UTC](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865 "2023-06-23T15:00:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoussamSABOUR](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/d6d6ee/32.png) [@HoussamSABOUR](https://verificationacademy.com/forums/u/HoussamSABOUR)\
**Post date:** [June 23, 2023, 3:00pm UTC](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865/1 "2023-06-23T15:00:04Z")

</div>

I want to get type name for my\_interface as showed :

```verilog
initial begin 
    
    $display ("%s",(typename(i_top_tb.master_interface))) ; 
    $display ("%s",(typename(i_top_tb.slave_interface))) ; 

  end

```

Thanks

---

<div class="post-metadata">

**Author:** ![Venkateshwara\_Rao](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/6bbea6/32.png) [@Venkateshwara\_Rao](https://verificationacademy.com/forums/u/Venkateshwara_Rao)\
**Post date:** [June 24, 2023, 5:58am UTC](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865/2 "2023-06-24T05:58:37Z")

</div>

_In reply to [uvm\_share](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865):_

You can use VPI to get the name of the module or interface.  
Here is an example you can use and works on questa:

test.sv:

```verilog
interface if1();
endinterface
interface if2();
endinterface

module top;
import "DPI-C" context task get_if_name(string scope);
  if1 master();
  if2 slave();
  initial begin
    get_if_name("top.master");
    get_if_name("top.slave");
  end
endmodule

```

test.c:

```auto
#include &lt;sv_vpi_user.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
extern int get_if_name(char *scope);
int get_if_name(char *scope) {
  vpiHandle h = vpi_handle_by_name(scope, NULL);
  if(h) {
    printf("If Name for %s is: %s\n", scope, vpi_get_str(vpiDefName, h));
  }
  return 0;
}

```

Compile and run Makefile:

> all: test.so  
> gcc -c -fPIC -I{QUESTA\_SIMULATOR\_HOME}/include test.c -o test.o  
> gcc -fPIC -shared test.o -o test.so   
> vlog -64 test.sv  
> vopt -64 top +acc -o top\_opt  
> vsim -64 -c -do “run -all; quit -f” top\_opt -sv\_lib test

Output:

> # If Name for top.master is: if1

# If Name for top.slave is: if2

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 24, 2023, 10:14pm UTC](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865/3 "2023-06-24T22:14:54Z")

</div>

_In reply to [uvm\_share](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865/2):_

It would help to show a complete example of what you tried and what you are expecting.

---

<div class="post-metadata">

**Author:** ![HoussamSABOUR](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/d6d6ee/32.png) [@HoussamSABOUR](https://verificationacademy.com/forums/u/HoussamSABOUR)\
**Post date:** [June 26, 2023, 9:58am UTC](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865/4 "2023-06-26T09:58:29Z")

</div>

```verilog
module top_tb ; 
  import "DPI-C" context task get_if_name(string scope);
  
  initial begin 
    get_if_name ( "top_tb.master_interface") ; 
    get_if_name ( "top_tb.slave_interface") ; 
  end 
endmodule

```

When I try this, i have thios fatal error :  
_Null foreign function pointer encountered when calling ‘get\_if\_name’_ **\_\_**

what is the solution in this case ?  
Thanks in adavance

---

<div class="post-metadata">

**Author:** ![Venkateshwara\_Rao](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/6bbea6/32.png) [@Venkateshwara\_Rao](https://verificationacademy.com/forums/u/Venkateshwara_Rao)\
**Post date:** [June 26, 2023, 11:03am UTC](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865/5 "2023-06-26T11:03:06Z")

</div>

_In reply to [uvm\_share](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865/4):_

What is the simulator command line you are using?  
It works with these commands:  
vlog test.sv test.c  
vopt top -o top\_opt  
vsim -c -do “run -all; quit -f” top\_opt

---

<div class="post-metadata">

**Author:** ![HoussamSABOUR](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/d6d6ee/32.png) [@HoussamSABOUR](https://verificationacademy.com/forums/u/HoussamSABOUR)\
**Post date:** [June 26, 2023, 11:13am UTC](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865/6 "2023-06-26T11:13:55Z")

</div>

_In reply to [Venkateshwara Rao](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865/5):_

I’m using Questa, and i do these commands but i have always same fatal error

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 26, 2023, 7:32pm UTC](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865/7 "2023-06-26T19:32:21Z")

</div>

_In reply to [uvm\_share](https://verificationacademy.com/forums/t/how-to-get-type-name-on-systemverilog/41865/6):_

It would help to show a complete example of what you tried and what you are expecting to do with the returned strings.

You shouldn’t need VPI for this.
