170 likes | 350 Views
Finite State Machine: Realization. Martinet Lee, Steve Chung. Outline. What is FSM? The theory Not only a theory Now, forget about FSM Implementing functions, by circuit Implementing functions, by logic When circuit meets logic, FSM A simple example : from functions to FSM to circuit.
E N D
Finite State Machine:Realization Martinet Lee, SteveChung
Outline • What is FSM? • The theory • Not only a theory • Now, forget about FSM • Implementing functions, by circuit • Implementing functions, by logic • When circuit meets logic, FSM • A simple example: from functions to FSM to circuit
Not only a theory… • You know how it looks like • But how to construct it?How to connect it with the target function?
Implementing functions, by circuit • Function: Simple Traffic Light: Red Green Yellow Red(repeat) • Construct a counter which will reset on 2 : • 0 1 2 0 … • 2’b00 2’b01 2’b10 2’b00 … • Construct the lights according to the counter’s value.
Implementing functions, by circuit • Function: Find target , identify target ( enemy and ally ) , act accordingly ( fire, supply ), then repeat. • How to construct? • Counter?
Implementing functions, by Logic • Function: Find target , identify target ( enemy and ally ) , act accordingly ( fire, supply ), then repeat. • How to construct? • Search Found if( target == Enemy ) Fire; else Supply; Search
Finite State Machine • Function: Find target , identify target ( enemy and ally ) , act accordingly ( fire, supply ), then repeat. • Aintuitive mapping from “function” to “FSM graph” • But how to implement?
Finite State Machine Implementation • Create a set of registers, to store STATE • Assign each state on FSM to a number • Search 0 3’b000 • Found 1 3’b001 • Enemy 2 3’b010 • Ally 3 3’b011 • Fire 4 3’b100 • Supply 5 3’b101 • A circuit that could differentiate enemy and ally, will produce a 1-bit signal.
FINITE STATE MACHINE IMPLEMENTATION • reg [2:0] State;reg [2:0] nextState;regdetected_smth;regisenemy;always@(posedgeclk)beginif(reset == 1’b1) State <= 3’b000; else begin State <= nextState; endendalways@(*) begincase(State) 3’b000: if(detected_smth == 1’b1) nextState = 3’b001; elsenextState = 3’b000; 3’b001: if( isenemy == 1’b1)nextstate = 3’b010; elsenextstate = 3’b011; 3’b010: nextState = 3’b100; 3’b011: nextState= 3’b101; 3’b100: nextState = 3’b000; 3’b101: nextState = 3’b000; default: nextState = 3’b000;endcaseend
Code Not Clear enough… • Use parameter! • parameter S_Search = 3’b000;parameter S_Found = 3’b001;parameter S_Enemy = 3’b010;parameter S_Ally = 3’b011;parameter S_Fire = 3’b100;parameter S_Supply = 3’b101;
Overall Code • reg [2:0] State;reg [2:0] nextState;regdetected_smth;regisenemy;parameter S_Search = 3’b000;parameter S_Found = 3’b001;parameter S_Enemy = 3’b010;parameter S_Ally = 3’b011;parameter S_Fire = 3’b100;parameter S_Supply = 3’b101;always@(posedgeclk)begin if(reset == 1’b1) State <= S_Search; else begin State <= nextState; endendalways@(*) begin case(State)S_Search: if(detected_smth == 1’b1) nextState = S_Found; elsenextState = S_Search;S_Found: if( isenemy == 1’b1)nextstate = S_Enemy; elsenextstate = S_Ally;S_Enemy:nextState = S_Fire;S_Ally:nextState = S_Supply;S_Fire:nextState = S_Search;S_Supply:nextState = S_Search; default: nextState = S_Search;endcaseend