Notes Lecture 3,4
Notes Lecture 3,4
M. D. Ciletti
Department
of
Electrical and Computer Engineering
University of Colorado
Colorado Springs, Colorado
[email protected]
Draft: Chap 5: Logic Design with Behavioral Models of Combinational and
Sequential Logic (Rev 9/23/2003)
Copyright 2000, 2002, 2003. These notes are solely for classroom use by the instructor. No part
of these notes may be copied, reproduced, or distributed to a third party, including students, in
any form without the written permission of the author.
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.
COURSE OVERVIEW
Data Types
Two families of data types for variables:
Nets: wire, tri, wand, triand, wor, trior, supply0, supply1
Registers: reg, integer, real, time, realtime
Nets establish structural connectivity
Register variables act as storage containers for the waveform of a signal
Default size of a net or reg variable is a signal bit
An integer is stored at a minimum of 32 bits
time is stored as 64 bit integer
real is stored as a real number
realtime stores the value of time as a real number
Behavioral Models
Behavioral models are abstract descriptions of functionality.
Widely used for quick development of model
Follow by synthesis
We'll consider two types:
o Continuous assignment (Boolean equations)
o Cyclic behavior (more general, e.g. algorithms)
Equivalent circuit:
enable
x_in1
x_in2
x_in3
x_in4
x_in5
y_out
Verilog
Description
Circuit
Schematic
Structural
Model
Truth
Table
User-defined
Primitive
Boolean
Equations
Continuous
Assignments
Simulation results:
Simulation results:
Example 5.15
module compare_2_RTL (A_lt_B, A_gt_B, A_eq_B, A1, A0, B1, B0);
input
A1, A0, B1, B0;
output
A_lt_B, A_gt_B, A_eq_B;
reg
A_lt_B, A_gt_B, A_eq_B;
always @ (A0 or A1 or B0 or B1) begin
A_lt_B =
({A1,A0} < {B1,B0});
A_gt_B = ({A1,A0} > {B1,B0});
A_eq_B = ({A1,A0} == {B1,B0});
end
endmodule
Modeling Trap
The order of execution of procedural statements in a cyclic behavior may
depend on the order in which the statements are listed
A procedural assignment cannot execute until the previous statement
executes
Expression substitution is recognized by synthesis tools
Example 5.16
module shiftreg_PA (E, A, clk, rst);
output A;
input E;
input clk, rst;
reg A, B, C, D;
always @ (posedge clk or posedge rst) begin
if (reset) begin A = 0; B = 0; C = 0; D = 0; end
else begin
Result of synthesis:
A = B;
B = C;
D
E
C = D;
D
D
Q
Q
D = E;
R
R
end
clk
end
rst
endmodule
B
D
Figure 5.8 Circuit synthesized as a result of expression substitution in an incorrect model of a 4-bit serial shift register.
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;
reg
always @ (A or B)
// Level-sensitive behavior
begin
A_lt_B = 0;
A_gt_B = 0;
A_eq_B = 0;
if (A == B)
A_eq_B = 1; // Note: parentheses are required
else if (A > B) A_gt_B = 1;
else
A_lt_B = 1;
end
endmodule
Result of synthesis:
A_eq_B
A_lt_B
A<0>
B<1:0>
B<0>
B<1>
A<1:0>
A<1>
A_gt_B
encoder
Code[2:0]
Code[2:0]
Data[6]
or4_a
Data[1]
Data[5]
Data[3]
Data[2]
Data[7:0]
Data[7]
or4_a
always @ (Data)
begin
if (Data[7]) Code = 7; else
if (Data[6]) Code = 6; else
if (Data[5]) Code = 5; else
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
3
Data[7:0]
priority
Code[2:0]
valid_data
: Code = 7;
: Code = 6;
: Code = 5;
: Code = 4;
: Code = 3;
: Code = 2;
: Code = 1;
: Code = 0;
: Code = 3'bx;
44
Synthesis Result:
Data[5]
Code[2]
Data[7:0]
Code[2:0]
Code[1]
Data[1]
Data[3]
Code[0]
Data[4]
Data[7]
Data[2]
Data[6]
Data[0]
valid_data
45
46
47
Synthesis Result:
Data[7:0]
Data[3]
nor3_a
Data[4]
nor3_a
Data[1]
nor2_a
Data[2]
nor2_a
Data[5]
nor2_a
Code[2]
inv_a
Data[6]
nor2_a
Data[0]
Code[0]
inv_a
nand2_a
Code[1]
and2_a
and3_a
Data[7]
Code[2:0]
inv_a
nand2_a
and3_a
48
b
g
c
e
d
// h01
// h4f
// h12
// h06
49
parameter
parameter
parameter
parameter
parameter
parameter
FOUR
FIVE
SIX
SEVEN
EIGHT
NINE
= 7'b100_1100;
= 7'b010_0100;
= 7'b010_0000;
= 7'b000_1111;
= 7'b000_0000;
= 7'b000_0100;
// h4c
// h24
// h20
// h0f
// h00
// h04
50
c N= 1
cN-1
c2
R
D
c1
R
Q
Y[1]
Clk
R
Q
Y[N-2]
Clk
R
Q
Clk
Y[N-1]
Y[N]
Clk
Clock
[1: Length]
[1: Length]
Clock, Reset;
Y;
Y;
51
52
c[8:1] = [1100_1111]
Y[1: 8]
Y[1]
1
Y[8]
+
c[7]
91H
c[1]
87H
8CH
46H
53
54
55
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
x 0 0 0 0 x
x x
x 1 1 1 1 x
x x
56
57
58
59
Clock, Reset;
Y;
Y;
k;
60
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
61
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
Note: Verilog 2001 includes arithmetic shift operators (See Appendix I)
62
63
clock
100
200
300
64
Example 5.34
65
66
esdpupd
reset
esdpupd
sum[5:0]
dffrgpqb_a
dffspqb_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
67
[3: 0]
[3: 0]
68
task add_values;
output
output
[3: 0]
input
[3: 0]
input
reg
reg
c_out;
sum;
data_a, data_b;
c_in;
sum;
c_out;
begin
{c_out, sum} <= data_a + (data_b + c_in);
end
endtask
endmodule
69
clk
dffrpqb_a
dffrpqb_a
dffrpqb_a
dffrpqb_a
data_a[3:0]
esdpupd
data_b[3:0]
c_in
reset
5
5
dffrpqb_a
c_out
sum[3:0]
70
71
[4: 0] result_1;
[3: 0] result_2;
[3: 0] operand_1, operand_2;
72
73
74
State Box
Conditional Output or
Register Operation Box
ASM Block
Decision Box
75
S_stop
S_med
rst
brake
Tail_Lite
Tail_Lite
brake
accel
1
S_high
accel
1
brake
S_slow
brake
accel
1
Tail_Lite
Tail_Lite
76
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
Data
8
P1[7: 0]
P1[7: 0]
P0[7: 0]
P0[7: 0]
R0[15: 0]
78
S_idle
rst
P1 <= Data
P0 <= P1
En
1
P1 <= Data
P0 <= P1
S_1
S_full
S_wait
Ld
1
Ld
En
See Problem 24
79
80
reset_
S_idle
2
0,3
up_dwn
1
1
S_decr
up_dwn
0,3
S_incr
up_dwn
2
0,3
81
82
count <= 0
count <= 0
reset_
S_running
count <= count - 1
0,3
up_dwn
reset_
2
0,3
up_dwn
83
84
85
reset
enable
clock
mux2_a
dffrgpqb_a
dffrgpqb_a
dffrgpqb_a
dffrgpqb_a
dffrgpqb_a
dffrgpqb_a
dffrgpqb_a
count[7]
count[6]
count[5]
count[4]
count[3]
count[2]
count[1]
dffspb_a
count[0]
count[7:0]
86
Copyright 2001, 2003 MD Ciletti
87
Data_in
3
count_up
u/d D_in
load
ld
reset
rst
counter_on
cnt
clk
clk count
3
Count
88
clk
xor2_a
count_up
aoi22_a
xor2_a
xor2_a
xor2_a
mux2_a
inv_a
mux2_a
dffrgpqb_a
dffrgpqb_a
Count[2:0]
Data_in[2:0]
inv_a
reset
inv_a
mux2_a
load
counter_on
nor2_a
dffrgpqb_a
89
90
Synthesis Result:
Data_in
clock
reset
Data_out
91
92
Synthesis Result
Data_in[1]
Data_in[2]
Data_in[3]
mux
mux
Data_in[0]
mux
mux
load
D
clock
reset
Data_out[3]
Data_out[2]
Data_out[1]
Data_out[0]
93
5
0
5
0
4
0
4
0
0
1
94
reset
load
Data_in[7:0]
clock
mux2_a
Data_out[0]
Data_out[1]
Data_out[2]
Data_out[3]
Data_out[4]
Data_out[5]
Data_out[6]
Data_out[7]
Data_out[7:0]
95
96
MSB_In
Universal_Shift_Reg
LSB_Out
MSB_Out
clk
rst
Data_Out
97
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;
assign MSB_Out = Data_Out[3];
assign LSB_Out = Data_Out[0];
always @ (posedge clk) begin
if (rst) Data_Out <= 0;
else case ({s1, s0})
0:
Data_Out <= Data_Out;
1:
Data_Out <= {MSB_In, Data_Out[3:1]};
2:
Data_Out <= {Data_Out[2: 0], LSB_In};
3:
Data_Out <= Data_In;
endcase
end
endmodule
// Hold
// Serial shift from MSB
// Serial shift from LSB
// Parallel Load
98
Simulation Results:
99
100
Read_Addr_1
Register File
Data_Out_1
32
Read_Addr_2
opcode
Alu_Zero
5
32
Write_Addr
Data_out
32
Data_In
Clock
Data_Out_2
Write_Enable
input
reg
[31: 0]
Write_Enable, Clock;
Reg_File [31: 0];
// 32bit x32 word memory declaration
101
1-20 ms
Vdd
Push button
R
Combinational
Logic
Q
Clk
102
Combinational
Logic
Q
Clk
1
Vdd
103
Metastability:
State_m
State_0
Metastable state
State_1
104
Synchronizer
Synch_out
Asynch_in
clock
reset
Q
R
Q
R
105
Synchronizer
q1
D
Asynch_in
clock
Q
Clr
q2
Q
Clr
Q
Clr
Synch_out
106
clock
Asynch_in
q1
q2
Synch_out
107
clock
Timing violation
Asynch_in
q1
q2
Synch_out
Metastable
state
Ambiguous
switching
time
108
Data_out
Data_in
clock_1
Q
R
clock_2
reset
Clock domain #1
Clock domain #2
109
Row[0]
Code[3]
Row[2]
Row[3]
Col[3]
Col[2]
Col[1]
Col[0]
Code[2]
Row[1]
Grayhill 072
Hex Keypad
Code
Generator
Code[1]
Code[0]
Valid
110
Keypad Codes
Key
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Row[3:0]
0001
0001
0001
0001
0010
0010
0010
0010
0100
0100
0100
0100
1000
1000
1000
1000
Col[3:0]
Code
0001
0010
0100
1000
0001
0010
0100
1000
0001
0010
0100
1000
0001
0010
0100
1000
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
111
Row_0,
1,2,3
0
S_4
/Col_3
Row_0,
1,2,3
0
S_3
/Col_2
Row_0,
1,2,3
0
S_2
/Col_1
Row_0,
1,2,3
1
S_1
/Col_0
S_Row
S_0
/Col_0,1,2,3
Valid
Valid
Valid
Valid
reset
Row_0, 1
1,2,3
S_5
/Col_0,1,2,3
112
Signal
Generator
for Keys
Code[2]
Row[1]
key
Row_Signal
Row[2]
16
Row[3]
Col[3]
Col[2]
Col[1]
Col[0]
Grayhill 072
Hex Keypad
Code
Generator
Code[1]
Code[0]
Valid
113
Co[0]
0
4
8
C
Col[1]
1
5
9
D
Col[2]
2
6
A
E
Col[3]
3
7
B
F
114
// A
115
// B
// C
// D
// E
// F
default:
endcase
Code = 0;
// Arbitrary choice
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 Synchronizer (S_Row, Row, clock, reset);
output
S_Row;
input [3: 0]
Row;
input
clock, reset;
reg
A_Row, S_Row;
// Two stage pipeline synchronizer
always @ (posedge clock or posedge reset) begin
if (reset) begin A_Row <= 0;
S_Row <= 0;
end
else begin
A_Row <= (Row[0] || Row[1] || Row[2] || Row[3]);
S_Row <= A_Row;
end
end
endmodule
117
118
reg
[15: 0]
Key;
integer
j, k;
reg[39: 0]
Pressed;
parameter [39: 0] Key_0 = "Key_0";
parameter [39: 0] Key_1 = "Key_1";
parameter [39: 0] Key_2 = "Key_2";
parameter [39: 0] Key_3 = "Key_3";
parameter [39: 0] Key_4 = "Key_4";
parameter [39: 0] Key_5 = "Key_5";
parameter [39: 0] Key_6 = "Key_6";
parameter [39: 0] Key_7 = "Key_7";
parameter [39: 0] Key_8 = "Key_8";
parameter [39: 0] Key_9 = "Key_9";
parameter [39: 0] Key_A = "Key_A";
parameter [39: 0] Key_B = "Key_B";
parameter [39: 0] Key_C = "Key_C";
parameter [39: 0] Key_D = "Key_D";
parameter [39: 0] Key_E = "Key_E";
parameter [39: 0] Key_F = "Key_F";
parameter [39: 0] None = "None";
always @ (Key) begin // "one-hot" code for pressed key
case (Key)
16'h0000: Pressed = None;
16'h0001: Pressed = Key_0;
119
Pressed = Key_4;
Pressed = Key_5;
Pressed = Key_6;
Pressed = Key_7;
16'h0100:
16'h0200:
16'h0400:
16'h0800:
Pressed = Key_8;
Pressed = Key_9;
Pressed = Key_A;
Pressed = Key_B;
16'h1000:
16'h2000:
16'h4000:
16'h8000:
Pressed = Key_C;
Pressed = Key_D;
Pressed = Key_E;
Pressed = Key_F;
120
121
122