24.03.2013 Views

ModelSim SE User's Manual - Electrical and Computer Engineering

ModelSim SE User's Manual - Electrical and Computer Engineering

ModelSim SE User's Manual - Electrical and Computer Engineering

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

UM-584 D - Verilog PLI / VPI / DPI<br />

DPI example<br />

<strong>ModelSim</strong> <strong>SE</strong> User’s <strong>Manual</strong><br />

The following example is a trivial, but complete DPI application. For win32 <strong>and</strong> RS6000<br />

platforms, one additional step is required along with some new arguments. For the latest<br />

detailed instructions for compiling <strong>and</strong> simulating DPI imported <strong>and</strong> exported tasks <strong>and</strong><br />

functions, see the modeltech/examples/dpi directory. There you will find examples with a<br />

subdirectory for each platform that contains the platform specific comm<strong>and</strong>s <strong>and</strong><br />

arguments.<br />

hello_c.c:<br />

#include "svdpi.h"<br />

#include "dpiheader.h"<br />

int c_task(int i, int *o)<br />

{<br />

printf("Hello from c_task()\n");<br />

verilog_task(i, o); /* Call back into Verilog */<br />

*o = i;<br />

return(0); /* Return success (required by tasks) */<br />

}<br />

hello.v:<br />

module hello_top;<br />

int ret;<br />

export "DPI" task verilog_task;<br />

task verilog_task(input int i, output int o);<br />

#10;<br />

$display("Hello from verilog_task()");<br />

endtask<br />

import "DPI" context task c_task(input int i, output int o);<br />

initial<br />

begin<br />

c_task(1, ret); // Call the c task named 'c_task()'<br />

end<br />

endmodule<br />

Compile the Verilog code:<br />

% vlib work<br />

% vlog -sv -dpiheader dpiheader.h hello.v<br />

Compile the DPI code for the Solaris operating system:<br />

% gcc -c -g -I/modeltech/include hello_c.c<br />

% ld -G -o hello_c.so hello_c.o<br />

Simulate the design:<br />

% vsim -c -sv_lib hello_c hello_top<br />

# Loading work.hello_c<br />

# Loading ./hello_c.so<br />

VSIM 1> run -all<br />

# Hello from c_task()<br />

# Hello from verilog_task()<br />

VSIM 2> quit

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!