Difficulty: Hard · Puzzle 6 of 6 · Topic: Race Condition Puzzles
When load and clear are both 1 on the same edge, what can data become? Is the result language-deterministic?
module tb;
bit clk = 0;
bit load = 1;
bit clear = 1;
byte in = 8'hA5;
byte data = 8'h00;
always #5 clk = ~clk;
always_ff @(posedge clk) begin
if (load) data <= in;
end
always_ff @(posedge clk) begin
if (clear) data <= 8'h00;
end
initial begin
@(posedge clk);
$strobe("[%0t] data=%0h", $time, data);
$finish;
end
endmoduleTwo different processes enqueue NBAs to same variable in same slot.
1) Both always_ff blocks schedule NBA updates for data at t=5.
2) Relative ordering of those cross-process NBAs is not guaranteed by intent and is tool/order dependent.
3) data may end as 8'hA5 or 8'h00 depending on process order.
4) Architecturally correct fix is single-writer sequential logic.Answer: Result is nondeterministic: data can be 8'hA5 or 8'h00 at t=5. Multiple sequential writers create a race.
Using always_ff does not legalize multiple writers to the same flopped signal.
Refactor this into one always_ff with explicit priority (clear over load or vice versa).