0
0
VerilogConceptBeginner · 3 min read

What is defparam in Verilog: Explanation and Example

defparam in Verilog is a keyword used to change the value of a parameter inside a module instance after it has been instantiated. It allows you to override default parameter values without modifying the original module code.
⚙️

How It Works

Think of a Verilog module like a cookie cutter with default settings for size and shape. When you create cookies (module instances), they all use the default settings unless you change them. defparam lets you change these settings for a specific cookie after you have already made the cookie cutter.

Normally, parameters are set when you create the module instance, but defparam allows you to override these parameters later in your code by specifying the path to the parameter inside the instance. This is useful when you want to customize behavior without changing the original module.

💻

Example

This example shows a simple module with a parameter and how defparam changes its value after instantiation.

verilog
module my_module #(parameter WIDTH = 8) (output reg [WIDTH-1:0] out);
  initial begin
    out = {WIDTH{1'b1}}; // Set all bits to 1
  end
endmodule

module top;
  wire [15:0] result;
  my_module instance1 (result);
  defparam instance1.WIDTH = 16;
  initial begin
    #1; // Wait for initial block
    $display("WIDTH after defparam: %d", instance1.WIDTH);
    $display("Output value: %b", result);
  end
endmodule
Output
WIDTH after defparam: 16 Output value: 1111111111111111
🎯

When to Use

Use defparam when you want to change parameter values of a module instance without editing the module's source code or when you cannot pass parameters directly during instantiation. It is helpful in large designs where modules are reused with different configurations.

However, modern Verilog coding prefers parameter overrides during instantiation for clarity. defparam is still supported but less common in new designs.

Key Points

  • defparam overrides parameters after module instantiation.
  • It uses hierarchical naming to specify which parameter to change.
  • It can make code harder to read if overused.
  • Parameter overrides during instantiation are generally preferred today.

Key Takeaways

defparam changes module parameters after instantiation using hierarchical names.
It allows customizing module behavior without modifying the original module code.
Modern practice favors parameter overrides during instantiation for better clarity.
Use defparam mainly when direct parameter passing is not possible.