Transcript Slide 1
Finite State Machines ECEn 224 16 FSM Page 1 © 2003-2008 BYU State Machine Concepts • State, current state, next state, state registers • IFL, OFL, Moore outputs, Mealy outputs • Transition tables – With output don’t cares (X’s) – With input don’t cares (-’s) • State graphs – And their correspondence to TT’s ECEn 224 16 FSM Page 2 © 2003-2008 BYU Counters as State Machines • A counter is a finite state machine – Where the state encodings are significant 1001 0000 0001 1000 Q0 0010 Q1 0111 0011 0110 Q2 7 Segment Decoder Q3 0100 0101 ECEn 224 16 FSM Page 3 © 2003-2008 BYU State Machines • A state machine is a sequential circuit which progresses through a series of states in reponse to inputs – The output values are usually significant – The state encodings are usually not significant • Unlike with counters ECEn 224 16 FSM Page 4 © 2003-2008 BYU State Encodings • In this machine, the state encodings don’t matter • The output values do… Event1 … Event2 Event1 Output3 Output2 Output1 Notice the alternate notation for X’ and outputs Event2 ECEn 224 Event3 16 FSM Page 5 © 2003-2008 BYU A State Machine Controller for a Photocopier Buttons Timers Lamps Inputs Finite State Machine Outputs Motors Switches Display 1) FSM receives inputs from copier 2) FSM generates control outputs in response 3) States help it remember where it is in the copy process… ECEn 224 16 FSM Page 6 © 2003-2008 BYU A Sequence Detector FSM Xin Because the encodings don’t matter, we will use symbolic state names S0 Xin’ S3 S1 Z Xin’ Xin Xin S2 Xin’ What does this machine do? ECEn 224 16 FSM Page 7 © 2003-2008 BYU A Sequence Detector FSM Xin It is called a sequence detector. It has 1 input (Xin) and one output (Z) S0 Xin’ S3 S1 Z Xin’ Xin Xin S2 Xin’ It detects the sequence 0..1..1 on the input. When detected, output Z is asserted. ECEn 224 16 FSM Page 8 © 2003-2008 BYU A Sequence Detector FSM Xin As long as Xin=1, we stay in state S0 S0 1..1..1..1..1.. Xin’ S3 S1 Z Xin’ Xin Xin S2 Xin’ ECEn 224 16 FSM Page 9 © 2003-2008 BYU A Sequence Detector FSM Xin When Xin=0, we go to state S1 S0 1..1..1..1..1..0.. Xin’ S3 S1 Z Xin’ Xin Xin S2 Xin’ ECEn 224 16 FSM Page 10 © 2003-2008 BYU A Sequence Detector FSM Xin As long as Xin=0, we stay in state S1 S0 1..1..1..1..1..0..0..0..0.. Xin’ S3 S1 Z Xin’ Xin Xin S2 Xin’ ECEn 224 16 FSM Page 11 © 2003-2008 BYU A Sequence Detector FSM Xin When Xin=1, we go to state S2 S0 1..1..1..1..1..0..0..0..0..1.. Xin’ S3 S1 Z Xin’ Xin Xin S2 Xin’ ECEn 224 16 FSM Page 12 © 2003-2008 BYU A Sequence Detector FSM Xin If Xin=1 again, we go to state S3 S0 1..1..1..1..1..0..0..0..0..1..1 Xin’ S3 S1 Z Xin’ SUCCESS! Raise the Z output Xin Xin S2 Xin’ ECEn 224 16 FSM Page 13 © 2003-2008 BYU A Sequence Detector FSM Xin Once we enter state S3, we never leave… S0 Xin’ S3 S1 Z Xin’ Xin Xin S2 Xin’ ECEn 224 16 FSM Page 14 © 2003-2008 BYU A Sequence Detector FSM Xin What if we don’t see the second Xin=1? S0 1..1..1..1..1..0..0..0..0..1..0.. Xin’ S3 S1 Z Xin’ Xin Xin S2 Xin’ ECEn 224 16 FSM Page 15 © 2003-2008 BYU Implementing the Sequence Detector FSM 1. 2. 3. 4. Create symbolic Transition Table Assign state encoding Create conventional Transition Table Do standard implementation steps Xin CS 0 1 0 1 0 1 - S0 S0 S1 S1 S2 S2 S3 NS Z S1 S0 S1 S2 S1 S3 S3 Symbolic TT 0 0 0 0 0 0 1 Xin Q1 Q0 N1 N0 Z S0 = 00 S1 = 01 S2 = 10 S3 = 11 State Assignment ECEn 224 0 1 0 1 0 1 - 0 0 0 0 1 1 1 0 0 1 1 0 0 1 0 0 0 1 0 1 1 1 0 1 0 1 1 1 0 0 0 0 0 0 1 Conventional TT 16 FSM Page 16 © 2003-2008 BYU Sequence Detector Implementation N1 = Q1•Q0 + Xin•Q1 + Xin•Q0 N0 = Xin’ + Q1 Z = Q1•Q0 Z Q1 Q0 N1 Q1 Xin D Q Q1 Xin Q0 CLK Xin’ Q1 N0 D Q Q0 CLK ECEn 224 16 FSM Page 17 © 2003-2008 BYU A Problem With the Sequence Detector • This FSM only detects first occurrence of 011 on input… • Here is a timing diagram – actual screenshot from a simulation 1 0 1 0 ECEn 224 0 1 1 1 16 FSM Page 18 1 © 2003-2008 BYU Improved Sequence Detector • This starts over each time 011 is detected… Xin S0 Xin’ Xin Z S3 Xin’ S1 Xin’ Xin Xin S2 ECEn 224 Xin’ 16 FSM Page 19 © 2003-2008 BYU Improved Detector Timing Diagram 1 0 0 0 0 1 1 1 0 1 1 0 0 Z=1 any time state is S3 Looking at Xin, does it look like Z is a cycle late? How could we make Z output a cycle earlier? ECEn 224 16 FSM Page 20 © 2003-2008 BYU Mealy Version of Sequence Detector Xin Output is asserted during the second ‘1’ of the 011 sequence… S0 Xin S3 Xin’ Xin’ S1 Xin’ Xin Xin / Y S2 Xin’ ECEn 224 16 FSM Page 21 © 2003-2008 BYU Mealy Version Timing Diagram 0 1 1 1 0 1 1 1 Y Note how Mealy output follows input during state S2 Output is a cycle earlier than in Moore machine – it appears in S2 rather than in S3 ECEn 224 16 FSM Page 22 © 2003-2008 BYU Simplified Mealy Sequence Detector • A characteristic of Mealy state machines is they often require fewer states than Moore state machines Xin S0 Xin’ • Here is simplified but basically equivalent FSM S1 Xin / Y Xin’ Xin S2 ECEn 224 16 FSM Page 23 Xin’ © 2003-2008 BYU Example FSM’s Two Car Wash Controllers ECEn 224 16 FSM Page 24 © 2003-2008 BYU Basic Car Wash FSM Operation 1. 2. 3. 4. Wait for a token to be inserted Reset timer Turn on water pump until timer expires Start over • This assumes existence of: – Token acceptance mechanism – Timer – Digitally-controlled water pump ECEn 224 16 FSM Page 25 © 2003-2008 BYU Basic Car Wash FSM SG TOKEN’ Wait for token… S_IDLE TOKEN Clear timer… S_TOKEN TDONE SPRAY CLRT Spray car while waiting for timer to expire… S_SPRAY TDONE’ ECEn 224 16 FSM Page 26 © 2003-2008 BYU TOKEN TDONE 0 1 - 0 1 TOKEN TDONE 0 1 - 0 1 CS NS CLRT SPRAY S_IDLE S_IDLE S_IDLE S_TOKEN S_TOKEN S_SPRAY S_SPRAY S_SPRAY S_SPRAY S_IDLE Q1 Q0 N1 N0 00 00 01 10 10 00 01 10 10 00 0 0 1 0 0 0 0 0 1 1 CLRT SPRAY 0 0 1 0 0 0 0 0 1 1 ECEn 224 TOKEN TDONE Q1 Q0 N1 N0 CLRT SPRAY 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 16 FSM Page 27 0 1 1 X 0 1 0 X 0 1 1 X 0 1 0 X 0 0 0 X 0 0 0 X 1 0 0 X 1 0 0 X 0 1 0 X 0 1 0 X 0 1 0 X 0 1 0 X 0 0 1 X 0 0 1 X 0 0 1 X 0 0 1 X © 2003-2008 BYU Basic Car Wash Implementation SPRAY Q0 N1 D Q Q1 Q1 TDONE’ CLK TOKEN Q1’ Q0’ N0 D Q CLRT Q0 CLK This is what you get from a K-map solution ECEn 224 16 FSM Page 28 © 2003-2008 BYU Simplified State Graph TOKEN’ S_IDLE TDONE Clear timer while waiting for a token and eliminate a state CLRT TOKEN TOKEN TDONE SPRAY S_SPRAY 0 1 - 0 1 CS NS CLRT SPRAY S_IDLE S_IDLE S_IDLE S_SPRAY S_SPRAY S_SPRAY S_SPRAY S_IDLE 1 1 0 0 0 0 1 1 TDONE’ ECEn 224 16 FSM Page 29 © 2003-2008 BYU Simplified Car Wash Implementation State Encoding: S_IDLE = 0, S_SPRAY = 1 NS = CS’•TOKEN + CS•TDONE’ CLRT = CS’ SPRAY = CS CS’ TOKEN CS CS NS D Q TDONE’ SPRAY CLRT CLK ECEn 224 16 FSM Page 30 © 2003-2008 BYU A Fancy Car Wash Controller • Two types of washes: – Regular wash (spray only) = 1 token – Deluxe wash (spray, soap, spray) = 2 tokens • Customer: – Inserts 1 token and pushes START for Regular – Inserts 2 tokens for Deluxe ECEn 224 16 FSM Page 31 © 2003-2008 BYU TOKEN’ Straightforward process to write transition table and reduce to gates S0 T1DONE TOKEN TOKEN’ • START’ CLRT1 S1 START T1DONE’ S4 TOKEN • START’ SPRAY SPRAY, CLRT2 T2DONE S2 T1DONE’ T1DONE S3 SOAP, CLRT1 T2DONE’ ECEn 224 Creativity in FSM design is designing the initial state graph… 16 FSM Page 32 © 2003-2008 BYU Enhancements to Fancy Controller ECEn 224 16 FSM Page 33 © 2003-2008 BYU TOKEN’ S0 T1DONE ACCEPTCOIN TOKEN CLRT1, ACCEPTCOIN TOKEN’ • START’ S1 START T1DONE’ There is no way to cause coinbox to refuse tokens. Coins inserted after wash commences are presumably kept by the machine. S4 TOKEN • START’ SPRAY SPRAY, CLRT2 T2DONE S2 T1DONE’ T1DONE S3 SOAP, CLRT1 T2DONE’ ECEn 224 Let’s add an ACCEPTCOIN output to control when coinbox will accept tokens… 16 FSM Page 34 © 2003-2008 BYU TOKEN’ S0 T1DONE ACCEPTCOIN TOKEN ACCEPTCOIN CLRT1 TOKEN’ • START’ S1 START T1DONE’ S4 If customer pushes START at precisely the same time as he inserts another TOKEN, the FSM will take the token, but deliver a Regular Wash. TOKEN • START’ SPRAY SPRAY, CLRT2 T2DONE T1DONE S3 SOAP, CLRT1 T2DONE’ ECEn 224 S2 T1DONE’ That is, START has precedence over TOKEN 16 FSM Page 35 © 2003-2008 BYU TOKEN’ S0 T1DONE ACCEPTCOIN TOKEN ACCEPTCOIN CLRT1 TOKEN’ • START’ S1 START•TOKEN’ T1DONE’ This changes the precedence so the machine won’t steal the second token, but will give a deluxe wash in this case. S4 TOKEN SPRAY SPRAY, CLRT2 T2DONE S2 T1DONE’ T1DONE S3 SOAP, CLRT1 T2DONE’ ECEn 224 16 FSM Page 36 © 2003-2008 BYU Completeness and Conflict Revisited • State S1 is a typical problem spot • Carefully analyze the state graph to ensure no conflicts exist and that all cases covered. ECEn 224 16 FSM Page 37 © 2003-2008 BYU Resetting State Machines • Ability to reset the FSM is essential for most systems • Always include a reset capability – Add CLR signal to state graph – Use flip flops with reset/set inputs – Either method will work ECEn 224 16 FSM Page 38 © 2003-2008 BYU Another Example An Electronic Key Lock ECEn 224 16 FSM Page 39 © 2003-2008 BYU Example: Electronic Key Lock 1) 2) 3) 4) 5) There are 10 keypads 0-9 The unlock sequence is 7..8..9 When a pad is pushed, the signal for that number is asserted When any of the keypads are pushed a PUSHED signal is asserted If you push a number out of sequence, you get an error indicator and you get to start over 6) After three wrong tries, you must wait ½ hour before trying again 7) If you entered the correct sequence, the lock unlocks. 8) Once the lock has opened, nothing happens until it is manually locked again. Inputs: Keypads signals 0-9 PUSHED signal ECNT3 (error count = 3) WAITDONE LOCKED ECEn 224 Outputs: INC CLRCNTR CLRTIMER ERROR UNLOCK 16 FSM Page 40 © 2003-2008 BYU LOCKED’ D LOCKED/ CLRCNTR PUSHED•9/ UNLOCK PUSHED’ PUSHED’ PUSHED’ A PUSHED•7 B PUSHED•8 C Start PUSHED•7’/ INC WAITDONE/ CLRCNTR PUSHED•8’/ INC PUSHED•9’/ INC ECNT3’ F ECNT3/ CLRTIMER E ERROR WAITDONE’ ECEn 224 16 FSM Page 41 © 2003-2008 BYU LOCKED’ Let’s create the transition table… D LOCKED/ CLRCNTR PUSHED’ PUSHED•9/ UNLOCK PUSHED’ A Start PUSHED’ F PUSHED•8 B C PUSHED•7’/ PUSHED•8’/ INC INC PUSHED•9’/ ECNT3’ INC WAITDONE/ CLRCNTR WAITDONE’ PUSHED•7 ECNT3/ CLRTIMER E ERROR PUSHED 7 8 9 ECNT3 WAITDONE LOCKED STATE NEXTSTATE INC ERROR CLRCNTR CLRTIMER UNLOCK 0 1 1 0 1 1 0 1 1 - 0 1 - 0 1 - 0 1 - 0 1 - 0 1 0 1 - A A A B B B C C C D D E E F F ECEn 224 A E B B E C C E D D A A F F A 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 16 FSM Page 42 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 © 2003-2008 BYU Now let’s expand State A PUSHED 7 8 9 ECNT3 WAITDONE LOCKED STATE NEXTSTATE INC ERROR CLRCNTR CLRTIMER UNLOCK 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 … 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ECEn 224 16 FSM Page 43 © 2003-2008 BYU This is going to get ugly fast… • 7 inputs + 3 state bits = 1024 rows in TT • Can you do a 10-variable K-map? • There is a technique called One-Hot state machines we can use instead (future lecture)… • We could also use behavioral Verilog ECEn 224 16 FSM Page 44 © 2003-2008 BYU