# How to connect DPI systemverilog to class member

**URL:** <https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816>\
**Category:** UVM\
**Tags:** UVM, DPI\
**Created:** [November 15, 2021, 12:35pm UTC](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816 "2021-11-15T12:35:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eranv](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/e47c2d/32.png) [@eranv](https://verificationacademy.com/forums/u/eranv)\
**Post date:** [November 15, 2021, 12:35pm UTC](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/1 "2021-11-15T12:35:31Z")

</div>

Hi all, I’m new to DPI.

our reference model is written in cpp.

we know how to call to cpp regular funct and then instant our class and call the methods and members.

but my question is if there is any solution to “access” to the same object many time from sv code.

something like that: (maybe using pointers)

```verilog

module top;

 import "DPI-C" context function int my_print();
 import "DPI-C" context function int my_instant_class();
 import "DPI-C" context function int my_change_member();

  initial
  begin
	
	void'(my_instant_class());
	void'(my_print());
	void'(my_change_member(8));
	void'(my_print());
  end
endmodule

```

my wanted output:

> x is : 0 // default value  
> x is : 8 // my new value

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [November 15, 2021, 12:51pm UTC](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/2 "2021-11-15T12:51:20Z")

</div>

_In reply to [eranv](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816):_

It’s not clear what you want to achieve, but the DPI C layer has the concept of ‘context’ so that the C code can determine which SV component is doing the calling. You can also set the ‘context’ on the C side so you can target a specific SV component.

Annex H section H.9 of the LRM discusses this implementation.

---

<div class="post-metadata">

**Author:** ![TomerMetz](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/t/bbe5ce/32.png) [@TomerMetz](https://verificationacademy.com/forums/u/TomerMetz)\
**Post date:** [November 15, 2021, 2:55pm UTC](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/3 "2021-11-15T14:55:31Z")

</div>

_In reply to [cgales](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/2):_

> _In reply to [eranv](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/2):_  
> It’s not clear what you want to achieve, but the DPI C layer has the concept of ‘context’ so that the C code can determine which SV component is doing the calling. You can also set the ‘context’ on the C side so you can target a specific SV component.  
> Annex H section H.9 of the LRM discusses this implementation.

Hi,  
If I understand the problem correctly, he’s looking for a way to instantiate a c++ object that would be accessible via the DPI functions but would be instantiated outside the scope of these functions.

Eran - is this what you’re trying to achieve?

---

<div class="post-metadata">

**Author:** ![eranv](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/e47c2d/32.png) [@eranv](https://verificationacademy.com/forums/u/eranv)\
**Post date:** [November 15, 2021, 3:02pm UTC](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/4 "2021-11-15T15:02:26Z")

</div>

_In reply to [TomerM\_](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/3):_

> _In reply to [cgales](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/3):_  
> Hi,  
> If I understand the problem correctly, he’s looking for a way to instantiate a c++ object that would be accessible via the DPI functions but would be instantiated outside the scope of these functions.  
> Eran - is this what you’re trying to achieve?

Yes, i’m trying to instantiate an object in the start of the simulation and access the members and methods of the **same** instance of the object via DPI call at different steps of the test.

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [November 15, 2021, 3:19pm UTC](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/5 "2021-11-15T15:19:29Z")

</div>

_In reply to [eranv](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/4):_

Similar to standard C function calls, the lifetime of the function and local variables is just during the function call. You can create global variables that can be shared as required, and use the context to determine the caller.

---

<div class="post-metadata">

**Author:** ![jstickle](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@jstickle](https://verificationacademy.com/forums/u/jstickle)\
**Post date:** [November 16, 2021, 2:35am UTC](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/6 "2021-11-16T02:35:10Z")

</div>

_In reply to [eranv](https://verificationacademy.com/forums/t/how-to-connect-dpi-systemverilog-to-class-member/39816/5):_

> Hi all, I’m new to DPI.  
> our reference model is written in cpp.  
> we know how to call to cpp regular funct and then instant our class and call the methods and members.  
> but my question is if there is any solution to “access” to the same object many time from sv code.  
> something like that: (maybe using pointers)

```verilog

module top;
import "DPI-C" context function int my_print();
import "DPI-C" context function int my_instant_class();
import "DPI-C" context function int my_change_member();
initial
begin
void'(my_instant_class());
void'(my_print());
void'(my_change_member(8));
void'(my_print());
end
endmodule

```

my wanted output:

You can try using the standard DPI functions svPutUserData(), svGetUserData(). They are  
documented in the SytsemVerlog standard. Basically you can associate a context pointer referred  
to as “user data” with a particular import DPI-C function being called from a specific  
SystemVerilog module instance.

This user data is represented as an opaque (void \*) pointer but can in fact be  
reinterpret\_cast’ed into a C++ object. Once this is done you can update members in that object,  
call methods, etc. You typically establish that context once at the beginning when you construct  
your C++ object - possibly from within an import DPI-C call or perhaps outside of it such as  
from a SystemC thread - by calling svPutUserData().

Then, inside all subsequent calls to import DPI-C functions from the same SV module instance  
you can retrieve the user data pointer using svGetUserData() and reinterpret\_cast it to  
your C++ object pointer.
