Verilog · Chapter 14.7.2 · Behavioural Modeling
Generate if in Verilog — Conditional Hardware by Parameter
The generate if construct includes or excludes whole blocks of hardware structure based on a parameter, decided when the design is elaborated. If the condition, a parameter or localparam constant, is true, the selected branch is built; if false, it is simply not elaborated and produces no hardware at all. This makes generate if the tool for optional features in parameterized RTL, such as a parity generator, a debug port, or an error-correction block that exists only when its configuration parameter enables it, with zero gate cost when disabled. It differs from a runtime if, which builds a multiplexer because both branches exist in hardware, and from ifdef, which is a text-level preprocessor mechanism. This lesson drills generate if and those key distinctions.
Foundation12 min readVeriloggenerate ifConditionalParameterOptional Features
Chapter 14 · Section 14.7.2 · Behavioural Modeling
1. The Engineering Problem
A reusable module needs optional features — a parity generator that exists only when configured — built with zero cost when disabled. A runtime if builds a mux (both branches exist); generate if includes or excludes the structure entirely:
generate ifincludes or excludes hardware structure based on a parameter, at elaboration — the unselected branch produces no hardware. Distinct from a runtimeif(a mux) and`ifdef(a macro).
This page drills generate if and those distinctions.
2. Mental Model — Include/Exclude Structure by Parameter
3. The generate if
generate
if (HAS_PARITY) begin : gen_parity // HAS_PARITY is a parameter
assign parity = ^data; // exists ONLY if HAS_PARITY
end else begin : gen_no_parity
assign parity = 1'b0;
end
endgenerate
// optional debug port instance:
generate
if (DEBUG) begin : gen_dbg
debug_monitor u (.clk(clk), .data(data)); // built only when DEBUG
end
endgenerateWhen HAS_PARITY is true, the parity logic is built; when false, only the else branch (or nothing) is. The DEBUG example includes a monitor instance only when configured — with no hardware when DEBUG is 0. Name the generate blocks for hierarchy (14.7.1).
4. generate if vs Runtime if vs `ifdef
The three conditional mechanisms, distinguished:
generate if | Runtime if (14.5) | `ifdef (7.4) | |
|---|---|---|---|
| Condition | parameter (constant) | runtime signal | macro (defined?) |
| When resolved | elaboration | runtime (every cycle) | preprocessing |
| What it does | include/exclude structure | build a mux | include/exclude text |
| Unselected branch | no hardware | hardware (mux input) | text stripped |
| Use | optional features by config | runtime selection | build targets / sim vs synth |
// generate if — parameter, structure included/excluded (no runtime cost):
generate if (FEATURE) begin : g ... end endgenerate
// runtime if — signal, builds a mux (both branches exist):
always @(*) if (sel) y = a; else y = b;
// `ifdef — macro, preprocessor text inclusion:
// `ifdef SIMULATION ... `endifUse generate if for parameter-driven optional structure (a feature on/off by config), a runtime if for signal-driven selection (a mux), and `ifdef for build-target/macro inclusion (sim vs synth). They are three different tools.
Visual A — generate if includes/excludes structure
generate if — by parameter, at elaboration
data flow5. Common Mistakes
- Runtime signal in a
generate ifcondition — the condition must be a parameter/constant (§2, DebugLab 1). - Using a runtime
iffor an optional feature — builds a mux (both branches exist); usegenerate iffor zero-cost exclusion (§4). - Confusing
generate ifwith`ifdef— parameter (language) vs macro (preprocessor) (§4). - Unnamed generate block — name it for hierarchy (14.7.1).
6. Debugging Lab
One generate-if debug post-mortem
7. Interview Q&A
8. Exercises
Exercise 1 — Optional feature
Write a generate if that includes an error-correction instance only when a parameter HAS_ECC is set.
Exercise 2 — Three conditionals
For each, choose generate if, runtime if, or `ifdef: (a) include a debug port by configuration parameter; (b) select a mux output by a runtime select; (c) gate $display lines out of synthesis.
Exercise 3 — Fix the condition
Why does generate if (enable) with a runtime enable fail, and what are the two correct alternatives?
9. Summary
The generate if conditionally includes hardware by parameter:
- Parameter-driven, elaboration-time — includes/excludes structure; the unselected branch produces no hardware.
- vs runtime
if— runtimeifbuilds a mux (both branches exist);generate ifexcludes the unselected structure entirely. - vs
`ifdef—generate ifis language-level/parameter;`ifdefis preprocessor/macro. - Use for parameter-driven optional features with zero cost when disabled.
The next sub-topic selects among variants: Chapter 14.7.3 Generate case drills generate case — choosing an implementation by a parameter.
Related Tutorials
- Generate Block — Chapter 14.7; the generate overview.
- Conditional Compilation — Chapter 7.4; the
`ifdefmechanism contrasted. - if/else Statements — Chapter 14.5.1; the runtime
ifcontrasted. - parameter — Chapter 6.1; the parameters that drive
generate if.