How to connect DPI systemverilog to class member

,

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)


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

In reply to eranv:

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.

In reply to cgales:

In reply to eranv:
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?

In reply to TomerM_:

In reply to cgales:
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.

In reply to eranv:

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.

In reply to eranv:

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)


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.