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
Pitfall — runtime signal used in a generate if condition
module feature (input enable, [7:0] data, output reg parity);
// Intent: include parity logic conditionally. But 'enable' is a runtime
// SIGNAL, not a parameter.
generate
if (enable) begin : gen_par // ILLEGAL — enable is a runtime signal
assign parity = ^data;
end
endgenerate
endmodule
// generate if conditions are evaluated at ELABORATION and must be constant
// (parameter/localparam). A runtime signal like 'enable' is not known at
// elaboration, so this is illegal — the tool errors.A module using 'generate if' with a runtime signal as the condition fails to compile with an error about a non-constant generate condition. The intent (conditional feature) seems reasonable.
A generate if is resolved at ELABORATION, so its condition must be a constant — a parameter or localparam known at build time. The code uses a runtime SIGNAL ('enable') as the condition, which is not constant, so the generate cannot be resolved and the tool errors. generate if is for PARAMETER-driven (configuration-time) structure inclusion, not runtime selection.
The fix depends on intent: if the feature is set by CONFIGURATION, make the condition a parameter (generate if). If it must be enabled at RUNTIME, use a normal runtime if (which builds a mux) — the parity logic exists, gated by 'enable'.
// If the feature is a build-time CONFIG → use a parameter with generate if:
module feature #(parameter HAS_PARITY = 1)(input [7:0] data, output reg parity);
generate
if (HAS_PARITY) begin : gen_par
always @(*) parity = ^data;
end else begin : gen_no
always @(*) parity = 1'b0;
end
endgenerate
endmodule
// If it must be a RUNTIME enable → use a runtime if (builds the logic, gated):
// always @(*) parity = enable ? ^data : 1'b0;
// generate if = parameter (config); runtime if = signal (mux).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.
Standards & specifications
- Governing standard
- IEEE Std 1364 (Verilog)(opens IEEE in a new tab)
Defines the Verilog language and its simulation semantics, including the event scheduling model. Synthesis support is defined by tools, not by this standard.
This page also covers RTL structure, verification approach and debugging technique. Those are engineering practice built on the standard, not requirements the standard itself imposes.
Where this fits
Part of the Verilog HDL curriculum.
