Verilog and C program interaction, PLI (programming Language Interface) through the TF,ACC,VPI and other modes.
With PLI, you can generate a time-lapse calculator to connect and synchronize multiple emulators, and to debug tools such as waveform display.
By using PLI to connect a simple C program, you need to write a lot of code and understand the concepts of synchronization, calling segments, instance pointers, and so on in the multi-emulation phase.
The PLI approach poses an additional burden to the simulation, and in order to protect the Verilog data structure, the emulator needs to constantly replicate data between Verilog and C.
SystemVerilog introduces a dpi (Direct programming Interface), which allows for a more concise connection to c,c++ or other non-Verilog programming languages.
As long as you use import declaration and use, importing a C subroutine, you can invoke it just like a subroutine in SystemVerilog.
The most basic data type passed between SystemVerilog and C is an int, a dual-state 32-bit data type,
Import "dpi-c" function int factorial (input int i);
program Automatic test;
Initial begin
for (int i=1;i<=10;i++)
$dispaly ("%0d! =%0d", i,factorial (i));
End
Endprogram
Each variable passed by DPI has two matching definitions, one in SystemVerilog and one in the C language. In use, you must confirm that you are using a compatible data type.
SystemVerilog C (Input) c (output)
byte Char char* shortint
short int short int short int*
int int int*
Longint Long Long int long int*
Bit svbit/unsigned Char svbit*/unsigned Char
Logic/reg svlogic/unsigned Char svlogic*/unsigned char*
BIT[N:0] Const svbitvecval* svbitvecval*
REG[N:0] Const svlogicvecval* svlogicvecval*
The prototype of the C task and function is defined by the import declaration, and the C function with the return value is mapped to a systemverilog function.
c Functions of type Void are mapped to a SystemVerilog task (Task) or void function
If there is a naming conflict in the C function name and SystemVerilog, you can give the new function name when import is imported.
program Automatic test;
Import "Dpi-c" test=function void My_test (); The test function in C is mapped to the my_test name of function void.
Intial begin
My_test ();
End
In SystemVerilog, all places that allow the declaration of subroutines can be imported, C subroutines,
For example, program,module,interface,package or compilation unit space, the imported subroutine is only valid in the space in which it is declared.
The C subroutine being imported, can have multiple parameters or no parameters, by default, the direction of the parameter is input (data flow from SystemVerilog to C function)
The direction of the parameter can also be defined as output and the Inout,ref type is not currently supported.
Import "dpi-c" function int addmul (input int A, B,
output int sum);
Parameters to the input can also be defined as Const. This will cause the C compiler to make an error once the input variable is written.
int factorial (const int i) {}
Examples of connecting C languages.
#include <svdpi.h>
void Counter7 (Svbitvecval * o,
Const SVBITVECVAL * I,
Const Svbit Reset,
Const SVBIT Load)
{static unsigned char count = 0;
if (reset) count = 0;
else if (load) count = * I;
else count++;
Count &= 0x7F;
*o = count;
}
Reset and load are a dual-state bit signal that is passed in the Svbit type.
Input I is a dual-state 7bit variable, passed with the Svbitvecval type.
The header file svdpi.h contains the definition of the SystemVerilog DPI structure and method.
Test Platform:
Import "dpi-c" function void Counter7 (output bit [6:0] out,
Input bit [6:0] in,
Input bit reset, load);
Program Automatic counter;
Bit[6:0] out, in;
bit reset, load;
Initial begin
$monitor ("sv:out=% 3d, in =%3d, reset =%0d, load =%0d\n", out,in,reset,load);
reset = 0;
Load = 0;
in = 126;
out = 42;
Counter7 (out, in, reset, load);
End
Endprogram
The CHandle type allows you to store a C + + pointer in system Verilog.
typedef struct{unsigned char CNT;} C7;
void *counter7_new () {c7* c= (c7*) malloc (sizeof (C7));
C-> cnt = 0;
return c;}
void Counter7 (C7* inst, ...)
Test Platform:
Import "Dpi-c" function CHandle counter7_new ();
Import "dpi-c" function void Counter7 (input CHandle inst, ...)
Use the keyword Dpi-c to save the data type in a way that compresses the value.
SV calls C through DPI