简体   繁体   English

使用 Verilator 和 VPI 读取 regs 数组

[英]Reading array of regs using Verilator and VPI

So I have the following register defined in my verilog所以我在我的 verilog 中定义了以下寄存器

reg [31:0] register_mem [0:15]/* verilator public */;

My goal is from my verilator c++ code to read each of the 16 values stored in it.我的目标是从我的 verilator c++ 代码中读取存储在其中的 16 个值中的每一个。

I have found that the documentation for this VPI stuff is rather difficult to find.我发现这个 VPI 东西的文档很难找到。 I still cannot figure out what a t_vpi_vecval is and what its parameters are or if it is even the right approach.我仍然无法弄清楚t_vpi_vecval是什么以及它的参数是什么,或者它是否是正确的方法。

Here is my approach at reading the 5th value in the register这是我读取寄存器中第 5 个值的方法

unsigned int read_regs() {
    const std::string path = "TOP.TOP.cpu.reg_file.register_mem";
    vpiHandle vh1 = vpi_handle_by_name((PLI_BYTE8*)path.c_str(), NULL);
    if (!vh1) { 
        printf("Name %s", path.c_str());
        vl_fatal(__FILE__, __LINE__, "sim_main", "No handle found: ");
    }
    const char* name = vpi_get_str(vpiName, vh1);


    s_vpi_value v;
    v.format = vpiVectorVal;
    vpi_get_value(vh1, &v);
    return v.value.vector[4].aval;
}

No-matter what I do here the method returns 0 suggesting that I am not looking at the register_mem array.无论我在这里做什么,该方法都会返回 0,这表明我没有查看 register_mem 数组。

What am I doing wrong?我究竟做错了什么?

In order to get values of the array, you need to get values for every element of an array separately.为了获取数组的值,您需要分别获取数组中每个元素的值。 VPI does not return values for the full array. VPI 不返回完整数组的值。

Actually the handle which you get for an array is a handle of the vpiRegArray type.实际上,您获得的数组句柄是vpiRegArray类型的句柄。 It can be iterated to access every separate element.可以迭代访问每个单独的元素。

Here is a simple code which does the iteration and prints values of every element in the array:这是一个简单的代码,它执行迭代并打印数组中每个元素的值:


#include "vpi_user.h"

PLI_INT32 preg_calltf( char *txt ) {
    vpiHandle hreg = vpi_handle_by_name("rarr.register_mem", 0);
    vpi_printf("reg type: %s\n", vpi_get_str(vpiType, hreg)); // vpiRegArray

    s_vpi_value val = {vpiDecStrVal}; // struct t_vpi_value

    vpiHandle arrayIterator = vpi_iterate( vpiReg, hreg); 
    if( arrayIterator != NULL ) {
        vpiHandle item = NULL;
        while( NULL != ( item = vpi_scan( arrayIterator ) ) ) {
            vpi_get_value(item, &val);
            vpi_printf("item type: %s = %s\n", vpi_get_str(vpiType, item), val.value.str); // vpiReg
            vpi_free_object( item );
        }
    }

    return 0;
}

In this case I initialized the val with vpiDecStrVal .在这种情况下,我用vpiDecStrVal初始化了val It instructs the compiler to prepare value results as a decimal string.它指示编译器将值结果准备为十进制字符串。 The value is now accessible as val.value.str .该值现在可以作为val.value.str访问。 You have multiple choices to get string or binary data in 2-state or 4-state representation.您有多种选择以 2 状态或 4 状态表示获取字符串或二进制数据。

For 2-state values up to 32 bit you can use an integer formatting.对于高达 32 位的二态值,您可以使用整数格式。 However, for longer values or 4-state, you need vpiVectorVal .但是,对于更长的值或 4 状态,您需要vpiVectorVal It actually requests verilog to create 2 arrays of 32-bit integers, aval and bval.它实际上要求 verilog 创建 2 个 32 位整数数组,aval 和 bval。 Both have sizes big enough to keep all bits of the value.两者都有足够大的大小来保留值的所有位。 Combination of bits in aval and bval represent the 4-state value of all bits in the vector. aval 和 bval 中的位组合表示向量中所有位的 4 态值。

All vpi information is available in LRM including relation diagrams and data structures. LRM 中提供了所有 vpi 信息,包括关系图和数据结构。 There are also some books, for example "the verilog pli handbook" by Sutherland.还有一些书籍,例如 Sutherland 的“verilog pli 手册”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM