9

How do I print a signed integer value stored in an 8-bit register declared as:

reg [7:0] acc;

Using:

$display("acc : %d", acc)

It prints the unsigned value.

What's the correct syntax for the $display function?

toolic
  • 57,801
  • 17
  • 75
  • 117
Nullpoet
  • 10,949
  • 20
  • 48
  • 65

2 Answers2

12

If you declare the reg as signed, $display will show the minus sign:

module tb;

reg signed [7:0] acc;

initial begin
    acc = 8'hf0;
    $display("acc : %d", acc);
end

endmodule

Prints out:

acc :         -16
toolic
  • 57,801
  • 17
  • 75
  • 117
9

Ran into this problem as well and looked through the SystemVerilog 2012 standard, but didn't see any mention of signedness in the section about format specifiers. Here's an alternative (basically equivalent) solution that also works:

$display("acc : %d", $signed(acc))

The "$signed" function converts the input value into a signed type with the same bitwidth.

campkeith
  • 612
  • 8
  • 9