Skip to content

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 if includes or excludes hardware structure based on a parameter, at elaboration — the unselected branch produces no hardware. Distinct from a runtime if (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.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   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
   endgenerate

When 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 ifRuntime if (14.5)`ifdef (7.4)
Conditionparameter (constant)runtime signalmacro (defined?)
When resolvedelaborationruntime (every cycle)preprocessing
What it doesinclude/exclude structurebuild a muxinclude/exclude text
Unselected branchno hardwarehardware (mux input)text stripped
Useoptional features by configruntime selectionbuild targets / sim vs synth
three-conditionals.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   // 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 ... `endif

Use 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 flow
generate if — by parameter, at elaborationif (PARAM)parameter conditiontrue → structurebuiltinstances/logic existfalse → NOTelaboratedno hardware
generate if evaluates a parameter at elaboration: the true branch's structure is built, the false branch is not elaborated and produces no hardware. This gives parameter-driven optional features with zero gate cost when disabled — unlike a runtime if, where both branches exist as mux inputs.

5. Common Mistakes

  1. Runtime signal in a generate if condition — the condition must be a parameter/constant (§2, DebugLab 1).
  2. Using a runtime if for an optional feature — builds a mux (both branches exist); use generate if for zero-cost exclusion (§4).
  3. Confusing generate if with `ifdef — parameter (language) vs macro (preprocessor) (§4).
  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 — runtime if builds a mux (both branches exist); generate if excludes the unselected structure entirely.
  • vs `ifdefgenerate if is language-level/parameter; `ifdef is 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.