DPI (Direct Programming Interface) is a mechanism in SystemVerilog that allows the integration of SystemVerilog with other programming languages like C, Python, MATLAB scripts, and more. It enables bidirectional communication between SystemVerilog testbenches and external applications, enabling more advanced and flexible testing setups, especially in the context of UVM (Universal Verification Methodology).
In UVM, DPI can be particularly useful for tasks such as:
By using DPI, we can extend the SystemVerilog environment with custom functions written in other languages. Although there may be some performance overhead, it provides an easy-to-apply solution.
While DPI is designed for native interaction with C, integrating other languages requires an intermediary approach. To communicate with Python, for instance, C acts as a "bypass." Here's why:
This approach may seem like an additional step, but it ensures compatibility and stability, as DPI relies on the robustness of C for cross-language communication.
Import and export are used to facilitate communication between SystemVerilog and external languages.
In short, import brings external functionality into SystemVerilog, and export allows SystemVerilog functionality to be accessed externally. These mechanisms are essential for enabling the interaction between SystemVerilog and other programming languages through DPI.
In this section, we will demonstrate how to use imports and exports to integrate SystemVerilog with C code. We will provide a simple example where SystemVerilog imports a C function to perform a mathematical operation, while also exporting a function from SystemVerilog that can be called by the external C program.
import "DPI-C" context function void divide_by_two(inout int b);
export "DPI-C" function display_message;
initial begin
int random_number;
if (!std::randomize (random_number)) begin
`uvm_error("random_number ", "Failed to randomize var!");
end
divide_by_two(random_number);
end
function void display_message(input string message);
$display("C message: %s", message);
endfunction
#include "svdpi.h"
#include <stdio.h>
void divide_by_two(int *A)
{
char message[100];
*A = *A / 2;
sprintf(message, "The result is %d", *A);
display_message(message);
}
For the compilation, if you're using QuestaSim, you can use the following commands:
vlog "$lib_path/c_algorithms.c"
vsim +UVM_TESTNAME=$uvm_testname project_tb_top
Alternatively, you can use GCC for compilation, which makes the process independent of the simulation tool you're using. On Windows, the command would be:
gcc -I C:\\questasim64_10.7c\\include -shared -o c_algorithms.dll c_algorithms.c
vsim +UVM_TESTNAME=$uvm_testname -sv_lib c_algorithms project_tb_top
When compiling with vlog, it's integrated with ModelSim/QuestaSim, ensuring compatibility with their simulation environment. Choose vlog if you're working within this ecosystem. On the other hand, gcc is a more general approach, ideal for cross-platform compilation or when you're not tied to a specific simulator. Use gcc if you're looking for broader flexibility.
DPI is a powerful feature in SystemVerilog and UVM that allows interaction with external languages like C and Python. Through the use of imports and exports, you can extend the functionality of your UVM testbenches, making them more powerful and flexible. The ability to compile and link external code with vlog and gcc makes the integration process efficient and straightforward.
By understanding DPI and its mechanisms, you can significantly enhance your verification environment, enabling more complex test scenarios and improving simulation accuracy.
In upcoming blog posts, we will dive deeper into these concepts by exploring how to integrate Wireshark for network packet analysis and capture, as well as how to build a TLP (Transaction Layer Packet) generator in Python, and connect it with SystemVerilog via DPI. These topics will expand the scope of your verification environment, enabling more sophisticated, real-world test scenarios and providing greater flexibility and control over your simulations.
Joaquin Lutri and Martin Sarasqueta Fierens
Design Verification Engineers at Emtech S.A.
Thanks Marcelo Pouso for their valuable feedback and insightful reviews.
If you need further info, contact us: info@emtech.com.ar