This website uses cookies. By using this site, you consent to the use of cookies. For more information, please take a look at our Privacy Policy.
Home > FPGA Technical Tutorials > Designing with Xilinx FPGAs Using Vivado > Simulation > Combining C with HDL Using DPI

TABLE OF CONTENTS

Xilinx FPGA FPGA Forum

Combining C with HDL Using DPI

FONT SIZE : AAA

With SystemVerilog, you can write a part of design in C or C++ and use it from  SystemVerilog. SystemVerilog can import C functions via import command. Once  a C function is imported, it can be called as a regular SystemVerilog function or task (depending on the import command syntax). Similarly SystemVerilog function or  task can be exported to C side. These exported SystemVerilog functions (or tasks)  can be called from C functions. Exported SystemVerilog tasks can be used to mimic  #delay or @wait from C functions.   

For simulating SystemVerilog with C code, it is important to make sure that they  are compatible. xelab has an option –dpiheader . This generates a C header fifi le for  the imported and exported functions. The C function defifi nition prototype must  match with this generated header for successful linking of functions.   

Use the Tcl command xsc to compile the C fifi les and then link with xelab . Only  simple scalar types are permitted as function return for imported or exported DPI functions. The permitted data types that can be passed between C and SystemVerilog  are mentioned in Table 11.1 . 

Let us take an example to elaborate. A SystemVerilog fifi le hdl.sv (with the design  top name TESTBENCH ) calls a C function defifi ned in fifi le helper.c . The prototype for  the function as defifi ned in hdl.sv is

Permitted.png

To generate the equivalent C prototype for function, you need to elaborate the  design using xelab with the additional command line option –dpiheader . This will  generate a header fifi le with the name dpi.h . For this case, the dpi.h will contain the  C prototype of the equivalent function.

Permitted.png

SV_PACKED_DATA_NELEMS() is defifi ned in svdpi.h which is included. SV_ PACKED_DATA_NELEMS rounds the bits into chunks of 32 bits needed to hold. So  number from 1 to 32 will become 1 , 33 – 64 will become 2 , and so on. The C code  needs to have the same prototype for function . The C code can be compiled into a  dynamic library dpi.so with the command xsc as:

xsc helper.c 

The next step is to create the simulation kernel with xelab command where the  name of the dpi library is specififi ed. 

xelab TESTBENCH –snapshot SIM1 –sv_lib dpi

Once the kernel is created, the simulation can be run using xsim command. This  will open up a Tcl shell that takes all the simulation commands.

  • XCV200E-7BG352I

    Manufacturer:Xilinx

  • FPGA Virtex-E Family 63.504K Gates 5292 Cells 400MHz 0.18um Technology 1.8V 352-Pin Metal BGA
  • Product Categories: Connecteurs

    Lifecycle:Obsolete -

    RoHS: No RoHS

  • XC4003A-5PQ100C

    Manufacturer:Xilinx

  • FPGA XC4000A Family 3K Gates 100 Cells 125MHz 5V 100-Pin PQFP
  • Product Categories:

    Lifecycle:Obsolete -

    RoHS: No RoHS

  • XCS30-4PQ240C

    Manufacturer:Xilinx

  • Spartan and Spartan-XL Families Field Programmable Gate Arrays FPGA
  • Product Categories: Interface IC

    Lifecycle:Obsolete -

    RoHS: -

  • XC5VLX110-1FF1760C

    Manufacturer:Xilinx

  • FPGA Virtex-5 LX Family 110592 Cells 65nm Technology 1V 1760-Pin FCBGA
  • Product Categories: FPGAs (Field Programmable Gate Array)

    Lifecycle:Active Active

    RoHS: No RoHS

  • XC5VLX110-1FFG1136C

    Manufacturer:Xilinx

  • Xilinx BGA
  • Product Categories:

    Lifecycle:Any -

    RoHS:

Need Help?

Support

If you have any questions about the product and related issues, Please contact us.