Advanced Digital Design With The Verilog
Advanced Digital Design With The Verilog
Advanced Digital Design With The Verilog
M. D. Ciletti
Department
of
Electrical and Computer Engineering
University of Colorado
Colorado Springs, Colorado
ciletti@vlsic.uccs.edu
Note to the instructor: These slides are provided solely for classroom use in academic
institutions by the instructor using the text, Advance Digital Design with the Verilog HDL by
Michael Ciletti, published by Prentice Hall. This material may not be used in off-campus
instruction, resold, reproduced or generally distributed in the original or modified format for any
purpose without the permission of the Author. This material may not be placed on any server or
network, and is protected under all copyright laws, as they currently exist. I am providing these
slides to you subject to your agreeing that you will not provide them to your students in
hardcopy or electronic format or use them for off-campus instruction of any kind. Please email
to me your agreement to these conditions.
I will greatly appreciate your assisting me by calling to my attention any errors or any other
revisions that would enhance the utility of these slides for classroom use.
Copyright 2001, 2003 MD Ciletti 3
COURSE OVERVIEW
•
•
Review of combinational and sequential logic design
•
Modeling and verification with hardware description languages
•
Introduction to synthesis with HDLs
•
Programmable logic devices
•
State machines, datapath controllers, RISC CPU
•
Architectures and algorithms for computation and signal processing
•
Synchronization across clock domains
•
Timing analysis
Fault simulation and testing, JTAG, BIST
Copyright 2001, 2003 MD Ciletti 4
Data Types
Behavioral Models
assign y_out = ~((x_in1 & x_in2) | (x_in3 & x_in4 & x_in5));
endmodule
• The LHS variable is monitored automatically and updates when the RHS
expression changes value
Copyright 2001, 2003 MD Ciletti 7
assign y_out = enable ? ~((x_in1 & x_in2) | (x_in3 & x_in4 & x_in5)) : 1'bz;
endmodule
Equivalent circuit:
enable
x_in1
x_in2 y_out
x_in3
x_in4
x_in5
Copyright 2001, 2003 MD Ciletti 9
wire y_out = enable ? ~((x_in1 & x_in2) | (x_in3 & x_in4 & x_in5)) : 1'bz;
endmodule
Copyright 2001, 2003 MD Ciletti 10
assign A_lt_B = (~A1) & B1 | (~A1) & (~A0) & B0 | (~A0) & B1 & B0;
assign A_gt_B = A1 & (~B1) | A0 & (~B1) & (~B0) | A1 & A0 & (~B0);
assign A_eq_B = (~A1) & (~A0) & (~B1) & (~B0) | (~A1) & A0 & (~B1) & B0
| A1 & A0 & B1 & B0 | A1 & (~A0) & B1 & (~B0);
endmodule
Logic Verilog
Description Description
Circuit Structural
Schematic Model
Truth User-defined
Table Primitive
Boolean Continuous
Equations Assignments
Copyright 2001, 2003 MD Ciletti 14
Example 5.7
module Latch_CA (q_out, data_in, enable);
output q_out;
input data_in, enable;
Simulation results:
Copyright 2001, 2003 MD Ciletti 17
Simulation results:
Copyright 2001, 2003 MD Ciletti 19
assign q_bar = ~ q;
assign A_lt_B = (A < B); // The RHS expression is true (1) or false (0)
assign A_gt_B = (A > B);
assign A_eq_B = (A == B);
endmodule
Copyright 2001, 2003 MD Ciletti 25
Example 5.15
Modeling Trap
Example 5.16
module shiftreg_PA (E, A, clk, rst);
output A;
input E;
input clk, rst;
reg A, B, C, D;
D = E;
end R R R
clk
end
endmodule rst
Copyright 2001, 2003 MD Ciletti 30
rst
Figure 5.8 Circuit synthesized as a result of expression substitution in an incorrect model of a 4-bit serial shift register.
Copyright 2001, 2003 MD Ciletti 31
Example 5.17
Algorithm-Based Models
Example 5.18
module compare_2_algo (A_lt_B, A_gt_B, A_eq_B, A, B);
output A_lt_B, A_gt_B, A_eq_B;
input [1: 0] A, B;
Result of synthesis:
A_eq_B
A_lt_B
A<0>
B<0>
B<1:0>
B<1>
A_gt_B
A<1:0> A<1>
Copyright 2001, 2003 MD Ciletti 35
module Mux_4_32_if
(mux_out, data_3, data_2, data_1, data_0, select, enable);
output [31: 0] mux_out;
input [31: 0] data_3, data_2, data_1, data_0;
input [1: 0] select;
input enable;
reg [31: 0] mux_int;
8 3
Data[7:0] encoder Code[2:0]
always @ (Data)
begin
if (Data == 8'b00000001) Code = 0; else
if (Data == 8'b00000010) Code = 1; else
if (Data == 8'b00000100) Code = 2; else
if (Data == 8'b00001000) Code = 3; else
if (Data == 8'b00010000) Code = 4; else
if (Data == 8'b00100000) Code = 5; else
if (Data == 8'b01000000) Code = 6; else
if (Data == 8'b10000000) Code = 7; else Code = 3'bx;
end
Copyright 2001, 2003 MD Ciletti 41
always @ (Data)
case (Data)
8'b00000001 : Code = 0;
8'b00000010 : Code = 1;
8'b00000100 : Code = 2;
8'b00001000 : Code = 3;
8'b00010000 : Code = 4;
8'b00100000 : Code = 5;
8'b01000000 : Code = 6;
8'b10000000 : Code = 7;
default : Code = 3'bx;
endcase
*/
endmodule
Copyright 2001, 2003 MD Ciletti 42
or4_a
Data[4]
Code[2:0]
Data[6]
or4_a
Data[1]
Data[5]
Data[3] or4_a
Data[2]
Data[7]
Data[7:0]
Copyright 2001, 2003 MD Ciletti 43
always @ (Data)
begin
3
if (Data[7]) Code = 7; else
if (Data[6]) Code = 6; else 8 Code[2:0]
if (Data[5]) Code = 5; else
Data[7:0] priority
valid_data
if (Data[4]) Code = 4; else
if (Data[3]) Code = 3; else
if (Data[2]) Code = 2; else
if (Data[1]) Code = 1; else
if (Data[0]) Code = 0; else
Code = 3'bx;
end
Copyright 2001, 2003 MD Ciletti 44
always @ (Data)
casex (Data)
8'b1xxxxxxx : Code = 7;
8'b01xxxxxx : Code = 6;
8'b001xxxxx : Code = 5;
8'b0001xxxx : Code = 4;
8'b00001xxx : Code = 3;
8'b000001xx : Code = 2;
8'b0000001x : Code = 1;
8'b00000001 : Code = 0;
default : Code = 3'bx;
endcase
*/
endmodule
Copyright 2001, 2003 MD Ciletti 45
Synthesis Result:
Code[2:0]
Code[2]
Data[5]
Code[1]
Data[7:0]
Data[6]
Data[2]
Data[1]
Data[3]
Data[4] Code[0]
Data[7]
Data[0]
valid_data
Copyright 2001, 2003 MD Ciletti 46
always @ (Code)
begin
if (Code == 0) Data = 8'b00000001; else
if (Code == 1) Data = 8'b00000010; else
if (Code == 2) Data = 8'b00000100; else
if (Code == 3) Data = 8'b00001000; else
if (Code == 4) Data = 8'b00010000; else
if (Code == 5) Data = 8'b00100000; else
if (Code == 6) Data = 8'b01000000; else
if (Code == 7) Data = 8'b10000000; else
Data = 8'bx;
end
Copyright 2001, 2003 MD Ciletti 47
Synthesis Result:
Data[7:0]
Data[3]
nor3_a
Data[4]
nor3_a
Data[1]
nor2_a
Data[2]
nor2_a
Data[5]
Code[2] nor2_a
inv_a Data[6]
nor2_a
Code[0] Data[0]
f b
g
e c
Reset
c N= 1 cN-1 c2 c1
R R R R
D Q + D Q + D Q + D Q
Y[1] Y[N-2] Y[N-1] Y[N]
Clk Clk Clk Clk
Clock
end
endmodule
Copyright 2001, 2003 MD Ciletti 53
Y[1] Y[8]
1 + 0 0 1 + 0 + 0 + 0 + 1 91H
c[7] c[1]
1 + 0 0 0 + 0 + 1 + 1 + 1 87H
1 + 0 0 0 + 1 + 1 + 0 + 0 8CH
0 + 1 0 0 + 0 + 1 + 1 + 0 46H
Copyright 2001, 2003 MD Ciletti 54
...
word_address = 0;
repeat (memory_size)
begin
memory [ word_address] = 0;
word_address = word_address + 1;
end
...
Copyright 2001, 2003 MD Ciletti 56
…
for (K = 4; K; K = K - 1)
begin
demo_register [K + 10] = 0;
demo_register [K + 2] = 1;
end
…
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
x 0 0 0 0 x x x x 1 1 1 1 x x x
Copyright 2001, 2003 MD Ciletti 57
count = 0;
temp_reg = reg_a; // load a data word
while (temp_reg)
begin
if (temp_reg[0]) count = count + 1;
temp_reg = temp_reg >> 1;
end
end
Copyright 2001, 2003 MD Ciletti 62
Alternative Description:
begin: count_of_1s
reg [7: 0] temp_reg;
count = 0;
temp_reg = reg_a; // load a data word
while (temp_reg)
begin
count = count + temp_reg[0];
temp_reg = temp_reg >> 1;
end
end
initial
begin: clock_loop // Note: clock_loop is a named block of statements
clock = 0;
forever
begin
#half_cycle clock = 1;
#half_cycle clock = 0;
end
end
initial
#350 disable clock_loop;
Copyright 2001, 2003 MD Ciletti 64
clock
t
100 200 300
Copyright 2001, 2003 MD Ciletti 65
Example 5.34
always @ (trigger)
begin: search_for_1
index_value = 0;
for (index_value = 0; index_value < 15; index_value = index_value + 1)
if (A_word[index_value] == 1) disable search_for_1;
end
endmodule
Copyright 2001, 2003 MD Ciletti 66
esdpupd
reset
esdpupd
sum[5:0]
dffspqb_a dffrgpqb_a
dffspqb_a
dffrgpqb_a
esdpupd
mux_2a
data[3:0] dffrgpqb_a
esdpupd
mux_2a
dffrgpqb_a
esdpupd
mux_2a
dffrgpqb_a
esdpupd
mux_2a
+
dffrgpqb_a
esdpupd
esdpupd
Copyright 2001, 2003 MD Ciletti 68
module adder_task (c_out, sum, clk, reset, c_in, data_a, data_b, clk);
output [3: 0] sum;
output c_out;
input [3: 0] data_a, data_b;
input clk, reset;
input c_in;
task add_values;
output c_out;
output [3: 0] sum;
input [3: 0] data_a, data_b;
input c_in;
reg sum;
reg c_out;
begin
{c_out, sum} <= data_a + (data_b + c_in);
end
endtask
endmodule
Copyright 2001, 2003 MD Ciletti 70
clk
dffrpqb_a c_out
dffrpqb_a sum[3:0]
dffrpqb_a
dffrpqb_a
data_a[3:0]
esdpupd
5
+ dffrpqb_a
data_b[3:0] 5
+ 5
c_in 5
reset
Copyright 2001, 2003 MD Ciletti 71
• STGs do not directly display the evolution of states resulting from an input
• ASM charts reveal the sequential steps of a machine's activity
• Focus on machine's activity, rather than contents of registers
• ASM chart elements
1. state box
2. decision box
3. conditional box
• Clock governs transitions between states
• Linked ASM charts describe complex machines
• ASM charts represent Mealy and Moore machines
Copyright 2001, 2003 MD Ciletti 75
ASM Block
Copyright 2001, 2003 MD Ciletti 76
S_stop S_med
1 1
rst Tail_Lite brake
1
brake Tail_Lite
accel
accel S_high
1
1
S_slow brake Tail_Lite
1
brake Tail_Lite
accel
1
Copyright 2001, 2003 MD Ciletti 77
ASMD Chart
• Form an ASMD (Algorithmic State Machine and datapath) chart by annotating each of
its paths to indicate the concurrent register operations that occur in the associated
datapath unit when the state of the controller makes a transition along the path
• Clarify a design of a sequential machine by separating the design of its datapath from
the design of the controller
• ASMD chart maintains a clear relationship between a datapath and its controller
• Annotate path with concurrent register operations
• Outputs of the controller control the datapath
Copyright 2001, 2003 MD Ciletti 78
Data
P1[7: 0] P0[7: 0]
8 8 8
1
rst
P1 <= Data En
P0 <= P1
1
P1 <= Data S_1 {P1, P0} <= {0, 0}
P0 <= P1
S_wait Ld
1 1
1
Ld En
See Problem 24
Copyright 2001, 2003 MD Ciletti 80
reset_
S_idle
2 1
0,3 2 1 0,3
up_dwn S_decr up_dwn S_incr up_dwn
1 0,3 2
Copyright 2001, 2003 MD Ciletti 82
Copyright 2001, 2003 MD Ciletti 83
2
0,3 1
up_dwn
2
Copyright 2001, 2003 MD Ciletti 84
endmodule
Copyright 2001, 2003 MD Ciletti 85
.
reset
mux2_a
count[0]
enable
dffspb_a
clock
count[7:0]
dffrgpqb_a
count[7]
dffrgpqb_a
count[6]
dffrgpqb_a
count[5]
dffrgpqb_a count[4]
dffrgpqb_a
count[3]
dffrgpqb_a
count[2]
count[1]
dffrgpqb_a
Copyright 2001, 2003 MD Ciletti 87
Data_in
load ld
reset rst
counter_on cnt
clk clk count
3
Count
Copyright 2001, 2003 MD Ciletti 89
clk
xor2_a
count_up
xor2_a
xor2_a
aoi22_a
xor2_a mux2_a dffrgpqb_a
inv_a
dffrgpqb_a
mux2_a
Data_in[2:0] Count[2:0]
inv_a
inv_a dffrgpqb_a
reset mux2_a
load
counter_on nor2_a
Synthesis Result:
Data_in Data_out
D Q D Q D Q D Q
R R R R
clock
reset
Copyright 2001, 2003 MD Ciletti 92
Synthesis Result
load
D Q D Q D Q D Q
R R R R
clock
reset
7 6 5 4 3 2 1 0
1 1 0 0 0 1 1 0
7 6 5 4 3 2 1 0
1 0 0 0 1 1 0 1
clock
Data_out[7] Data_out[7:0]
Data_in[7:0]
mux2_a
Data_out[6]
Data_out[5]
Data_out[4]
Data_out[3]
Data_out[2]
Data_out[1]
load
reset
95
Copyright 2001, 2003 MD Ciletti 96
Data_In
s0
s1
MSB_In LSB_In
Universal_Shift_Reg
MSB_Out LSB_Out
clk
rst
Data_Out
Copyright 2001, 2003 MD Ciletti 98
module Universal_Shift_Reg
(Data_Out, MSB_Out, LSB_Out, Data_In, MSB_In, LSB_In, s1, s0, clk, rst);
output [3: 0] Data_Out;
output MSB_Out, LSB_Out;
input [3: 0] Data_In;
input MSB_In, LSB_In;
input s1, s0, clk, rst;
reg [3: 0] Data_Out;
Simulation Results:
Copyright 2001, 2003 MD Ciletti 100
5 Register File
Read_Addr_1
5 Data_Out_1 32
Read_Addr_2
opcode
5 Alu_Zero
Write_Addr Data_out
32
32
Data_In
Clock Data_Out_2
Write_Enable
1-20 ms
R
Combinational
D Q
Logic
Clk
Copyright 2001, 2003 MD Ciletti 103
0 1
R
Combinational
D Q
Logic
Clk
1 0 1 0
Vdd
Copyright 2001, 2003 MD Ciletti 104
Metastability:
State_0 State_1
Copyright 2001, 2003 MD Ciletti 105
Synchronizer
Synch_out
Asynch_in D Q D Q
clock R R
reset
Copyright 2001, 2003 MD Ciletti 106
VCC
Synchronizer
q1 q2
D Q D Q D Q Synch_out
clock
Copyright 2001, 2003 MD Ciletti 107
clock
Asynch_in
q1
q2
Synch_out
Copyright 2001, 2003 MD Ciletti 108
clock
Timing violation
Asynch_in
Metastable
q1 state
Ambiguous
q2 switching
time
Synch_out
Copyright 2001, 2003 MD Ciletti 109
Data_out
Data_in D Q D Q
clock_1 R R
clock_2
reset
Row[0]
0 1 2 3 Code[3]
Row[1] Code[2]
4 5 6 7 Grayhill 072
Hex Keypad Code[1]
Code
Row[2] Generator
8 9 A B Code[0]
Valid
Row[3]
C D E F
Col[3]
Col[2]
Col[1]
Col[0]
Copyright 2001, 2003 MD Ciletti 111
Keypad Codes
0
S_Row
1
S_1
/Col_0
Row_0, 1
Valid
1,2,3
0
S_2 S_5
/Col_1 /Col_0,1,2,3
Row_0, 1 Row_0, 1
Valid
1,2,3 1,2,3
0 0
S_3
/Col_2
Row_0, 1
Valid
1,2,3 Copyright 2001, 2003 MD Ciletti
0
S_4
/Col_3
Row_0, 1
Valid
1,2,3
0
112
Copyright 2001, 2003 MD Ciletti 113
Row[0]
Code[3]
Row[1] Code[2]
key Grayhill 072
Signal
Row_Signal Hex Keypad Code[1]
Generator
Code
for Keys 16 Row[2] Generator Code[0]
Valid
Row[3]
Col[3]
Col[2]
Col[1]
Col[0]
Copyright 2001, 2003 MD Ciletti 114
// Does not matter if the row signal is not the debounced version.
// Assumed to settle before it is used at the clock edge
8'b0010_0001: Code = 4;
8'b0010_0010: Code = 5;
8'b0010_0100: Code = 6;
8'b0010_1000: Code = 7;
8'b0100_0001: Code = 8;
8'b0100_0010: Code = 9;
8'b0100_0100: Code = 10; // A
Copyright 2001, 2003 MD Ciletti 116
S_4: begin Col = 8; if (Row) next_state = S_5; else next_state = S_0; end
// Assert all rows
S_5: begin Col = 15; if (Row == 0) next_state = S_0; end
endcase
end
endmodule
module Row_Signal (Row, Key, Col); // Scans for row of the asserted key
output [3: 0] Row;
input [15: 0] Key;
input [3: 0] Col;
reg [3: 0] Row;