5

When executing the following piece of SystemVerilog code (compiled and run with Questa)

bit [7:0] test = 255;
$display("%b %b %b", test,        test == 255,      test == '1);
$display("%b %b %b", ~test,       ~test == 0,       ~test == '0);
$display("%b %b %b", 8'b00000000, 8'b00000000 == 0, 8'b00000000 == '0);

the output is

11111111 1 1
00000000 0 1
00000000 1 1

My question is about the 2nd number on the 2nd output line: How is binary 00000000 different from 0? And why is it only different if it is the result of ~test, and not when it is a literal? Is this a Questa bug or a property of the language?

PieterNuyts
  • 496
  • 5
  • 20

1 Answers1

8

The difference is that 0, without any width prefix defaults to 32-bit value. In an equality, the operands get sized to the maximum width between the expressions on the LHS and RHS expressions before evaluating those expressions. Try ~test == 9'h0 and ~test == 9'h100 and see what you get.

The size of '0 is based on its context. So ~test =='0 become ~test == 8'b0 in that context.

toolic
  • 57,801
  • 17
  • 75
  • 117
dave_59
  • 39,096
  • 3
  • 24
  • 63
  • 2
    So you're saying that (in your 9-bit example) `test` gets expanded to `9'h0ff` and then inverted to `9'h100`? I was considering something like this but I was assuming it would first calculate `~test` (which would be `8'h00`) and then expand that to `9'h000`. – PieterNuyts Apr 12 '17 at 15:24
  • That's what I'm saying. See _11.8.2 Steps for evaluating an expression_ in the 1800-2012 LRM. – dave_59 Apr 12 '17 at 15:49
  • I would hardly consider that to be a feature but maybe that's personal taste... Thanks for explaining! – PieterNuyts Apr 25 '17 at 13:19