LAB#4
LAB#4
LAB#4
Electrical Engineering
Handout#04
Use of Constants and Parameters in Verilog
Instructor: Irsa Jan
Background Theory:
Constant
HDL code frequently uses constant values in expressions and array boundaries. These
values are fixed within the module and cannot be modified. One good design practice is
to replace the "hard literals" with symbolic constants. It makes code clear and helps future
maintenance and revision. In Verilog, a constant can be declared using the localparam
(for "local parameter") keyword. For example, we can declare the width and range of a
data bus as
localparam DATA-WIDTH = 8,
DATA-RANGE = 2**DATA_WIDTH – 1;
Parameter
A Verilog module can be instantiated as a component and becomes a part of a larger
design, as discussed in previous labs. Verilog provides a construct, known as a
parameter, to pass information into a module. This mechanism makes the module
versatile and reusable. A parameter cannot be modified inside the module and thus
functions like a constant. A parameter declaration section can be added in the header,
before the port declaration. Its simplified syntax is
module [module-name]
#(
parameter [parameter-name] = [default-value] ,
...
[parameter-name] = [default-value] ;
)
(
. . . // I/O port declaration
);
Lab Activity:
Explore sample HDL codes given in listings 3.9, 3.10, 3.11 and 3.12 in your textbook. You
may write a testbench code with some test vectors of your own choice.
Exercise:
1. Explain in your own words how Parameters and Constants are different.
2. A parameter is declared with a constant value inside a module. Is it possible to
modify the default value when the module is instantiated? Please explain with a
programming example.