Lecture 7 - State Machines
Lecture 7 - State Machines
State Machines
Getachew Teshome (Room 120-B)
Addis Ababa University, AAIT
School of Electrical and Computer Engineering
System Representation
• State Machine Model
– FSM/FSMD, Program-State Machine (PSM) Model
• Concurrent Process Model
– Communication,Synchronization,Implementation
• Dataflow Model
• Real-Time Systems
Elevator Control
System interface
Partial English description up
Unit
Control down
open
“Move the elevator either up or down to floor
reach the requested floor. Once at the req
5
Finite-state machine (FSM) model
req > floor
!(req<floor)
u,d,o,t = 0,1,0,0
GoingDn
6
Formal definition
• An FSM is a 6-tuple F<S, I, O, F, H, s0>
– S is a set of all states {s0, s1, …, sl}
– I is a set of inputs {i0, i1, …, im}
– O is a set of outputs {o0, o1, …, on}
– F is a next-state function (S x I → S)
– H is an output function (S → O)
– s0 is an initial state
• Moore-type
– Associates outputs with states (as given above, H maps S → O)
• Mealy-type
– Associates outputs with transitions (H maps S x I → O)
• Shorthand notations to simplify descriptions
– Implicitly assign 0 to all unassigned outputs in a state
– Implicitly AND every transition condition with clock edge (FSM is synchronous)
7
Finite-state machine with
datapath model (FSMD)
• FSMD = FSM + complex data types & variables for storing data
– FSMs use only Boolean data types and operations, no variables
We described UnitControl as an FSMD
• FSMD: 7-tuple <S, I , O, V, F, H, s0>
req > floor
– S is a set of states {s0, s1, …, sl}
u,d,o, t = 1,0,0,0 GoingUp !(req > floor)
– I is a set of inputs {i0, i1, …, im}
req > floor timer < 10
– O is a set of outputs {o0, o1, …, on} u,d,o,t = 0,0,1,0 !(timer < 10)
Idle DoorOpen
– V is a set of variables {v0, v1, …, vn} req ==
floor
req < floor
u,d,o,t =
0,0,1,1
– F is a next-state function (S x I x V → S) u,d,o,t = 0,1,0,0 GoingDn
!(req<floor)
8
Describing a system as a state machine
9
FSM in sequential programming
10
Language subset
approach
• Equivalent sequential language
constructs (e.g. C and VHDL) #define IDLE0
#define GOINGUP1
#define GOINGDN2
• State machine in C #define DOOROPEN3
– Enumerate all states (#define) void UnitControl() {
int state = IDLE;
– Declare state variable initialized to while (1) {
switch (state) {
initial state (IDLE) IDLE: up=0; down=0; open=1; timer_start=0;
if (req==floor) {state = IDLE;}
– Single switch statement branches to if (req > floor) {state = GOINGUP;}
current state’s case if (req < floor) {state = GOINGDN;}
break;
– Each case has actions GOINGUP: up=1; down=0; open=0; timer_start=0;
if (req > floor) {state = GOINGUP;}
• up, down, open, timer_start if (!(req>floor)) {state = DOOROPEN;}
break;
– Each case checks transition conditions GOINGDN: up=1; down=0; open=0; timer_start=0;
to determine next state if (req < floor) {state = GOINGDN;}
if (!(req<floor)) {state = DOOROPEN;}
• if(…) {state = …;} break;
DOOROPEN: up=0; down=0; open=1; timer_start=1;
if (timer < 10) {state = DOOROPEN;}
if (!(timer<10)){state = IDLE;}
break;
}
}
}
11
General template
#define S0 0
#define S1 1
...
#define SN N
void StateMachine() {
int state = S0; // or whatever is the initial state.
while (1) {
switch (state) {
S0:
// Insert S0’s actions here & Insert transitions Ti leaving S0:
if( T0’s condition is true ) {state = T0’s next state; /*actions*/ }
if( T1’s condition is true ) {state = T1’s next state; /*actions*/ }
...
if( Tm’s condition is true ) {state = Tm’s next state; /*actions*/ }
break;
S1:
// Insert S1’s actions here
// Insert transitions Ti leaving S1
break;
...
SN:
// Insert SN’s actions here
// Insert transitions Ti leaving SN
break;
}
}
}
HCFSM
• Hierarchical/concurrent state machine model (HCFSM)
– Extension to state machine model to support hierarchy and concurrency
– States can be decomposed into another state machine
• With hierarchy has identical functionality as Without hierarchy, but has one less transition (z)
• Known as OR-decomposition
– States can execute concurrently
• Known as AND-decomposition
A2 z A2 C2 D2
Concurrent processes
• Communication Encoded video packets
– Shared memory processA() {
// Decode packet
• No time overhead, easy to implement // Communicate packet to B
}
}
• But, hard to use – mistakes are common
– Message passing
Decoded video packets
• Data explicitly sent from one process to processB() {
another // Get packet from A
// Display packet
• Safer model, but less flexible }
• Synchronization
• Scheduling To display
16
Scheduling: priority
• Process with highest priority selected first
– Statica, Dynamic
• FIFO
– Runnable processes added to end of FIFO as created or become runnable
– Front process removed from FIFO when time quantum of current process is up
or process is blocked
• Priority queue
– Runnable processes again added as created or become runnable
– Process with highest priority chosen when new process needed
– If multiple processes with same highest priority value then selects from them
using first-come first-served
– non preemptive Priority scheduling
– Preemptive Called round-robin
17
Priority assignment
• Rate monotonic scheduling
– Processes with shorter periods have higher priority Rate monotonic
Process Period Priority
– Typically used when execution deadline = period
• Deadline monotonic scheduling A 25 ms 5
B 50 ms 3
– Processes with shorter deadlines have higher priority C 12 ms 6
D 100 ms 1
– Typically used when execution deadline < period E 40 ms 4
F 75 ms 2
Deadline monotonic
Process Deadline Priority
G 17 ms 5
H 50 ms 2
I 32 ms 3
J 10 ms 6
K 140 ms 1
L 32 ms 4
18
Creating Tasks in C
Main()