1

我在 EDA Playground 的 VCS 中使用 DPI-C 的以下代码没有得到正确的输出。我希望 6 作为答案,但无论 a 和 b 值如何,我每次都会得到 248。我尝试使用 svLogic、int 和 unsigned char 作为 helloFromC.c 中 a_int 的数据类型。

module automatic test;

  import "DPI-C" function void helloFromC(logic [2:0] a, logic [2:0] b);

  initial run();

  task run();
    logic [2:0] a; 
    logic [2:0] b; 
    logic [2:0] c;
    a = 3'b100;
    b = 3'b010;
    c = a+b;
    $display("Output from SV is %0d", c);
    helloFromC(a,b);

  endtask

endmodule

这是我的 C 程序

#include <stdio.h>
#include <svdpi.h>


extern "C" int helloFromC(svLogic a, svLogic b) {
  svLogic a_int = a+b;
  printf("Output from C is %d", a_int);
  return 0;
}

我得到输出

Output from SV is 6
Output from C is 248
4

2 回答 2

2

svLogic应该映射到一个位logic。你有一个向量(又名打包数组),因此你应该使用svLogicVecVal. 它仍然是一个 4 状态值,因此在 C 端执行的 SystemVerilog 值的算法操作可能无法按您期望的方式工作。bit [2:0]在 SystemVerilog 端和C 端使用svBitVecVal会更符合您的预期。或者简化事情并int在双方使用。

有关 DPI 的更多信息,请参阅IEEE1800-2012第 35 节、附录 H 和附录 I。

于 2017-09-10T22:41:02.333 回答
2

从其中一个链接中,使用 DPI 调用添加,我可以找到我正在寻找的内容

#include <stdio.h>
#include <svdpi.h>


extern "C" void
add_bpv(
    const svBitVecVal* a,
    const svBitVecVal* b,
    svBitVecVal* c) {
    *c = *a + *b;
    printf("Output from C is %d", *c);
}

现在 SV 程序 DPI 调用

module automatic test;

  import "DPI-C" function void add_bpv(input bit [3:0] a,b, output bit [3:0] c);

  initial run();

  task run();
    bit [3:0] a,b,c;
    a = 3'b100;
    b = 3'b010;
    c = a+b;
    $display("Output from SV is %d", c);
    add_bpv(a,b,c);
  endtask

endmodule

输出是我想要的

Output from SV is  6
Output from C is 6 
于 2017-09-11T06:18:49.460 回答