what is the difference between this two code snippets?
always @(posedge clk) begin
r3 <= @(posedge clk) 1;
r2 <= @(posedge clk) 1;
ready = 0;
while (r2 <= n) begin
r2 <= @(posedge clk) r2 + 1; <--- never stops executing
end
end
always @(posedge clk) begin
r3 <= @(posedge clk) 1;
r2 <= @(posedge clk) 1;
ready = 0;
while (r2 <= n) begin
@(posedge clk) r2 <= r2 + 1; <--- normally executes
end
end
When the simulator executes
r2 <= @(posedge clk) r2 + 1, it performs the following steps:r2 + 1When the simulator executes
@(posedge clk) r2 <= r2 + 1, it performs the following steps:alwaysprocess and schedule it to resume at the next posedge clkr2 <= r2 + 1NBA, by doing the following:r2 + 1The first form parses as a non-blocking assignment, and executes in zero time. The delay only applies to the update event generated when the NBA executes. The second form parses as a statement delay control followed by an NBA. It does not execute in zero time because the delay applies to the execution of the statement rather than just the update event.
The first form is an infinite loop because the body of the
whileloop executes in zero time and the events that will incrementr2are scheduled for a future time.With the second form, you will still want to be careful about boundary conditions when the loop terminates. After scheduling an update that sets
r2ton + 1, the condition will evaluate as true one more time before that update is applied.