当前位置: 代码迷 >> 综合 >> 怎样在systemverilog DPI中调用SV,C与C++(二)
  详细解决方案

怎样在systemverilog DPI中调用SV,C与C++(二)

热度:28   发布时间:2024-01-15 12:29:53.0

有的时候需要交互struct,union数据类型,下面举个简单例子。

在SV里定义这个一个结构体:

typedef struct {int     id; string  name;
} xaction; 

同样的,在C里定义同样的结构体,结构体名可以不一致:

typedef struct {int     id; char*   name;} c_xaction;
下面把这个结构体传递给C,让C打印出来,SV中做好接口:

import "DPI-C" function void c_print(input xaction x);


C中实现打印函数:

void c_print (c_xaction* x) {                                                                                                                                                                printf("id = %0d, name = %s\n", x->id, x->name);
}
这里需要注意很重要的一点,SV中的形参类型是xaction, 而C中的形参类型是结构体指针,不可以搞错。

        xaction x;x.id    = 2;x.name  = "abc";c_print(x);

打印出:id = 2, name = abc


下面再来看看union的情形,DPI只能传递packed union, 不熟悉packed概念的请参考SV LRM IEEE1800.

SV:

typedef union packed{ int i; bit[31:0] f; } num;

C:

typedef union { int i; float f; } num; 

这时和struct一样,C中的形参只能为指针类型。这里取个巧,把第二个数做成float类型。在sv中赋值:

 n.i = 32'h3FFF_FFFF;

这时在C中printf n.f会得到2.000000.注意高低位


struct中套struct可以支持,只要是packed类型就可以,但是注意struct指定位宽是用不了DPI的:

typedef struct {int     n0 : 10 ; int     n1 : 6;
} invalid_xaction;