Parameters
Hardcoding bit widths limits code reusability. When you write a fixed 8-bit adder, you must duplicate the module to create 16-bit or 32-bit versions. Duplicating files introduces maintenance errors when you fix bugs in one copy. Verilog parameters solve this issue by making modules scalable and reusable.
Fixed-Width Module Limitations
Consider an adder with hardcoded signal widths:
`timescale 1ns / 1ps
`default_nettype none
module adder_8bit ( // Fixed width in module name
input wire [7:0] a,
input wire [7:0] b,
input wire cin,
output wire [7:0] sum,
output wire cout
);
assign {cout, sum} = a + b + cin;
endmodule
The underlying logic does not depend on an 8-bit width. Only the vector ranges require adjustment for different bus sizes.
Declaring Module Parameters
A parameter is an elaboration-time constant with a default value. In ANSI-style Verilog, declare parameters inside the #(...) parameter list before the port list:
`timescale 1ns / 1ps
`default_nettype none
module adder #(
parameter WIDTH = 8
)(
input wire [WIDTH-1:0] a,
input wire [WIDTH-1:0] b,
input wire cin,
output wire [WIDTH-1:0] sum,
output wire cout
);
assign {cout, sum} = a + b + cin;
endmodule
You can use declared parameters in port ranges, internal wire widths, and arithmetic expressions throughout the module.
Overriding Parameters at Instantiation
When you instantiate a module, override its default parameters by specifying new values in the instantiation statement:
`timescale 1ns / 1ps
`default_nettype none
module top_level (
input wire [15:0] data_a,
input wire [15:0] data_b,
input wire carry_in,
output wire [15:0] result,
output wire carry_out
);
adder #(
.WIDTH(16)
) u_adder16 (
.a (data_a),
.b (data_b),
.cin (carry_in),
.sum (result),
.cout(carry_out)
);
endmodule
Always use named parameter assignments (#(.WIDTH(16))). Named assignments prevent errors when you change the parameter order in the module header. A single source module can support multiple widths within the same design:
adder u_byte_adder ( // Uses default WIDTH = 8
.a (byte_a),
.b (byte_b),
.cin (1'b0),
.sum (byte_sum),
.cout(byte_cout)
);
adder #(.WIDTH(32)) u_word_adder ( // Overrides WIDTH to 32
.a (word_a),
.b (word_b),
.cin (1'b0),
.sum (word_sum),
.cout(word_cout)
);
Internal Constants with localparam
A localparam is an internal constant that external modules cannot override. Use localparam for internal state encodings, derived bit widths, and fixed mathematical constants:
`timescale 1ns / 1ps
`default_nettype none
module simple_fsm #(
parameter NUM_STATES = 4
)(
input wire clk,
input wire rst_n,
input wire start,
output reg done
);
localparam STATE_BITS = $clog2(NUM_STATES); // Derived internal width
localparam IDLE = 2'd0; // Internal state encodings
localparam RUN = 2'd1;
localparam WAIT = 2'd2;
localparam DONE = 2'd3;
reg [STATE_BITS-1:0] state, next_state;
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
state <= IDLE;
else
state <= next_state;
end
always @(*) begin
next_state = state;
done = 1'b0;
case (state)
IDLE: if (start) next_state = RUN;
RUN: next_state = WAIT;
WAIT: next_state = DONE;
DONE: begin
done = 1'b1;
next_state = IDLE;
end
default: next_state = IDLE;
endcase
end
endmodule
Declaring internal state constants as localparam prevents external instantiations from corrupting state machine encodings.
Calculating Widths with $clog2
The system function $clog2(N) calculates the ceiling of the base-2 logarithm ($\lceil \log_2 N \rceil$). It returns the exact number of address bits required to index $N$ memory locations:
| Number of Items ($N$) | Address Bits ($\lceil \log_2 N \rceil$) |
|---|---|
| 2 | 1 |
| 4 | 2 |
| 8 | 3 |
| 256 | 8 |
| 1000 | 10 |
| 1024 | 10 |
The following example sizes the address pointers of a synchronous FIFO:
`timescale 1ns / 1ps
`default_nettype none
module sync_fifo #(
parameter DATA_WIDTH = 8,
parameter DEPTH = 16
)(
input wire clk,
input wire rst_n,
input wire wr_en,
input wire rd_en,
input wire [DATA_WIDTH-1:0] wr_data,
output reg [DATA_WIDTH-1:0] rd_data,
output wire full,
output wire empty
);
localparam ADDR_BITS = $clog2(DEPTH); // 4 bits for depth of 16
reg [DATA_WIDTH-1:0] mem [0:DEPTH-1];
// Extra bit distinguishes full status from empty status
reg [ADDR_BITS:0] wr_ptr;
reg [ADDR_BITS:0] rd_ptr;
assign full = (wr_ptr[ADDR_BITS] != rd_ptr[ADDR_BITS]) &&
(wr_ptr[ADDR_BITS-1:0] == rd_ptr[ADDR_BITS-1:0]);
assign empty = (wr_ptr == rd_ptr);
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
wr_ptr <= {(ADDR_BITS+1){1'b0}};
end else if (wr_en && !full) begin
mem[wr_ptr[ADDR_BITS-1:0]] <= wr_data;
wr_ptr <= wr_ptr + 1'b1;
end
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
rd_ptr <= {(ADDR_BITS+1){1'b0}};
rd_data <= {DATA_WIDTH{1'b0}};
end else if (rd_en && !empty) begin
rd_data <= mem[rd_ptr[ADDR_BITS-1:0]];
rd_ptr <= rd_ptr + 1'b1;
end
end
endmodule
When you change DEPTH from 16 to 1024, $clog2 expands the address pointers from 4 bits to 10 bits automatically.
[!NOTE] The function
$clog2(1)evaluates to 0. If a parameter can equal 1, add a conditional boundary check:localparam ADDR_BITS = (DEPTH > 1) ? $clog2(DEPTH) : 1;
Parameter Data Types
Parameters can include explicit type qualifiers:
module param_types #(
parameter integer WIDTH = 8, // Standard integer parameter
parameter ENABLE = 1'b1, // Single-bit flag
parameter [3:0] MASK = 4'b1010, // Sized bit vector
parameter string NAME = "default" // Simulation-only string
)(/* Port declarations */);
endmodule
For synthesizable RTL design, use integers and sized bit vectors. Real floating-point and string parameters are restricted to simulation testbenches.
Deprecated defparam Statements
Older Verilog code overrides parameters using external defparam statements:
defparam u_adder.WIDTH = 16; // Deprecated legacy syntax
Do not use defparam. Modern EDA tools reject defparam in hierarchical designs, and SystemVerilog deprecates the construct. Always override parameters directly within the module instantiation block.
Parameterized Barrel Shifter Example
`timescale 1ns / 1ps
`default_nettype none
module barrel_shifter #(
parameter DATA_WIDTH = 8
)(
input wire [DATA_WIDTH-1:0] data_in,
input wire [$clog2(DATA_WIDTH)-1:0] shift_amt, // Sized automatically
input wire left_right, // 1 = left, 0 = right
output wire [DATA_WIDTH-1:0] data_out
);
assign data_out = left_right ? (data_in << shift_amt)
: (data_in >> shift_amt);
endmodule
The shift_amt port dynamically calculates its width from DATA_WIDTH. An 8-bit bus creates a 3-bit shift port, while a 32-bit bus creates a 5-bit shift port.
Practice Exercise
Design a synchronous up-counter module. The module includes a parameter named MAX_COUNT. The counter counts from 0 to MAX_COUNT - 1 and wraps to 0. Use $clog2 to derive the output port width.
Solution
`timescale 1ns / 1ps
`default_nettype none
module up_counter #(
parameter MAX_COUNT = 100
)(
input wire clk,
input wire rst_n,
input wire en,
output reg [$clog2(MAX_COUNT)-1:0] count // 7 bits for 100
);
always @(posedge clk) begin
if (!rst_n) begin
count <= {$clog2(MAX_COUNT){1'b0}};
end else if (en) begin
if (count == MAX_COUNT - 1)
count <= {$clog2(MAX_COUNT){1'b0}};
else
count <= count + 1'b1;
end
end
endmodule
Instantiate this counter to measure 60 seconds:
up_counter #(.MAX_COUNT(60)) u_seconds (
.clk (clk),
.rst_n(rst_n),
.en (tick_1hz),
.count(seconds) // 6 bits because $clog2(60) = 6
);
Parameters define scalable module dimensions. The next article covers number literals and rules for expressing constant bit widths in Verilog.