38
Finite State Machines CS 64: Computer Organization and Design Logic Lecture #16 Ziad Matni Dept. of Computer Science, UCSB

Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

FiniteStateMachines

CS64:ComputerOrganizationandDesignLogicLecture#16

ZiadMatni

Dept.ofComputerScience,UCSB

Page 2: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

Administrative

•  LESSTHAN2weeksleft!!!!!!OMGOMGOMGOMGOMGOMG!!!!!!!!!!!!!!

•  FinalexamisonWednesday,June13thfrom12:00–3:00PM.– Detailswillbegivennextweek

5/31/18 Matni,CS64,Sp18 2

Page 3: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

LectureOutline

•  FiniteStateMachines

– Moorevs.Mealytypes

– StateDiagrams

– “OneHot”Method

5/31/18 Matni,CS64,Sp18 3

Page 4: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

AnyQuestionsFromLastLecture?

5/31/18 Matni,CS64,Sp18 4

Page 5: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

5/31/18 Matni,CS64,Sp18 5

IfacombinationallogiccircuitisanimplementationofaBooleanfunction,

thenasequentiallogiccircuitcanbeconsideredanimplementationof

afinitestatemachine.

Page 6: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

FiniteStateMachines(FSM)

•  Anabstractmachinethatcanbeinexactlyoneofafinitenumberofstatesatanygiventime

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

5/31/18 Matni,CS64,Sp18 6

Page 7: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

FiniteStateMachines(FSM)•  TheFSMcanchangefromonestatetoanotherinresponsetosome

externalinputs

•  Thechangefromonestatetoanotheriscalledatransition.

•  AnFSMisdefinedbyalistofitsstates,itsinitialstate,andtheconditionsforeachtransition.

5/31/18 Matni,CS64,Sp18 7

STATE“A”

StartingpointSTATE

“B”

TransitionA->B

TransitionB->A

Page 8: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

ExampleofaSimpleFSM:TheTurnstile

initialstateStateTransitionTable

5/31/18 Matni,CS64,Sp18 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.

Page 9: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

ExampleofaSimpleFSM:TheTurnstile

initialstateStateTransitionTable

5/31/18 Matni,CS64,Sp18 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.

Page 10: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

GeneralFormofFSMs

5/31/18 Matni,CS64,Sp18 10

Page 11: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

Example

5/31/18 Matni,CS64,Sp18 11

A

CLK

Q*

Q*=QO.A

Onthenextrisingedgeoftheclock,theoutputoftheD-FFQ(Q*)willbecome

thepreviousvalueofQ(QO)ANDthevalueofinputA

QoCombinatoriallogic

StateregisterClocksignal

Output-to-inputfeedback

Page 12: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

FSMTypes

Thereare2types/modelsofFSMs:•  Mooremachine–  Outputisfunctionofpresentstateonly

•  Mealymachine–  Outputisfunctionofpresentstateandpresentinput

5/31/18 Matni,CS64,Sp18 12

Page 13: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

MooreMachine

5/31/18 Matni,CS64,Sp18 13

Outputisfunctionofpresentstateonly

Page 14: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

ExampleofaMooreMachine(with1state)

5/31/18 Matni,CS64,Sp18 14

A

CLK

Z

Z=(Q*+B)=(QO.A+B)

Onthenextrisingedgeoftheclock,theoutputoftheentirecircuit(Z)willbecome

(thepreviousvalueofQ(QO)ANDthevalueofinputA)NORB

B

Q

Outputisfunctionofpresentstateonly

Page 15: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

MealyMachine

5/31/18 Matni,CS64,Sp18 15

Outputisfunctionofpresentstateandpresentinput

Page 16: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

ExampleofaMealyMachine(with1state)

5/31/18 Matni,CS64,Sp18 16

A

CLKB

Z

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

Onthenextrisingedgeoftheclock,theoutputoftheentirecircuit(Z)willbecome…etc…

Q

Outputisfunctionofpresentstateandpresentinput

Page 17: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

DiagramingStateMachines

•  AsimpleFSMexample•  2states:– Dooropened– Doorclosed

•  Thisiscalledastatediagram

5/31/18 Matni,CS64,Sp18 17

Page 18: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

ExampleofaMooreMachine1

WASHER_DRYER

•  Let’s“build”asequentiallogicFSMthatactsasacontrollertoasimplisticwasher/dryermachine

•  Thismachinetakesinvariousinputsinitsoperation(we’llonlyfocusonthefollowingsensor-basedones):

•  Thismachinealsoissuesoutputswhilerunning:

5/31/18 Matni,CS64,Sp18 18

Coinputin(vsitisn’t) Timerisunder30minutes(vsitisn’t)Soapispresent(vsusedup) Wetnessindicator(vsclothesaredry)

“TimerBegin”indicator “Fill-Water”indicator“Drain-Then-Refill”indicator “DrainCompletely”indicator

Page 19: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

ExampleofaMooreMachine1WASHER_DRYER

•  Beforewebegin,themachineisinaninitialstatethatiswaitingforyoutoinsertacoin.We’llcallthatthe“InitialState”

•  Themachinewillstartawashertimerassoonasacoinisinserted.–  Byissuinganoutput,“TimerBegin”

•  Thetimeriscontrolledbyasignal(i.e.inputvar)calledTIMER_LT_30,whichisalwaysinitializedtobe1(i.e.True).

•  Wethustransitionfrom“InitialState”to“WashState”

5/31/18 Matni,CS64,Sp18 19

Page 20: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

ExampleofaMooreMachine1WASHER_DRYER

•  WashState:Thiswilloutputasignaltofillthewasherwithwater(FILL_WATER).

•  Aslongasthetimerisbelow30mins(TIMER_LT_30=1),thecyclecontinues.

•  Whenthetimersurpasses30mins(i.e.TIMER_LT_30=0),thisstatewillend

•  Wethustransitionfrom“WashState”to“RinseState”

5/31/18 Matni,CS64,Sp18 20

Page 21: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

ExampleofaMooreMachine1WASHER_DRYER

•  RinseState:Thiswilloutputasignaltodrainthewaterandthenrefillwithnewwater(DRAIN_REFILL=1).

•  Aslongasthesoapsensorison(SOAP=1),thecyclecontinues.

•  Whenthesoapsensorturnsoff(i.e.SOAP=0),thisstatewillend

•  Wethustransitionfrom“RinseState”to“DryState”

5/31/18 Matni,CS64,Sp18 21

Page 22: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

ExampleofaMooreMachine1WASHER_DRYER

•  DryState:Thiswilloutputasignaltodrainthewaterforgoodandbegindrying(DRAIN=1).

•  Aslongasthewetclothessensorison(WET=1),thecyclecontinues.

•  Whenthewetclothessensorisoff(WET=0),wewillstop!•  Thismeansgoingbacktothe“InitialState”

5/31/18 Matni,CS64,Sp18 22

Page 23: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

5/31/18 Matni,CS64,Sp18 23

StateDiagramforWasher-DryerMachine

Wash Rinse

Dry

InitialState

COIN=0

TIMER_LT_30=0

DRAIN=1

SOAP=1

WET=0

TIMER_LT_30=1

TIMER_BEGIN=1

FILL_WATER=1DRAIN_REFILL=1

WET=1

inputs

outputsstate

transition

Page 24: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

UnconditionalTransitions

•  Sometimesthetransitionisunconditional–  Doesnotdependonanyinput–itjusthappens

•  Wethendiagramthisasa“1”(for“alwaysdoesthis”)

5/31/18 24

State2State0K=1

State1

K=01

K=0K=1

Page 25: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

RepresentingTheStates

•  HowmanybitsdoIneedtorepresentallthestatesinthisWasher-DryerMachine?

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

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

•  Thereisanotherschemetodothiscalled“OneHotMethod”.–  Moreonthislater…

5/31/18 Matni,CS64,Sp18 25

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

Page 26: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

ExampleofaMooreMachine2DETECT_1101

•  Let’sbuildasequentiallogicFSMthatalwaysdetectsaspecificserialsequenceofbits:1101

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

–  Don’tgotoS1ifallwefindisa0!

•  We’llthenkeeplookingforanother1.We’llcallthat“State11”(S2)•  Then…a0.We’llcallthat“State110”(S3)•  Thenanother1.

We’llcallthat“State1101”(S4)–thiswilloutputaFOUNDsignal

•  Wewillalwaysbedetecting“1101”(itdoesn’tend)•  Example:iftheinputstreamis 111101110101101000011111011011

wedetect“1101”at ññññ ñ

5/31/18 Matni,CS64,Sp18 26

Page 27: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

StateDiagram2

5/31/18 Matni,CS64,Sp18 27

“1” “11”

“110”“1101”

InitialState

Input=0 Input=1

Input=1

Input=0

Input=0

FOUND=1

Input=1

S0

S4 S3

S2S1

Page 28: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

RepresentingTheStates

•  HowmanybitsdoIneedtorepresentallthestatesinthis“Detect1101”Machine?

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

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

5/31/18 Matni,CS64,Sp18 28

State S2 S1 S0Initial 0 0 0Found“1” 0 0 1Found“11” 0 1 0Found“110” 0 1 1Found“1101” 1 0 0

Page 29: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

DesigningtheCircuitfortheFSM

1. WestartwithaT.T2. MakeK-Mapsandsimplify3. Designthecircuit

5/31/18 Matni,CS64,Sp18 29

Page 30: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

1.TheTruthTable

5/31/18 Matni,CS64,Sp18 30

State S2 S1 S0 I S2* S1* S0* FOUNDInitial 0 0 0 0 0 0 0 0

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

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

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

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

1 0 1 0 1

Page 31: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

2.K-Maps

•  Example:forS2*–  Youneedtodothisforall4outputs

S2*=!S2.S1.!S0.!I +!S2.S1.S0.I = !S2.S1.(!S0.!I+S0.I) =!S2.S1.!(S0+I)

5/31/18 Matni,CS64,Sp18 31

S2.S1S0.I

00 01 11 10

00 10111 110

Page 32: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

3.DesigntheCircuit

•  Seeblackboard…

5/31/18 Matni,CS64,Sp18 32

Page 33: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

The“OneHot”Method•  MostpopularlyusedinbuildingFSMs•  Giveeachstateit’sownD-FFoutput

–  #ofFFsneeded=#ofstates–  YouendupusingMORED-FFs,buttheimplementationiseasier

toautomate

•  InputstotheFFsarecombinatoriallogicthatcansimplifiedintoa“sum-of-products”typeofBooleanexpression

•  CurrentCADsoftwarecandothisautomatically•  Implementationisusuallydoneonasimulator(software),or

prototypehardwareIntegratedCircuit(FPGA)5/31/18 Matni,CS64,Sp18 33

Page 34: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

EncodingourStatesPerthelastexample:Wehad5separatestates:NAME BinaryCode“OneHot”Code OUTPUTS•  InitialState S0 000 00001 •  “1” S1 001 00010•  “11” S2 010 00100•  “110” S3 011 01000•  “1101” S4 100 10000 FOUND

•  Advantageofthis“OneHot”approach?–  Whenweimplementthemachinewithcircuits,wecanuseaD-FFfor

everystate(so,inthisexample,we’duse5ofthem)

5/31/18 Matni,CS64,Sp18 34

Page 35: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

Usingthe“OneHot”CodetoDeterminetheCircuitDesign

•  Everystatehas1D-FF•  Wecanseethat

(followthearrows!!):S1*=S0.IS2*=S1.I+S2.I+S4.IS3*=S2.IS4*=S3.IS0*=S0.I+S1.I+S3.I+S4.IAlso,whenS4happens,FOUND=1,i.e.FOUND=S4WehavenowdescribedALLtheoutputsofthemachineascombinationsofcertaininputs

5/31/18 Matni,CS64,Sp18 35

“1” “11”

“110”“1101”

InitialState

Input=0 Input=1

Input=1

Input=0Input=0

FOUND=1

Input=1

S0

S4 S3

S2S1

Page 36: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

5/31/18 Matni,CS64,Sp18 36

ImplementingtheCircuit

Page 37: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

YourToDos

•  Lab#8isdueMonday!•  Lab#9isonFSMandwillbeissuedtomorrow– Dueonthelastdayofclasses

5/31/18 Matni,CS64,Sp18 37

Page 38: Finite State Machines - GitHub Pages · 2019-12-04 · a finite state machine. Finite State Machines (FSM) • An abstract machine that can be in exactly one of a finite number of

5/31/18 Matni,CS64,Sp18 38