SystemVerilog DPI: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Dekart (talk | contribs)
 
(36 intermediate revisions by 27 users not shown)
Line 1:
{{Short description|Hardware description language API}}
'''Direct Programming Interface''' (DPI) is an interface which can be used to interface [[SystemVerilog]] with foreign languages. These Foreign languages can be a C, C++, System C as well as others. DPI's consists of two layers: A [[SystemVerilog]] Layer and a Foreign language layer. Both the layers are isolated from each other.
{{Use American English|date = April 2019}}
'''DirectSystemVerilog Programming InterfaceDPI''' (DPIDirect Programming Interface) is an interface which can be used to interface [[SystemVerilog]] with foreign languages. These Foreignforeign languages can be a[[C (programming language)|C]], [[C++]], System C[[SystemC]] as well as others. DPI'sDPIs consistsconsist of two layers: Aa [[SystemVerilog]] Layerlayer and a Foreignforeign language layer. Both the layers are isolated from each other.
 
==Explanation==
Direct Programming Interface ('''DPI''') allows direct inter language function calls between the [[SystemVerilog]] and Foreign language. The functions implemented in Foreign language can be called from SystemVerilog and such functions are called '''Import''' functions. similarlySimilarly, functions implemented in [[SystemVerilog]] can be called from Foreign language ('''C/C++ or System C'''); such functions are called '''Export''' functions. DPI'sDPIs allowsallow transfer of data between two domains through function arguments and return.
 
==Function Importimport and Exportexport==
Direct Programming Interface ('''DPI''') allows direct inter language function calls between the [[SystemVerilog]] and Foreign language. The functions implemented in Foreign language can be called from SystemVerilog and such functions are called '''Import''' functions similarly functions implemented in [[SystemVerilog]] can be called from Foreign language ('''C/C++ or System C''') such functions are called '''Export''' functions. DPI's allows transfer of data between two domains through function arguments and return.
 
==Function Import and Export==
 
1) Function Import:- A function implemented in Foreign language can be used in [[SystemVerilog]] by importing it. A Foreign language function used in [[SystemVerilog]] is called Imported function.
 
==Properties of Importedimported Functionfunction and Tasktask==
 
# An importedImported function shall complete their execution instantly and consume zero simulation time. Imported task can consume time.
# Imported function can have input, output, and inout arguments.
#* The formal input arguments shall not be modified. If such arguments are changed within a function, the chageschanges shall not be visible outside the function.
#* Imported function shall not assume any initial values of formal output arguments. The initial value of output arguments is undetermined and implementation dependent.
#* Imported function can access the initial value of a formal inout argument. Changes that the Imported function makes to a formal inout argument shall be visible outside the function.
# An Imported function shall not free the memory allocated by SystemVerilog code nor expect [[SystemVerilog]] code to free memory allocated by Foreign code or (Foreign Compiler).
# A call to an Imported task can result in suspension of the currently executing thread. This occurs when an Imported task calls an Exported task, and the Exported task executes a delay control, event control or wait statement. Thus it is possible for an Imported task to be simultaneously active in multiple execution threads.
# AAn Imported function or task can be equip with special properties called pure or context.
 
== Pure and Contextcontext Taskstasks and Functionsfunctions ==
 
=== Pure Functionsfunctions ===
 
A function whose results solely depends on the value of its input arguments with no side effects is called Pure function.
 
==== Properties of Purepure Functionsfunctions ====
 
1)# Only Non-Void functions with no output or inout arguments can be called as Pure functions.
2)# Functions specified as Pure shall have no side effects, their results need to depend solely on the values of their input arguments.
# A Pure function call can be safely eliminated if its result is not needed or if its results for the same value of input arguments is available for reuse without needing to recalculate.
4)# A Pure function is assumed not to directly or indirectly perform the following:
a)## Perform any file operation.
b)## Read or Write anything in Environment Variable, Shared memory, Sockets etc.
c)## Access any persistent data like Global or Static variable.
5)# An Imported task can never be declared Pure.
 
=== Context Taskstasks and Functionsfunctions ===
2) Functions specified as Pure shall have no side effects, their results need to depend solely on the values of their input arguments.
 
3)An AImported Puretask or function callwhich cancalls be"Exported" safelytasks eliminatedor iffunctions itsor resultaccesses isSystemVerilog notdata neededobjects orother ifthan its results for the same value of inputactual arguments is availablecalled forContext reusetask withoutor needing to recalculatefunction.
 
==== Properties of Contextcontext Taskstasks and Functionsfunctions ====
4) A Pure function is assumed not to directly or indirectly perform the following:
 
a) Perform any file operation.
b) Read or Write anything in Environment Variable, Shared memory, Sockets etc.
c) Access any persistent data like Global or Static variable.
 
5) An Imported task can never be declared Pure.
 
=== Context Tasks and Functions ===
 
An Imported task or function which call "Exported" tasks or functions or accesses SystemVerilog data objects other than its actual arguments is called Context task or function.
 
==== Properties of Context Tasks and Functions ====
 
1) A Context Imported task or function can access (read or write) any SystemVerilog data object by calling (PLI/VPI) or by calling Export task or function. Therefore, a call to Context task or function is a barrier for SystemVerilog compiler optimization.
 
== Import Declarationdeclaration ==
<syntaxhighlight lang="systemverilog">
import “DPI"DPI-C”C" function int calc_parity (input int a);
</syntaxhighlight>
 
== Export declaration ==
import “DPI-C” function int calc_parity (input int a);
<syntaxhighlight lang="systemverilog">
export “DPI"DPI-C”C" my_cfunction = function myfunction;
</syntaxhighlight>
 
== ExportCalling DeclarationUnix functions ==
 
export “DPI-C” my_cfunction = function myfunction;
 
== Calling Unix Functions ==
 
SystemVerilog code can call Unix functions directly by importing them, with no need for a wrapper.
 
== DPI Exampleexample ==
=== Calling 'C' Functionsfunctions in SystemVerilog ===
==== C - Codecode Filefile ====
 
<sourcesyntaxhighlight lang="c">
#include <stdio.h>
#include <stdlib.h>
 
extern int add() {
int a = 10, b = 20;
a = a + b;
Line 78 ⟶ 76:
return a;
}
</syntaxhighlight>
</source>
==== SystemVerilog Code File ====
 
==== SystemVerilog Codecode Filefile ====
<source lang="verilog">
 
<syntaxhighlight lang="systemverilog">
 
module tb_dpi;
Line 92 ⟶ 91:
begin
$display("Entering in SystemVerilog Initial Block");
#20;
j = add();
$display("Value of J = %d", j);
Line 102 ⟶ 101:
endmodule
</syntaxhighlight>
</source>
 
== References ==
* {{cite news |last=Gopi Krishna |url=http://www.testbench.in |title=SystemVerilog DPI Tutorial |date=2005-11-09 }}
 
* [http://www.project-veripage.com/dpi_tutorial_1.php SystemVerilog DPI Tutorial] from Project VeriPage
 
{{Programmable Logic}}
 
[[Category:Application programming interfaces]]

Latest revision as of 01:41, 18 January 2024

SystemVerilog DPI (Direct Programming Interface) is an interface which can be used to interface SystemVerilog with foreign languages. These foreign languages can be C, C++, SystemC as well as others. DPIs consist of two layers: a SystemVerilog layer and a foreign language layer. Both the layers are isolated from each other.

Explanation[edit]

Direct Programming Interface (DPI) allows direct inter language function calls between the SystemVerilog and Foreign language. The functions implemented in Foreign language can be called from SystemVerilog and such functions are called Import functions. Similarly, functions implemented in SystemVerilog can be called from Foreign language (C/C++ or System C); such functions are called Export functions. DPIs allow transfer of data between two domains through function arguments and return.

Function import and export[edit]

1) Function Import:- A function implemented in Foreign language can be used in SystemVerilog by importing it. A Foreign language function used in SystemVerilog is called Imported function.

Properties of imported function and task[edit]

  1. An Imported function shall complete their execution instantly and consume zero simulation time. Imported task can consume time.
  2. Imported function can have input, output, and inout arguments.
    • The formal input arguments shall not be modified. If such arguments are changed within a function, the changes shall not be visible outside the function.
    • Imported function shall not assume any initial values of formal output arguments. The initial value of output arguments is undetermined and implementation dependent.
    • Imported function can access the initial value of a formal inout argument. Changes that the Imported function makes to a formal inout argument shall be visible outside the function.
  3. An Imported function shall not free the memory allocated by SystemVerilog code nor expect SystemVerilog code to free memory allocated by Foreign code or (Foreign Compiler).
  4. A call to an Imported task can result in suspension of the currently executing thread. This occurs when an Imported task calls an Exported task, and the Exported task executes a delay control, event control or wait statement. Thus it is possible for an Imported task to be simultaneously active in multiple execution threads.
  5. An Imported function or task can be equip with special properties called pure or context.

Pure and context tasks and functions[edit]

Pure functions[edit]

A function whose results solely depends on the value of its input arguments with no side effects is called Pure function.

Properties of pure functions[edit]

  1. Only Non-Void functions with no output or inout arguments can be called as Pure functions.
  2. Functions specified as Pure shall have no side effects, their results need to depend solely on the values of their input arguments.
  3. A Pure function call can be safely eliminated if its result is not needed or if its results for the same value of input arguments is available for reuse without needing to recalculate.
  4. A Pure function is assumed not to directly or indirectly perform the following:
    1. Perform any file operation.
    2. Read or Write anything in Environment Variable, Shared memory, Sockets etc.
    3. Access any persistent data like Global or Static variable.
  5. An Imported task can never be declared Pure.

Context tasks and functions[edit]

An Imported task or function which calls "Exported" tasks or functions or accesses SystemVerilog data objects other than its actual arguments is called Context task or function.

Properties of context tasks and functions[edit]

1) A Context Imported task or function can access (read or write) any SystemVerilog data object by calling (PLI/VPI) or by calling Export task or function. Therefore, a call to Context task or function is a barrier for SystemVerilog compiler optimization.

Import declaration[edit]

import "DPI-C" function int calc_parity (input int a);

Export declaration[edit]

export "DPI-C" my_cfunction = function myfunction;

Calling Unix functions[edit]

SystemVerilog code can call Unix functions directly by importing them, with no need for a wrapper.

DPI example[edit]

Calling 'C' functions in SystemVerilog[edit]

C - code file[edit]

#include <stdio.h>
#include <stdlib.h>

extern int add() {
  int a = 10, b = 20;
  a = a + b;

  printf("Addition Successful and Result = %d\n", a);
  return a;
}

SystemVerilog code file[edit]

module tb_dpi;

  import "DPI-C" function int add();
  import "DPI-C" function int sleep(input int secs);
  int j;
  
  initial
  begin
    $display("Entering in SystemVerilog Initial Block");
    #20
    j = add();
    $display("Value of J = %d", j);
    $display("Sleeping for 3 seconds with Unix function");
    sleep(3);
    $display("Exiting from SystemVerilog Initial Block");
    #5 $finish;
  end
  
endmodule

References[edit]

  • Gopi Krishna (2005-11-09). "SystemVerilog DPI Tutorial".
  • SystemVerilog DPI Tutorial from Project VeriPage