Skip to main content

Parametric Hardware with `generate` in Verilog

· loading · loading · · ·
HDL Verilog Generate Genvar Parametric Design RTL
Axolot Logic
Author
Axolot Logic
Digital Design Engineer
Table of Contents
Verilog HDL Series - This article is part of a series.
Part 8: This Article

🏗️ generate Block in Verilog
#

The generate construct allows parameterized and scalable hardware generation. Unlike initial blocks, it is synthesizable and used during compile-time elaboration.


🎯 Key Properties
#

Property Description
Synthesizable ✅ Yes – translated into hardware
Purpose Looping or conditional instantiation
Variants for-generate, if-generate, case-generate
Loop variable type Requires genvar (compile-time only)

🔁 for-generate – Repetition Example
#

genvar i;
generate
  for (i = 0; i < 8; i = i + 1) begin : reverse_bits
    assign out[i] = in[7 - i];
  end
endgenerate

➡️ Used for bit-wise reversal or replicated logic (e.g., pipelines, adders).


🧱 Nested Loops & Parameterized Widths
#

parameter WIDTH = 16;
genvar k;
generate
  for (k = 0; k < WIDTH; k = k + 4) begin : nibble_adder
    adder u_add (
      .a(a[k+3:k]),
      .b(b[k+3:k]),
      .s(sum[k+3:k])
    );
  end
endgenerate

🔀 if-generate – Conditional Instantiation
#

generate
  if (USE_DSP48) begin
    dsp_mult u_dsp (.a(a), .b(b), .p(p));
  end else begin
    basic_mult u_basic (.a(a), .b(b), .p(p));
  end
endgenerate

✅ Compile-time parameter decides which logic to include.


⚠️ Common Mistakes and Warnings
#

  1. Invalid usage of genvar
genvar i;
always @(*) begin
  for (i = 0; i < 4; i = i + 1)  // ❌ Error: genvar not allowed in runtime
    ...
end
  1. Conditions must be constant
if (runtime_signal == 1)  // ❌ Not allowed

✅ Must use parameters or literals only.


✅ Best Practices
#

  • Always label blocks (: block_name) for clarity and tracing
  • Use genvar only in generate-time contexts
  • Prefer if-generate for feature toggles, for-generate for replication
  • Check synthesis reports to ensure expected instances are created
  • When writing portable code, avoid SystemVerilog-only constructs unless toolchain supports them

💡 generate blocks bring the power of code reuse, parameterization, and hardware scalability to Verilog RTL design — essential for modern FPGA and ASIC development.

Verilog HDL Series - This article is part of a series.
Part 8: This Article

Related

Procedural vs Continuous Assignments in Verilog
· loading · loading
HDL Verilog Procedural Assignment Continuous Assignment Wire Reg RTL FPGA Design
Understanding the `initial` Block in Verilog
· loading · loading
HDL Verilog Initial Block Simulation Testbench RTL Design
Compiler Directives & Macros
· loading · loading
HDL Verilog HDL
Module Definition, Usage, and Constructs
· loading · loading
HDL Verilog HDL
Task and Function
· loading · loading
HDL Verilog HDL
Blocking vs Non-Blocking Assignments
· loading · loading
HDL Verilog HDL