資工系王俊堯教授數位邏輯設計Unit10 - Introduction to Verilog HDL
資工系王俊堯教授數位邏輯設計Unit10 - Introduction to Verilog HDL
Verilog
1
Hardware Description Language (HDL)
․High-Level Programming Language
․Special constructs to model microelectronic circuits
Describe the operation of a circuit at various levels of abstraction
Behavioral
Functional (Register Transfer Level)
Structural
Express the concurrency of circuit operation
Serve as input to synthesis
Verilog 2
Major HDLs
․Verilog HDL
Started by Gateway in 1984
Became open to public by Cadence in 1990
IEEE standard 1364 in 1995
Slightly better at gate/transistor level
Language style close to C/C++
Pre-defined data type, easy to use
․VHDL
Started by VHSIC project in 1980s
IEEE standard 1076 in 1987, IEEE 1164 in 1993
Slightly better at system level
Language style close to Ada/Pascal
User-defined data type, more flexible
․Equally effective, personal preference
Verilog 3
Why HDL
․Facilitate a top-down design methodology using synthesis
Design at a high implementation-independent level
Delay decision on implementation details
Easily explore design alternatives
Automatically map high-level description to a technology-specific
implementation
․Provides greater flexibility
Reuse earlier design components
Move designs between multiple vendors and tools
Verilog 4
Typical Design Flow
Behavioral Description
Physical Layout
RTL Description
Post-Layout Simulation
Functional Simulation
Layout Verification
(DRC, LVS,…)
Logic Synthesis
Implementation
Gate-Level Netlist
Pre-Layout Simulation
Verilog 5
Verilog-Supported Levels of Abstraction
․Behavioral
Describes a system by the flow of data between its functional blocks
Defines signal values when they change
․Functional or Register Transfer Level (RTL)
Describes a system by the flow of data and control signals between
and within its functional blocks
Defines signal values with respect to a clock
․Structural
Describes a system by connecting predefined components
Uses technology-specific, low-level components when mapping from
an RTL description to a gate-level netlist, such as during synthesis.
Verilog 6
Block Diagram & Schematic
a sum
b Add_half c_out
block diagram
a
sum
b
c_out_bar
c_out
schematic
Verilog 7
Modules
Verilog 8
Module Ports
Verilog 10
Predefined Primitives
xor, xnor
buf, not
Verilog 11
Full Adder
Add_full
Verilog 12
Hierarchical Description
Add_full
M1 M2
Add_half Add_half or
Verilog 13
RTL Level Description
․An RTL model provides sufficient architectural details that a
synthesis tool can construct the circuit
endmodule
Verilog 14
The Behavioral Level
Verilog 15
Behavioral Level Description
always@(a or b)
begin
sum = a ^ b; //exclusive or
c_out = a & b; //and
end
procedural statements
endmodule
Verilog 16
Behavioral Description of D F/F
clk declaration of
always @ (posedge clk) synchronous
begin behavior
if(rst == 1) q=0;
block
else q = data_in;
diagram
end
endmodule
Verilog 17
Variables
․Nets “wire”: structural connectivity
․Registers “reg”: abstraction of storage (may or may
not be physical storage)
․Both nets and registers are informally called signals,
and may be either scalar or vector
Verilog 18
Logic Values
․Verilog signal values
0: logical 0, or a FALSE condition
1: logical 1, or a TRUE condition
x: an unknown value
Verilog 19
Hierarchical De-Referencing
․To reference a variable defined inside an instantiated module
․Supported by a variable’s hierarchical path name
X.w
X.Y.Z.w
Module A - Instance X
wire w
Module B - Instance Y
Module C - Instance Z
Wire w
Verilog 20
if…else
․else is paired with nearest if when ambiguous (use
begin…end in nesting to clarify)
Verilog 21
Example
module mux4_PCA (a, b, c, d, select, y_out);
input a, b, c, d;
input [1:0] select;
output y_out;
reg y_out;
always @ (select or a or b or c or d)
if (select == 0) y_out = a; else
if (select == 1) y_out = b; else
if (select == 2) y_out = c; else
if (select == 3) y_out = d; else
y_out = 1’bx;
endmodule
Verilog 22
case
Verilog 24
Continuous Assignment
module or_nand_2 (enable, x1, x2, x3, x4, y);
input enable, x1, x2, x3, x4;
output y;
x1 w1
oai22_a
x2 x1
x2
enable x3 nand2i_a
x4 y
x3 w3 enable
x4
Synthesis result
Verilog 25
Behavioral Description
module or_nand_3 (enable, x1, x2, x3, x4, y);
input enable, x1, x2, x3, x4;
output y;
reg y;
oai22_a
x1
x2
x3 nand2i_a
x4 y
enable
if(enable) DL0A
q = data; enable
always@(enable or data)
Latch inferred
begin
if(enable)
enable AN2A
q = data; data
q
else
q = 0;
Specify all output values
end
Verilog 27
Avoid Latch Inference (2/2)
always@(a or b or c)
always@(a or b or c) case(a) Add a default
case(a) No latch statement
2’b11 : e = b;
2’b11 : e = b; 2’b10 : e = ~c;
2’b10 : e = ~c; default : e = 0;
endcase endcase
result in a latch
Verilog 28
Code Converter (1/3)
Verilog 29
Code Converter (2/3)
․Equations: (share terms to minimize cost)
W = A + BC + BD = A + B(C + D)
X = BC + BD + BCD = B(C + D) + BCD
Y = CD + CD = C + D
Z=D D’ Z
CD C D CD
AB 00 01 11 10 C Y
00 (C + D)’
C+D
01 1 1 1 B B
11 X
A
10 1 1
D
W
W = A + BC + BD A
Verilog 30
Code Converter (3/3)
Verilog 31
Decoder (1/2)
․A decoder is to generate D0 = x’y’z’
the 2n (or fewer)
minterms of n input z
D1 = x’y’z
variables
D2 = x’yz’
․Ex: a 3-to-8 line decoder
y
D3 = x’yz
Inputs Outputs
x y z D0 D1 D2 D3 D4 D5 D6 D7 D4 = xy’z’
0 0 0 1 0 0 0 0 0 0 0
x
0 0 1 0 1 0 0 0 0 0 0
0 1 0 0 0 1 0 0 0 0 0 D5 = xy’z
0 1 1 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 1 0 0 0
D6 = xyz’
1 0 1 0 0 0 0 0 1 0 0
1 1 0 0 0 0 0 0 0 1 0
1 1 1 0 0 0 0 0 0 0 1 D7 = xyz
Verilog 32
Decoder (2/2)
․ Behavior style 1 ․ Behavior style 2
input x, y, z; input x, y, z;
output [7:0] D; output [7:0] D;
reg [7:0] D; wire [2:0] addr;
always @(x or y or z) begin reg [7:0] D;
case ({x, y, z}) assign addr = {x, y, z};
3‘b000: D = 8‘b00000001; always @(addr) begin
3‘b001: D = 8‘b00000010; D = 8‘b0;
… D[addr] = 1;
3‘b111: D = 8‘b10000000; end
default: D = 8‘b0;
endcase
end
Verilog 33
Multiplexer (1/2)
․A multiplexer uses n I0
maximum of 2n unique Y
input lines I2
S1 S 0 Y
I3
0 0 I0
0 1 I1
1 0 I2
1 1 I3
(b) Function table
S1
S0 decoder !!
(a) Logic diagram
Verilog 34
Multiplexer (2/2)
․Behavior style 1 ․Behavior style 2
input [1:0] S; input [1:0] S;
input [3:0] I; input [3:0] I;
output Y; output Y;
reg Y; reg Y;
always @(S or I) begin always @(S or I)
case (S) begin
2‘b00: Y = I[0]; Y = I[S];
2‘b01: Y = I[1]; end
2‘b10: Y = I[2];
2‘b11: Y = I[3];
default: Y = 0;
endcase
end
Verilog 35
BCD Adder (1/2)
Addend Augend
C detects whether the binary sum
is greater than 9 (1001)
Carry Carry
K 4-bit binary adder
out in
Z8 Z4 Z2 Z1
C = K + Z8Z4 + Z8Z2
Output
carry
S8 S4 S2 S1
Verilog 36
BCD Adder (2/2)
module bcd_add (S, Cout, A, B, Cin);
input [3:0] A, B;
input Cin;
output [3:0] S;
output Cout;
reg [3:0] S;
reg Cout;
Verilog 38
More Latches
․D latch with gated enable
always @(enable or d or gate)
if (enable & gate)
q = d; d
q
enable
gate
Y2 = X2;
endmodule Clock
Verilog 40
Modeling Reset
Verilog 41
The Simplest Shift Register
Serial SI D D D D SO Serial
input output
C C C C
CLK
SRG4
Clock
SI SO
Symbol
Verilog 42
Procedural Assignment
Verilog 43
Examples
a = 1; a = 1;
b = 0; b = 0;
… …
a <= b; // use b=0 b <= a; // use a=1
b <= a; // use a=1 a <= b; // use b=0
a = 1; a = 1;
b = 0; b = 0;
… …
a = b; // use b=0 b = a; // use a=1
b = a; // use a=0 a = b; // use b=1
Verilog 44
HDL Modeling for Shift Reg.
․ Blocking assignment ․ Non-blocking assignment
assign SO = D; assign SO = D;
always @(posedge CLK) begin always @(posedge CLK)begin
if (reset) begin if (reset) begin
A=0; B=0; C=0; D=0; A<=0; B<=0; C<=0; D<=0;
end end
else if (shift) begin else if (shift) begin
D = C; A <= SI;
C = B; C <= B;
B = A; D <= C;
A = SI; B <= A;
end end
end end can be any order
order dependent
Verilog 45
Non-Blocking Assignment
always@(posedge clk) always@(posedge clk)
begin outa=in;
outa<=in; always@(posedge clk)
outb<=outa; outb=outa;
outc<=outb; always@(posedge clk)
end outc=outb;
in outc
D Q D Q D Q
clk
Verilog 46
References
․T.-C. Wang, course slides of “HDL and Synthesis”,
Dept. of CS, NTHU.
․C.-N. Liu, course slides of “Digital System Design”,
Dept. of EE. NCU.
Verilog 47