Interfacing With C SV - DPI-C
Interfacing With C SV - DPI-C
Interfacing with C
In Verilog. you can communicate with C routines using the Programming Language
Interface. With the three generations ofthe PLI (TF. ACe. and VPI routines). you can
create delay calculators. connect and synchronize multiple simulators. and add debug
tools such as waveform displays. However. the PLI's greatest strength is also its
greatest weakness. If you just want to connect a simple C routine using the PLI. you
need to write dozens of lines of code. and understand many different concepts such as
synchronizing with multiple simulation phases. call frames, and instance pointers.
Additionally. the PLI adds overhead to your simulation as it copies data between the
Verilog and C domains. in order to protect Verilog data structures from corruption.
SystemVerilog introduces the Oirect Programming Interface (OPI), an easier way to
interface with C. C++. or any other foreign language. On ce you declare or "import"
the C routine with the import statement. you can call it as if it were any System Ver-
ilog routine. Additionally. your C code can call SystemVerilog routines. With the OPI
you can connect C code that reads stimulus. contains a reference model. or just
extends SystemVerilog with new functionality.
If you have a SystemC model that does not consume time. and that you want to con-
nect to SystemVerilog. you can use the OPI. SystemC models with time-consuming
methods are best connected with the utilities built into your favorite simulator.
The first half ofthis chapter is data-centric and shows how you can pass different data
types between SystemVerilog and e. The second halfis control centric. showing how
you can pass control back and forth between SystemVerilog and e.
The first few examples in this chapter show you how to pass integral values between
System Verilog and C, and the mechanics of how to declare routines and their argu-
ments on both sides. Later sections show how to pass arrays and structures.
The most basic data type that you can pass between SystemVeriiog and C is an int,
the 2-state, 32-bit type. Sample 12.1 shows the System Veri log code that calls a C fac-
torial routine, shown in Sample 12.2.
The import declaration defines the prototype of the C task or function, but using
System Veri log types. A C function with a return value is mapped to a SystemVeriiog
function. A void C function can be mapped to a System Veri log task or void function.
lThe SystemVerilog int is always 32 bits, which the width of an int in C is operating system dependant.