SystemVerilog / DPI 中的类型擦除
Type erasure in SystemVerilog / DPI
是否可以将 SystemVerilog 结构转换为某些类型擦除的指针,例如 void *
?
我需要将不同结构类型的对象从 SV 传递到 C。我希望有一个 DPI 函数来处理任何类型的支持结构。
在 SystemVerilog 方面我有很多类型:
typedef struct { ... } stype1;
typedef struct { ... } stype2;
typedef struct { ... } stype3;
typedef struct { ... } stype4;
stype1 var1;
stype2 var2;
stype3 var3;
stype4 var4;
在 C 端,我有一个接受任何类型的函数:
void send_sv_data(const char* type_name, void *ptr);
我希望在 SV 端像这样使用它:
send_sv_data("stype1", var1);
send_sv_data("stype2", var2);
一般都能用,唯一的问题是SystemVerilog不支持重载。所以我只能为一种类型声明它:
import "DPI" function void send_sv_data(string port_name, stype1 data);
我试过用 chandle 作为参数:
import "DPI" function void send_sv_data(string port_name, chandle data);
但是我还没有找到将结构类型的变量转换为 chandle 的方法。
SystemVerilog 中没有指针,也没有指针转换。最简单的做法是将您的 SystemVerilog 结构打包成一个字节数组,然后将它们解压到您的 C 结构中。
是否可以将 SystemVerilog 结构转换为某些类型擦除的指针,例如 void *
?
我需要将不同结构类型的对象从 SV 传递到 C。我希望有一个 DPI 函数来处理任何类型的支持结构。
在 SystemVerilog 方面我有很多类型:
typedef struct { ... } stype1;
typedef struct { ... } stype2;
typedef struct { ... } stype3;
typedef struct { ... } stype4;
stype1 var1;
stype2 var2;
stype3 var3;
stype4 var4;
在 C 端,我有一个接受任何类型的函数:
void send_sv_data(const char* type_name, void *ptr);
我希望在 SV 端像这样使用它:
send_sv_data("stype1", var1);
send_sv_data("stype2", var2);
一般都能用,唯一的问题是SystemVerilog不支持重载。所以我只能为一种类型声明它:
import "DPI" function void send_sv_data(string port_name, stype1 data);
我试过用 chandle 作为参数:
import "DPI" function void send_sv_data(string port_name, chandle data);
但是我还没有找到将结构类型的变量转换为 chandle 的方法。
SystemVerilog 中没有指针,也没有指针转换。最简单的做法是将您的 SystemVerilog 结构打包成一个字节数组,然后将它们解压到您的 C 结构中。