RTL Design / Module 3: Verilog from Scratch
Lesson 3.6

Parameters & generate β€” reusable RTL

Copy-pasting the same module four times with different widths is a maintenance nightmare. Parameters make modules configurable; generate unrolls loops and conditionals before synthesis so you write once and scale to N lanes.

Core idea

Parameters are compile-time constants. Generate blocks are a preprocessor β€” they create real hardware instances, not runtime loops.

1

parameter vs localparam

KeywordOverridable at instantiation?Typical use
parameterYes β€” #(.WIDTH(16))Module "API" knobs: width, depth, features
localparamNo β€” internal onlyDerived constants: ADDR_BITS = $clog2(DEPTH)
param_syntax.v
module fifo #(
  parameter DATA_W = 32,
  parameter DEPTH  = 16
)(
  input  wire [DATA_W-1:0] din,
  output wire [DATA_W-1:0] dout
);
  localparam ADDR_W = $clog2(DEPTH);  // 4 when DEPTH=16
  // ... storage [DEPTH-1:0][DATA_W-1:0]
endmodule

// Override at instance site:
// fifo #(.DATA_W(8), .DEPTH(64)) u_fifo (...);
Analogy

parameter is like a function argument with a default. localparam is a private constant computed inside the function β€” callers cannot change it.

2

Overriding parameters at instantiation

Named parameter override is the professional style β€” order-independent and readable in reviews.

param_override.v
module top;
  wire [7:0]  narrow_count;
  wire [15:0] wide_count;

  counter #(.WIDTH(8))  u_cnt8  (.clk(clk), .rst_n(rst_n), .count(narrow_count));
  counter #(.WIDTH(16)) u_cnt16 (.clk(clk), .rst_n(rst_n), .count(wide_count));
endmodule

Legacy style: positional #(8, 16) still works but breaks when someone adds a parameter β€” use named overrides in new code.

3

generate β€” when and why

Use generate when structure depends on a parameter: N identical slices, optional features, or replicated interconnect.

Generate unroll diagram

Generate is elaboration-time metaprogramming for hardware structure.

ConstructPurposeLoop variable
generate forReplicate instances or assignsgenvar i
generate ifInclude/exclude hardware by paramβ€”
generate caseMulti-way structural choiceβ€”
4

generate for β€” replicated lanes

gen_for_lanes.v
module shift_reg #(
  parameter WIDTH = 8,
  parameter STAGES = 4
)(
  input  wire             clk,
  input  wire             rst_n,
  input  wire [WIDTH-1:0] din,
  output wire [WIDTH-1:0] dout
);
  wire [WIDTH-1:0] stage [0:STAGES];

  assign stage[0] = din;
  assign dout     = stage[STAGES];

  genvar i;
  generate
    for (i = 1; i <= STAGES; i = i + 1) begin : pipe
      reg [WIDTH-1:0] q;
      always @(posedge clk or negedge rst_n) begin
        if (!rst_n) q <= {WIDTH{1'b0}};
        else        q <= stage[i-1];
      end
      assign stage[i] = q;
    end
  endgenerate
endmodule
Trap

Using a normal integer or int as the loop variable inside generate is illegal β€” you need genvar. Regular for in always is simulation-only behavior and usually not synthesizable.

5

generate if β€” optional features

gen_if_crc.v
module tx_path #(
  parameter ENABLE_CRC = 1
)(
  input  wire [7:0] data_in,
  output wire [7:0] data_out
);
  wire [7:0] raw_out;
  assign raw_out = data_in; // placeholder datapath

  generate
    if (ENABLE_CRC) begin : with_crc
      crc8 u_crc (.data(raw_out), .out(data_out));
    end else begin : no_crc
      assign data_out = raw_out;
    end
  endgenerate
endmodule
When generate helps

Feature flags that would leave dangling ports or dead modules without conditional instantiation. Synthesis only builds the branch that matches the parameter β€” zero cost for the unused path.

6

Parameterized mux β€” worked example

param_mux.v
module param_mux #(
  parameter WIDTH = 32,
  parameter N     = 4
)(
  input  wire [WIDTH-1:0]       in  [0:N-1],
  input  wire [$clog2(N)-1:0]   sel,
  output wire [WIDTH-1:0]       out
);
  genvar g;
  generate
    for (g = 0; g < N; g = g + 1) begin : sel_eq
      wire sel_hit = (sel == g[$clog2(N)-1:0]);
      // One-hot style β€” synthesis often maps to a clean mux tree
    end
  endgenerate

  // Compact readable form (same idea):
  assign out = in[sel];
endmodule
7

Parameterized counter β€” full template

param_counter.v
module counter #(
  parameter WIDTH = 8,
  parameter MAX   = {WIDTH{1'b1}}  // default: count to all 1s
)(
  input  wire             clk,
  input  wire             rst_n,
  input  wire             en,
  output reg  [WIDTH-1:0] count,
  output wire             terminal
);
  assign terminal = (count == MAX);

  always @(posedge clk or negedge rst_n) begin
    if (!rst_n)
      count <= {WIDTH{1'b0}};
    else if (en)
      count <= terminal ? {WIDTH{1'b0}} : count + {{(WIDTH-1){1'b0}}, 1'b1};
  end
endmodule
β–Ά Parameterize a counter in the simulator
8

Decision guide β€” parameters vs generate

πŸ“

Use parameters when…

Widths, depths, or constants change bus sizes and numeric literals β€” no structural duplication needed.

πŸ”

Use generate when…

You need N copies of an instance, conditional modules, or indexed port arrays that scale with N.

  • I can override a parameter with named #(.NAME(val))
  • I know localparam is for internal derived constants
  • I use genvar in generate for, not a runtime loop
  • I can explain why generate is not a synthesizable while-loop

🎯 What to remember

  • Parameters configure modules; localparams hide derived math.
  • Override with named #(.PARAM(val)) at instantiation.
  • Generate unrolls structure at elaboration β€” real hardware per iteration.
  • generate if cleanly optionalizes features without dead logic.
  • Reusable RTL = parameters + generate, not copy-paste.
Advertisement