Verilog 2.7
A New Mental Model: Procedural Repetition vs Structural Replication
So far, we have studied procedural loops such as:
for
while
repeat
For example:
always@(*) begin
for (i = 0; i < 8; i = i + 1) begin
out[i] = in[7-i];
end
end
This is a PROCEDURAL 'for' loop.
It describes repeated procedural operations.
Now we are introducing something different:
GENERATE CONSTRUCTS
Generate constructs are used to control the STRUCTURE that is created when the design is elaborated.
A generate 'for' loop can be used to create repeated structural elements.
Therefore:
PROCEDURAL 'for'
↓
Repeated procedural operations
GENERATE 'for'
↓
Repeated structural generation during elaboration
Although both use the keyword 'for', they serve different purposes.
Why Do We Need Generate Constructs?
Suppose we want to create the same structural pattern many times.
For example, imagine two 8-bit vectors:
a[7:0]
and:
b[7:0]
and we want:
out[0] = a[0] & b[0]
out[1] = a[1] & b[1]
out[2] = a[2] & b[2]
...
out[7] = a[7] & b[7]
We could manually write eight continuous assignments.
However, Verilog allows us to describe repeated structure using a generate loop.
Conceptually:
One structural pattern
↓
Generate loop
↓
Repeated structural elements are elaborated
This becomes especially useful when the repeated structure contains module instances or other structural constructs.
Introducing 'genvar'
A generate 'for' loop commonly uses a special variable declared using:
genvar
For example:
genvar i;
Here:
i
is a GENERATE LOOP VARIABLE.
It is used while the design hierarchy and generated structure are being elaborated.
This is different from the:
integer i;
that we previously used as a procedural loop variable.
'integer i' vs 'genvar i'
For a PROCEDURAL loop, we previously used:
integer i;
Example:
integer i;
always@(*) begin
for (i = 0; i < 8; i = i + 1) begin
out[i] = in[7-i];
end
end
For a GENERATE loop, we can use:
genvar i;
Example:
genvar i;
generate
for (i = 0; i < 8; i = i + 1) begin : AND_STAGE
assign out[i] = a[i] & b[i];
end
endgenerate
Therefore:
integer
↓
Can serve as a procedural loop variable
genvar
↓
Used as a generate-loop variable during elaboration
Do not treat 'genvar' as an ordinary runtime signal inside the finished circuit.
Basic Generate 'for' Syntax
A traditional explicit form is:
genvar i;
generate
for (i = 0; i < 8; i = i + 1) begin : BLOCK_NAME
// generated structure
end
endgenerate
The major parts are:
genvar i;
↓
Declares the generate-loop variable
generate
↓
Begins the explicit generate region
for (...)
↓
Specifies the repeated generation
begin : BLOCK_NAME
↓
Creates a named generated block
endgenerate
↓
Ends the explicit generate region
Example: Generating Eight AND Relationships
Consider:
module vector_and(
input wire [7:0] a,
input wire [7:0] b,
output wire [7:0] out
);
genvar i;
generate
for (i = 0; i < 8; i = i + 1) begin : AND_STAGE
assign out[i] = a[i] & b[i];
end
endgenerate
endmodule
The generate loop describes repeated structural assignments.
Conceptually, it produces the equivalent relationships:
assign out[0] = a[0] & b[0];
assign out[1] = a[1] & b[1];
assign out[2] = a[2] & b[2];
assign out[3] = a[3] & b[3];
assign out[4] = a[4] & b[4];
assign out[5] = a[5] & b[5];
assign out[6] = a[6] & b[6];
assign out[7] = a[7] & b[7];
Does the Generate Loop Run While the FPGA Is Operating?
No.
This is one of the most important ideas in this Unit.
We should NOT imagine:
FPGA starts operating
↓
i = 0
↓
Generate first AND relationship
↓
i = 1
↓
Generate another AND relationship
↓
...
The generate loop is resolved while the design is being ELABORATED.
Conceptually:
Verilog source
↓
Generate loop evaluated during elaboration
↓
Required structural elements/scopes are created
↓
Elaborated design contains the resulting structure
↓
The design is synthesized and implemented
The finished FPGA does not repeatedly execute the generate loop during normal runtime.
What Is Elaboration?
ELABORATION is the stage in which the HDL design is expanded and resolved into a specific design hierarchy/configuration.
During elaboration, tools resolve things such as:
- Module hierarchy
- Parameter values
- Module instances
- Generate loops
- Generate conditions
- Generated scopes
Conceptually:
HDL source
+
Parameter values
↓
ELABORATION
↓
Specific design hierarchy and generated structure
This happens before we think about the circuit operating with runtime input signals.
Elaboration vs Runtime
These are fundamentally different stages.
ELABORATION
Happens while the design structure is being constructed by the HDL tool flow.
Examples:
- Resolve WIDTH
- Evaluate generate-loop bounds
- Create generated scopes
- Select generate branches
RUNTIME / HARDWARE OPERATION
Happens after the design has been implemented and is operating.
Examples:
- Input a changes
- Clock rises
- Register changes
- Combinational output responds
Therefore:
genvar
↓
Elaboration concept
input wire enable
↓
Runtime signal concept
Elaboration vs Synthesis
Elaboration and synthesis are closely related in an HDL tool flow, but they are not conceptually identical.
A useful mental model is:
Verilog source
↓
ELABORATION
↓
Resolve parameters, hierarchy, instances, and generate constructs
↓
Elaborated design
↓
SYNTHESIS
↓
Infer, optimize, and map the described logic into an implementable hardware representation
Some software tools may combine or overlap these stages internally.
However, conceptually:
ELABORATION
↓
Determines what configured design structure exists
SYNTHESIS
↓
Determines how the synthesizable design is represented and optimized as hardware
Structural Replication
STRUCTURAL REPLICATION means creating multiple copies of a structural pattern.
For example:
generate
for (i = 0; i < 8; i = i + 1) begin : AND_STAGE
assign out[i] = a[i] & b[i];
end
endgenerate
describes eight generated structural relationships.
Conceptually:
One description:
assign out[i] = a[i] & b[i];
↓
Generate loop
↓
Repeated for each generate index
↓
Eight output relationships exist in the elaborated design
This is different from using a procedural loop to repeatedly execute procedural statements.
Generate Loop Bounds Must Be Resolvable During Elaboration
Consider:
for (i = 0; i < 8; i = i + 1)
The tool knows that the generate loop creates eight iterations.
Likewise:
for (i = 0; i < WIDTH; i = i + 1)
can be used when WIDTH is an elaboration-time parameter.
The important idea is:
The generate-loop structure must be determinable while the design is being elaborated.
An ordinary runtime input signal should not be thought of as controlling how many generated structural instances exist after the FPGA has already started operating.
Named Generate Blocks
Consider:
begin : AND_STAGE
The identifier:
AND_STAGE
gives the generated block a name.
This is called a NAMED GENERATE BLOCK.
For example:
generate
for (i = 0; i < 8; i = i + 1) begin : AND_STAGE
assign out[i] = a[i] & b[i];
end
endgenerate
The generated scopes can conceptually appear as:
AND_STAGE[0]
AND_STAGE[1]
AND_STAGE[2]
AND_STAGE[3]
AND_STAGE[4]
AND_STAGE[5]
AND_STAGE[6]
AND_STAGE[7]
Each generated iteration has its own generated scope.
Why Name Generate Blocks?
Named generate blocks make the generated hierarchy easier to understand and identify.
They can be useful for:
- Viewing hierarchy
- Simulation
- Debugging
- Waveform inspection
- Referring to generated scopes or instances when appropriate
Instead of having several anonymous-looking repeated structures, a name such as:
AND_STAGE
communicates what the generated group represents.
Generate Hierarchy
A generate loop does not merely repeat source text conceptually.
Each generate iteration can introduce a separate scope in the elaborated hierarchy.
For example:
AND_STAGE[0]
can represent the generated scope associated with:
i = 0
while:
AND_STAGE[1]
corresponds to:
i = 1
and so on.
Conceptually:
vector_and
│
├── AND_STAGE[0]
├── AND_STAGE[1]
├── AND_STAGE[2]
├── AND_STAGE[3]
├── AND_STAGE[4]
├── AND_STAGE[5]
├── AND_STAGE[6]
└── AND_STAGE[7]
This generated hierarchy can be useful when examining the design in simulation or design tools.
Generate Loops Can Contain Continuous Assignments
One possible use of a generate loop is repeating continuous assignments.
Example:
genvar i;
generate
for (i = 0; i < 8; i = i + 1) begin : AND_STAGE
assign out[i] = a[i] & b[i];
end
endgenerate
Each iteration creates the appropriate structural assignment for that index.
This is especially useful when the assignment forms part of a larger repeated structural pattern.
Do We Always Need a Generate Loop for Vector AND?
No.
For a simple vector AND, we can write:
assign out = a & b;
This is much shorter.
So why learn the generate version?
Because the example clearly demonstrates how structural replication works.
Generate loops become much more valuable when each iteration contains something more substantial, such as:
- A module instance
- Several structural connections
- Parameterized repeated hardware
- Different per-index wiring
- Repeated structural blocks
Therefore, the vector-AND example is primarily a simple teaching example for generate structure.
Generate Loops Can Instantiate Modules
This is one of the most important uses of generate loops.
Suppose we have a module:
module and_gate(
input wire a,
input wire b,
output wire y
);
assign y = a & b;
endmodule
We can instantiate one copy using:
and_gate U1(
.a(a),
.b(b),
.y(y)
);
But suppose we want eight copies.
Instead of manually writing eight module instances, we can use a generate loop.
Repeated Module Instantiation
For example:
genvar i;
generate
for (i = 0; i < 8; i = i + 1) begin : GATES
and_gate U1(
.a(a[i]),
.b(b[i]),
.y(out[i])
);
end
endgenerate
The generate loop causes the elaborated design to contain eight instances of:
and_gate
Conceptually:
GATES[0].U1
GATES[1].U1
GATES[2].U1
...
GATES[7].U1
Each generated module instance receives a different pair of vector bits.
Expanding the Module-Instantiation Example
Conceptually, the generate loop:
for (i = 0; i < 8; i = i + 1) begin : GATES
and_gate U1(
.a(a[i]),
.b(b[i]),
.y(out[i])
);
end
represents structural instances equivalent in purpose to:
and_gate instance for bit 0
↓
a[0], b[0] → out[0]
and_gate instance for bit 1
↓
a[1], b[1] → out[1]
and_gate instance for bit 2
↓
a[2], b[2] → out[2]
...
and_gate instance for bit 7
↓
a[7], b[7] → out[7]
The important point is:
There are multiple module instances in the elaborated design.
It is not one module instance being repeatedly reused over time.
One Instance Reused Over Time vs Multiple Generated Instances
This distinction is essential.
DO NOT think:
One and_gate instance
↓
Processes bit 0
↓
Then processes bit 1
↓
Then processes bit 2
↓
...
That would describe time-multiplexed behavior requiring additional control.
Instead, a generate loop that instantiates the module eight times produces:
and_gate instance 0
and_gate instance 1
and_gate instance 2
...
and_gate instance 7
as separate structural instances in the elaborated design.
Therefore:
GENERATE REPLICATION
≠
Runtime reuse of one hardware instance
Parameterized Generate Loops
Generate loops become even more useful when combined with parameters.
For example:
module vector_and #(
parameter WIDTH = 8
)(
input wire [WIDTH-1:0] a,
input wire [WIDTH-1:0] b,
output wire [WIDTH-1:0] out
);
genvar i;
generate
for (i = 0; i < WIDTH; i = i + 1) begin : AND_STAGE
assign out[i] = a[i] & b[i];
end
endgenerate
endmodule
Now the number of generated iterations depends on WIDTH.
What Happens When WIDTH Changes?
Suppose:
WIDTH = 8
Then the generate loop elaborates:
8 generated iterations.
If:
WIDTH = 16
then:
16 generated iterations.
If:
WIDTH = 32
then:
32 generated iterations.
Conceptually:
Parameter WIDTH
↓
Known during elaboration
↓
Controls generate-loop bound
↓
Determines amount of generated structure
This is different from changing an ordinary runtime input.
Parameterized Module Replication
The same idea can be applied to repeated module instances.
For example:
parameter WIDTH = 8;
genvar i;
generate
for (i = 0; i < WIDTH; i = i + 1) begin : GATES
and_gate U1(
.a(a[i]),
.b(b[i]),
.y(out[i])
);
end
endgenerate
If WIDTH = 8:
8 module instances are elaborated.
If WIDTH = 16:
16 module instances are elaborated.
Therefore, one source description can represent different amounts of repeated hardware structure.
Generate Index vs Runtime Signal
Suppose the generate loop uses:
genvar i;
The values of i are used while elaborating the design.
For example:
i = 0
↓
Create the structure for generated iteration 0
i = 1
↓
Create the structure for generated iteration 1
and so on.
But after elaboration, we should not imagine a physical signal called i counting through these values during normal operation.
Therefore:
genvar i
≠
Runtime counter
genvar i
≠
Input signal
genvar i
≠
Register changing every clock
It is an elaboration-time generate index.
Procedural 'for' vs Generate 'for'
Now we can make the major comparison.
PROCEDURAL 'for'
Example:
integer i;
always@(*) begin
for (i = 0; i < 8; i = i + 1) begin
out[i] = in[7-i];
end
end
Purpose:
Describe repeated PROCEDURAL operations.
GENERATE 'for'
Example:
genvar i;
generate
for (i = 0; i < 8; i = i + 1) begin : GATES
and_gate U1(...);
end
endgenerate
Purpose:
Create repeated STRUCTURAL constructs during elaboration.
Procedural Loop Variable vs Generate Loop Variable
PROCEDURAL:
integer i;
The variable participates in procedural execution/simulation of the block.
GENERATE:
genvar i;
The variable is used to control generate-loop elaboration.
Therefore:
integer loop variable
↓
Procedural context
genvar
↓
Generate/elaboration context
They should not be treated as interchangeable concepts even though both may appear in similar-looking 'for' syntax.
Similar Syntax, Different Meaning
Compare:
for (i = 0; i < 8; i = i + 1)
in both cases.
The syntax looks almost identical.
But context changes the meaning.
Inside:
always@(*)
it is a procedural loop.
Inside a generate context using a genvar:
it is a generate loop.
Therefore, when reading Verilog, do not identify the purpose of a loop merely by seeing:
for
Instead ask:
WHERE is the loop?
WHAT kind of variable controls it?
WHAT does the loop body contain?
IS it describing procedural behavior or generating structure?
Generate Loops Are Not Clocked Loops
A generate loop has nothing inherently to do with clock edges.
For example:
generate
for (...)
...
endgenerate
does not mean:
Every clock edge → generate another instance.
All of the generated structure is determined before normal hardware operation.
Therefore:
Generate iteration
≠
Clock cycle
and:
Generate loop
≠
Sequential runtime process
Generate Constructs and Hardware Structure
Generate constructs are particularly useful when the STRUCTURE of a module depends on elaboration-time information.
Examples include:
- Number of repeated module instances
- Number of repeated structural connections
- Optional hardware blocks
- Different hardware architectures selected by parameters
Generate 'for' handles repeated structure.
In the next Unit, generate 'if' and generate 'case' will allow us to choose between different structural alternatives.
Explicit 'generate' and 'endgenerate'
In the examples in these notes, we use the explicit traditional form:
generate
...
endgenerate
This clearly marks the generate region.
For example:
generate
for (...) begin : BLOCK_NAME
...
end
endgenerate
This form is valid and useful for learning generate constructs.
Depending on the Verilog/SystemVerilog version and context, some generate constructs can also be written without explicitly surrounding them with the 'generate' and 'endgenerate' keywords.
Therefore, do not form the rule:
"Every generate construct must always literally contain the words generate and endgenerate."
The important concept is the GENERATE CONSTRUCT itself and its elaboration-time role.
Generated Structure Can Still Be Optimized
Suppose a generate loop creates repeated logic.
That does not mean the final physical FPGA layout must remain an exact literal copy of the source-code hierarchy.
After elaboration:
Generated design
↓
Synthesis
↓
Optimization
↓
Technology mapping
↓
Implementation
The synthesis tools may simplify or optimize logic while preserving the required behavior.
Therefore:
GENERATED SOURCE STRUCTURE
≠
GUARANTEED EXACT PHYSICAL FPGA LAYOUT
This is consistent with what we learned earlier about synthesis.
Generate 'for' Is Especially Useful for Repeated Modules
For extremely simple expressions such as:
assign out = a & b;
a generate loop may be unnecessary.
But consider a more complicated repeated component:
my_processing_block U1(...);
If we need 32 copies, manually writing:
U0
U1
U2
...
U31
would be repetitive and error-prone.
A generate loop allows us to express:
Create WIDTH copies of this structural pattern.
This is one of the strongest practical reasons to use generate 'for'.
Common Mistake: Thinking 'genvar' Becomes Hardware Storage
The declaration:
genvar i;
does NOT mean:
Create a register named i.
It does NOT mean:
Create a counter named i.
It does NOT mean:
Store i somewhere inside the FPGA during runtime.
Instead:
genvar
↓
Generate-loop variable
↓
Used while elaborating generated structure
The generated hardware remains after elaboration.
The generate variable itself should not be imagined as a runtime state-holding element.
Common Mistake: Thinking Generate Creates Hardware Dynamically at Runtime
Suppose:
WIDTH = 8
and the generate loop creates eight module instances.
Those instances do not appear one-by-one after the FPGA starts operating.
They are part of the elaborated design before implementation.
Likewise, the hardware cannot normally decide during runtime:
"I want 16 generated copies now instead of 8."
To obtain a different generated structure, the design must be elaborated with the corresponding configuration and then synthesized/implemented accordingly.
Therefore:
GENERATE
↓
Static structural configuration
NOT:
Dynamic runtime hardware creation
Common Mistake: Confusing Generate 'for' with Procedural Loop Unrolling
There is a similarity:
Both can involve a statically known number of iterations.
But their language roles are different.
PROCEDURAL 'for'
Uses procedural statements to describe repeated procedural behavior.
Synthesis may unroll the operations to determine hardware.
GENERATE 'for'
Is itself an elaboration mechanism for creating repeated structural constructs/scopes.
Therefore, do not reduce the distinction to:
"They are exactly the same because both get unrolled."
The correct distinction is:
PROCEDURAL REPETITION
vs
STRUCTURAL GENERATION
When Should I Think of Procedural 'for'?
Think of procedural 'for' when the repeated operation naturally belongs inside procedural logic.
Examples:
For every bit:
↓
calculate something
For every array element:
↓
perform a procedural assignment
Across several elements:
↓
accumulate a result
Typical context:
always@(*)
or another procedural block.
When Should I Think of Generate 'for'?
Think of generate 'for' when you want to create repeated STRUCTURAL constructs.
Examples:
Create 8 module instances
Create WIDTH repeated structural blocks
Create repeated continuous assignments as part of a structural pattern
Create a named hierarchy of repeated instances
Typical clues include:
genvar
module instantiation
named generated blocks
elaboration-time repetition
Final Comparison
PROCEDURAL 'for'
Purpose:
Describe repeated procedural operations.
Common loop variable:
integer
Example context:
always@(*)
GENERATE 'for'
Purpose:
Create repeated structural constructs during elaboration.
Loop variable:
genvar
Common contents:
Module instances, continuous assignments, and other legal structural generate items.
GENVAR
Purpose:
Generate-loop variable used during elaboration.
ELABORATION
Purpose:
Resolve the configured design hierarchy, parameters, instances, and generate constructs.
STRUCTURAL REPLICATION
Purpose:
Create multiple copies of a structural pattern.
NAMED GENERATE BLOCK
Example:
begin : AND_STAGE
Purpose:
Give generated scopes a meaningful hierarchical name.
PARAMETERIZED GENERATE LOOP
Example:
for (i = 0; i < WIDTH; i = i + 1)
Purpose:
Generate an amount of structure determined by an elaboration-time parameter.
Final Mental Model
Before this Unit, we knew:
PROCEDURAL 'for'
↓
Repeated procedural operations
Now we know:
GENERATE 'for'
↓
Repeated structural generation
For example:
genvar i;
generate
for (i = 0; i < WIDTH; i = i + 1) begin : GATES
and_gate U1(
.a(a[i]),
.b(b[i]),
.y(out[i])
);
end
endgenerate
should be understood as:
WIDTH is resolved
↓
Generate loop is evaluated during elaboration
↓
WIDTH generated scopes are created
↓
Each scope contains an and_gate module instance
↓
The elaborated design contains WIDTH separate structural instances
↓
Synthesis operates on that resulting design
The most important ideas are:
GENERATE 'for' IS USED FOR STRUCTURAL REPLICATION DURING ELABORATION.
'genvar' IS A GENERATE-LOOP VARIABLE, NOT A RUNTIME COUNTER OR REGISTER.
GENERATE LOOPS DO NOT RUN WHILE THE FPGA IS OPERATING.
ELABORATION RESOLVES PARAMETERS, HIERARCHY, MODULE INSTANCES, AND GENERATE STRUCTURE.
ELABORATION AND SYNTHESIS ARE CONCEPTUALLY DIFFERENT STAGES.
'begin : NAME' CREATES A NAMED GENERATED BLOCK/SCOPE.
GENERATE LOOPS CAN CREATE REPEATED CONTINUOUS ASSIGNMENTS.
GENERATE LOOPS CAN CREATE MULTIPLE MODULE INSTANCES.
A GENERATED MODULE INSTANCE IS A SEPARATE STRUCTURAL INSTANCE; IT IS NOT ONE INSTANCE BEING REUSED ACROSS TIME.
PARAMETERS CAN CONTROL HOW MUCH STRUCTURE A GENERATE LOOP CREATES.
PROCEDURAL 'for' AND GENERATE 'for' MAY LOOK SIMILAR, BUT THEIR PURPOSES ARE DIFFERENT.
PROCEDURAL 'for'
=
REPEATED PROCEDURAL OPERATIONS
GENERATE 'for'
=
REPEATED STRUCTURAL GENERATION DURING ELABORATION
This distinction prepares us for the next Unit:
GENERATE 'if' AND GENERATE 'case'
where elaboration-time conditions will determine WHICH hardware structure is included in a configured design.