SystemVerilog DPI Tutorial

Previous: Import functions | Next: Pure and context functions

Import declarations

Any imported C function must have a corresponding import declaration. In the previous example, for the imported function my_function(), an import declaration similar to what is shown below is required.

   import "DPI" function integer my_function();

Note the keywords import "DPI" which must preceed an import declaration. Also note that the return type (integer) of my_function() is also specified in the import declaration.

For a task, a similar import declaration will look like:

   import "DPI" task my_task();

An import declaration can occur anywhere where a SystemVerilog task or function definition is allowed.

Result of an imported function

DPI restricts the return type for an imported function to the following.

  • void, byte, shortint, int, logint, real, shortreal, chandle and string
  • packed bit arrays of upto 32 bits or equivalent.
  • scalara values of type bit and logic.

Since a task does not have a return value, a C function corresponding to an imported task must return a void type.

Arguments of an imported function

Similar to a native Verilog or SystemVerilog function, an imported function can take formal arguments. In the following example, we use standard C library function strlen() (as defined in string.h) as an imported function in SystemVerilog.

An important point to note in this example is the use of the identifier s in the formal argument. You will recall this is different from C where an argument type is sufficient for a prototype decalaration. The identifier name in a DPI import declaration can be arbitrary and does not need to match the actual C function argument.

   import "DPI" function integer strlen(string s);

Moreover, a formal argument to an imported function optionally can have one of the directional qualifires input, output or inout. To illustrate this with my_function2() that we have already seen:

   import "DPI" function integer my_function2(input logic [31:0] inlogic);

Few self-explanatory rules for formal arguments with with directional qualifiers:

  • An imported function can not change the value of an input argument.
  • Similarly, an imported function can not assume an initial value of an output argument without actually calculating it.

Previous: Import functions | Next: Pure and context functions

Share/Save/Bookmark



Verification Management
Join Verification Management Group


Talk UVM with Friends
Shop Amazon - Contract Cell Phones & Service Plans


Fun Stuffs!