如何使用PLI例程获取Verilog向量端口的尺寸? [英] How to get dimensions of a verilog vector port using PLI routines?

查看:196
本文介绍了如何使用PLI例程获取Verilog向量端口的尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用vpi PLI例程获取矢量端口的尺寸?例如,对于矢量端口声明输出[2:1] out;",如何将左侧尺寸设为2,将右侧尺寸设为1? 我尝试使用vpiRange属性,但似乎Ports不支持vpiRange属性.谢谢!为了清楚起见,将代码放在这里.

How can I fetch the dimensions of a vector port using the vpi PLI routines? For example, for the vector port declaration "output [2:1] out;", how can I get the left dimension as 2 and right dimension as 1? I tried using vpiRange property but it seems that vpiRange property is not supported for Ports. Thanks! Putting the code here for clarity.

vpiHandle lowconn = vpi_handle(vpiLowConn, portH);   
int dim = 0;
int ldim[10];
int rdim[10];

vpiHandle range_itr = vpi_iterate(vpiRange, lowconn );

vpiHandle range;
while ((range = vpi_scan(range_itr))) {
  ldim[dim] = vpi_get(vpiLeftRange, range);
  rdim[dim] = vpi_get(vpiRightRange, range);
  dim++;
}
int size = vpi_get(vpiSize, portH);
cout << endl << vpi_get_str(vpiName, portH) << " size = " << size << " LeftRange = " << vpi_get(vpiLeftRange, lowconn ) << " RightRange = " << vpi_get(vpiRightRange, lowconn );
for ( int i = 0; i < dim; i++ ) {
  cout << vpi_get_str(vpiName, portH) << " = " << ldim[i] << ":" << rdim[i];
}

我从vpi_get(vpiLeft/RightRange)以及ldim和rdim中得到-1.我的代码有什么错误吗?

I am getting -1 from vpi_get(vpiLeft/RightRange) as well as in ldim and rdim. Is there anything erroneos with my code?

推荐答案

您的问题是vpiLeftRange和vpiRightRange都返回vpiHandle,而不是值.实例化模块时.它们可以是常量表达式,也可以是变量索引表达式(对于highcon).因此,您可以尝试获取它们的值.这是适用于vcs的示例.

Your problem is that both vpiLeftRange and vpiRightRange return vpiHandle, not the value. When you instantiate a module. They could be constant expressions, or variable index expressions (for highcon). so, you can try to get values for them. Here is an example which works for vcs.

verilog模块如下所示:

the verilog module looks like this:

module pvpi (pa, pb);
   input wire [3:0] pa;
   input wire [7:6] pb;

   child child_inst(.a(pa), .b(pb));
   initial $report_ports;
endmodule 

module child(input [4:1] a, input [1:2] b);
endmodule // child

c代码在这里

#include "vpi_user.h"

static int getExprValue(vpiHandle conn, int r) {
    vpiHandle expr = vpi_handle(r, conn);

    s_vpi_value val = {vpiIntVal};
    vpi_get_value(expr, &val);
    return val.value.integer;
}

void report_ports() {
    vpiHandle moduleInst = vpi_handle_by_name("pvpi.child_inst", 0);
    vpiHandle ports = vpi_iterate(vpiPort, moduleInst);
    vpiHandle port;

    vpi_printf("%s %s\n", vpi_get_str(vpiDefName, moduleInst), vpi_get_str(vpiFullName, moduleInst));

    while ((port = vpi_scan(ports))) {

        vpiHandle highConn = vpi_handle(vpiHighConn, port);
        vpiHandle lowConn = vpi_handle(vpiLowConn, port);

        vpi_printf("   %d %s [%d:%d]/[%d:%d]\n", vpi_get(vpiDirection, port),  vpi_get_str(vpiFullName, port),
                   getExprValue(highConn, vpiLeftRange), getExprValue(highConn, vpiRightRange),
                   getExprValue(lowConn, vpiLeftRange), getExprValue(lowConn, vpiRightRange)); 

    }
}

这是结果

child pvpi.child_inst
   1 pvpi.child_inst.a [3:0]/[3:0]
   1 pvpi.child_inst.b [7:6]/[1:2]

这应该适用于带有参数和文字的简单常量表达式.

This should work for simple constant expressions with parameters and literals.

这篇关于如何使用PLI例程获取Verilog向量端口的尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆