What is System Verilog DPI (Direct Programming Interface)
Direct programming interface or DPI is a interface between the System Verilog and the C language. It allows you to call the sytem verilog functions in C language and Vice Versa
why :
1. you can reuse your existing c code with system verilog
Sytem Verilog :
module hello();
import "DPI-C" function void print_hello();
initial
begin
print_hello();
end
endmodule
C :
===
void print_hello() {
printf("Hello World From C \n");
}
How to run :
Step 1. Compile your C code
gcc -shared -o hello.so hello.c
Step 2. Compile your SV code
vlog -sv hello.sv
Step 3. link and load
vsim -c hello -sv_lib hello
Step 4. Run
VSIM> run -all
output :
VSIM 1> run -all
# Hello World From C
# ** Note: $finish : hello.sv(7)
# Time: 0 ns Iteration: 0 Instance: /hello
GCC option :
-shared
Produce a shared object which can then be linked with other objects to form an executable.
Explanation :
In the above example we are calling a C funtion print_hello() in system
verilog code. Following are the points to note :
- we need to import the C function in system verilog with statement 'import "DPI-C"'
- we should have actual funtion implementiaon in C file
- Call the C funtion in your system verilog code
- remember to link library at loading time
- import declaration can occur anywhere where a SystemVerilog task or function definition is allowed
NOTE : C function should not comsume time like the system verilog functions
Imported Functions :
- Functions implemented in foreign language can be called in system verilog, such functions are called Imported functions
Exported Functions :
- System verilog functions that are called from foeign language should appear in exported function declerations


