generate for and Procedural for
A procedural for loop cannot instantiate module instances. Procedural loops operate only inside always or initial blocks. In Verilog, module instantiations are concurrent structural statements. Replicating structural modules requires the generate for construct introduced in Section 4.4.
Comparison of Loop Roles
A procedural for loop operates inside an always or initial block. It unrolls repeated expressions within a single behavioral block. Examples include parity chains, population counters, and comparators. All generated logic remains contained in that block.
A generate for construct operates at the module level outside procedural blocks. It replicates structural elements including module instances, internal wire nets, and continuous assign statements. Each unrolled iteration creates an independent, hierarchically named hardware block.
Use this guideline:
- Use a procedural loop to replicate algorithmic logic or arithmetic expressions.
- Use a
generateloop to replicate structural module instances or independent continuous assignments.
Parameterised Adder Implementation
The following example demonstrates structural replication with generate:
genvar i;
generate
for (i = 0; i < WIDTH; i = i + 1) begin : gen_fa
full_adder u_fa (
.a (a[i]),
.b (b[i]),
.cin (carry[i]),
.sum (sum[i]),
.cout(carry[i+1])
);
end
endgenerate
The WIDTH parameter configures a 4-bit, 32-bit, or 64-bit adder. The loop creates WIDTH full-adder instances and connects internal carry signals. The loop index must use a genvar type declaration. A genvar exists only during elaboration and does not create physical hardware registers.
Hierarchical Instance Labels
The label : gen_fa following the begin keyword is mandatory. It assigns a unique hierarchical identifier to each unrolled block. Verification environments access specific instances using these labels:
tb.u_dut.gen_fa[2].u_fa.cin
This hierarchical path accesses the carry-in port of the third full adder. Without block labels, tools cannot address individual instances during debugging.
Replicating Continuous Assignments
Generate blocks also replicate continuous assignments:
genvar j;
generate
for (j = 0; j < N; j = j + 1) begin : gen_xor
assign out[j] = in[j] ^ mask[j];
end
endgenerate
This code creates $N$ independent XOR gates. For uniform bus widths, the vector assignment assign out = in ^ mask; is more concise. Use generate constructs when individual iterations require unique wiring patterns or custom instance parameters.
Figure 1 illustrates the architectural difference. A procedural loop generates a single merged logic expression. A generate loop creates distinct, addressable module instances.
Practice Exercise
Design an array of $N$ independent 2-to-1 multiplexers with a shared select line. Choose between procedural and generate implementations. Write the module and its verification testbench.
Solution
Because each multiplexer operates as an independent parallel component, use a generate block:
`timescale 1ns / 1ps
`default_nettype none
module mux_array #(
parameter N = 8
) (
input wire [N-1:0] a, b,
input wire sel,
output wire [N-1:0] y
);
genvar i;
generate
for (i = 0; i < N; i = i + 1) begin : gen_mux
assign y[i] = sel ? b[i] : a[i];
end
endgenerate
endmodule
module tb_mux_array;
parameter N = 8;
reg [N-1:0] a, b;
reg sel;
wire [N-1:0] y;
mux_array #(.N(N)) dut (.a(a), .b(b), .sel(sel), .y(y));
initial begin
$dumpfile("tb.vcd");
$dumpvars(0, tb_mux_array);
a = 8'hAA; b = 8'h55;
sel = 0; #5; $display("sel=0: y=%h (expect %h)", y, a);
sel = 1; #5; $display("sel=1: y=%h (expect %h)", y, b);
$finish;
end
endmodule
When all bits share identical control connections, assign y = sel ? b : a; provides a simpler description. Use generate blocks when individual bit lines connect to distinct conditions or parameters.
This concludes Section 5.4. Section 5.5 examines sequential building blocks: flip-flops, clock enables, reset strategies, and pipeline registers.