Parameterized modules with parameter keyword in Verilog - Time & Space Complexity
When using parameterized modules in Verilog, it's important to understand how the size of parameters affects the work done inside the module.
We want to see how the execution time grows as parameters change.
Analyze the time complexity of the following parameterized module.
module adder #(parameter WIDTH = 8) (
input wire [WIDTH-1:0] a, b,
output wire [WIDTH-1:0] sum
);
assign sum = a + b;
endmodule
This module adds two numbers of size WIDTH bits, where WIDTH is set when the module is used.
Look for operations that repeat based on the parameter.
- Primary operation: Bitwise addition of WIDTH bits.
- How many times: The addition logic processes each bit, so roughly WIDTH times.
The work done grows as the WIDTH increases because more bits need to be added.
| Input Size (WIDTH) | Approx. Operations |
|---|---|
| 8 | 8 bit additions |
| 16 | 16 bit additions |
| 32 | 32 bit additions |
Pattern observation: Doubling WIDTH roughly doubles the number of bit operations.
Time Complexity: O(WIDTH)
This means the time to add grows linearly with the number of bits.
[X] Wrong: "The addition time stays the same no matter the WIDTH."
[OK] Correct: Each bit must be processed, so bigger WIDTH means more work and more time.
Understanding how parameters affect module complexity helps you design efficient hardware and explain your design choices clearly.
"What if the module used two parameters for WIDTH and DEPTH, how would that affect the time complexity?"
Practice
parameter keyword in a Verilog module?Solution
Step 1: Understand the role of
Theparameterparameterkeyword defines constants inside a module that can be set when the module is created.Step 2: Compare with other options
Input/output ports are declared withinputandoutput, notparameter. Clock frequency is not set byparameteralone. Variables that change during simulation are declared differently.Final Answer:
To define constants that can be changed when the module is instantiated -> Option BQuick Check:
Parameter = adjustable constant [OK]
- Confusing parameters with input/output ports
- Thinking parameters change during simulation
- Assuming parameters set clock frequency directly
WIDTH with a default value of 8 inside a Verilog module?Solution
Step 1: Recall Verilog parameter syntax
The correct way to declare a parameter is using the keywordparameterfollowed by the name, an equals sign, and the value, ending with a semicolon.Step 2: Check each option
parameter WIDTH = 8; matches the correct syntax. param WIDTH = 8; uses incorrect keywordparam. parameter WIDTH : 8; uses colon instead of equals. define WIDTH = 8; usesdefinewhich is for macros, not parameters.Final Answer:
parameter WIDTH = 8; -> Option DQuick Check:
Parameter syntax = keyword + name + = + value [OK]
- Using 'param' instead of 'parameter'
- Using colon ':' instead of equals '='
- Confusing parameters with macros (#define)
out?
module my_module #(parameter WIDTH = 4) (input [WIDTH-1:0] in, output [WIDTH-1:0] out); assign out = in; endmodule my_module #(.WIDTH(6)) instance1 (.in(6'b101010), .out(out_signal));
Solution
Step 1: Identify parameter override in instantiation
The module has a default parameter WIDTH=4, but the instantiation overrides it with WIDTH=6.Step 2: Determine output width from parameter
The output width is defined as [WIDTH-1:0], so with WIDTH=6, output is 6 bits wide.Final Answer:
6 bits -> Option CQuick Check:
Parameter override sets WIDTH=6 [OK]
- Ignoring parameter override and using default
- Assuming output width is always 1 bit
- Confusing input width with output width
module adder #(parameter WIDTH = 8) (input [WIDTH-1:0] a, b, output [WIDTH-1:0] sum); assign sum = a + b; endmodule adder #(WIDTH=16) my_adder (.a(a_signal), .b(b_signal), .sum(sum_signal));
Solution
Step 1: Check parameter override syntax
The correct syntax for parameter override uses dot notation with parentheses:#(.WIDTH(16)), not#(WIDTH=16).Step 2: Verify other parts
Semicolons and port widths are correct. Addition on vectors is valid in Verilog.Final Answer:
Parameter override syntax is incorrect in instantiation -> Option AQuick Check:
Use #(.PARAM(value)) for parameter override [OK]
- Using = instead of ( ) in parameter override
- Forgetting dot before parameter name
- Assuming addition on vectors is invalid
shift_reg that shifts input bits by a configurable amount SHIFT. Which of the following module declarations correctly uses the parameter keyword and allows setting SHIFT during instantiation?Solution
Step 1: Check parameter declaration syntax
module shift_reg #(parameter SHIFT = 2) (input [7:0] in, output [7:0] out); assign out = in << SHIFT; endmodule correctly declaresparameter SHIFT = 2inside#(...)after module name, which is the standard syntax.Step 2: Validate other options
module shift_reg (parameter SHIFT = 2) (input [7:0] in, output [7:0] out); assign out = in << SHIFT; endmodule incorrectly placesparameterinside port list. module shift_reg #(param SHIFT = 2) (input [7:0] in, output [7:0] out); assign out = in << SHIFT; endmodule uses invalid keywordparam. module shift_reg #(parameter SHIFT : 2) (input [7:0] in, output [7:0] out); assign out = in << SHIFT; endmodule uses colon instead of equals.Final Answer:
module shift_reg #(parameter SHIFT = 2) (input [7:0] in, output [7:0] out); assign out = in << SHIFT; endmodule -> Option AQuick Check:
Parameter with default value inside #(parameter ...) [OK]
- Placing parameter inside port list
- Using 'param' instead of 'parameter'
- Using colon ':' instead of equals '='
Ready to prove it?
Pass the test and this pattern moves unknown
