Verilog Chapter3 Module+and+Hierarchical+Structure
Verilog Chapter3 Module+and+Hierarchical+Structure
LECTURE
Subject: VERILOG
Hardware Description Language
Chapter3: Modules and
Hierarchical structure
Lecturer: Lam Duc Khai
1
Agenda
Hierarchical structure
Modules
Instances
3
Hierarchical structure
• The Verilog HDL supports a hierarchical hardware description
structure by allowing modules to be embedded within other
modules. Higher level modules create instances of lower level
modules and communicate with them through input, output,
and bidirectional ports. These module input/output (I/O) ports
can be scalar or vector.
4
Hierarchical structure (Cont’d)
Top-down design methodology
Top-level block
identify
enough
to build
5
Hierarchical structure (Cont’d)
build
7
Hierarchical structure (Cont’d)
Example: 4-bit Ripple Carry Counter
Design Hierarchy 8
Modules
• A module is the basic building block in Verilog
– Can be an element or a collection of lower-level design
blocks
– Provide functionality for higher-level block through its
port interface
– Hide internal implementation
– Is used at many places in the design
– Allows designers modify module internals without
effecting the rest of design
9
Modules (Cont’d)
Typical Module Components Diagram
Module name, Port list (optional, if there are ports)
Port declarations
Parameter list
endmodule declaration
10
Modules (Cont’d)
Example: 4-bit Ripple Carry Counter
Module
Module
Design Hierarchy 11
Modules (Cont’d)
Module description
A module definition
12
Modules (Cont’d)
Module_port declaration
module module name ( port name, port name,…);
module_port declaration Declare whether the ports are input and/or output
input <port_size> port name, port name, …;
output <port_size> port name, port name, …;
inout <port_size> port name, port name, …;
module
module data_conv ( a, b, …);
4 4
input [3:0] a; a e
8 A part of a chip, 4
input [7:0] b; b f
1 or whole the 16
output [3:0] e, f; c g
1 chip
output [15:0] g; d
inout c, d;
ports 13
Modules (Cont’d)
Outside connectors
Port Rules Diagram to internal ports, i.e.,
variables corresponding
to ports in instantiation
EXTERNAL
of internal module
MODULE wire
Example:
module external
reg a; wire inout
wire b;
internal in(a, b); //instantiation
Internal ports
port-connector
…
endmodule input output
module internal(x, y) reg or wire wire wire
INTERNAL reg or wire
input x;
MODULE
output y;
wire x;
reg y;
…
endmodule General rule (with few exceptions) Ports in all modules except for the
stimulus module should be wire. Stimulus module has registers to set data
for internal modules and wire ports only to read data from internal modules.
14
Modules (Cont’d)
Data type declaration
module module name ( port name, port name,…);
module_port declaration
Data type declaration Declare characteristics of variables
for net data type
wire <size> variable name, variable name, …;
wire <size> variable name, variable name, …;
module
wire [3:0] a;
4 q1 4
wire [7:0] b; a q3 e
8 SEL 4
wire c, d; b f
1 16
wire [3:0] f; c g
1 q2 sel3
wire [7:0] q1, q2, q3, q4; d
wire sel3, …;
…. 15
Modules (Cont’d)
Data type declaration (Cont’d)
module module name ( port name, port name,…);
module_port declaration Define signals which are output of FF, registers, and
other memory elements as register type variable.
Data type declaration
for register data type
reg <size> variable name, variable name, …;
reg <size> variable name, variable name, …;
module
4 4
wire [3:0] e; a q2 e
8 4
wire [15:0] g; b f
1 16
wire q2; c g
1
…. d
reg module2_1
reg
reg reg
wire wire
reg wire wire
reg
: memory element
17
Modules (Cont’d)
Example: Mistakes and correct on register and net data type (Cont’d)
module1 module2
20
Instances
Example: 4-bit Ripple Carry Counter
21
Instances (Cont’d)
Connecting module instance ports by ordered list
The port expressions listed for the module instance shall be in the same order as the
ports listed in the module declaration.
module topmod;
wire [4:0] v;
wire a,b,c,w;
modB b1 (v[0], v[3], w, v[4]);
endmodule
Connections are made by name, the order in which they appear is irrelevant.
module topmod;
wire [4:0] v;
wire a,b,c,w;
modB b1 (.wb(v[3]),.wa(v[0]),.d(v[4]),.c(w));
endmodule
23
END
24