Search code examples
system-verilogquartus

Correct syntax of SystemVerilog $display to produce formatted messages in Quartus message window


I often use $display, $error, $fatal functions in SystemVerilog to display all sorts of numeric values. When working with Questa for example, the following will correctly display the output to the console:

localparam uint64_t timeout_active = integer'(500e-6 / (g_clockPeriod * 1e-9));
$display(g_instance_name, ": Timeout to get 500us active time is %0d clock cycles", 
         timeout_active);

The following correctly displays in the console:

# motor: Timeout to get 500us active time is 25000 clock cycles

When I write %d instead of %0d, modelsim prepends spaces in place of zeros, the resulting console string looks as:

# motor: Timeout to get 500us active time is                25000 clock cycles

The same thing, during Quartus compilation however, it displays the messages completely differently.

This is how it shows when using %0d in the display:

Info (10648): Verilog HDL Display System Task info at motor.sv(162): rtm: 
Timeout to get 500us active time is %0d clock 
cycles64'b0000000000000000000000000000000000000000000000001111010000100100

The Quartus seems to ignore any formatting requests, and in addition, for certain types (as here, 64-bit integer), ignores decimal display, and shows binary representation.

The latter one can be caused just by 'logic' type used (uint64_t = logic [63:0]), but Quartus ignores formatting on ordinary integers/reals as well.

What is the proper way to use $display with Quartus?


Solution

  • Some tools permit string formatting to only with the first argument. Looking through the IEEE Std 1800-2023 LRM, most of the examples of $display and $write use only the first argument for string formatting.

    Per the IEEE Std 1800-2023 § 21.3.3 Formatting data to a string:

    Unlike the display and write family of output system tasks, $sformat always interprets its second argument, and only its second argument, as a format string.

    Quartus probability reusing the same underlining code to implement $display as it did with the the more restrictive $sformat.

    Moving your g_instance_name after the format sting and including it into the the format string should work:

    $display("%s: Timeout to get 500us active time is %0d clock cycles",
             g_instance_name, timeout_active);