SystemVerilog DPI Tutorial

Previous: Import declaration

Pure function

An imported function is specified as pure if

  • The result of the function solely depends on the values of its inputs.
  • The function has no side effects, i.e., it does not change the value of any variable directly or indirectly (by calling other functions).

Such a function can not have an output or inout argument (in which case the function will have side effect of changing the value of the output/inout argument). Since a task does not have a result or return value, a task can not be pure.

The import declaration for a pure function is shown below. Note the keyword pure in the declaration.

   import "DPI" pure function real sin (real r);

Context function

If an imported task or function accesses SystemVerilog data (other than the arguments passed to the function) by PLI/VPI or other means, such a function is called context function. A context function must identify itself in the import declaration. This is required in order to optimize the code. An import declaration of a context function is shown below.

   import "DPI" context function a_complicated_function 
      (input bit bb, output string ss);

C linkage name

It is possible (but not required) to give a local SystemVerilog name to an imported function. This can be done by introducing a small change in the import declaration as shown below.

   import "DPI" local_sv_function_name = function integer c_function_name();

Here, c_function_name is the C function that is imported with the SystemVerilog name local_sv_function_name. Using this facility, a single C function can be imported in various places (but not within the same scope) in the SystemVerilog side with different SystemVerilog names.


Compiling your code that has imported function in it is no different from ordinary compilation. You specify the name(s) of the file(s) containing the imported function (or binary libraries that have them in a compiled form already), along with those for your SystemVerilog code and compile all of them using a DPI-aware SystemVerilog compiler.

Support of DPI

Support for SystemVerilog DPI in simulators are increasingly becoming available. According to Steve Smith of Synopsys, VCS will support imported functions of DPI (i.e., what you have learnt in this tutorial) from version 7.2 onward. Dennis Brophy of Mentor Graphics said Modelsim already supported of a 'restricted part of DPI' from version 6.0. A query from Project VeriPage to Cadence Design System regarding its roadmap of DPI went unanswered.

With this, we wrap-up our discussion on imported task and functions in DPI. Next time, we will see the other side of DPI - how to call a SystemVerilog task or function from your C code and how these 'exported' tasks and functions give DPI the added advantage over previous interfaces.

Previous: Import declaration Share/Save/Bookmark

Verification Management
Join Verification Management Group

Shop Amazon - Contract Cell Phones & Service Plans

Book of the Month