Looking at this always for loop:
always@ (posedge clk) begin
for (i=1; i<(2*r+3)+1; i=i+1)
begin
v <= v+i;
end
end
This verilog always clause doesn't look like it can be synthesized into hardware.
The for loop describes a single set of hardware with i taking values between 1 and 43 (because the for loop initializes i=1, and r was previously assigned 20, so the limit condition is 43<44; the first value is 1 and the last value is 43).
At first I thought this was going to describe a set of 43 regs v[1..43], but instead the body of the for loop is making multiple non-blocking assignments to a single reg v.
If you unroll the for loop, you get this equivalent code:
always@ (posedge clk) begin
// unrolled r=20; for (i=1; i<(2*r+3)+1; i=i+1)
v <= v+1;
v <= v+2;
v <= v+3;
v <= v+4;
v <= v+5;
// ... etc. ...
v <= v+41;
v <= v+42;
v <= v+43;
end
But it doesn't make sense to have multiple non-blocking assignments to a variable, unless they are split up across different logic paths. There can only be one assigned value at the clock edge, so which one of these assignments is it supposed to use? It can't assign 43 different values to the same register v on every clock edge. So instead of accumulating v=(v+1)+2+3+4+5+6...+41+42+43 like it would if verilog was a programming language, the compiler should instead throw an error message because it can't synthesize this code.
Non-blocking assignment <=
all run at the same time, they don't run in sequence like in a programming language (those are called blocking assignment =
). It's very common to see non-blocking assignment used inside an always@(posedge clk), but each logic path has to determine each value just once, otherwise it can't be synthesized with real hardware.
always @(posedge clk) begin
// shift register Non-blocking assignment example
a1 <= a0;
a2 <= a1;
a3 <= a2;
// all three regs a1, a2, a3 are updated simultaneously, on every posedge clk event
end
always @(posedge clk) begin
// state machine Non-blocking assignment example
state <= NextStateTable[state, inputVector];
outs <= OutputTable[state, inputVector];
// both state and outs are updated simultaneously, on every posedge clk event
end