How can I use a DPI C function within a class

Hello everyone,

I’m using System Verilog now just since 2 months.

I would like to use a C function inside a class.

Can anyone please explain why it’s just allowed to use inside e.g. modules.

Is there any way to use a C function inside a class or how to use it inside a class if I Import the DPI outside a class?

Many thanks in advance,
René

In reply to RenéPi:

You can certainly use (or call) a C function from a class method, you just can’t import a DPI C function from within a class. You can only import a C function or task from a point where you would be allowed to declare a function or task that is not a method of a class.

The reason for this restriction is that class methods have an implicit this argument set with the handle of the class object calling the method. That handle represents the “context” of the method. Non-class functions or tasks have no such context.

So your class method should be able to call a C function just like any other non-class function as long as the function name is visible from the scope where the class is defined.

Thanks a lot for the reply Dave!

I have now imported the C function outside the class with:

import “DPI-C” context function CCRC32_crcbitbybit();

I am calling the C function by calling a function within the System Verilog class

function imp_func(byte data);

 longint crc_ret = 0;

$display("Importing C Function");
crc_ret = CCRC32_crcbitbybit();
$display("Return Vaule of CCRC32: %h",crc_ret);

endfunction

I have another question regarding datatypes between SV and C:

The CRC C function uses an unsigned char* which compares to be a string in SV, but I am using a byte as Parameter for the SV function.

Is there any possibility to convert a byte to a string in SV?

Thanks a lot,
René

In reply to dave_59:

In reply to RenéPi:

You can certainly pass strings through the DPI as well as convert bytes (or an array of bytes) to a string using a cast. Just remember that a string cannot contain the ‘\0’ (null) character.