Module 7: Advanced RTL Design Techniques · 7.1 Parameterized & Scalable Modules

generate if and generate case

Parameters can configure data bus widths. Parameters can also select between completely different hardware architectures.

Module 5 introduced the generate for construct to replicate identical hardware structures. In contrast, generate if and generate case select between distinct architectural implementations at elaboration time. The synthesis compiler evaluates the condition and discards unselected branches before netlist generation.

Selecting Architectures with generate if

The generate if construct evaluates parameter conditions during compile-time elaboration:

module adder_auto #(parameter WIDTH = 8) (
    input  wire [WIDTH-1:0] a, b,
    input  wire             cin,
    output wire [WIDTH-1:0] sum,
    output wire             cout
);
    generate
        if (WIDTH <= 8) begin : gen_ripple
            // Small bit widths: ripple-carry architecture minimizes silicon area
            assign {cout, sum} = a + b + cin;
        end else begin : gen_fast
            // Large bit widths: carry-select architecture maximizes clock speed
            wire [WIDTH/2-1:0] lower_sum;
            wire               lower_cout;
            wire [WIDTH/2-1:0] upper_sum0, upper_sum1;
            wire               upper_cout0, upper_cout1;

            assign {lower_cout, lower_sum} = a[WIDTH/2-1:0] + b[WIDTH/2-1:0] + cin;
            assign {upper_cout0, upper_sum0} = a[WIDTH-1:WIDTH/2] + b[WIDTH-1:WIDTH/2] + 1'b0;
            assign {upper_cout1, upper_sum1} = a[WIDTH-1:WIDTH/2] + b[WIDTH-1:WIDTH/2] + 1'b1;

            assign sum  = {lower_cout ? upper_sum1 : upper_sum0, lower_sum};
            assign cout = lower_cout ? upper_cout1 : upper_cout0;
        end
    endgenerate
endmodule

When an engineer instantiates this module with WIDTH = 4, the synthesis tool elaborates only the gen_ripple block. The compiler completely removes the gen_fast logic. Conversely, instantiating the module with WIDTH = 32 elaborates the gen_fast block. Because selection occurs during elaboration, unselected branches consume no logic gates, multiplexers, or power.

An if statement inside an always procedural block infers physical runtime multiplexers. A generate if statement evaluates statically at compile time and infers no runtime hardware overhead.

Named Generate Blocks and Hierarchy Paths

Always provide labels for generate blocks, such as : gen_ripple or : gen_fast. These labels create explicit hierarchical paths in simulation databases:

tb.u_dut.gen_fast.upper_sum0

If an instance elaborates the gen_ripple branch, the gen_fast hierarchy path does not exist in the simulation database. This absence allows verification engineers to confirm architectural selection directly.

Multi-Way Architecture Selection with generate case

When selecting between three or more hardware architectures, use generate case:

module encoder_selectable #(parameter STYLE = "PRIORITY") (
    input  wire [7:0] req,
    output reg  [2:0] enc,
    output reg        valid
);
    generate
        case (STYLE)
            "PRIORITY": begin : gen_priority
                always @(*) begin
                    enc = 3'd0; valid = 1'b0;
                    if      (req[7]) begin enc = 3'd7; valid = 1'b1; end
                    else if (req[6]) begin enc = 3'd6; valid = 1'b1; end
                    else if (req[5]) begin enc = 3'd5; valid = 1'b1; end
                    // Remaining bit conditions omitted for clarity
                    else if (req[0]) begin enc = 3'd0; valid = 1'b1; end
                end
            end
            "ONEHOT": begin : gen_onehot
                always @(*) begin
                    enc = 3'd0; valid = |req;
                    case (1'b1)
                        req[0]: enc = 3'd0;
                        req[1]: enc = 3'd1;
                        req[2]: enc = 3'd2;
                        req[3]: enc = 3'd3;
                        req[4]: enc = 3'd4;
                        req[5]: enc = 3'd5;
                        req[6]: enc = 3'd6;
                        req[7]: enc = 3'd7;
                        default: ;
                    endcase
                end
            end
            default: begin : gen_invalid
                // Report invalid parameter values during elaboration
                initial begin
                    $display("ERROR: encoder_selectable STYLE=%s is invalid", STYLE);
                    $finish;
                end
            end
        endcase
    endgenerate
endmodule

Parameters can contain ASCII string literals. Setting .STYLE("ONEHOT") clarifies design intent at instantiation sites without requiring numeric encoding tables.

The default branch acts as an elaboration safeguard. If an engineer misspells a parameter string, such as "PRIORTY", the compiler triggers an error immediately. This prevents designs from elaborating into empty, unconnected circuits.

Practical Applications for Conditional Generation

Apply generate if and generate case under specific design conditions:

  • Target Architecture Selection: Switch between vendor-specific FPGA primitives and portable ASIC behavioral RTL models.
  • Verification and Debug Logic: Compile internal assertion checkers, monitor probes, or performance counters only during simulation builds.
  • Optimization Trade-offs: Select between area-optimized serial architectures and high-throughput parallel pipelines based on performance parameters.

Verification Exercise

Implement a parameterized shifter module with a MODE string parameter. Configure the module to select between "LOGICAL" (zero-fill right shift) and "ARITHMETIC" (sign-extended right shift) operations using generate case. Include a default branch that stops simulation when an unsupported mode is passed.

View Solution
`timescale 1ns / 1ps
`default_nettype none

module shifter #(parameter WIDTH = 8, parameter MODE = "LOGICAL") (
    input  wire [WIDTH-1:0] data_in,
    input  wire [$clog2(WIDTH)-1:0] shamt,
    output wire [WIDTH-1:0] data_out
);
    generate
        case (MODE)
            "LOGICAL": begin : gen_logical
                assign data_out = data_in >> shamt;
            end
            "ARITHMETIC": begin : gen_arithmetic
                assign data_out = $signed(data_in) >>> shamt;
            end
            default: begin : gen_invalid
                initial begin
                    $display("ERROR: shifter MODE=%s is not recognized", MODE);
                    $finish;
                end
            end
        endcase
    endgenerate
endmodule

Instantiating shifter #(.WIDTH(8), .MODE("ARITHMETIC")) elaborates only the arithmetic shift logic. Passing an unsupported string causes the elaboration check to terminate execution with an informative message.

The generate if and generate case constructs provide powerful structural configuration without runtime performance penalties. The next article applies these concepts to construct a parameterized memory model.

← Why Parameterize Everything A Parameterized Memory Model →