I\'m trying to understand why we use generate in verilog along with a for loop.
Using a generate and for loop together:
reg [3:0] temp;
genvar i;
gen
In general, the main difference between generate for loop and regular for loop is that the generate for loop is generating an instance for each iteration. Meaning that in your example there will be 3 always blocks (as opposed to 1 block in the regular loop case).
A good example of code that requires generate for is:
module A();
..
endmodule;
module B();
parameter NUM_OF_A_MODULES = 2; // should be overriden from higher hierarchy
genvar i;
for (i=0 i
In this example a regular for cannot do the work of creating NUM_OF_A_MODULES instances.
In your example, you can acheive the required result in both ways. (as long as you fix some minor bugs :) )