Verilog Parameters and Operators
Verilog Parameters and Operators
Verilog Parameters and Operators
Operators
Contents
Parameter in Verilog
Types of Parameters
Difference Between Specify and Module
Parameters
Localparam
Overriding module parameters
Verilog Operators
Operator Precedence
Interview Questions
Parameter in Verilog
Parameters are constants typically used to specify the
width of variables and time delays.
Syntax:
parameter identifier = constant_expression;
Example : Parameter lsb = 7 ;
parameter size = 8 ,
word = 32 ;
We cannot modify parameter values at runtime, but we
can modify a parameter value using
the defparam statement.
Types of Parameters
There are two types of parameters:
1. module parameters
2. specify parameters
Module Parameters
Module parameter can be used to override parameter
definitions within a module and makes the module have
a different set of parameters at compile time.
Example 1: module design_ip ( addr, wdata, write, sel, rdata);
parameter BUS_WIDTH = 32,
DATA_WIDTH = 64,
FIFO_DEPTH = 512;
input addr;
input wdata;
input write;
input sel;
output rdata;
wire [BUS_WIDTH-1:0] addr;
wire [DATA_WIDTH-1:0] wdata;
reg [DATA_WIDTH-1:0] rdata;
reg [7:0] fifo [FIFO_DEPTH];
endmodule
Example 2 :
module counter
# ( parameter N = 2, parameter DOWN = 0)
(input clk, input rstn, input en, output reg [N-1:0] out);
always @ (posedge clk) begin
if (!rstn) begin
out <= 0;
end else begin
if (en)
if (DOWN)
out <= out - 1;
else
out <= out + 1;
else
out <= out;
end
end
endmodule
Specify Parameters
These parameters are used to provide time and delay values
and declared using the 'specparam' keyword.
It is allowed to use both within the specified block and the
main module body.
Example :
// Use of specify block
Specify
specparam t_rise = 200, t_fall = 150;
specparam clk_to_q = 70, d_to_q = 100;
endspecify
// Within main module
module my_block ( );
specparam dhold = 2.0;
specparam ddly = 1.5;
parameter WIDTH = 32;
endmodule
Difference Between Specify and Module
Parameters
Syntax :
localparam name = value;
Example :
module adder(a,b,sum) ;
parameter height = 8;
parameter width = 10;
localparam length = 4;
input [width-1:0] a;
input [height-1:0] b;
input [length-1:0] c;
output [width-1:0] sum;
assign sum = a + b + c;
endmodule
module top;
reg [15:0] a;
reg [15:0] b;
wire [15:0] sum1;
adder #(.width(16), .height(4), .length(5) add_0 (a,b,sum2); //error as
length is not accessible outside the module adder
...
endmodule
Overriding module parameters
There are two methods to override a module parameter value during a module
instantiation.
1)By using the defparam keyword.
2)And module instance parameter value assignment.
Example : module top;
reg Clk ;
reg [7:0] D ;
wire [7:0] Q ;
my_module #(7, 25) inst_1(Clk, D, Q) ;
endmodule
When one parameter depends on the other, remember
that if you change the first one, the second will
automatically be updated.
Example :
parameter width = 4 ;
parameter data = width / 10;
precedence
increases
Interview Questions
How do I prevent selected parameters of a module from
being overridden during instantiation?
What is the difference between == and ===?
What is the difference between bit wise, unary and
logical operators?