34
Finite State Machines CS 64: Computer Organization and Design Logic Lecture #15 Fall 2018 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB

Finite State Machines - GitHub Pages · Finite State Machines (FSM) • The FSM can change from one state to another in response to some external inputs • The change from one state

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

  • FiniteStateMachines

    CS64:ComputerOrganizationandDesignLogicLecture#15Fall2018

    ZiadMatni,Ph.D.

    Dept.ofComputerScience,UCSB

  • Administrative

    •  TheLast2WeeksofCS64:

    12/3/18 Matni,CS64,Fa18 2

    Date L# Topic Lab LabDue11/26 13 CombinatorialLogic,

    SequentialLogic8(CL+SL) Sun.12/2

    11/28 14 SequentialLogic12/3 15 FSM 9(FSM) Fri.12/712/5 16 FSM,

    CSEthics&Impact10(Ethics) Fri.12/7

  • Administrative•  NewLab#9issuedtomorrow– DueFriday

    •  PracticeExamwillbeonlinev.soon–  LookforPiazzaannouncement

    •  YourfinalexamisonWednesday,Dec.12that8:00AM

    –  Inthisclassroom–  BeONTIME!Arrive10minutesEARLY!

    12/3/18 Matni,CS64,Fa18 3

  • LectureOutline

    •  FiniteStateMachines

    – Moorevs.Mealytypes

    – StateDiagrams

    – FiguringoutacircuitforaFSM

    12/3/18 Matni,CS64,Sp18 4

  • 12/3/18 Matni,CS64,Fa18 5

    IfacombinationallogiccircuitisanimplementationofaBooleanfunction,

    thenasequentiallogiccircuitcanbeconsideredanimplementationof

    afinitestatemachine.

  • FiniteStateMachines(FSM)

    •  Anabstractmachinethatcanbeinexactlyoneofafinitenumberofstatesatanygiventime

    •  It’saverysimplemodelofacomputationalmachine,unlikePushdownAutomatonsandTuringMachines–  You’lldiscovertheseinotherCSupper-divclasses

    12/3/18 Matni,CS64,Fa18 6

  • FiniteStateMachines(FSM)•  TheFSMcanchangefromonestatetoanotherinresponsetosome

    externalinputs

    •  Thechangefromonestatetoanotheriscalledatransition.

    •  AnFSMisdefinedbyalistofitsstates,itsinitialstate,andtheconditionsforeachtransition.

    12/3/18 Matni,CS64,Fa18 7

    STATE“A”

    StartingpointSTATE“B”

    TransitionA->B

    TransitionB->A

  • ExampleofaSimpleFSM:TheTurnstile

    initialstateStateTransitionTable

    12/3/18 Matni,CS64,Fa18 8Source:Wikipedia

    CurrentState

    Input NextState

    Output

    Locked Coin Unlocked Unlockstheturnstilesothatthecustomercanpushthrough.

    Locked Push Locked Nothing–you’relocked!J

    Unlocked Coin Unlocked Nothing–youjustwastedacoin!J

    Unlocked Push Locked Whenthecustomerhaspushedthrough,lockstheturnstile.

  • ExampleofaSimpleFSM:TheTurnstile

    initialstateStateTransitionTable

    12/3/18 Matni,CS64,Fa18 9Source:Wikipedia

    CurrentState

    Input NextState

    Output

    Locked Coin Unlocked Unlockstheturnstilesothatthecustomercanpushthrough.

    Locked Push Locked Nothing–you’relocked!J

    Unlocked Coin Unlocked Nothing–youjustwastedacoin!J

    Unlocked Push Locked Whenthecustomerhaspushedthrough,lockstheturnstile.

    Thisiscalledastatediagram

    à

  • GeneralFormofFSMs

    12/3/18 Matni,CS64,Fa18 10

  • Example

    12/3/18 Matni,CS64,Fa18 11

    A

    CLK

    Q*

    Q*=QO.A

    Onthenextrisingedgeoftheclock,theoutputoftheD-FFQ(Q*)willbecome

    thepreviousvalueofQ(QO)ANDthevalueofinputA

    QoCombinatoriallogic

    StateregisterClocksignal

    Output-to-inputfeedback

  • FSMTypes

    Thereare2types/modelsofFSMs:•  Mooremachine–  Outputisfunctionofpresentstateonly

    •  Mealymachine–  Outputisfunctionofpresentstateandpresentinput

    12/3/18 Matni,CS64,Fa18 12

  • MooreMachine

    12/3/18 Matni,CS64,Fa18 13

    Outputisfunctionofpresentstateonly

  • ExampleofaMooreMachine(with1state)

    12/3/18 Matni,CS64,Fa18 14

    A

    CLK

    Z

    Z=(Q*+B)=(QO.A+B)Onthenextrisingedgeoftheclock,theoutputoftheentirecircuit(Z)

    willbecome(thepreviousvalueofQ(QO)ANDthevalueofinputA)NORB

    NOTE:CLKisNOWHEREINTHEEQUATION!!!

    B

    Q

    Outputisfunctionofpresentstateonly

  • MealyMachine

    12/3/18 Matni,CS64,Fa18 15

    Outputisfunctionofpresentstateandpresentinput

  • ExampleofaMealyMachine(with1state)

    12/3/18 Matni,CS64,Fa18 16

    A

    CLKB

    Z

    Z=(Q*+A+B)=(QOXORA)+(A+B)

    Onthenextrisingedgeoftheclock,theoutputoftheentirecircuit(Z)willbecome…etc…

    Q

    Outputisfunctionofpresentstateandpresentinput

  • ExampleofaMooreMachine

    WASHER_DRYER

    •  Let’s“build”asequentiallogicFSMthatactsasacontrollertoasimplisticwasher/dryermachine

    •  Thismachinetakesinvariousinputsinitsoperation(we’llonlyfocusonthefollowingsensor-basedones):

    •  Thismachinealsoissues1outputwhilerunning:

    12/3/18 Matni,CS64,Fa18 17

    Coinisin(vsitisn’tin)Soapispresent(vsit’susedup) Clothesarestillwet(vsclothesaredry)

    “Done”indicator

  • MachineDesign

    •  Wewantthismachinetohave4distinctstatesthatwegofromonetothenextinthissequence:

    1.   InitialState–  Wherewearewhenwearewaitingtostartthewash

    2.   Wash–  Wherewewashwithsoapandwater

    3.   Dry–  Wherewedrytheclothes

    4.   Done

    12/3/18 Matni,CS64,Fa18 18

  • CombiningtheInputs

    •  Let’screateavariablecalledGTNS(i.e.GoToNextState)

    •  GTNSis1ifanyofthefollowingistrue:–  Coinisin–  Soapisnolongerdetected–  Clothesarenowdry–  Iassumethatthese3inputstobemutuallyexclusive

    12/3/18 Matni,CS64,Fa18 19

    Coinisin(vsitisn’tin)Soapisnolongerdetected(vsit’sstillthere) Clothesarenowdry(vsclothesarestillwet)

  • What’sGoingtoHappen? 1/2

    •  Westartatan“Initial”statewheneverwestartupthemachine–  Let’salsoassumethisstageiswhenyou’dputinthesoapandclothes–  Onceinput“Coinisin”is1,GTNSisnow1–  Thiseventtriggersleavingthecurrentstatetogotothenextstate

    •  Thisisfollowedbythenextstate,“Wash”–  “Coininserted”isnow0atthispoint(soGTNSgoesbackto0)–  Whilesoapisstillpresent,GTNSgoesbackto0–  Whentheinput“Soapisnolongerpresent”goesto1,GTNSgoesto1–  Thiseventtriggersleavingthecurrentstatetogotothenextstate

    12/3/18 Matni,CS64,Fa18 20

    Coinisin(vsitisn’tin)Soapisnolongerdetected(vsit’sstillthere)Clothesarenowdry(vsclothesarestillwet)

  • •  Thisisfollowedbythenextstate,“Dry”–  Thisnewstatesetsanoutputthattriggersatimer–  Theinput“Soapisnolongerpresent”goesto0,soGTNSis0also–  Whiletheinput“Clothesarenowdry”is0,GTNSremainsat0too–  Whentheinput“Clothesarenowdry”is1,GTNSchangesto1–  Thiseventtriggersleavingthecurrentstatetogotothenextstate

    •  Thisisfollowedbythenextandlaststate,“Done”–  Whenyou’rehere,yougobacktothe“initial”state–  Noinputstoconsider:youdomovethisregardless

    12/3/18 Matni,CS64,Fa18 21

    What’sGoingtoHappen? 2/2

    Coinisin(vsitisn’tin)Soapisnolongerdetected(vsit’sstillthere)Clothesarenowdry(vsclothesarestillwet)

  • 12/3/18 Matni,CS64,Fa18 22

    StateDiagramforWasher-DryerMachine

    Wash

    Done

    InitialState

    GTNS

    DONE=1

    DONE=0

    inputs

    outputsstate

    transition

    GTNS

    GTNS

    1

    Dry

    GTNS

    GTNS=COIN_IN+NO_SOAP+CLTHS_DRY

  • UnconditionalTransitions

    •  Sometimesthetransitionisunconditional–  Doesnotdependonanyinput–itjusthappens

    •  Wethendiagramthisasa“1”(for“alwaysdoesthis”)

    12/3/18 23

    State2State0K=1

    State1

    K=01

    K=0K=1

  • RepresentingTheStates•  HowmanybitsdoIneedtorepresentallthestatesinthisWasher-Dryer

    Machine?

    •  Thereare4uniquestates(including“init”)–  So,2bits

    •  Ifmystatemachinewillbebuiltusingamemorycircuit(mostlikely,aD-FF),howmanyoftheseshouldIhave?–  2bits=2D-FFs

    •  Thereisanotherschemetodothiscalled“OneHotMethod”.–  Moreonthislater…

    12/3/18 Matni,CS64,Fa18 24

    State S1 S0Initial 0 0Wash 0 1Rinse 1 0Dry 1 1

  • ExampleofaMooreMachine2DETECT_1101

    •  Let’sbuildasequentiallogicFSMthatalwaysdetectsaspecificserialsequenceofbits:1101

    •  We’llstartatan“Initial”state(S0)•  We’llfirstlookfora1.We’llcallthat“State1”(S1)

    –  Don’tgotoS1ifallwefindisa0!

    •  We’llthenkeeplookingforanother1.We’llcallthat“State11”(S2)

    12/3/18 Matni,CS64,Fa18 25

  • ExampleofaMooreMachine2DETECT_1101

    •  Then…a0.We’llcallthat“State110”(S3)

    •  Thenanother1.We’llcallthat“State1101”(S4)–thiswillalsooutputaFOUNDsignal

    •  Wewillalwaysbedetecting“1101”(itdoesn’tend)So,asSOONasS4isdone,wekeeplookingfor1sor0s

    •  Example:iftheinputstreamis111101110101101000011111011011 wedetect“1101”at ññññ ñ

    12/3/18 Matni,CS64,Fa18 26

    111101110101101000011111011011

  • StateDiagram2

    12/3/18 Matni,CS64,Fa18 27

    “1” “11”

    “110”“1101”

    InitialState

    Input=0 Input=1

    Input=1

    Input=0

    Input=0

    FOUND=1

    Input=1

    S0

    S4 S3

    S2S1

  • RepresentingTheStates•  HowmanybitsdoIneedtorepresentallthestatesinthis

    “Detect1101”Machine?

    •  Thereare5uniquestates(including“init”)–  So,3bits

    •  HowmanyD-FFsshouldIhavetobuildthismachine?–  3bits=3D-FFs

    12/3/18 Matni,CS64,Fa18 28

    State B2 B1 B0

    Initial 0 0 0Found“1” 0 0 1Found“11” 0 1 0Found“110” 0 1 1Found“1101” 1 0 0

  • DesigningtheCircuitfortheFSM

    1.  WestartwithaT.T–  Alsocalleda“StateTransitionTable”

    2.  MakeK-Mapsandsimplify–  Usuallygiveyouranswerasa“sum-of-products”form

    3.  Designthecircuit–  HavetouseD-FFstorepresentthestatebits

    12/3/18 Matni,CS64,Fa18 29

  • 1.TheTruthTable(TheStateTransitionTable)

    12/3/18 Matni,CS64,Fa18 30

    State B2 B1 B0 I B2* B1* B0* FOUND

    Initial 0 0 0 0 0 0 0 0

    1 0 0 1 0

    Found“1” 0 0 1 0 0 0 0 0

    1 0 1 0 0

    Found“11” 0 1 0 0 0 1 1 0

    1 0 1 0 0

    Found“110” 0 1 1 0 0 0 0 0

    1 1 0 0 0

    Found“1101” 1 0 0 0 0 0 0 1

    1 0 1 0 1

    CURRENTSTATE NEXTSTATEINPUT(S) OUTPUT(S)

  • 2.K-MapsforB2*andB1*

    •  Youneedtodothisforall3stateoutputs

    •  B2*=!B2.B1.B0.I– Nofurthersimplification

    •  B1*=!B2.!B1.B0.I +B2.!B1.!B0.I +!B2.B1.!B0

    12/3/18 Matni,CS64,Fa18 31

    B2.B1B0.I

    00 01 11 10

    00

    01

    11 1

    10

    B2.B1B0.I

    00 01 11 10

    00 1

    01 1 1

    11 1

    10

  • 2.K-MapforB0*OutputFOUND

    •  B0*=!B2.!B1.!B0.I +!B2.B1.!B0.!I

    •  FOUND =B2.!B1.!B0– NotethatFOUNDdoesnotneedaK-Map.Itisalways“1”(i.e.True)whenweareinstateS4(i.e.whenB2=1,B1=0,B0=0)

    12/3/18 Matni,CS64,Fa18 32

    B2.B1B0.I

    00 01 11 10

    00 1

    01 1

    11

    10

  • 12/3/18 Matni,CS64,Fa18 33

    3.DesigntheCircuit

    NotethatCLKistheinputtoALLtheD-FFs’clockinputs.Thisisasynchronousmachine.Notetheuseoflabels(example:B2orB0-bar)insteadofroutingwiresallovertheplace!NotethatIissuedbothBnandBn-barfromalltheD-FFs–itmakesiteasierwiththelabelingandyouwon’thavetouseNOTgates!Notethatthesoleoutput(FOUND)doesnotneedaD-FFbecauseitisNOTASTATEBIT!

  • 12/3/18 Matni,CS64,Fa18 34