550 likes | 807 Vues
Unit 3 Finite State Machine. If we hear, we forget; if we see, we remember; if we do, we understand. -- Proverb. 3.1 Derivation of State Graphs and Tables.
E N D
Unit 3 Finite State Machine If we hear, we forget; if we see, we remember; if we do, we understand. -- Proverb Department of Communication Engineering, NCTU
3.1 Derivation of State Graphs and Tables Department of Communication Engineering, NCTU
A Finite State Machine (FSM) is simply a state register that holds the current state and some combinational logic which calculates the next state and outputs based on the current state and the inputs • FSM types • Moore machine : the outputs are functions of the present state only • Mealy machine : the outputs are functions of both the present state and the inputs Department of Communication Engineering, NCTU
Initialization Easy Cards Passing Locked Unlocked Passing Violation (Unlocked) Alarm Reset • A Moore FSM • MTS turnstile Department of Communication Engineering, NCTU
State diagram representation • Locked ST: S0,Unlocked ST : S1and Violation ST: S2 • Inputs • Presence of Easy Card u0 =1, otherwise u0 =0 • Passenger passing u1 =1, otherwise u1 =0 • Alarm reset u2 =1, otherwise u2 =0 • Outputs (Z) • S0 Locked • S1 Unlocked • S2 Unlocked u0=0 XNOR u1=0 S0 u0=1 u1=1 u2=1 u1=1 S2 S1 u1=0 u2=0 Department of Communication Engineering, NCTU
u0=0 XNOR u1=0 S0 u0=1 u1=1 u2=1 u1=1 S2 S1 u1=0 u2=0 • State transition table Department of Communication Engineering, NCTU
Binary state assignment Department of Communication Engineering, NCTU
Next-State maps u1u0 u1u0 01 11 10 01 11 10 Q1Q0 Q1Q0 00 00 01 01 u2 1/0 u2 1/0 11 11 10 10 Q1+ Q0+ Department of Communication Engineering, NCTU
Characteristic equations • Q1+= Q1u2’ + Q0’u2’u1u0’ Q0+= Q0u1’ + Q0u0 + u2’u1’u0 u1u0 u1u0 00 00 01 11 10 01 11 10 Q1Q0 Q1Q0 00 00 01 01 u2 1/0 u2 1/0 11 11 10 10 Q1+ Q0+ Department of Communication Engineering, NCTU
Output equations • Characteristic equations:Q1+= Q1u2’ + Q0’u2’u1u0’ Q0+= Q0u1’ + Q0u0 + u2’u1’u0 • Output equation :Z = Q1 Q0 (or = (Q1|| Q0)’ Department of Communication Engineering, NCTU
D D Q Q CK CK CLR CLR • Circuit realization with D-FFs • Characteristic equations:Q1+= Q1u2’ + Q0’u2’u1u0’ Q0+= Q0u1’ + Q0u0 + u2’u1’u0 • Output equation :Z = Q1 Q0 Q1+ u2 Q1 u1 Z u0 Q0+ u1 u0 Q0 u2 CLK Department of Communication Engineering, NCTU
req > floor u,d,o, t = 1,0,0,0 GoingUp !(req > floor) timer < 10 req > floor !(timer < 10) u,d,o,t = 0,0,1,0 Idle DoorOpen u,d,o,t = 0,0,1,1 req == floor req < floor !(req<floor) u,d,o,t = 0,1,0,0 GoingDn u is up, d is down, o is open req < floor t is timer_start • Another Moore FSM • An elevator controller (Up, Down, Open and Timer start) Department of Communication Engineering, NCTU
D D D Q Q Q CK CK CK CLR CLR CLR • The general model of a Moore machine X1 Q1+ Q1 Com. Logic Com. Logic X2 Z1 Xm Q2+ Q2 Z2 Zm Qm+ Qm Department of Communication Engineering, NCTU
A Mealy FSM • A traffic light controller Department of Communication Engineering, NCTU
Tabulation of inputs and outputs • Tabulation of unique states Input Signal Reset C TS TL Output Signal HG, HY, HR FG, FY, FR ST Description place FSM in initial state detect vehicle on farmroad short time interval expired long time interval expired Description assert green/yellow/red highway lights assert green/yellow/red farmroad lights start timing a short or long interval Description Highway green (farmroad red) Highway yellow (farmroad red) Farmroad green (highway red) Farmroad yellow (highway red) State S0 S1 S2 S3 Department of Communication Engineering, NCTU
Reset TL’ + C’ S0 TL & C /ST TS/ST S1 S3 TS’ TS’ TS/ST TL & C’/ST S2 TL’ + C • The state diagram S0: HG (FR) S1: HY (FR) S2: FG (HR) S3: FY (HR) Department of Communication Engineering, NCTU
Reset TL’ + C’ S0 TL& C /ST TS/ST S3 TS’ S1 TS’ TS/ST S2 TL& C’/ST TL’ + C • State transition table HG = S0 FR = S0+ S1 HY = S1 FG = S2 HR = S2+ S3 FY = S3 ST = ? Department of Communication Engineering, NCTU
TLTS TLTS 01 11 10 01 11 10 Q1Q0 Q1Q0 00 00 01 01 C 1/0 C 1/0 11 11 10 10 Q1+ Q0+ Department of Communication Engineering, NCTU
TLTS 01 11 10 Q1Q0 HG = S0 FR = S0+ S1 HY = S1 FG = S2 HR = S2+ S3 FY = S3 ST = Q1Q0’TS + Q1’Q0 TS + Q1’Q0’CTL+ Q1Q0C’TL 00 01 C 1/0 11 10 ST Department of Communication Engineering, NCTU
X1 Com. Logic Z1 X2 Z2 Xm Zm Q1+ Q1 D D D Q Q Q Q2+ Q2 CK CK CK CLR CLR CLR Qm+ Qm • The general model of a Mealy machine Department of Communication Engineering, NCTU
Reset TL’ + C’ S0 Plant TL& C /ST TS/ST S3 ST S1 TS’ TS/ST S2 TL& C’/ST CntPre TL’ + C VDD DN D DN D D Q Q D Counter for TS CK CK CK CK Pre CLR Pre CLR TS Counter for TL TL • Circuit realization using a Mealy machine TS’ CLK FSM Department of Communication Engineering, NCTU
CLK Q1Q0 S0 S1 S2 S3 TL TS C TL’ + C’ Reset HG = S0 S0 TL& C /ST TS/ST ST S3 S1 CntPre TS’ TL& C’/ST TS/ST S2 TL’ + C • Timing diagram using a Mealy machine ST = Q1Q0’TS + Q1’Q0 TS + Q1’Q0’CTL+ Q1Q0C’TL Department of Communication Engineering, NCTU
Plant VDD TL’ + C’ Reset Counter for TS S0 ST TL& C TS ST’ TS TS’ S3 S1 TS’ CLK ST ST ST DN DN Counter for TL ENA ENA D D TS S2 TL& C’ ST’ CK CK Pre Pre TL TL’ + C FSM • An alternative realization with a Moore machine ST = Q1Q0’+ Q1’Q0 Notice: need more states to generate the Pre signal if needed!! Department of Communication Engineering, NCTU
CLK Q1Q0 S0 S1 S2 S3 TL TS C TL’ + C’ Reset HG = S0 S0 TL& C TS ST’ ST S3 S1 TS’ ST ST ST TS S2 TL& C’ ST’ TL’ + C • Timing diagram using a Moore machine TS’ ST = Q1Q0’+ Q1’Q0 Department of Communication Engineering, NCTU
X1 Plant Z1 X2 Z2 XP ZQ IN1 FSM CS1 IN2 CS2 INR CSM • The general model of a digital circuit Department of Communication Engineering, NCTU
D D Q Q CK CK CLR CLR ST • Difference between a Moore machine and a Mealy machine • The outputs of a Moore FSM are synchronized with the CLK, however the outputs of a Mealy machine are not • The disadvantages of Moore FSMs • In general, more states are required to generate outputs • The action of the plant that the FSM controls is always one CLK period lagging behind the control signals • The above two points are right the advantages of Mealy FSMs • The disadvantages of Mealy FSMs • Outputs of FSMs must be sampled with FFs if synchronization is required • Glitches and spikes CntPre Department of Communication Engineering, NCTU
Serial data code converter • Design a code converter that convert an NRZ-coded bit stream to a Manchester-coded bit stream Department of Communication Engineering, NCTU
Reset S0 0 /0 1/1 0/1 1/0 S1 S2 • Realize the code converter with a Mealy machine • Use a clock Clock2 which is twice the data rate • The only two possible input sequences are 00 and 11 Department of Communication Engineering, NCTU
0 1 1 1 0 0 1 1 0 1 0 1 0 0 1 S0 S1 S0 S2 S0 S2 S0 S2 S0 S1 0 1 1 0 1 0 1 0 0 1 S0 0 /0 1/1 S1 S2 0/1 1/0 • The timing diagram using the Mealy machine CLK2 NRZ Manchester (ideal) Z (actual) Department of Communication Engineering, NCTU
Redesign the data converter with a Moore machine Department of Communication Engineering, NCTU
The timing diagram using the Moore machine Output is one-clock lagging behind the input sequence Department of Communication Engineering, NCTU
3.2 Data Path and FSM Department of Communication Engineering, NCTU
The general model of a digital circuit X1 Plant Z1 X2 Z2 XP ZQ IN1 FSM CS1 IN2 CS2 INR CSM Department of Communication Engineering, NCTU
A plant usually includes • A data path over which data are processed • A data path may include • An arithmetic and logic unit (ALU) • Registers • Counters • Decoders • Peripherals which may include • Input devices like key boards and mice • Storages devices like memories and disks • Output devices • Communications interface like USB, RS232, and Eithernet • Speaker • Printer • Displays like LCD and 7-segment displays Department of Communication Engineering, NCTU
Data path and FSM go hand in hand in digital design • A signal processing scheme is usually partitioned into a sequence of processing stages • Data path decides the number of stages the data is processed and the paths of data flow in between the stages • Data at the outputs of each stage are stores in registers • The complexity or in other words the levels of logics in each stage decides the processing speed • On the other hand the number of stages decides the processing delay • FSM controls data’s movement in data path Department of Communication Engineering, NCTU
Reg 0 DIP SW CS0 Reg 1 A A > B A MUX CS1 CS4 COMP MUX B Reg 2 B A = B CS2 CS5 Reg 3 S0 S1 S2 S3 • Data path for bubble sorting of 4 words CS3 Department of Communication Engineering, NCTU
Draw a flow chart Idle Load B N St Load A Cnt1=0 Load Data Counter -- Cnt 0 -- B>A Y Load N Cnt3=0 Load LW Cnt0=0 Cnt 0 = 2 N Load HW Cnt1=Cnt0 Cnt 1 -- Department of Communication Engineering, NCTU
CLK Idle St Load Cnt 3 XX 11 10 01 00 Cnt 0 XX 10 Cnt 1 XX 10 • Checking the Timing diagram Department of Communication Engineering, NCTU
Sequential machines are commonly partitioned into data path units and control units • The synthesis of a sequential machine usually includes: • Constructing the datapath units • Designing FSMs to control the data flow • Realizing the control signals • Checking the timing of signals Datapath Unit Datapath Logic Control inputs FSM Control signals Clock DatapathRegisters Department of Communication Engineering, NCTU
3.3 Algorithm State Machine Department of Communication Engineering, NCTU
Datapath units consist of: • Arithmetic units : • Arithmetic and logic units (ALU) • Storage registers • Logic for moving data : • through the system • between the computation units and internal registers • to and from the external environments • Control units are commonly modeled by • State transition graphs (STGs) • Algorithm state machine (ASM) charts for FSM • A combined control-dataflow sequential machine is modeled by ASM and datapath (ASMD) charts Department of Communication Engineering, NCTU
Algorithm State Machine (ASM) Charts • State transition graphs only indicate the transitions that result from inputs • Not only does ASM display the state transitions, it also models the evolution of states under the application of input datas • An ASM chart is formed with three fundamental elements Department of Communication Engineering, NCTU
Start En C <= C+1 • Both Mealy and Moore machines can be represented by ASM • The outputs of a Moore machine are listed inside a state box • Conditional outputs (Mealy outputs) are placed in conditional output boxes Department of Communication Engineering, NCTU
A sequential machine is partitioned into a controller and a datapath, and the controller is described by an ASM • The ASM chart can be modified to link to the datapath that is under control of the ASM • The modified ASM is referred to as the algorithm state machine and datapath (ASMD) chart • ASMD is different from ASM in that :each of the transition path of an ASM is annotated with the associated concurrent register operations of datapath Department of Communication Engineering, NCTU
An ASMD chart for a up-down counter Up-down counter with asynchronous reset Up-down counter with synchronous reset Count <= 0 Count <= 0 Reset Count <= Count - 1 Count <= Count + 1 Start Start Clr Count <= Count - 1 Up Up Count <= Count + 1 Department of Communication Engineering, NCTU
Start En C <= C+1 • ASM v.s. ASMD charts for a counter with enable ASM chart representation ASMD chart representation Start Count <= Count + 1 En Enable DP Department of Communication Engineering, NCTU
A electronic dice game Department of Communication Engineering, NCTU
Flowchart for dice game Department of Communication Engineering, NCTU
Convert flowchart to state machine chart Department of Communication Engineering, NCTU