Make a module reusable without turning its internal settings into hardware inputs.
A parameter adapts a module before simulation and synthesis. Register width or lane count becomes an architectural choice; software cannot change that value while the circuit runs.
Set the width
This register updates only the bits selected by i_mask:
module
masked_register
#(
parameter WIDTH = 8
) (
input wire i_clk,
input wire i_reset,
input wire i_load,
input wire [WIDTH-1:0] i_data,
input wire [WIDTH-1:0] i_mask,
output reg [WIDTH-1:0] o_data
);
always @(posedge i_clk) begin
if (i_reset)
o_data <= {WIDTH{1'b0}};
else if (i_load)
o_data <= (o_data & ~i_mask) | (i_data & i_mask);
end
endmodule
WIDTH sizes the ports and register. {WIDTH{1'b0}} repeats zero across that width. Override the parameter at instantiation to build a 16-bit version:
Connections must match the expected width. Fix truncation and extension warnings even when synthesis succeeds.
Separate public settings from internal constants
parameter exposes a choice to the module user. localparam defines an internal constant that cannot be overridden at instantiation.
module pulse_every #( parameter PERIOD = 10) ( input wire i_clk, input wire i_reset, output reg o_tick); localparam COUNTER_WIDTH = 32; localparam LAST_COUNT = PERIOD - 1; reg [COUNTER_WIDTH-1:0] count; always @(posedge i_clk) begin if (i_reset) begin count <= 0; o_tick <= 1'b0; end else if (count == LAST_COUNT) begin count <= 0; o_tick <= 1'b1; end else begin count <= count + 1'b1; o_tick <= 1'b0; end endendmodule
PERIOD sets the interval between pulses; LAST_COUNT follows from it. The counter runs from zero through PERIOD - 1. This version uses 32 bits and assumes a positive integer period within its supported range. Check those limits before reuse.
Build repeated hardware with generate
A generate loop creates instances during elaboration. It does not process lanes over successive cycles:
module parity_per_byte #( parameter BYTE_COUNT = 4) ( input wire [BYTE_COUNT*8-1:0] i_data, output wire [BYTE_COUNT-1:0] o_parity); genvar lane; generate for (lane = 0; lane < BYTE_COUNT; lane = lane + 1) begin : g_parity assign o_parity[lane] = ^i_data[lane*8 +: 8]; end endgenerateendmodule
Each lane gets its own parity calculation. lane*8 +: 8 selects eight bits starting at the calculated index. The g_parity name makes the generated instances easy to find in the hierarchy.
A generate condition can also select one architecture:
module optional_output_register #( parameter WIDTH = 8, parameter REGISTER_OUTPUT = 1) ( input wire i_clk, input wire i_reset, input wire [WIDTH-1:0] i_data, output wire [WIDTH-1:0] o_data); generate if (REGISTER_OUTPUT) begin : g_registered reg [WIDTH-1:0] data_reg; always @(posedge i_clk) begin if (i_reset) data_reg <= {WIDTH{1'b0}}; else data_reg <= i_data; end assign o_data = data_reg; end else begin : g_direct assign o_data = i_data; end endgenerateendmodule
With REGISTER_OUTPUT enabled, the output passes through a register and reflects the input sampled at the rising edge. Otherwise, the path is combinational. This choice changes latency: document it in the interface and check every offered configuration.