VPI printf 4-bit logic type

If I have something like


module dut;
  logic[3:0] a = 4'b1100;
  logic[3:0] b, = 4'b1010;
endmodule   

module top;
 dut d;
endmodule

test.c :

void print_data() {
 vpiHandle signalHandle, vh, iteration;
  s_vpi_value value;
  vh =  vpi_handle_by_name("top.d", NULL);
  iteration = vpi_iterate(vpiReg, vh);    [size=4]--> is vpiReg correct ?[/size] 
  while ( signalHandle = vpi_scan(iteration)) {
    vpi_get_value(signalHandle, &value);
    vpi_printf("value of iteration = %04x\n",value.value.integer);
    }
}
```verilog
i epxect vpi_printf will print hex value of 4'b1100, and 4'b1010, but i got 
 value of iteration = 0xb6255cb0
 value of iteration = 0xb6255cb0

If you want to print a string of binary digits, you need to set the value.format and use the appropriate string union member str.

void print_data() {
 vpiHandle signalHandle, vh, iteration;
  s_vpi_value value;
  value.format = vpiBinStrVal;
  vh =  vpi_handle_by_name("top.d", NULL);
  iteration = vpi_iterate(vpiVariables, vh);  
  while ( signalHandle = vpi_scan(iteration)) {
    vpi_get_value(signalHandle, &value);
    vpi_printf("value of iteration = %s\n",value.value.str);
    }
}

See Table 38-3—Return value field of the s_vpi_value structure union in the IEEE 1800-2023 SystemVerilog LRM

You can use vpiVariables instead of vpiReg to iterate over a wider range of data types.

1 Like