【问题标题】:Is it possible to use packed structs with DPI是否可以使用带有 DPI 的打包结构
【发布时间】:2019-09-19 17:35:57
【问题描述】:

假设我有一个打包的结构:

typedef struct packed {
   logic a;
   logic [7:0] b;
   bit  [7:0] c;
   logic [7:0] [31:0] d;
} my_struct;

我想将它传递给 C 函数:

import "DPI" context function int my_dpi_function (input my_struct data);

如何读取 C 端的值?:

int my_dpi_function (void* data) 
{  
    ... ?

    return 0;
}

【问题讨论】:

  • 是的,您可以使用它们。在c 一侧,您只会得到一个比特流。你已经完成了,使用来自svdpi.hget 函数来重新创建你的结构字段。

标签: system-verilog system-verilog-dpi


【解决方案1】:

您需要的类型在svdpi.h 标头中定义:

svLogicVecVal

所以,你需要这样的东西:

int my_dpi_function (svLogicVecVal* data) 
{  
    ... 
    return 0;
}

svLogicVecVal 本身就是一个结构。它有两个字段-avalbval(或者有时,例如在Cadence 中,ab)。来自svdpi.h

typedef struct t_vpi_vecval {
#ifdef P1800_2005_VECVAL
    uint32_t a;
    uint32_t b;
#else
    uint32_t aval;
    uint32_t bval;
#endif
} s_vpi_vecval, *p_vpi_vecval;
#endif

/* (a chunk of) packed logic array */
typedef s_vpi_vecval svLogicVecVal;

avalbval 字段是这样编码的(所谓的“规范表示”):

bval aval | 4-state verilog value
----------|----------------------
  0    0  |   0
  0    1  |   1
  1    0  |   X
  1    1  |   Z

因此,您可以访问 C 中的 avalbval 字段。事实证明,对于大于 32 位的向量,最高有效的 32 位字位于最高指针地址。


https://www.edaplayground.com/x/2k33

SV

module test;

  typedef struct packed {
    logic a;
    logic [7:0] b;
    bit  [7:0] c;
    logic [7:0] [31:0] d;
  } my_struct;

  import "DPI-C" context function int my_dpi_function (logic [272:0] data);

  initial
    begin
      automatic my_struct data = '{1'b0,8'hAA,8'h55,256'h0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF};
      $display("%h",data);
      my_dpi_function (data);
    end

endmodule

C++

#include <iostream>
#include <iomanip>
#include <svdpi.h>

using namespace std;

extern "C" int my_dpi_function (svLogicVecVal* data) {
    data+=8;
    cout << "# C++: ";
    for (int i=0; i<9; i++)
      cout << std::hex << std::setw(8) << std::setfill('0') << (data--)->aval;
    cout << "\n";
  return 0;
}

【讨论】:

  • 这看起来不对。 X 编码为 11。您似乎将 X 和 Z 颠倒了。
猜你喜欢
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多