Professional Documents
Culture Documents
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-1
Objectives
After completing this chapter, you will be able to: Describe the features of finite-state machines (FSMs) Understand how to model FSMs Describe basic structures of register-transfer designs Describe basic structures and features of algorithmic-state machine (ASM) charts Understand how to model ASMs Describe the features and structures of datapath and control designs Describe the realizations of RTL designs Understand the relationship between iterative logic and FSMs
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley 11-2
utputs
Moore machine
Next state
Inputs
Combinational circuit
Combinational circuit
utputs
Present state
Mealy machine
11-4
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
State Encoding
Common FSM encoding options: One-hot code Binary code Gray code Random code Binary State
A B C D 00 01 10 11
Gray 00 01 11 10
One-hot encoding is usually used in F GA-based design, because there are a lot of registers in theses devices.
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley 11-5
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-6
State register can be declared as one register: harder to follow and model. two registers: easier to follow and model. Output and next state functions: continuous assignment function always block a combination of the above
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley 11-7
tate diagram
t6 t7 t8 t9 t10 t11
1 0
0 0
1 1
0 0
1 1
0 0
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-10
un1_x_3 fsm_next_state_0.un12_state
un1_un12_state
e d e d e d e d e d e d
[0]
[0]
un1_x_2
D[1:0] R
Q[1:0]
[1:0]
[1]
state[1:0]
[0]
state_3[1]
un1_state_1
reset_n clk x
[0] [1]
un1_x_4 fsm_next_state_0.un18_state
un1_x
un1_un18_state
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-11
// a behavioral description of 0101 sequence detector: style 2 // Mealy machine example --- using two state registers and three always blocks. module sequence_detector_mealy (clk, reset_n, x, z); input x, clk, reset_n; output z; // local declaration reg [1:0] present_state, next_state; // present state and next state reg z; parameter A = 2'b00, B = 2'b01, C = 2'b10, D = 2'b11; // initialize to state A and update state register always @(posedge clk or negedge reset_n) if (!reset_n) present_state <= A; // update present state else present_state <= next_state;
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-13
[ ] [ ]
[ : ]
t st t [ ]
pr s
t st t
r s t
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
pr s
t st t [ : ]
pr s
t st t
[ : ]
[ : ]
pr s
t st t
pr s
t st t
[ ]
[ ] [ ]
pr s t st t
z
"
!
11-14
RTL result
clk r s t
I C
[ ]
Gate-level result
IBUF
% $ $%
pr s
t st t [
LUT
I O
ib f
F C
clk I O
z ob f
$
r s t
INV
I O
r s t c i r s t ib f
$ $ $ 6 # #
Although we declare two state registers, the synthesis tool only yields one state register.
11-15
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
IBUF
pr s
t st t
h.pr s
$ $ % $ #3 4# # (543 )
#3
4# #
$ %$ ) )
pr s
t st t
h.pr s
t st t [ ]
t s t t [ ]z
)#3
4# #
#3
4# #
clk i b f
$
[ ]
[ ]
[ ]
C CL
[ ]
BUFGP
6
[ ]
[ ]
$ )2 '
$ $
LUT
9 7
L C
'
$ $
& # # 6 $ & CC
c i OBUF
Coding Style
State name should be described using parameters: parameter or localparam. Combinational logic for computing the next state should be separated from the state registers, i.e., using its own always block or a function. The combinational logic of next state function should be implemented with a case statement.
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-16
// an implicit FSM example module sum_3data_implicit(clk, data, total); parameter = 8; input clk; input [ -1:0] data; un1[2:0] output [ -1:0] total; t tal[7:0] t tal_9[7:0] reg [ -1:0] total; + // we do not declare state registers. un1_t tal[7:0] always begin Both implicit and explicit fsm @(posedge clk) total <= data; have the same synthesized results. @(posedge clk) total <= total + data; @(posedge clk) total <= total + data; end endmodule
clk
[1:0] [2]
D[2:0]
Q[2:0]
[2:0]
[0]
[7:0] [7:0]
0 1
[7:0]
[7:0]
data[7:0]
[7:0]
[7:0] [7:0]
[7:0]
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
D[7:0]
H
Q[7:0]
[7:0] [7:0]
t tal[7:0]
11-17
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
dh
t [ : ]
[ : ]
[ : ]
UY P
U Y `XP
[ : ]
i q
[ : ]
[ : ]
tot l[ : ]
11-18
[ : ]
[ : ]
[ : ] [ : ]
dh dh
dh
dh
[ : ]
dh dh
dc
UTQP
UY P
XWSV
r s t clk
I C
QS
PRQ P
dh
dh
dh
s s a
i qq
Register-Transfer-Level Design
Features of register-transfer-level (RTL) designs are sequential machines. are structural. concentrate on functionality, not details of logic design. Two types of register-transfer-level design are ASM (algorithmic-state machine) chart Datapath and controller
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-19
ASM Charts
ASM (algorithmic state machine) charts specify RTL operations on the per-cycle basis. show clearly the flow of control from state to state. are very suitable for data path-controller architectures. An ASM chart is composed of State block Decision block Conditional output block
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-20
Output/operations
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-21
Decision Blocks
A decision block describes the condition under which ASM will execute specific actions. selects the next state based on the value of primary input or present state. can be drawn in either two-way selection or multi-way selection.
Two-way selection
(invalid) 0 (Valid) 1
Multi-way selection
ondition (invalid) 0 (Valid) 1
ondition
ondition
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-22
utput/operation
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-23
ASM Blocks
An ASM block has exactly one entrance path and one or more exit paths.
A 00 B n B+3 0 cn 0 1 d n 5 0 1 x2 x1
A 00 B n B+3
0 1 c n 0
x1 d n 5
1 0
x2
Serial testing
arallel testing
11-24
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
x1
x2
c n 0 C
d n 5
10
01
A 00 B n B+3 1 c n 0 1 C x2 0
x1 0 10 B
d n 5 01
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-26
At ith step:
Add 0 to partial product P if xi xi 1 ! 00 Add Y to partial product P if xi xi 1 ! 01 Subtract Y from partial product P if xi xi 1 ! 10 Add 0 to partial product P if xi xi 1 ! 11
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-29
1 1 1
7 6 5
01010111
1 1 1
3 2 1
00010001 00001000
11010101
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
01
mp[1:0] 00 11
10
acc + mcand
acc
acc - mcand
finish
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-31
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-34
pro
ltiplic
[ : ]
[ : ]
[ : ] [ ]
[ : ]
[ : ]
[ : ]
[ : ]
[ : ]
[ : ]
cc 5
[ ]
cc
[ : ]
[ ]
fi ish
[ ]
[ ]
[ ]
[ ]
fi ish
st t [ : ]
st t
fi ish
st t [ ]
[ ]
[ ] [ ]
cc 6
cc 5
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
st rt
st t
c t[ : ]
[ ]
st t
[ ]
[ : ]
[ : ]
[ : ]
[ : ]
[ ]
ug
[ : ]
i ie
[ ]
st t
chi
[ : ]
[ ]
I[ : ] C
ltipli r[ : ] clk
[ : ]
[ : ]
cc[ : ]
wt t
i y x y
g f w u
ug
c t 5[ : ]
cc 5
[ ]
[ : ] [ : ] =
[ : ]
[ : ]
[ : ]
[ : ]
[ : ]
[ ]
cc[ : ]
p 5[ : ]
p[ : ]
uv
h h e
uv
uv
y u w
[ ]
[ : ]
[ : ]
[ : ]
[ : ]
[ : ]
[ : ]
[ : ]
[ : ]
[ ]
d de
c t[ : ]
[ ]
[ ]
u ut wv u
ut
ut
ut
ut
u w
[ : ]
[ ]
ug
w u
ug
[ : ]
[ : ]
[ ] *
[ : ]
g ut f vu w
[ : ]
[ ]
ut ut
ut
[ : ]
[ : ]
wv ut
ct[ 5: ]
y x ut ut
ut
d de y x
w ut
uuuw w w ww ug ug ug w
w g u f w w u u
ut ut
d jm m
d kl
kl
11-35
ata
n
ata input
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
clk
ontroller
ontrol signals
o q n
atapath
ata output
tatus
11-36
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-37
start_n mcand_load
add_sub
start_n mp_mode mp
mp[1:0]
acc_rso acc_qout
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-38
A Three-Step Paradigm
1. Model the design (usually described by an ASM chart) using any modeling style described above as a single module. 2. Extract the datapath from the module and construct it as an independent module. 3. Extract control-unit module and construct top module. Add a top module that instantiates both datapath and control-unit
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-39
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-40
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-41
D C
D C
D C
D C
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-43
A Single-Cycle Example
// a single-cycle example module single_cycle_example(clk, data_a, data_b, data_c, total); parameter N = 8; input clk; input [N-1:0] data_a, data_b, data_c; output reg [N-1:0] total; // compute total = data_c - (data_a + data_b) always @(posedge clk) begin total <= data_c - (data_a + data_b); end endmodule
clk data_c[7:0] data_a[7:0] data_ [7:0]
t
+
un2_t tal[7:0]
[7:0]
[7:0] 1
+
t tal_1[7:0]
[7:0]
[7:0]
D[7:0]
Q[7:0]
[7:0]
[7:0]
t tal[7:0]
t tal[7:0]
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-44
A Multiple-Cycle Example
// a multiple cycle example --- an implicit FSM module multiple_cycle_example(clk, data_a, data_b, data_c, total); parameter N = 8; input clk; input [N-1:0] data_a, data_b, data_c; output reg [N-1:0] total; reg [N-1:0] qout_a, qout_b; // compute total = data_c - (data_a + data_b) always @(posedge clk) begin qout_a <= data_a; @(posedge clk) qout_b <= qout_a + data_b; @(posedge clk) total <= data_c - qout_b; end endmodule
t c[ : ] t b[ : ] t [ : ]
[ : ] [ : ] [ : ] [ : ] [ : ]
clk
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
{ ~ }|x
[ : ]
qo t
[ : ]
{~ }
{ ~ |}
{~
|x
{ ~ | |x
{ z zyx
[ : ]
[ : ]
qo t b
[ : ]
qo t b[ : ]
[ : ] [ ]
[ : ]
[ : ]
[ : ]
[ : ] [ ]
[ : ]
[ : ]
tot l
[ : ]
tot l[ : ]
11-45
[ : ]
[ : ]
[ : ]
[ : ]
tot l[ : ]
[ : ]
[ : ] [ ]
[ : ]
[ : ]
[ : ]
[ : ] [ ]
[ : ] [ : ]
w v
v v
vw
w u v
v v v
Pipeline Principles
Pipelining: a pipeline is composed of several stages with each stage consisting of a combinational logic circuit and a register, called pipeline register. Pipeline latency: the number of cycles between the presentation of an input value and the appearance of its associated output.
clock period
latency
2 3 4 5 Pipeline depth
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-46
Pipelined Systems
The pipelined clock fpipe is set by the slowest stage and is larger than the frequency used in the original cascaded system.
Input register Input register Input register Input register
Logic
Logic
Logic
Input
Logic
Output
clk
For the ith-stage, the smallest allowable clock period Ti is determined by the following condition:
i
The pipelined clock period for an m-stage pipeline is chosen to be: pipe ! max{ i | i ! 1, 2, ..., m}
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley 11-47
"t
t setup t d ,i t skew ,i 1
qo t b
[ : ]
qo t b[ : ]
qo t
[ : ]
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
[ : ]
[ : ]
tot l[ : ] tot l [ : ]
clk [ : ]
[ : ]
[ : ]
[ : ]
[ : ]
b[ : ]
[ : ]
c[ : ]
[ : ]
tot l[ : ]
11-48
t tal_1[7:0]
t tal[7:0]
ut_a[7:0]
ut_d_1[7:0]
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
data_a[7:0]
D[7:0] Q[7:0]
[7:0]
[7:0]
D[7:0] Q[7:0]
[7:0]
ut_d[7:0]
ut_ [7:0]
11-49
ut_c[7:0]
[7:0]
[7:0]
[7:0]
D[7:0] Q[7:0]
[7:0]
D[7:0] Q[7:0]
[7:0]
[7:0]
D[7:0] Q[7:0]
[7:0]
[7:0] [7:0]
ut_e[7:0]
[7:0]
[7:0]
D[7:0] Q[7:0]
[7:0] [7:0]
t tal[7:0]
s s
ps[i+1]
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-50
yi1 yi2 00 01 10 11
0 0 0 0 0
1 0 0 0 1
xi
yi1yi2 00
0
01
2
11
6
10
4
xi
yi1yi2 00
0
01
2
11
6
10
4
xi
yi1yi2 00
0
01
2
11
6
10
4
0 1
0
1
0
3
0
7
1
5
0 1
1
1
1
3
1
7
1
5
0 1
0
1
0
3
0
7
0
5
0 Yi1
0 Yi2
0 x'i
0 zi
1 xiyi1yi2
xiyi2 + x iyi1y i2
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-51
State diagram
zi
0 x1 0
1 x2
0 x3 1
1 x4
0 x5
1 x6
0 0
y11
1 1 y12 z Y12 1 0
Y11
y21 y22
Y21 1 2 0 z2 Y22 0
y31
3 1 y32 z Y32 3 0
Y31
y41 y42
Y41 1 4 0 z4 Y42 1
y51 y52
Y51 1 5 1 z5 Y52 0
y61 y62
Y61 1 6 0 z6 Y62 1
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-52
Operation Shift only Add and shift Subtract and shift Shift only
Pin
0 1 1 0
yi
add_sub_sel
add_sub_en
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
11-55
y3 0
y2 1
y1 1
0 1 CAS
y0
1 add_sub_sel C RL CAS 1 add_sub_en 1 1 0 1 0 CAS 0 0 1 0 0 CAS 1 0 1 1 0 CAS 0 P6 1 0 P5 CAS 0 0 P4 1 0 1 1 1 CAS 0 P3 CAS 1 1 1 0 1 CAS 0 CAS 1 1 0 CAS 1 0
x0
CAS 0 1 CAS 0 1 P1 0
0,2
0 1 P0 0
0,1
0,0
x1
C RL 1
1 1 P7
Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008, John Wiley
x3
C RL
x2
C RL
C0,i
add_sub_sel add_sub_en
11-56