I'm sharing this code which is a demo of how to manipulate a
SystemVerilog Unpacked Array using DPI. This code will work with
IUS5.83.
-----file top.v--------
module top ();
`define ARRAY_LO 4
`define ARRAY_HI 13
int xyz[`ARRAY_HI: `ARRAY_LO]
// define import function to pass reference to
// open array of int's. This makes the call generic for
// any array size.
import "DPI-C" context function void doArray(inout int abc[] );
initial
begin
// stuff the packed structure with data
for (int i = `ARRAY_LO; i<= `ARRAY_HI ; i++)
xyz[i] = i + 32'h7777_5555 ; // fill up the array
doArray( xyz ); // call dpi function, pass array formal name
// allow the above dpi call to modify array contents, now print it out
for (int i = `ARRAY_LO; i<= `ARRAY_HI ; i++)
$display("Vlog: %d %x",i, xyz[i] );
end
endmodule
---file mydpi.c----------
#include <stdio.h>
#include <svdpi.h>
// Created by Todd Mackett (tmackett@cadence.com) with some help from
// Abhishek Raheja
void doArray(const svOpenArrayHandle x ) { // pass in open handle
io_printf("Array Handle is %x \n", x);
io_printf("Array Pointer is %x \n", svGetArrayPtr(x) );
io_printf("Array Left %d, Array Right %d \n", svLeft(x,1), svRight(x, 1) );
io_printf("Array Low %d, Array High %d \n", svLow(x,1), svHigh(x, 1) );
//io_printf("Length %d \n", svLength(x,1) ); // not implemented 6.02v138
// io_printf("%d \n", svDimensions(x )); // not implemented 6.02v138
io_printf("Size of Array in bytes %d \n", svSizeOfArray(x) ); // returns bytes
// Print out 1Darray contents using Array Low to High indexes
int i;
for (i= svLow(x,1); i <= svHigh(x,1); i++) {
io_printf("DPI: %d is: %x \n", i, *(int*)svGetArrElemPtr1(x, i) );
}
int *p;
p = (int*)svGetArrayPtr(x);
// the Array Index is normalized from 0 (not Vlog array index)
// at this point
for (i= 0; i <= (svHigh(x,1) - svLow(x,1)); i++) {
p[i] = 0x22221111 + i;
io_printf("DPI2: %d %x\n", i, p[i]);
}
}
-----file RUN_NC (script)----
rm *.so
rm *.log
rm *.h
# Create .h file for Exported tasks/functions only
# (imported function do NOT need .h file):
ncverilog +sv top.v +ncdpiheader+dpi.h +elaborate +ncelabargs+-messages
# ncvlog -sv top.v -mess
# ncelab top -sv -dpiheader dpi.h -mess
gcc -fPIC -shared -o mydpi.so mydpi.c -I/`ncroot`/tools/inca/include
ncverilog +sv top.v +sv_lib=mydpi.so +ncsimargs+"-sv_root ./" +linedebug
----file ncverilog.log-------
ncsim> run
Array Handle is 938d8c8
Array Pointer is 93f6a00
Array Left 13, Array Right 4
Array Low 4, Array High 13
Size of Array in bytes 40
DPI: 4 is: 77775559
DPI: 5 is: 7777555a
DPI: 6 is: 7777555b
DPI: 7 is: 7777555c
DPI: 8 is: 7777555d
DPI: 9 is: 7777555e
DPI: 10 is: 7777555f
DPI: 11 is: 77775560
DPI: 12 is: 77775561
DPI: 13 is: 77775562
DPI2: 0 22221111
DPI2: 1 22221112
DPI2: 2 22221113
DPI2: 3 22221114
DPI2: 4 22221115
DPI2: 5 22221116
DPI2: 6 22221117
DPI2: 7 22221118
DPI2: 8 22221119
DPI2: 9 2222111a
Vlog: 4 22221111
Vlog: 5 22221112
Vlog: 6 22221113
Vlog: 7 22221114
Vlog: 8 22221115
Vlog: 9 22221116
Vlog: 10 22221117
Vlog: 11 22221118
Vlog: 12 22221119
Vlog: 13 2222111a
ncsim: *W,RNQUIE: Simulation is complete.
ncsim> exit
TOOL: ncverilog 05.83-p003: Exiting on Feb 05, 2007 at 11:29:36 EST (total: 00:00:03)
|