cirosantilli / cirosantilli.github.io

Source for: https://cirosantilli.com and https://ourbigbook.com/cirosantilli Build HTML with https://github.com/ourbigbook/ourbigbook with "npm install && npx ourbigbook ." You can use this issue tracker for anything you want to tell me, even if it is not related to the repo. I also use this repo as generic issue tracker for myself.
https://cirosantilli.com
Creative Commons Attribution Share Alike 4.0 International
42 stars 9 forks source link

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

Open cirosantilli opened 4 years ago

cirosantilli commented 4 years ago

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.

cirosantilli commented 4 years ago
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.