Verilog Systemtask
Verilog Systemtask
Verilog Systemtask
FiniteStateMachine
Out e Outline
FiniteStateMachine
Describethesequentialbehaviorusinga FSM ExampleofFSM Convert C tafi finite it state t t machine hi t toa Controller asequentialcircuithaving
AregisterandCombinationallogic
SequentialCircuit:FSM
FS SM inp puts Combinational Circuit s1 s0 State register/ History/ Sequence n1 n0
FS SM outp puts
clk
NeedBetterWaytoDesignSequentialCircuits
Trialanderror:notagooddesign method
Willwebeabletoguessacircuitthat worksforotherdesiredbehavior?
Howaboutcountingupfrom1to9? Pulsinganoutputfor1cycleevery10cycles? Detecting gthesequence q 135inbinary yona 3bitinput?
FiniteStateMachine
FiniteStateMachine(FSM)
Awaytodescribedesiredbehaviorof sequentialcircuit Akin k toBoolean l equationsfor f combinationalbehavior Liststates,andtransitionsamongstates
FiniteStateMachine:Example
Example:Makexchangetoggle(0to1,or1 to0)everyclockcycle Twostates:Off(x=0),andOn(x=1) Transition T iti from f Offt toOn, O orOn O t toOff, Off on risingclockedge Arrowwithnostartingstatepointstoinitial state(whencircuitfirststarts)
FiniteStateMachine:Example
Outputs: x clk^ x=0 Off clk^
Off O On Off O On O Off On O Off On O
x=1 On
cycle 1
cycle 2
cycle 3
cycle 4
Off
On
Off
On
ControllerforOnOff
Input CLK P RE 1 0 RE1 1
CLK P N
ThinkofClockEnable:onlyRisingEdge(RE) Aboveonemaynotwork:LevelSensitive RisingEdge:Clockimplicit
Output X N 1 1 0 0
X
X N
X N
FSMExample:0,1,1,1,repeat
Want0,1,1,1,0,1,1,1,...
Eachvalueforoneclockcycle
x=0 clk^ x=1 clk ^ x=1 clk^ x=1 Off On1 On2 On3
Outputs: x
clk^
ExtendFSMtoThreeCyclesHighLaserTimer
Sox=1forthreecyclesafterbutton pressed
ExtendFSMtoThreeCyclesHighLaserTimer
Inputs: b; Outputs: x x=0 Off b clk ^ b*clk x=1 clk^ On1
clk Inputs: b S tate Off Output s : x
clk^
Off
Off
Off
FSMSimplification:RisingClockEdgesImplicit
Showingrisingclockoneverytransition:cluttered Makeimplicit assumeeveryedgehasrising clock Whatifwewantedatransitionwithout arising g edge
AsynchronousFSMs lesscommon,andadvanced topic Weconsidersynchronous FSMs All transitiononrising gedge g
FSMSimplification:RisingClockEdgesImplicit
Inputs: b; Outputs: x
x=0 Off
^ b *clk ^ x=1 clk ^ b b *clk ^ clk
x=1 On3
On1
On2
On3
Note: Transition with no associated condition thus transistions to next state on next clock cycle
FSMDefinition
Set S tof fstates t t
Ex:{Off,On1,On2,On3}
Inputs: b; Outputs: x x=0 Off b x=1 On1 x=1 On2 x=1 On3 b
Initialstate
Ex:Off
Setoftransitions
ib next tstates, t t E iti D Describes Ex:H Has5t transitions
Setofactions
Sets S t outputs t t while hil in i states t t Ex:x=0,x=1,x=1,andx=1
FSMExample: p CodeDetector
Unlockdoor(u=1)onlywhenbuttonspressed insequence:
start,thenred,blue,green,red
Input I f fromeach h b button:s,r,g,b
Also,outputa indicatesthatsomecoloredbutton pressed
a
u r g b a
Code
detector
Door lock
FSMExample:CodeDetector
Waitforstart(s=1)inWait, Oncestarted(Start) Ifseered,gotoRed1 Then, h if fseebl blue,goto Blue, l Then, h if fsee green,gotoGreen,Then,ifseered,goto Red2 Red2 Inthatstate,openthedoor(u=1) Wrongbutton b atanystep,returntoWait i
FSMExample:CodeDetector
Wait u=0 s Start u=0 ar Red1 u=0 ab a Blue u=0 ag a
a
s a
ar ar
ab ab
ag ag
ar ar
Green u=0
ar a
Red2 u=1
Q: Can you trick this FSM to open the door, without knowing the code? A: Yes, hold all buttons simultaneously
ImproveFSMforCodeDetector
Inputs: s,r,g,b,a; Outputs: u Wait u=0 s s Start u=0 0 a ar Red1 u=0 ab a u=0 ag Blue a u=0 Green a u=1 ar Red2 ar ab ag ar
a
Thanks