System Verilog DPI Example

Refer to the previous post for introduction.

System Verilog DPI NCVerilog

The above example gives a demo with NCVerilog.

Here is the run script for running the same example with Modelsim under windows.

Download here. system-verilog-dpi-modelsim.pdf

#File name run.tcl

# Author: A.G.Raja

# Website:

# License: GPL

#Type at Transcript terminal: source run.tcl

vlib work

vlog -sv -dpiheader dpiheader.h dpi_top.v

exec gcc -c -g -IE:/modelsim/include dpi_main.c -o dpi_main.obj

exec gcc -shared -o dpi_main.dll dpi_main.obj -LE:/modelsim/win32

vsim -novopt top -sv_lib dpi_main


# EOF run.tcl

Replace “E:/modelsim” with “modelsim installation path”

1) Don’t create a new project while starting Modelsim

2) Open and proceed directly to Modelsim.

3) At the transcript terminal change to the working directory

4) No need to create a folder named work.

5) The run script given above does it.

Download here. system-verilog-dpi-modelsim.pdf

Download NCVerilog demo here.


System Verilog DPI Example

String passing example

Download as PDF document.


1) For illustration strings are used;
But strings can’t be used in module ports;
2) Use SystemVerilog “program” instead of “module” for testbenches.
See this: SystemVerilog program example

//  dpi_top.v
module top ();
import "DPI-C" context v2c_c=
task v2c_sv(input string a);

import "DPI-C" context c2v_c=
function string c2v_sv();

export "DPI-C" print_string_c =
function print_string_sv;

string v2c_string,c2v_string;

function void
print_string_sv(input string aaa);
$display("Exporting VerilogFunction:%s", aaa);

v2c_string = "A v2c_string";
c2v_string = c2v_sv();
$display("c2v_string: %s n",

// dpi.h
#include “svdpi.h”

#ifdef __cplusplus
extern “C” {

extern void print_string_c (const char *_a1);

#ifdef __cplusplus

//  dpi_main.c
#include <stdio.h>
#include "svdpi.h"
#include "dpi.h"
#include "veriuser.h"
void v2c_c(char* v2c_string)
io_printf("v2c_string: %sn",v2c_string);
print_string_c("string passed from C");
char* c2v_c(void)
char* c2v_string;
c2v_string="This is c2v_string";
//io_printf("c2v_string: %sn",
return c2v_string;
#####   shell script to run simulation
 ncverilog +sv dpi_top.v +elaborate +ncelabargs+-messages
 gcc -fPIC -shared -o  dpi_main.c  -I/$CDS_INST_DIR/tools.lnx86/inca/include
 ncverilog +sv dpi_top.v +access+r +ncsimargs+"-sv_root ./"   
Download NCVerilog demo here.


 Here is a demo for running the same example 
using Modelsim under Windows. System Verilog DPI Modelsim Download Modelsim demo here. system-verilog-dpi-modelsim.pdf