我有可以生成可变长度数组作为其结果的 C 代码(预测模型)。在调用 C 代码之前不知道这个数组的大小是多少,并且涉及到一些随机化(噪声建模)
我需要从 SystemVerilog 调用这个 C 预测器模型,并取回输出结果数组。
作为 DPI-C 的新手,我遇到了 3 个限制:
- SV 端的结果数组需要在调用 C 代码之前分配。因为我不知道大小会是多少,所以我有可能过度分配或分配不足。
- 我无法在导出函数中从 C -> SV 传递一个开放数组。
- 导出函数不能用于类方法(!)
为了解决这个问题,我在 2 个接口和全局变量/任务之间创建了一个 hacky 杂耍。
我已经发布了我的解决方案,它运行良好,但我想知道是否有人有比这更优雅的解决方案。我特别不喜欢使用全局变量。
SV:
export "DPI-C" function allocate_mem;
export "DPI-C" function set_item;
import "DPI-C" context function void predictForMe (input int noiseA, input int noiseB);
int result[];
function void allocate_mem(int size);
result = new[size];
endfunction
function void set_item(int index, int item);
result[index] = item;
endfunction
class my_class;
// constructor etc etc - assume is valid
// my_func
function void my_func();
int noiseA = 10; // hardcode to simplify example
int noiseB = 20; // hardcode to simplify example
// call imported function
predictForMe( noiseA, noiseB );
endfunction
endclass
C:
extern void allocate_mem(int size);
extern void set_item(int index, int item);
void predictForMe(int noiseA, int noiseB)
{
// do some calcualation based on noiseA and noiseB
// generates an answer_array with num elements = X
allocate_mem(X);
for(i = 0; i < X; i++) set_item(i, answer_array[i]);
}
欢迎任何更好的解决方案。