Skip to content
VLSI Mentor

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

Azvya Education Pvt. Ltd.VLSI Mentor
generate-if.v
   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
Azvya Education Pvt. Ltd.VLSI Mentor
three-conditionals.v
   // 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

Pitfall — runtime signal used in a generate if condition
Buggy Code
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.
Symptom

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.

Root Cause

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'.

Fix
// 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 — runtime if builds a mux (both branches exist); generate if excludes the unselected structure entirely.
  • vs `ifdef — generate 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.

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.