what is the difference in the delay of before and after non-blocking statements inside a loop?
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开发者_JAVA技巧 <= 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:
- evaluate
r2 + 1
- schedule a non-blocking assignment (NBA) update event to be executed at the next posedge clk
When the simulator executes @(posedge clk) r2 <= r2 + 1
, it performs the following steps:
- suspend the
always
process and schedule it to resume at the next posedge clk - when the process resumes, execute the
r2 <= r2 + 1
NBA, by doing the following:- evaluate
r2 + 1
- schedule an NBA update event for the current time
- evaluate
The 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 while
loop executes in zero time and the events that will increment r2
are 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 r2
to n + 1
, the condition will evaluate as true one more time before that update is applied.
精彩评论