2

SystemVerilog LRM 有一些示例展示了如何通过 DPI-C 层将 SystemVerilog 中的结构传递给\from C。但是,当我尝试自己的示例时,它似乎在 Incisive 或 Vivado 模拟器中根本不起作用(它在 ModelSim 中确实起作用)。我想知道我是否做错了什么,或者这是否是模拟器的问题。我的例子如下:

#include <stdio.h>

typedef struct {
               char f1;
               int f2;
} s1;

void SimpleFcn(const s1 * in,s1 * out){
    printf("In the C function the struct in has f1: %d\n",in->f1);
    printf("In the C function the struct in has f2: %d\n",in->f2);
    out->f1=!(in->f1);
    out->f2=in->f2+1;    
}

我将上面的代码编译成一个共享库:

gcc -c -fPIC -Wall -ansi -pedantic -Wno-long-long -fwrapv -O0 dpi_top.c -o dpi_top.o
gcc -shared -lm dpi_top.o -o dpi_top.so

和 SystemVerilog 代码:

`timescale 1ns / 1ns
typedef struct {
               bit f1;
               int f2;
               } s1;

import "DPI-C" function void SimpleFcn(input s1 in,output s1 out);

module top();
  s1 in,out;
  initial
    begin    
    in.f1=1'b0;  
    in.f2 = 400;
    $display("The input struct in SV has f1: %h and f2:%d",in.f1,in.f2);
    SimpleFcn(in,out);
    $display("The output struct in SV has f1: %h and f2:%d",out.f1,out.f2);
 end

endmodule 

在 Incisive 中,我使用 irun 运行它:

irun -sv_lib ./dpi_top.so -sv ./top.sv

但它是 SegV 的。

在 Vivado 中,我使用

xvlog -sv ./top.sv 
xelab top -sv_root ./ -sv_lib dpi_top.so -R

它运行良好,直到退出模拟,然后出现内存损坏:

Vivado Simulator 2017.4
Time resolution is 1 ns
run -all
The input struct in SV has f1: 0 and f2:        400
In the C function the struct in has f1: 0
In the C function the struct in has f2: 400
The output struct in SV has f1: 1 and f2:        401
exit
*** Error in `xsim.dir/work.top/xsimk': double free or corruption (!prev): 0x00000000009da2c0 ***
4

1 回答 1

2

你很幸运这在 Modelsim 中有效。您的 SystemVerilog 原型与您的 C 原型不匹配。您在 C 和SystemVerilog中都有f1作为。bytebit

Modelsim/Questa 有一个 -dpiheader 开关,可以生成一个 C 头文件,您可以#include将其放入 dpi_top.c 文件中。这样,当原型不匹配时会出现编译器错误,而不是不可预测的运行时错误。这是您的 SV 代码的 C 原型。

typedef struct {
    svBit f1;
    int f2;
}  s1;

void SimpleFcn(
    const s1* in,
    s1* out);

但我建议在 SystemVerilog 中坚持使用 C 兼容类型。

于 2018-05-15T14:45:56.597 回答