0% found this document useful (0 votes)
156 views2 pages

Interfacing With C SV - DPI-C

This section discusses interfacing SystemVerilog with C code using the Direct Programming Interface (DPI). It shows how to pass simple integer and real values between the languages, as well as the mechanics of declaring routines and arguments. Later sections cover passing more complex data types like arrays and structures.

Uploaded by

Sam Honey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views2 pages

Interfacing With C SV - DPI-C

This section discusses interfacing SystemVerilog with C code using the Direct Programming Interface (DPI). It shows how to pass simple integer and real values between the languages, as well as the mechanics of declaring routines and arguments. Later sections cover passing more complex data types like arrays and structures.

Uploaded by

Sam Honey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter 12

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.

C. Spear, System Verilog for Verification


© Springer Science+Business Media, LLC 2008
382 Chapter 12:Interfacing with C

12.1 Passing Simple Values

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.

12.1.1 Passing Integer and Real Values

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.

Sample 12.1 SystemVerilog code calling C factorial routine


import "DPI-C" function int factorial(input int i);

program automatic test;


initial begin
for (int i=l; i<=10; i++)
$disp1ay(II%Od! = %Od", i, factorial(i));
end
endprogram

The import statement declares that SystemVeriiog routine factorial is imple-


mented in a foreign language such as C or C++. The modifier ,. DPI -c" specifies that
this is a DPI routine, and the rest of the statement describes the routine arguments.
Sample 12.1 passes 32-bit signed values using the SystemVerilog int data type that
maps directly to the C int type'. The C function in Sample 12.2 takes an integer as
an input and so the DPI passes the argument by value.

Sample 12.2 C factorial function


int factorial(int i) {
if (i<=l) return 1;
else return i*factoria1(i-1);
}

12.1.2 The Import Declaration

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.

You might also like