Difficulty: Easy · Puzzle 2 of 6 · Topic: Event Scheduling Puzzles
Predict both prints at time 0 and explain why they differ even though there is no real time advance.
module tb;
int x;
initial begin
x = 0;
x <= 1;
#0 x = 2;
$display("[%0t INACTIVE] x=%0d", $time, x);
$strobe("[%0t POSTPONED] x=%0d", $time, x);
end
endmodule#0 lands in inactive; NBA updates happen after inactive.
1) Active region schedules x<=1 for NBA.
2) #0 resumes in inactive and assigns x=2.
3) $display in inactive prints x=2 at t=0.
4) NBA region then updates x to 1.
5) $strobe runs in postponed and prints x=1 at t=0.Answer: Inactive print is [0] x=2, postponed print is [0] x=1. #0 executes before NBA in the same time slot.
Checked by running the code in Icarus Verilog 13 and Verilator 5; the printed output supports the answer above. LRM 4.4: #0 resumes in the Inactive region, before the NBA, so the strobe prints x=1. Verilator 5.048 completes the NBA before the #0 resumes (it does not model the Inactive region) and prints x=2; the LRM answer is x=1.
Many engineers think #0 moves after NBA because it 'feels later'. It does not.
If you replace #0 with #1, what changes in ordering and printed time?
At time zero the #0 resumes in the Inactive region, which comes before the NBA region, so the display prints x equal to 2. The NBA then sets x to 1, and the strobe in Postponed prints 1. The trap is thinking that #0 moves you after the NBA because it feels later. I would also mention that Verilator completes the NBA first and prints 2 for the strobe, so I would trust the LRM and an event-driven simulator here, not that one.