lecture 4pages.hmc.edu/harris/class/e85/lect4.pdf · digital design and computer architecture:...
TRANSCRIPT
Lecture 4: Finite State Machines
E85 Digital Design & Computer Engineering
Lecture 4 <2> Digital Design and Computer Architecture: ARM® Edition © 2015
• Finite State Machines
Lecture 4
Lecture 4 <3> Digital Design and Computer Architecture: ARM® Edition © 2015
• Breaks cyclic paths by inserting registers• Registers contain state of the system
• State changes at clock edge: system synchronized to the clock
• Rules of synchronous sequential circuit composition:– Every circuit element is either a register or a combinational circuit
– At least one circuit element is a register– All registers receive the same clock signal
– Every cyclic path contains at least one register
• Two common synchronous sequential circuits– Finite State Machines (FSMs)– Pipelines
Synchronous Sequential Logic Design
Lecture 4 <4> Digital Design and Computer Architecture: ARM® Edition © 2015
NextState
CurrentState
S’ S
CLK
CL
Next StateLogic
NextState
CL
OutputLogic
Outputs
• Consists of:– State register
• Stores current state • Loads next state at clock edge
– Combinational logic• Computes the next state• Computes the outputs
Finite State Machine (FSM)
Lecture 4 <5> Digital Design and Computer Architecture: ARM® Edition © 2015
CLKM Nk knext
statelogic
outputlogic
Moore FSM
CLKM Nk knext
statelogic
outputlogic
inputs
inputs
outputs
outputsstate
statenextstate
nextstate
Mealy FSM
• Next state determined by current state and inputs• Two types of finite state machines differ in output logic:
– Moore FSM: outputs depend only on current state– Mealy FSM: outputs depend on current state and inputs
Finite State Machines (FSMs)
Lecture 4 <6> Digital Design and Computer Architecture: ARM® Edition © 2015
TA
LA
TA
LB
TB
TB
LA
LB
Academic Ave.
BravadoBlvd.
Dorms
Fields
DiningHall
Labs
• Traffic light controller
– Traffic sensors: TA, TB (TRUE when there’s traffic)
– Lights: LA, LB
FSM Example
Lecture 4 <7> Digital Design and Computer Architecture: ARM® Edition © 2015
TA
TB
LA
LB
CLK
Reset
TrafficLight
Controller
• Inputs: CLK, Reset, TA, TB• Outputs: LA, LB
FSM Black Box
Lecture 4 <8> Digital Design and Computer Architecture: ARM® Edition © 2015
S0LA: greenLB: red
Reset
FSM State Transition Diagram
• Moore FSM: outputs labeled in each state• States: Circles• Transitions: Arcs
Lecture 4 <9> Digital Design and Computer Architecture: ARM® Edition © 2015
• Moore FSM: outputs labeled in each state• States: Circles• Transitions: Arcs
FSM State Transition Diagram
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
Lecture 4 <10> Digital Design and Computer Architecture: ARM® Edition © 2015
Current State Inputs
Next State
S TA TB S'S0 0 X S1S0 1 X S0S1 X X S2S2 X 0 S3S2 X 1 S2S3 X X S0
FSM State Transition Table
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
Lecture 4 <11> Digital Design and Computer Architecture: ARM® Edition © 2015
Current State Inputs Next StateS1 S0 TA TB S'1 S'0
0 0 0 X 0 10 0 1 X 0 00 1 X X 1 01 0 X 0 1 11 0 X 1 1 01 1 X X 0 0
State Encoding
S0 00
S1 01
S2 10
S3 11
FSM Encoded State Transition Table
S'1 = S1 xor S0
S'0 = S1S0TA + S1S0TB
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
Lecture 4 <12> Digital Design and Computer Architecture: ARM® Edition © 2015
Current State OutputsS1 S0 LA1 LA0 LB1 LB0
0 0 0 0 1 00 1 0 1 1 01 0 1 0 0 01 1 1 0 0 1
Output Encoding
green 00
yellow 01
red 10
FSM Output Table
LA1 = S1
LA0 = S1S0
LB1 = S1
LB0 = S1S0
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
Lecture 4 <13> Digital Design and Computer Architecture: ARM® Edition © 2015
S1
S0
S'1
S'0
CLK
state register
Resetr
FSM Schematic: State Register
Lecture 4 <14> Digital Design and Computer Architecture: ARM® Edition © 2015
S1
S0
S'1
S'0
CLK
next state logic state register
Reset
TA
TB
inputs
S1 S0
r
FSM Schematic: Next State Logic
Lecture 4 <15> Digital Design and Computer Architecture: ARM® Edition © 2015
S1
S0
S'1
S'0
CLK
next state logic output logicstate register
Reset
LA1
LB1
LB0
LA0
TA
TB
inputs outputs
S1 S0
r
FSM Schematic: Output Logic
Lecture 4 <16> Digital Design and Computer Architecture: ARM® Edition © 2015
• Multi-input XOR: Odd parity
SystemVerilog Descriptionmodule trafficFSM(input logic clk, reset,
input logic ta, tb,output logic [1:0] la, lb);
logic [1:0] state, nextstate, sb;logic tab, tbb, p1, p2;
// state registerflopr #(2) statereg(clk, reset, nextstate, state);
// next state logicnot n0(sb[0], s[0]);not n1(sb[1], s[1]); not n2(tab, ta);not n3(tbb, tb);xor x1(nextstate[1], state[0], state[1]);and a1(p1, sb[0], sb[1], tab);and a2(p2, sb[0], s[1], tbb);or o1(nextstate[0], p1, p2);
// output logicbuf b1(la[1], s[1]);and a3(la[0], s[0], sb[1]);inv n4(lb[1], s[1]);and a4(lb[0], s[0], s[1]);
endmodule
Lecture 4 <17> Digital Design and Computer Architecture: ARM® Edition © 2015
CLK
Reset
TA
TB
S'1:0
S1:0
LA1:0
LB1:0
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S1 (01) S2 (10) S3 (11) S0 (00)
t (sec)
??
??
S0 (00)
S0 (00) S1 (01) S2 (10) S3 (11) S1 (01)
??
??
0 5 10 15 20 25 30 35 40 45
Green (00)
Red (10)
S0 (00)
Yellow (01) Red (10) Green (00)
Green (00) Red (10)Yellow (01)
S0LA: greenLB: red
S1LA: yellowLB: red
S3LA: redLB: yellow
S2LA: redLB: green
TATA
TB
TB
Reset
FSM Timing Diagram
Lecture 4 <18> Digital Design and Computer Architecture: ARM® Edition © 2015
• Binary encoding: – i.e., for four states, 00, 01, 10, 11
• One-hot encoding– One state bit per state– Only one state bit HIGH at once– i.e., for 4 states, 0001, 0010, 0100, 1000
– Requires more flip-flops– Often next state and output logic is simpler
FSM State Encoding
Lecture 4 <19> Digital Design and Computer Architecture: ARM® Edition © 2015
• Alyssa P. Hacker has a snail that crawls down a paper tape with 1’s and 0’s on it. The snail smiles whenever the last two digits it has crawled over are 01. Design Moore and Mealy FSMs of the snail’s brain.
Moore vs. Mealy FSM
Lecture 4 <20> Digital Design and Computer Architecture: ARM® Edition © 2015
Mealy FSM: arcs indicate input/output
State Transition Diagrams
Moore FSMReset
S00
S10
S21
0
0 1
1 01
Reset
S0 S1
1/1
0/0
1/0 0/0
Mealy FSM
Lecture 4 <21> Digital Design and Computer Architecture: ARM® Edition © 2015
Current State Inputs Next State
S1 S0 A S'1 S'0
0 0 0 0 10 0 1 0 00 1 0 0 10 1 1 1 0
1 0 0 0 1
1 0 1 0 0
State Encoding
S0 00
S1 01
S2 10
Moore FSM State Transition Table
S1’ = S0A
S0’ = A
Moore FSMReset
S00
S10
S21
0
0 1
1 01
Lecture 4 <22> Digital Design and Computer Architecture: ARM® Edition © 2015
Moore FSM Output Table
Y = S1
Current State OutputS1 S0 Y0 0 00 1 01 0 1
Moore FSMReset
S00
S10
S21
0
0 1
1 01
Lecture 4 <23> Digital Design and Computer Architecture: ARM® Edition © 2015
Current State Input
Next State Output
S0 A S'0 Y0 0 1 00 1 0 01 0 1 01 1 0 1
State Encoding
S0 0
S1 1
Mealy FSM State Transition & Output Table
S0’ = A
Y = S0 A
Reset
S0 S1
1/1
0/0
1/0 0/0
Mealy FSM
Lecture 4 <24> Digital Design and Computer Architecture: ARM® Edition © 2015
Moore Mealy
Y
CLK
Reset
A
r
S'0 S0
S'1 S1
S'0 Y
CLK
Reset
A
r
S0
Lecture 4 <25> Digital Design and Computer Architecture: ARM® Edition © 2015
Moore & Mealy Timing Diagram
Mealy Machine
Moore Machine
CLK
Reset
A
S
Y
S
Y
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
S0 S2?? S2 S2S0 S1
1 0 1 1 0 1 1 10
S1
S0 S0?? S0 S1 S0S1
S1 S0
S1
Cycle 11
Lecture 4 <26> Digital Design and Computer Architecture: ARM® Edition © 2015
• Break complex FSMs into smaller interacting FSMs
• Example: Modify traffic light controller to have Parade Mode.– Two more inputs: P, R– When P = 1, enter Parade Mode & Bravado Blvd
light stays green
– When R = 1, leave Parade Mode
Factoring State Machines
Lecture 4 <27> Digital Design and Computer Architecture: ARM® Edition © 2015
Unfactored FSM
Factored FSM
ControllerFSMTA
TB
LA
LB
PR
ModeFSM
LightsFSM
P
M
ControllerFSM
TA
TB
LA
LB
R
Parade FSM
Lecture 4 <28> Digital Design and Computer Architecture: ARM® Edition © 2015
Unfactored FSM
Lecture 4 <29> Digital Design and Computer Architecture: ARM® Edition © 2015
S0LA: greenLB: red
S1LA: yellow
LB: red
S3LA: red
LB: yellow
S2LA: red
LB: green
TA
TA
M + TB
MTB
Reset
Lights FSM
S0M: 0
S1M: 1
PReset P
Mode FSM
R
R
Factored FSM
Lecture 4 <30> Digital Design and Computer Architecture: ARM® Edition © 2015
1. Identify inputs and outputs2. Sketch state transition diagram3. Write state transition table4. Select state encodings5. For Moore machine:
a. Rewrite state transition table with state encodingsb. Write output table
5. For a Mealy machine:Rewrite combined state transition and output table with state encodings
6. Write Boolean equations for next state and output logic7. Sketch the circuit schematic
FSM Design Procedure