I will use this example code to ask some general questions about for loops.
The following code is intended to create a simple multiplier function between two 3-bit numbers, a
and b
:
1 function [5:0] mult;
2 input [2:0] a,b;
3 reg [5:0] r;
4 integer i;
5 begin
6 if(a[0] == 1)
7 r = b;
8 else
9 r = 0;
10 for(i=1; i<=2; i = i+1) begin
11 if(a[i]==1)
12 r = r + b <<i;
13 end
14 mult = r; // set function name equal to product
15 end
16 endfunction
I believe this gives the desired result but I can only make sense of it if:
- Lines
7
or9
(depending on line6
obviously) add to the end result of the 'for loop' (which begins on line10
) - The definition of
r
from lines7
or9
is not carried into line12
in the 'for loop' - Each iteration (or 'step') of the 'for loop' does not carry its calculated value for
r
from a prior index valuei
into a subsequent index valuei
If these three conclusions are correct then I have these questions:
- Do 'For Loops' sum their iterations after all possible steps have been made?
- Do 'For Loops' not carry information about an identifier within the 'for loop' derived from one step to the next despite the same identifier appearing in both steps (
r
in this case)? Referring to line12
here. - How should I have known conclusion #1 was true? What is the underlying rule(s)?
Finally, I understand 'for loops' don't necessarily add, they simply repeat if the condition is met. Only in this example is the 'for loop' adding.