一個簡單的DPI Array case
這個例子是在Cadence的IUS9.2上面所測的
gcc –I/IUS9.2/linux/tools/include/ -shared –o test.so test.c
使用gcc時, 副檔名必須為c
g++ –I/IUS9.2/linux/tools/include/ -shared –o test.so test.cpp
使用g++時, 副檔名必須為cpp
irun_92 main.sv –sv_lib test.so
//test.c
#include <stdio.h>
#include “svdpi.h”
void pass_array(const svOpenArrayHandle dyn_arr ) {
// test.cpp 如果用g++ 就改成下面這一行
// extern “C” void pass_array(const svOpenArrayHandle dyn_arr ) {
int i;
printf("Array Left %d, Array Right %d \n\n",
svLeft(dyn_arr,1), svRight(dyn_arr, 1) );
for (i= svRight(dyn_arr,1); i <= svLeft(dyn_arr,1); i++) {
printf("C: %d %d \n", i, *(int*)svGetArrElemPtr1(dyn_arr, i) );
}
printf("\n\n");
}
for (i= svRight(dyn_arr,1); i <= svLeft(dyn_arr,1); i++) {
printf("C: %d %d \n", i, *(int*)svGetArrElemPtr1(dyn_arr, i) );
}
printf("\n\n");
}
module main();
int fxd_arr_1[8:3];
int fxd_arr_2[12:1];
import "DPI-C" context function void pass_array(input int dyn_arr[] );
initial
begin
for (int i = 3; i<=8 ; i++)
begin
fxd_arr_1[i] = $random() ;
$display("SV:fxd_arr_1 %0d %d ",i, fxd_arr_1[i] );
end
$display("\n Passing fxd_arr_1 to C \n");
pass_array( fxd_arr_1 );
for (int i = 1; i<=12 ; i++)
begin
fxd_arr_2[i] = $random() ;
$display("SV: fxd_arr_2 %0d %d ",i, fxd_arr_2[i] );
end
$display("\n Passing fxd_arr_2 to C \n");
pass_array( fxd_arr_2 );
end
endprogram
int fxd_arr_1[8:3];
int fxd_arr_2[12:1];
import "DPI-C" context function void pass_array(input int dyn_arr[] );
initial
begin
for (int i = 3; i<=8 ; i++)
begin
fxd_arr_1[i] = $random() ;
$display("SV:fxd_arr_1 %0d %d ",i, fxd_arr_1[i] );
end
$display("\n Passing fxd_arr_1 to C \n");
pass_array( fxd_arr_1 );
for (int i = 1; i<=12 ; i++)
begin
fxd_arr_2[i] = $random() ;
$display("SV: fxd_arr_2 %0d %d ",i, fxd_arr_2[i] );
end
$display("\n Passing fxd_arr_2 to C \n");
pass_array( fxd_arr_2 );
end
endprogram
svOpenArrayHandle 是systemverilog在DPI 上傳送用的一個 open arrays
上面有用到svLeft及svRight, 他們屬於Array querying functions, 列表如下
/* h= handle to open array, d=dimension */
int svLeft(const svOpenArrayHandle h, int d); // 回傳 left bound of the array
int svRight(const svOpenArrayHandle h, int d); // 回傳 right bound of the array
int svLow(const svOpenArrayHandle h, int d);// == svLeft
int svHigh(const svOpenArrayHandle h, int d);// == svRight
int svSizeOfArray (const svOpenArrayHandle h);//回傳array所佔用的記憶體
沒有留言:
張貼留言