Digital System Design Verilog HDL Modules and Ports

  • Slides: 28
Download presentation
Digital System Design Verilog® HDL Modules and Ports 2005 Verilog HDL Maziar Goudarzi

Digital System Design Verilog® HDL Modules and Ports 2005 Verilog HDL Maziar Goudarzi

Today program l Components of a Verilog Module l Defining and Connecting Ports 2005

Today program l Components of a Verilog Module l Defining and Connecting Ports 2005 Verilog HDL 2

Modules l Basic building block in Verilog ¡ ¡ Hierarchical design (top -down vs.

Modules l Basic building block in Verilog ¡ ¡ Hierarchical design (top -down vs. bottom-up) Multiple modules in a single file l 2005 Order of definition not important Verilog HDL 3

Module Example: SR-Latch 2005 Verilog HDL 4

Module Example: SR-Latch 2005 Verilog HDL 4

SR-Latch Example module SR_latch(Q, Qbar, Sbar, Rbar); output Q, Qbar; input Sbar, Rbar; //

SR-Latch Example module SR_latch(Q, Qbar, Sbar, Rbar); output Q, Qbar; input Sbar, Rbar; // Instantiate lower level modules // Note, how the wires are connected in a cross coupled fashion. nand n 1(Q, Sbar, Qbar); nand n 2(Qbar, Rbar, Q); endmodule Top; wire q, qbar; reg set, reset; // Instantiate lower level modules. In this case, instantiate SR_latch // Feed inverted set and reset signals to the SR latch SR_latch l 1(q, qbar, ~set, ~reset); // Behavioral block, initial begin $monitor($time, " set = %b, reset= %b, q= % bn", set, reset, q); set = 0; reset = 0; #5 reset = 1; #5 reset = 0; #5 set = 1; #5 set = 0; reset = 0; #5 set = 1; reset = 0; 2005 Verilog HDL endmodule 5

2005 Verilog HDL 6

2005 Verilog HDL 6

2005 Verilog HDL 7

2005 Verilog HDL 7

Ports l Interface of the module to the environment ¡ Internals of the module

Ports l Interface of the module to the environment ¡ Internals of the module are not visible to the environment ¡ Internals can be changed as long as the interface (ports) is not changed 2005 Verilog HDL 8

List of Ports l Example: 4 -bit full-adder inside a top module fulladd 4(sum,

List of Ports l Example: 4 -bit full-adder inside a top module fulladd 4(sum, c_out, a, b, c_in); // Module with a list // of ports module top; // No list of ports, top-level module // in simulation 2005 Verilog HDL 9

Port Declaration l All ports in the list of ports must be declared in

Port Declaration l All ports in the list of ports must be declared in the module l Verilog keyword Type (mode) of port input output inout Input port Output port Bidirectional port l Example: module fulladd 4(sum, c_out, a, b, c_in); output [3: 0] sum; output c_out; input [3: 0] a, b; input c_in; . . . endmodule 2005 Verilog HDL 10

Port Declaration (cont’d) l Note: all ports are wire by default ¡ ¡ ¡

Port Declaration (cont’d) l Note: all ports are wire by default ¡ ¡ ¡ No need to declare it again as wire If expected to be reg, the port needs to be declared again (only valid for output ports. Why? ) Example: the q port in DFF module DFF(q, d, clk, reset); output q; reg q; // Output port q holds value => reg input d, clk, reset; . . . endmodule 2005 Verilog HDL 11

Module Instantiation l Recall the Ripple-carry counter and TFF module TFF(q, clk, reset); output

Module Instantiation l Recall the Ripple-carry counter and TFF module TFF(q, clk, reset); output q; input clk, reset; . . . endmodule ripple_carry_counter(q, clk, reset); output [3: 0] q; input clk, reset; //4 instances of the module TFF are created. TFF tff 0(q[0], clk, reset); TFF tff 1(q[1], q[0], reset); TFF tff 2(q[2], q[1], reset); TFF tff 3(q[3], q[2], reset); endmodule 2005 Verilog HDL 12

Module Instantiation (cont’d) l General syntax <module_name> <instance_name>(port connection list); l Example: // assuming

Module Instantiation (cont’d) l General syntax <module_name> <instance_name>(port connection list); l Example: // assuming module ripple_carry_counter(q, clk, reset); ripple_carry_counter cntr 1(wire_vec 1, wire 2, wire 3); 2005 Verilog HDL 13

Port Connection Rules l Two parts of a port: internal and external to the

Port Connection Rules l Two parts of a port: internal and external to the module 2005 Verilog HDL 14

Port Connection Rules (cont’d) l Width matching ¡ Legal l to connect items of

Port Connection Rules (cont’d) l Width matching ¡ Legal l to connect items of different sizes A warning may be issued by Verilog simulator l Unconnected ports ¡ Allowed in Verilog ¡ Example: // module fulladd 4(sum, c_out, a, b, c_in); fulladd 4 fa 0(SUM, , A, B, C_IN); // output port c_out is unconnected 2005 Verilog HDL 15

Port Connection Rules (cont’d) l Example of illegal port connection module Top; // Declare

Port Connection Rules (cont’d) l Example of illegal port connection module Top; // Declare connection variables reg [3: 0] A, B; reg C_IN; reg [3: 0] SUM; wire C_OUT; // Instantiate fulladd 4, call it fa 0 fulladd 4 fa 0(SUM, C_OUT, A, B, C_IN); // Illegal connection because output port sum is connected to reg. . . endmodule // Top 2005 Verilog HDL 16

2005 Verilog HDL 17

2005 Verilog HDL 17

Connecting Ports to External Signals l Two ways for port mapping ¡ Connecting by

Connecting Ports to External Signals l Two ways for port mapping ¡ Connecting by ordered list More intuitive for beginners l Mostly used when having few ports l ¡ Connecting by name Used when having more ports l Gives independence from order of ports l • The order of ports in the port list of a module can be changed without changing the instantiations 2005 Verilog HDL 18

Connecting by Ordered List module fulladd 4(sum, c_out, a, b, c_in); . . .

Connecting by Ordered List module fulladd 4(sum, c_out, a, b, c_in); . . . endmodule Top; // Declare connection variables reg [3: 0] A, B; reg C_IN; wire [3: 0] SUM; wire C_OUT; // Signals are connected to ports in order (by position) fulladd 4 fa_ordered(SUM, C_OUT, A, B, C_IN); . . . endmodule 2005 Verilog HDL 19

Connecting Ports by Name module fulladd 4(sum, c_out, a, b, c_in); . . .

Connecting Ports by Name module fulladd 4(sum, c_out, a, b, c_in); . . . endmodule Top; // Declare connection variables reg [3: 0] A, B; reg C_IN; wire [3: 0] SUM; wire C_OUT; // Signals are connected to ports by name fulladd 4 fa_byname(. c_out(C_OUT), . sum(SUM), . b(B), . a(A)); . . . endmodule 2005 Verilog HDL 20

Hierarchical Names l Hierarchical design ¡ An identifier for every signal, variable, or module

Hierarchical Names l Hierarchical design ¡ An identifier for every signal, variable, or module instance ¡ The same identifier can be used in different levels of the hierarchy ¡ Hierarchical name referencing Unique name to every identifier in the hierarchy l Syntax: l <top-module-name>. <instance-name>. <identifier> 2005 Verilog HDL 21

Hierarchical Names (cont’d) Stimulus set reset 2005 S m 1 Q R Qbar q

Hierarchical Names (cont’d) Stimulus set reset 2005 S m 1 Q R Qbar q qbar Verilog HDL 22

Hierarchical Names (cont’d) module stimulus; wire q, qbar; reg set, reset; SR_latch m 1(q,

Hierarchical Names (cont’d) module stimulus; wire q, qbar; reg set, reset; SR_latch m 1(q, qbar, ~set, ~reset); initial. . . endmodule SR_latch(Q, Qbar, S, R); output Q, Qbar; input S, R; nand n 1(Q, S, Qbar); nand n 2(Qbar, R, Q); endmodule l 2005 Stimulus set reset S m 1 Q R Qbar q qbar Hierarchical names stimulus. qbar stimulus. reset stimulus. m 1. Q stimulus. m 1. S stimulus. n 1 stimulus. q stimulus. set stimulus. m 1. Qbar stimulus. m 1. R Verilog HDL stimulus. n 2 23

Primitive Gates in Verilog 2005 Verilog HDL 24

Primitive Gates in Verilog 2005 Verilog HDL 24

Primitive Gates in Verilog l Syntax ¡ Similar to module instantiations l Examples wire

Primitive Gates in Verilog l Syntax ¡ Similar to module instantiations l Examples wire out, in 1, in 2, in 3; and a 1(out, in 1, in 2); nand na 1_3 inp(out, in 1, in 2, in 3); 2005 Verilog HDL 25

What we learned today l How to ¡ ¡ ¡ Define modules Instantiate modules

What we learned today l How to ¡ ¡ ¡ Define modules Instantiate modules Connect ports of modules together and to external signals (port mapping) Got familiar to different components of a module l Hierarchical Design in Verilog down to primitive gates l 2005 Verilog HDL 26

Exercise l Design 2005 a 4 -bit full-adder module Verilog HDL 27

Exercise l Design 2005 a 4 -bit full-adder module Verilog HDL 27

Other Notes l Homework 3 ¡ Chapter 4 exercises ¡ Design and simulate a

Other Notes l Homework 3 ¡ Chapter 4 exercises ¡ Design and simulate a 3 -to-8 decoder in Verilog ¡ Due date: Next Sunday (Aban 15 th) 2005 Verilog HDL 28