在 DPI-C 中,内部变量使用什么数据类型?

In DPI-C, what data types to be used for internal variables?

我在 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

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

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

从其中一个链接,addition using DPI call我可以找到我要找的东西

#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