Why Parameterize Everything
Module 6 established self-checking testbench architectures. Module 7 focuses on writing production-grade RTL code that remains reusable across multiple engineering projects. The foundational technique for code reusability is hardware parameterization.
Hard-coding bit widths causes code duplication. When an engineering team needs 8-bit, 16-bit, and 32-bit adders, developers frequently copy source files and edit bit slices manually. If an engineer later identifies a bug in the carry chain, the fix must be applied manually across all copies. Duplicated code bases increase maintenance overhead and defect rates. Verilog parameters solve this issue by defining configurable module boundaries.
The Parameterization Discipline
Module 4 introduced the parameter and localparam keywords.
True reusability requires strict design discipline.
Every hardware element that depends on a data width must reference the parameter.
This includes port declarations, internal registers, reset values, and loop bounds.
Consider a fixed-width adder implementation:
// Fixed-width implementation: restricted to 8-bit operations
module adder8 (
input wire [7:0] a, b,
input wire cin,
output wire [7:0] sum,
output wire cout
);
assign {cout, sum} = a + b + cin;
endmodule
Compare this with a parameterized implementation:
// Parameterized implementation: scales to arbitrary widths
module adder #(parameter WIDTH = 8) (
input wire [WIDTH-1:0] a, b,
input wire cin,
output wire [WIDTH-1:0] sum,
output wire cout
);
assign {cout, sum} = a + b + cin;
endmodule
The parameterized version supports any bit width through a single source file. When developers fix logic bugs, every instantiated module updates automatically across the entire project.
Overriding Parameters at Instantiation
Instantiating modules with parameter overrides uses the #(...) syntax:
adder #(.WIDTH(32)) u_audio_adder (
.a(audio_a), .b(audio_b), .cin(1'b0),
.sum(audio_sum), .cout(audio_cout)
);
adder #(.WIDTH(4)) u_nibble_adder (
.a(nib_a), .b(nib_b), .cin(1'b0),
.sum(nib_sum), .cout(nib_cout)
);
Both instances originate from a single source file. Verification teams write and maintain a single self-checking testbench instead of validating distinct files for each bit width.
Scaling Internal Signals and Reset Values
Do not restrict parameterization to module port lists. Internal registers, combinational datapath wires, and reset literals must scale with configuration parameters:
module shift_reg #(parameter WIDTH = 8) (
input wire clk, rst_n, serial_in,
output reg [WIDTH-1:0] shift_reg // Internal register scales with parameter
);
always @(posedge clk or negedge rst_n) begin
if (!rst_n) shift_reg <= {WIDTH{1'b0}}; // Scaled replication operator
else shift_reg <= {shift_reg[WIDTH-2:0], serial_in};
end
endmodule
Notice the replication operator {WIDTH{1'b0}}.
Never hard-code literal vectors such as 8'b0.
Examine every literal in a parameterized block.
If the literal value depends on the parameter, express it dynamically using parameter calculations.
Deriving Address Bus Widths with $clog2
Memory blocks define capacity using address depth.
Calculating required address bits requires computing $\lceil \log_2(\text{DEPTH}) \rceil$.
Verilog-2005 provides the built-in $clog2 system function to compute this logarithm at elaboration time:
module simple_mem #(
parameter DATA_WIDTH = 8,
parameter DEPTH = 256
) (
input wire clk,
input wire we,
input wire [$clog2(DEPTH)-1:0] addr,
input wire [DATA_WIDTH-1:0] data_in,
output reg [DATA_WIDTH-1:0] data_out
);
reg [DATA_WIDTH-1:0] mem [0:DEPTH-1];
always @(posedge clk) begin
if (we) mem[addr] <= data_in;
data_out <= mem[addr];
end
endmodule
The function call $clog2(256) evaluates to 8.
If an engineer updates DEPTH to 512, the synthesis tool automatically widens the address bus to 9 bits.
This automated scaling prevents address truncation defects.
A hierarchical module is only as scalable as its least parameterized sub-block. Always parameterize all child blocks throughout the design hierarchy.
Verification Exercise
Instantiate the parameterized adder at 4-bit, 8-bit, and 16-bit widths.
Apply boundary stimulus to cause arithmetic overflow and verify that cout asserts correctly.
View Solution
`timescale 1ns / 1ps
`default_nettype none
module adder #(parameter WIDTH = 8) (
input wire [WIDTH-1:0] a, b,
input wire cin,
output wire [WIDTH-1:0] sum,
output wire cout
);
assign {cout, sum} = a + b + cin;
endmodule
module tb_adder_multi_width;
// 4-bit instance
reg [3:0] a4, b4;
wire [3:0] sum4;
wire cout4;
adder #(.WIDTH(4)) u_adder4 (.a(a4), .b(b4), .cin(1'b0), .sum(sum4), .cout(cout4));
// 8-bit instance
reg [7:0] a8, b8;
wire [7:0] sum8;
wire cout8;
adder #(.WIDTH(8)) u_adder8 (.a(a8), .b(b8), .cin(1'b0), .sum(sum8), .cout(cout8));
// 16-bit instance
reg [15:0] a16, b16;
wire [15:0] sum16;
wire cout16;
adder #(.WIDTH(16)) u_adder16 (.a(a16), .b(b16), .cin(1'b0), .sum(sum16), .cout(cout16));
initial begin
$dumpfile("tb.vcd");
$dumpvars(0, tb_adder_multi_width);
// Apply boundary stimulus at each width
a4 = 4'hF; b4 = 4'h1; // 15 + 1 = 16, overflows 4 bits
a8 = 8'hFF; b8 = 8'h01; // 255 + 1 = 256, overflows 8 bits
a16 = 16'hFFFF; b16 = 16'h0001; // Overflows 16 bits
#5;
$display("4-bit: sum=%h cout=%b (expected 0, 1)", sum4, cout4);
$display("8-bit: sum=%h cout=%b (expected 0, 1)", sum8, cout8);
$display("16-bit: sum=%h cout=%b (expected 0, 1)", sum16, cout16);
$finish;
end
endmodule
All three instances originate from the identical adder module.
Each instance overflows correctly at its respective mathematical threshold.
Parameterizing module ports, internal registers, and derived constants ensures modularity.
The next article explores generate if and generate case constructs to build conditionally structured hardware.