我的 SystemVerilog 测试平台中有一个多维动态解包数组,并使用 DPI-C 将其作为参数传递到 C 代码中。我正在使用 Questasim 10.4b。按照 Questa 的流程,我运行 -dpiheader 来生成头文件并将其包含在我的 C 代码中。
SystemVerilog:
// C function declaration
import "DPI-C" function void modulate_my_frame (input byte data[][]);
// multidimensional array
byte unsigned frame_packed[][];
// assume this is randomized in my testbench
int num_frames = 5;
// init 1st dimension
frame_packed = new[num_frames];
// iterate through num_frames
for (int num_frame = 0; num_frame < num_frames; num_frame++) begin
// item
noob_frame fr = noob_frame::type_id::create("noob_frame");
// randomize
if (!noob_frame.randomize()) `uvm_error(get_name(), "Cannot randomize noob_frame");
// pack noob_frame
// I am casting this to void to simplify this example
// This can return a variable sized array for frame_packed[num_frame]
void'(fr.pack_bytes(frame_packed[num_frame]);
end
// fr[][] now contains all 5 noob_frames packed into byte arrays
// call DPI-C function
modulate_my_frame( frame_packed );
这是我的 C 代码:
void modulate_my_frame (const svOpenArrayHandle data)
{
printf( "svLeft1 = %0d\n", svLeft ( data, 1));
printf( "svRight1 = %0d\n", svRight ( data, 1));
printf( "svLow1 = %0d\n", svLow ( data, 1));
printf( "svHigh1 = %0d\n", svHigh ( data, 1));
printf( "svIncrement1 = %0d\n", svIncrement ( data, 1));
printf( "svSize1 = %0d\n", svSize ( data, 1));
printf( "svLeft2 = %0d\n", svLeft ( data, 2));
printf( "svRight2 = %0d\n", svRight ( data, 2));
printf( "svLow2 = %0d\n", svLow ( data, 2));
printf( "svHigh2 = %0d\n", svHigh ( data, 2));
printf( "svIncrement2 = %0d\n", svIncrement ( data, 2));
printf( "svSize2 = %0d\n", svSize ( data, 2));
printf( "svDimensions = %0d\n", svDimensions( data ));
}
我得到的打印输出:
# svLeft1 = 0
# svRight1 = 2
# svLow1 = 0
# svHigh1 = 2
# svIncrement1 = -1
# svSize1 = 3
# svLeft2 = -1
# svRight2 = -1
# svLow2 = -1
# svHigh2 = -1
# svIncrement2 = 0
# svSize2 = -2
# svDimensions = 2
我不明白如何获得子数组的尺寸?
奇怪的是,我可以svGetArrElemPtr2
用来访问整个数组的元素。我只是不知道尺寸是多少。此时,我的解决方法是将另一个 int 数组传递到包含子数组维度的 C 代码中。