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.
Parameters are compile-time constants. Generate blocks are a preprocessor β they create real hardware instances, not runtime loops.
| Keyword | Overridable at instantiation? | Typical use |
|---|---|---|
parameter | Yes β #(.WIDTH(16)) | Module "API" knobs: width, depth, features |
localparam | No β internal only | Derived constants: ADDR_BITS = $clog2(DEPTH) |
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 (...);
parameter is like a function argument with a default. localparam is a private constant computed inside the function β callers cannot change it.
Named parameter override is the professional style β order-independent and readable in reviews.
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.
Use generate when structure depends on a parameter: N identical slices, optional features, or replicated interconnect.
Generate is elaboration-time metaprogramming for hardware structure.
| Construct | Purpose | Loop variable |
|---|---|---|
generate for | Replicate instances or assigns | genvar i |
generate if | Include/exclude hardware by param | β |
generate case | Multi-way structural choice | β |
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
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.
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
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.
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
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
Widths, depths, or constants change bus sizes and numeric literals β no structural duplication needed.
You need N copies of an instance, conditional modules, or indexed port arrays that scale with N.
parameter with named #(.NAME(val))localparam is for internal derived constantsgenvar in generate for, not a runtime loop#(.PARAM(val)) at instantiation.generate if cleanly optionalizes features without dead logic.