Verilog Parameters

Parameters make modules configurable and reusable. They're essential for creating flexible IP that can be adapted to different requirements.

parameter Keyword

Parameters can be overridden during module instantiation:

Parameterized FIFO
module fifo #(
    parameter DATA_WIDTH = 8,
    parameter DEPTH = 16,
    parameter ADDR_WIDTH = $clog2(DEPTH)  // Derived parameter
) (
    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
);
    reg [DATA_WIDTH-1:0] mem [0:DEPTH-1];
    reg [ADDR_WIDTH:0] count;  // Extra bit for full detection
    // ... FIFO logic
endmodule
// Instantiation with parameter override
fifo #(
    .DATA_WIDTH (32),
    .DEPTH      (64)
) u_fifo_32x64 (
    .clk     (clk),
    .rst_n   (rst_n),
    // ...
);

localparam Keyword

localparam cannot be overridden – it's local to the module:

Local Parameters
module state_machine (
    input  wire clk,
    input  wire rst_n,
    input  wire start,
    output reg  done
);
    // State encoding - should NOT be overridable
    localparam IDLE  = 2'b00;
    localparam RUN   = 2'b01;
    localparam WAIT  = 2'b10;
    localparam DONE  = 2'b11;
    reg [1:0] state, next_state;
    // ... FSM logic
endmodule

When to Use Each

  • parameter – User-configurable values (width, depth)
  • localparam – Internal constants (state encoding, derived values)

Parameter Override Methods

Override Styles
// Method 1: Named association (RECOMMENDED)
counter #(
    .WIDTH (16),
    .MAX   (1000)
) u_counter (...);
// Method 2: Positional (NOT recommended - error prone)
counter #(16, 1000) u_counter (...);
// Method 3: defparam (DEPRECATED - avoid!)
counter u_counter (...);
defparam u_counter.WIDTH = 16;
defparam u_counter.MAX = 1000;

Common Interview Questions

  1. What's the difference between parameter and localparam?

    parameter can be overridden during instantiation; localparam cannot and is always local to the module.

  2. Can you use parameters in port declarations?

    Yes, parameters can size ports and internal signals, making modules truly generic.

  3. What is $clog2?

    A system function that returns ceiling of log base 2. Used to calculate address width from memory depth.