Professional Documents
Culture Documents
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
Portions of this work are from the book, Digital Design: An Embedded
Systems Approach Using Verilog, by Peter J. Ashenden, published by Morgan
Kaufmann Publishers, Copyright 2007 Elsevier Inc. All rights reserved.
Verilog
Sequential Basics
Sequential circuits
Outputs depend on current inputs and
previous inputs
Store state: an abstraction of the history of
inputs
Usually governed by a periodic clock
signal
D-Flipflops
1-bit storage element
We will treat it as a basic component
clk
D Q
D
clk
Q
Registers
Store a multi-bit encoded value
One D-flipflop per bit
Stores a new value on d (0 ) D Q q (0 )
each clock cycle clk
d (1 ) D Q q (1 )
wire [n:0] d; clk
reg [n:0] q;
event list
…
...
d (n) D Q q (n)
always @(posedge clk) clk clk
q <= d;
n n
D Q
nonblocking
clk
asignment
Digital Design — Chapter 4 — Sequential Basics 4
Verilog
clk
Pipeline Example
Compute the average of corresponding
numbers in three input streams
New values arrive on each clock edge
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) // Pipeline register 2
saved_sum <= sum;
assign sum_div_3 = saved_sum * 14'b00000001010101;
always @(posedge clk) // Pipeline register 3
avg <= sum_div_3;
endmodule
clk
D Q
CE
CE
clk D
Example: Accumulator
Sum a sequence of signed numbers
A new number arrives when data_en = 1
Clear sum to 0 on synch reset
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
Shift Registers
Performs shift operation on stored data
Arithmetic scaling
Serial transfer
0
D(n –1 ) 1
D Q Q(n –1 )
CE
of data clk
0
D Q Q(n –2 )
D(n –2 ) 1
CE
D_in clk
D Q
lo ad _e n
CE
clk
0
D Q Q(0 )
D(0 ) 1
CE
lo a d _e n
CE clk
clk
Latches
Level-sensitive storage
Data transmitted while enable is '1'
transparent latch
Data stored while enable is '0'
LE
D Q
D
LE
Q
Feedback Latches
Feedback in gate circuits produces
latching behavior
Example: reset/set (RS) latch
+V
R
S Q
Q
R Q
S
Latches in Verilog
Latching behavior is usually an error!
always @*
if (~sel) begin
z1 <= a1; z2 <= b1;
end
else begin Oops!
z1 <= a2; z3 <= b2; Should be
end z2 <= ...
Counters
Stores an unsigned integer value
increments or decrements the value
Used to count occurrences of
events
repetitions of a processing step
Used as timers
count elapsed time intervals by
incrementing periodically
Free-Running Counter
+1 D Q Q
clk
clk
0 0
D Q
clk
1 1
D Q
clk
+1 ctrl
2 2
D Q
clk
3 3
D Q
clk
clk
+1
D Q Q
CE CE
re s e t re s e t
clk clk
Terminal Count
Status signal indicating final count value
co u n te r
Q0
Q1 TC
…
…
…
clk Qn
Divider Example
Alarm clock beep: 500Hz from 1MHz clock
1 0 -b it
co u n te r
co u n t
Q D Q to n e
to n e 2
clk TC CE
clk
clk
clk
co u n t 0 1 2 0 1 2 0 1 2 0 1
1023 1023 1023
to n e 2
to n e
Divide by k
Decode k–1 as terminal count and reset
counter register
Counter increments modulo k
Example: decade counter
Terminal count = 9
co u n te r
clk clk Q0 Q0
Q1 Q1
Q2 Q2
re s e t Q3 Q3
–1 0
D Q Q
1
D clk
lo a d =0 ? TC
clk
Ripple Counter
Each bit toggles between 0 and 1
clk clk Q Q0
D Q
when previous bit changes from 1 to 0
clk Q Q1 clk
D Q
Q0
clk Q Q2 Q0
D Q
Q1
Q1
clk Q Qn Q2
D Q
Q2
a _r 0
D Q D Q p _r
a _i 1 CE CE
a _s e l × clk ± clk
b _r 0
b _i 1
D Q D Q p _i
b _s e l CE CE
p p 1 _ce
p p 2 _ce clk clk
sub
p _r_ce
p _i_ce
clk
1 0 0 1 0 – 0 0
2 1 1 0 1 – 0 0
3 0 1 1 0 1 1 0
4 1 0 0 1 – 0 0
5 – – 0 0 0 0 1
Finite-State Machines
Used the implement control sequencing
Based on mathematical automaton theory
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 → Γ
Digital Design — Chapter 4 — Sequential Basics 42
Verilog
FSM in Hardware
Mealy FSM
only
Mealy FSM: ω: S × Σ → Γ
Moore FSM: ω: S → Γ
Digital Design — Chapter 4 — Sequential Basics 43
Verilog
State Encoding
Encoded in binary
N states: use at least log2N bits
Encoded value used in circuits for transition
and output function
encoding affects circuit complexity
Optimal encoding is hard to find
CAD tools can do this well
One-hot works well in FPGAs
Often use 000...0 for idle state
reset state register to idle
Digital Design — Chapter 4 — Sequential Basics 45
Verilog
FSMs in Verilog
Use parameters for state values
Synthesis tool can choose an alternative
encoding
δ defined by diagram 0, 0
s3 0, 1
1, 1
1, 0
function
s1 1, 0 / 1, 0, 0 s2
Annotate states for 0, 0 / 0, 0, 0
1, 0 0, 0
Moore-style outputs
Annotate arcs for
1, 1 / 1, 1, 1
/ 0, 1, 1
Mealy-style outputs
Example 0, 0 / 0, 0, 0
s3 0, 1 / 0, 1, 1
x1, x2: Moore-style 0, 1
1, 1 / 1, 1, 1
y1, y2, y3: Mealy-style 1, 0 / 1, 0, 0
s te p 1 1 s te p 2
0
0 , 0 , 1 , 0 , –, 0 , 0 1 , 1 , 0 , 1 , –, 0 , 0
s te p 5 s te p 4 s te p 3
–, –, 0 , 0 , 0 , 0 , 1 1 , 0 , 0 , 1 , –, 0 , 0 0, 1, 1, 0, 1, 1, 0
outputs
inputs
control section
clk
tco Q1 tpd D2 tsu tco
Q1
tpd t tsu
tco + tpd + tsu < tc D2
tpd-ns
tsu Ignore tpd-s for a Moore FSM
Timing Constraints
Inequalities must hold for all paths
If tco and tsu the same for all paths
Combinational delays make the difference
Critical path
The combinational path between registers with
the longest delay
Determines minimum clock period for the entire
system
Focus on it to improve performance
Reducing delay may make another path critical
Interpretation of Constraints
1. Clock period depends on delays
System can operate at any frequency up
to a maximum
OK for systems where high performance
is not the main requirement
2. Delays must fit within a target clock
period
Optimize critical paths to reduce delays if
necessary
May require revising RTL organization
Clock Skew
clk1
Q1
Q1 D2
clk2
th
D2
Off-Chip Connections
Delays going off-chip and inter-chip
Input and output pad delays, wire delays
Same timing rules apply
Use input and output registers to avoid
adding external delay to critical path
Q1 D2
Asynchronous Inputs
External inputs can change at any time
Might violate setup/hold time constraints
Can induce metastable state in a flipflop
0 1 0 1
Synchronizers
s yn ch _in
a s yn ch _in D Q D Q
clk clk
clk
+V
Assumption
Extra circuitry inside the chip
is cheaper than extra
components and connections
outside
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
Asynchronous Timing
Clocked synchronous timing requires
global clock distribution with minimal skew
path delay between registers < clock period
Hard to achieve in complex multi-GHz systems
Globally asynch, local synch (GALS) systems
Divide the systems into local clock domains
Inter-domain signals treated as asynch inputs
Simplifies clock managements and constraints
Delays inter-domain communication
Delay-insensitive asynchronous systems
no clock signals
Summary
Registers for storing data
synchronous and asynchronous control
clock enable, reset, preset
Latches: level-sensitive
usually unintentional in Verilog
Counters
free-running dividers, terminal count,
reset, load, up/down
Summary
RTL organization of digital systems
datapath and control section
Finite-State Machine (FSM)
states, inputs, transition/output functions
Moore and Mealy FSMs
bubble diagrams
Clocked synch timing and constraints
critical path and optimization
Asynch inputs, switch debouncing
Verification of sequential systems
Digital Design — Chapter 4 — Sequential Basics 73