Digital Design: An Embedded Systems Approach Using Verilog: Sequential Basics
Digital Design: An Embedded Systems Approach Using Verilog: Sequential Basics
An Embedded Systems
Approach Using Verilog
Chapter 4
Sequential Basics
Verilog
Sequential Basics
Sequential circuits
Verilog
D-Flipflops
clk
clk
D
Q
Verilog
Registers
d(0)
q(0)
q(1)
q(n)
clk
d(1)
wire [n:0] d;
reg [n:0] q;
...
D
clk
event list
d(n)
clk
D
clk
nonblockin
g
asignment
clk
Verilog
d_in
d_in
combinational
circuit 1
D
clk
combinational
circuit 2
combinational
circuit 2
combinational
circuit 3
D
clk
combinational
circuit 3
d_out
clk
clk
Verilog
Pipeline Example
Verilog
Pipeline Example
...
assign a_plus_b = a + b;
always @(posedge clk) begin // Pipeline register 1
saved_a_plus_b <= a_plus_b;
saved_c
<= c;
end
assign sum = saved_a_plus_b + saved_c;
always @(posedge clk)
saved_sum <= sum;
// Pipeline register 2
// Pipeline register 3
endmodule
Verilog
D
Q
CE
clk
CE
D
Q
Verilog
Verilog
clk
D
Q
CE
reset
clk
reset
CE
D
Q
10
Verilog
D
Q
CE
reset
clk
clk
reset
CE
D
Q
11
Verilog
12
Verilog
Example: Accumulator
module accumulator
( output reg signed [7:-12] data_out,
input
signed [3:-12] data_in,
input
data_en, clk, reset );
wire signed [7:-12] new_sum;
assign new_sum = data_out + data_in;
always @(posedge clk)
if
(reset)
data_out <= 20'b0;
else if (data_en) data_out <= new_sum;
endmodule
Digital Design Chapter 4 Sequential Basics
13
Verilog
pre
D
Q
CE
clk Q
clr
14
Verilog
Shift Registers
Arithmetic scaling
Serial transfer
D(n1)
of data
0
1
D_in
D
Q
load_en
CE
clk
Q(n1)
Q(n2)
Q(0)
CE
clk
D(n2)
D
CE
clk
D(0)
load_en
CE
clk
D
CE
clk
15
Verilog
Example: Sequential
Multiplier
1616 multiply over 16 clock cycles,
y(15...0)
y_load_en
y_ce
16-bit
adder
x
c16
y
16-bit reg
x(15...0)
x_ce
15...0
31...16
15
P(31...15)
reset
CE
clk
CE
clk
P_reset
P_ce
clk
c0
16
17-bit reg
15-bit
shift reg
D_in Q
P(14...0)
CE
clk
16
Verilog
Latches
Level-sensitive storage
transparent latch
D
LE
D
Q
17
Verilog
Feedback Latches
Q
R
18
Verilog
Latches in Verilog
always @*
if (~sel) begin
z1 <= a1; z2 <= b1;
end
else begin
z1 <= a2; z3 <= b2;
end
Oops!
Should be
z2 <= ...
19
Verilog
Counters
Used as timers
20
Verilog
Free-Running Counter
+1
clk
clk
21
Verilog
clk
1
+1
2
clk
D
ctrl
Q
clk
3
clk
clk
22
Verilog
23
Verilog
CE
reset
clk
CE
reset
clk
24
Verilog
Terminal Count
TC
clk Qn
25
Verilog
Divider Example
count
tone2
TC
tone
CE
clk
clk
clk
count
0
1023
0
1023
1023
tone2
tone
26
Verilog
Divide by k
Terminal count = 9
counter
clk
clk
Q0
Q1
Q2
reset Q3
Q0
Q1
Q2
Q3
27
Verilog
28
Verilog
Terminal count = 0
0
1
D
load
clk
clk
=0?
TC
29
Verilog
Loadable Counter in
Verilog
module interval_timer_rtl ( output
tc,
input [9:0] data,
input
load, clk );
reg [9:0] count_value;
always @(posedge clk)
if (load) count_value <= data;
else
count_value <= count_value - 1;
assign tc = count_value == 0;
endmodule
30
Verilog
Reloading Counter in
Verilog
31
Verilog
Ripple Counter
clk
clk Q
D
Q1
clk Q
D
clk Q
D
Q0
clk
Q0
Q2
Q0
Q1
Q1
clk Q
D
Qn
Q2
Q2
32
Verilog
Ripple counter is ok if
length is short
clock period long relative to flipflop
delay
transient wrong values can be tolerated
area must be minimal
33
Verilog
34
Verilog
Example: Complex
Multiplier
Cartesian form, fixed-point
a ar jai
b br jbi
35
Verilog
Complex Multiplier
Datapath
a_r
a_i
a_sel
b_r
b_i
b_sel
pp1_ce
pp2_ce
CE
p_r
p_i
CE
clk
clk
0
1
D
CE
clk
D
CE
clk
sub
p_r_ce
p_i_ce
clk
36
Verilog
Complex Multiplier in
Verilog
module multiplier
( output reg signed [7:-24] p_r, p_i,
input
signed [3:-12] a_r, a_i, b_r, b_i,
input
clk, reset, input_rdy );
reg a_sel, b_sel, pp1_ce, pp2_ce, sub, p_r_ce, p_i_ce;
wire signed [3:-12] a_operand, b_operand;
wire signed [7:-24] pp, sum
reg signed [7:-24] pp1, pp2;
...
37
Verilog
Complex Multiplier in
Verilog
assign a_operand = ~a_sel ? a_r : a_i;
assign b_operand = ~b_sel ? b_r : b_i;
assign pp = {{4{a_operand[3]}}, a_operand, 12'b0} *
{{4{b_operand[3]}}, b_operand, 12'b0};
always @(posedge clk) // Partial product 1 register
if (pp1_ce) pp1 <= pp;
always @(posedge clk) // Partial product 2 register
if (pp2_ce) pp2 <= pp;
assign sum = ~sub ? pp1 + pp2 : pp1 - pp2;
always @(posedge clk) // Product real-part register
if (p_r_ce) p_r <= sum;
always @(posedge clk) // Product imaginary-part register
if (p_i_ce) p_i <= sum;
...
endmodule
Digital Design Chapter 4 Sequential Basics
38
Verilog
Multiplier Control
Sequence
39
Verilog
Multiplier Control
Sequence
Merge steps where no resource conflict
Revised attempt
1.
2.
3.
4.
5.
40
Verilog
a_sel
b_sel
pp1_c
e
pp2_c
e
sub
p_r_ce p_i_ce
41
Verilog
Finite-State Machines
A FSM is defined by
set of inputs:
set of outputs:
set of states: S
initial state: s0 S
transition function: : S S
output function: : S or : S
42
Verilog
FSM in Hardware
D
reset
clk
inputs
reset
current_state
next
state
logic
clk
output
logic
outputs
Mealy
FSM only
Mealy FSM: : S
Moore FSM: : S
Digital Design Chapter 4 Sequential Basics
43
Verilog
Output function
Transition function
current
_state
input_
rdy
next_
state
step1
step1
step1
step2
step2
step3
step3
step4
step4
step5
step5
step1
44
Verilog
State Encoding
Encoded in binary
45
Verilog
FSMs in Verilog
46
Verilog
=
=
=
=
=
=
step1;
step2;
step3;
step4;
step5;
step1;
47
Verilog
48
Verilog
Example
0, 1
1, 0
s1
s2
1, 1
0, 0
1, 1
s3
0, 1
1, 0
49
Verilog
Annotate diagram
to define output
function
Example
0, 1 / 0, 1, 1
0, 0 / 0, 0, 0
1, 0 / 1, 0, 0
s1
s2
1, 0
0, 0
1, 1 / 1, 1, 1
0, 0 / 0, 0, 0
/ 0, 1, 1
s3
0, 1 / 0, 1, 1
0, 1
1, 1 / 1, 1, 1
1, 0 / 1, 0, 0
50
Verilog
Input: input_rdy
Outputs
step1
0, 0, 1, 0, , 0, 0
step5
, , 0, 0, 0, 0, 1
step2
1, 1, 0, 1, , 0, 0
step4
1, 0, 0, 1, , 0, 0
step3
0, 1, 1, 0, 1, 1, 0
51
Verilog
Bubble Diagrams or
Verilog?
Your choice...
or your manager's!
Digital Design Chapter 4 Sequential Basics
52
Verilog
outputs
inputs
control section
53
Verilog
Clocked Synchronous
Timing
Registers driven by a common clock
tco
Q1
tpd
D2
tsu
clk
tco
Q1
tpd
tsu
D2
54
Verilog
tsu
tpd-s
tpd-c
tpd-o
tpd-ns
tsu
55
Verilog
Timing Constraints
Critical path
56
Verilog
Interpretation of
Constraints
57
Verilog
Clock Skew
clk1
Q1
D2
Q1
clk2
th
D2
58
Verilog
Off-Chip Connections
Q1
D2
59
Verilog
Asynchronous Inputs
Unbounded time to
recover
1
k 2t
e
MTBF
k1 f f f 2
60
Verilog
Synchronizers
asynch_in
D
clk
synch_in
clk
clk
61
Verilog
contact bounce
Requires two
inputs and two
R
resistors
Must use a breakS
Q
before-make
double-throw
Digital Design Chapter 4 Sequential Basics
62
switch
Verilog
Assumption
63
Verilog
Debouncing in Verilog
module debouncer ( output reg pb_debounced,
input pb,
input clk, reset );
reg [18:0] count500000; // values are in the range 0 to 499999
wire
clk_100Hz;
reg
pb_sampled;
always @(posedge clk or posedge reset)
if
(reset)
count500000 <= 499999;
else if (clk_100Hz) count500000 <= 499999;
else
count500000 <= count500000 - 1;
assign clk_100Hz = count500000 == 0;
always @(posedge clk)
if (clk_100Hz) begin
if (pb == pb_sampled) pb_debounced <= pb;
pb_sampled <= pb;
end
endmodule
Digital Design Chapter 4 Sequential Basics
64
Verilog
Verifying Sequential
Circuits
Verification Testbench
Apply
Test Cases
Design Under
Verification
(DUV)
Checker
65
Verilog
Example: Multiplier
Testbench
`timescale 1ns/1ns
module multiplier_testbench;
parameter t_c = 50;
reg
reg
wire signed [3:-12]
wire signed [7:-24]
clk, reset;
input_rdy;
a_r, a_i, b_r, b_i;
p_r, p_i;
66
Verilog
Example: Multiplier
Testbench
multiplier duv ( .clk(clk), .reset(reset),
.input_rdy(input_rdy),
.a_r(a_r), .a_i(a_i),
.b_r(b_r), .b_i(b_i),
.p_r(p_r), .p_i(p_i) );
always begin // Clock generator
#(t_c/2) clk = 1'b1;
#(t_c/2) clk = 1'b0;
end
initial begin // Reset generator
reset <= 1'b1;
#(2*t_c) reset = 1'b0;
end
67
Verilog
Example: Multiplier
Testbench
initial begin // Apply test cases
@(negedge reset)
@(negedge clk)
apply_test(0.0, 0.0, 1.0, 2.0);
apply_test(1.0, 1.0, 1.0, 1.0);
// further test cases ...
$finish;
end
assign
assign
assign
assign
a_r
a_i
b_r
b_i
=
=
=
=
$rtoi(real_a_r
$rtoi(real_a_i
$rtoi(real_b_r
$rtoi(real_b_i
*
*
*
*
2**12);
2**12);
2**12);
2**12);
68
Verilog
Example: Multiplier
Testbench
69
Verilog
Asynchronous Timing
no clock signals
Digital Design Chapter 4 Sequential Basics
70
Verilog
Inter-chip clocking
In multi-PCB systems
71
Verilog
Summary
Latches: level-sensitive
Counters
72
Verilog
Summary
73