User guide
2-6
Modeling Your Design
if (state0)
// do something
end
The modification of state may propagate to state0 before the if
statement, causing unexpected behavior. You can avoid this by using
the nonblocking assignment to state in the procedural code as
follows:
state <= 0;
if (state0)
// do something
This guarantees that state is not updated until the end of the time
step, that is, after the if statement has executed.
Counting Events
A different type of race condition occurs when code depends on the
number of times events are triggered in the same time step. For
instance, in the following example, if A and B change at the same
time, it is unpredictable whether count is incremented once or twice:
always @(A or B)
count = count + 1;
Another form of this race condition is to toggle a register within the
always block. If toggled once or twice, the result may be unexpected
behavior.
The solution to this race condition is to make the code inside the
always block insensitive to the number of times it is called.