Skip to content

Verilog · Chapter 14.7 · Behavioural Modeling

Generate Block in Verilog — Structural Replication at Elaboration

The generate construct is the Verilog tool for building hardware structure programmatically. It replicates and conditionally includes module instances, always blocks, continuous assignments, and declarations at elaboration, controlled by parameters and a special compile-time loop variable. Where a procedural loop replicates logic inside an always block, a generate loop replicates structure, such as an array of identical sub-module instances, a row of registers, or a bit-sliced datapath built from cells. Where a runtime conditional builds a multiplexer, a generate conditional includes or excludes whole blocks of hardware based on a parameter, at elaboration and with no runtime cost. This makes generate the backbone of parameterized, reusable RTL, where one module scales its structure with a width parameter or includes optional features by configuration. This page surveys the construct and sets up the sub-topics.

Foundation15 min readVeriloggenerategenvarStructuralParameterizedRTL Design

Chapter 14 · Section 14.7 · Behavioural Modeling

1. The Engineering Problem

You want a parameterized N-bit ripple-carry adder built from N full_adder instances — and N is a parameter. A procedural for loop (14.6) cannot do this: it replicates logic inside an always block, not module instances. You need to generate structure:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
genvar i;
generate
    for (i = 0; i < WIDTH; i = i + 1) begin : adder_chain
        full_adder u_fa (.a(a[i]), .b(b[i]), .cin(carry[i]),
                         .sum(sum[i]), .cout(carry[i+1]));
    end
endgenerate

This generate for loop, at elaboration, instantiates WIDTH full_adder modules — adder_chain[0].u_fa, adder_chain[1].u_fa, … — wired into a chain. The number of instances scales with the parameter, all created at build time. This is structural replication — generating hardware structure (instances and blocks), not runtime logic.

The generate construct replicates and conditionally includes hardware structure — module instances, always blocks, assignments — at elaboration, controlled by parameters and genvars. It is the structural counterpart to procedural loops.

This page surveys generatefor, if, case, and the genvar — the tool of parameterized, reusable RTL.

2. Mental Model — Generate Replicates Structure at Elaboration

Visual A — generate replicates structure

generate — structural replication at elaboration

data flow
generate — structural replication at elaborationgenerate for(genvar)elaboration-timereplicateinstances/blocksWIDTH copies of structuregenerate if/case(parameter)include/exclude structureparameterizedhardwarescales/configures with params
generate builds hardware structure at elaboration: a generate for loop (indexed by a genvar) replicates module instances and blocks, and a generate if/case includes or excludes structure based on a parameter. The result is parameterized hardware whose structure scales and configures with parameters — the basis of reusable RTL.

3. generate for — Replicate Structure

A generate for loop replicates structural items, indexed by a genvar:

generate-for.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   genvar i;                          // elaboration-time loop variable
   generate
       for (i = 0; i < WIDTH; i = i + 1) begin : gen_cells
           and_cell u (.a(a[i]), .b(b[i]), .y(y[i]));   // WIDTH instances
       end
   endgenerate
  • The genvar is a compile-time loop variable (not a runtime reg).
  • The loop body can contain module instances, always blocks, continuous assignments, and declarations — replicated WIDTH times at elaboration.
  • The named block (begin : gen_cells) gives each replica a hierarchical name (gen_cells[0].u, gen_cells[1].u, …) for referencing.

This builds arrays of instances (a ripple adder, a register file, a bit-sliced datapath) that scale with the parameter. (Drilled in 14.7.1.)

4. generate if — Conditionally Include Structure

A generate if includes or excludes structure based on a parameter — at elaboration:

generate-if.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   generate
       if (HAS_PARITY) begin : gen_parity
           assign parity = ^data;     // this logic exists ONLY if HAS_PARITY
       end else begin : gen_no_parity
           assign parity = 1'b0;
       end
   endgenerate
  • The condition is a parameter/localparam (a compile-time constant), not a runtime signal.
  • The selected branch's structure is built; the unselected branch is not elaborated — it produces no hardware.
  • This differs from a runtime if (which builds a mux for both branches) and from `ifdef (a preprocessor/macro mechanism): generate if is parameter-driven, language-level, elaboration-time structure selection. (Drilled in 14.7.2.)

5. generate case — Select Structure by Parameter

A generate case selects among structural variants by a parameter:

generate-case.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   generate
       case (IMPL)
           "FAST":  fast_mult  u (.a(a), .b(b), .y(y));   // structure per parameter
           "SMALL": small_mult u (.a(a), .b(b), .y(y));
           default: gen_mult   u (.a(a), .b(b), .y(y));
       endcase
   endgenerate

Selects which implementation (instance/block) to elaborate based on the IMPL parameter — only the chosen variant is built. Used to pick among implementation options per configuration. (Drilled in 14.7.3.)

6. The genvar

The genvar is what makes generate for an elaboration-time construct:

genvar.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   genvar g;                          // declared with 'genvar', not 'integer'
   generate for (g = 0; g < N; g = g + 1) begin : loop ... end endgenerate
  • A genvar is an elaboration-time integer used only as a generate for index — it does not exist as runtime hardware.
  • It must be statically bounded (like all unrolled loops), and the body is replicated once per genvar value at build time.
  • Contrast the integer used in a procedural for (14.6.1), which is a runtime loop variable inside an always block.

7. What This Chapter Covers

§Sub-topicCovers
14.7.1Generate forreplicating instances/blocks; instance arrays; genvar
14.7.2Generate ifconditional structure by parameter; vs runtime if and `ifdef
14.7.3Generate caseselecting structure variants by parameter
14.7.4Generate Advanced Techniquesnested generate, named scopes/hierarchy, combining

8. Industry Perspective

  • generate is the backbone of parameterized IP. Reusable modules use generate for to scale their structure (an N-wide datapath, an N-entry register file) and generate if/case to include optional features and select implementations — one source serving every configuration.
  • It replaces copy-paste structure. Instead of hand-writing N identical instances, a generate for creates them, scaling with a parameter — less code, fewer errors.
  • Conditional structure has no runtime cost. generate if includes or excludes hardware at elaboration, so unused features produce no gates — unlike a runtime if, which builds logic for both branches.
  • Named generate blocks give navigable hierarchy. The block labels create hierarchical paths (gen_cells[3].u) used in waveform tools, constraints, and debug.

9. Common Misconceptions

"generate runs at runtime." False. generate is resolved at elaboration (build time) — it constructs structure before simulation/synthesis runs. There is no runtime "generate."

"A genvar is a register." False. A genvar is an elaboration-time loop index, not runtime hardware — it disappears after the structure is generated (contrast the integer in a procedural for).

"generate if is the same as a runtime if." False. A generate if includes/excludes structure based on a parameter at elaboration (the unselected branch produces no hardware); a runtime if builds a mux selecting between branches that both exist in hardware.

10. Debugging Lab

One generate debug post-mortem

11. Interview Q&A

12. Summary

The generate construct builds hardware structure programmatically at elaboration:

  • generate for replicates structure (module instances, always blocks, assignments), indexed by a genvar — instance arrays that scale with a parameter.
  • generate if/case conditionally include or select structure by a parameter — at elaboration, with no runtime cost for the unselected branches.
  • genvar is the elaboration-time loop variable (not runtime hardware).
  • vs procedural loopsgenerate replicates structure; a procedural for replicates logic.

The discipline: use generate to build parameterized, configurable structure — scaling instances, including optional features, selecting implementations — all at elaboration.

The sub-topics drill each: Chapter 14.7.1 Generate for (instance arrays), 14.7.2 Generate if (conditional structure), 14.7.3 Generate case (variant selection), and 14.7.4 Generate Advanced Techniques. This closes Chapter 14 — and the RTL core.

  • Loops — Chapter 14.6; the procedural loops generate contrasts with (logic vs structure).
  • Module Instantiation — Chapter 9.2; the instances generate for replicates.
  • parameter — Chapter 6.1; the parameters that drive generate.
  • Conditional Compilation — Chapter 7.4; the `ifdef mechanism generate if contrasts with.