cirosantilli/cirosantilli.github.io

How to print an integer to stdout from C++ in printable base 10 ASCII characters in Verilator?

cirosantilli opened this issue · 1 comments

https://stackoverflow.com/questions/38420389/how-to-print-an-integer-to-stdout-from-c-in-printable-base-10-ascii-characters

Note* Verilator transpiles Verilog to C++, so it is not a simple matter of "find the type on the API".

If I have on my Verilog:

module counter (
    /* ... */
    output reg [1:0] out
);

and from the C++ wrapper I do:

Vcounter *top = new Vcounter;
/* Run simulation. */
std::cout << top->out << std::endl;

it prints literal bytes to stdout, so unreadable for numbers like 0 and 1, as can be seen with hd.

How do I get base 10 ASCII human readable output instead?

Tested on Verilator 3.884, Ubuntu 16.04.

std::cout << (int)top->out << std::endl;

seems to work, but I'm not sure if it is the best way. In particular, maybe there is a more suitable type than int.