What Is State?: Prof. Chung-Ta King Department of Computer Science National Tsing Hua University
What Is State?: Prof. Chung-Ta King Department of Computer Science National Tsing Hua University
What Is State?
State 有不同的類型 !
• 新、舊
• 運作良好、堪用、待修、報廢
• 停止、行進、牽行
• ...
A Better Question
機車的「運作狀態」有幾種 ?
熄火靜止、熄火行進、
發動靜止、發動行進
不同「運作狀態」之間如何轉換 ?
State transition
熄火
推動 發動
行進
熄火 熄火 發動
停止
靜止 行進
啟動
發動
發動 煞車停止
熄火
靜止
Finite state machine
5
Outline
What is finite state machine?
Finite state machine for program modeling
The important concept of “state”
Finite state machine for circuit control
Finite state machine for embedded control
Finite state machine for recognition
Modeling with finite state machine
6
Finite State Machines (FSM)
or Finite State Automation (FSA)
Models of behaviors of a system or object, with a
limited number of defined conditions or modes,
where modes change with circumstance
4 main elements:
States: define behavior and may produce actions
State transitions: move from one state to another
Rules (or conditions): must be met to allow a
state transition
Input events: externally or internally generated;
may trigger rules and lead to state transitions
An initial state and a current state
Good for representation and
7
modeling sequences of behaviors
Outline
What is finite state machine?
Finite state machine for program modeling
The important concept of “state”
Finite state machine for circuit control
Finite state machine for embedded control
Finite state machine for recognition
Modeling with finite state machine
8
Consider the Program
main()
{
i=5; j=6; k=0;
k=i+j;
if (k>0) i=0;
else j=0;
}
main()
{ Memory
i=5; j=6; k=0;
k=i+j;
6 j
if (k>0) i=0;
else j=0;
} CPU 5 i
0 k
10
State 1
main()
{ Memory
i=5; j=6; k=0;
k=i+j;
6 j
if (k>0) i=0;
else j=0;
} CPU 5 i
11 k
11
State 2
main()
{ Memory
i=5; j=6; k=0;
k=i+j;
6 j
if (k>0) i=0;
else j=0;
} CPU 0 i
11 k
12
State 3
main()
{ Memory
i=5; j=6; k=0;
k=i+j;
0 j
if (k>0) i=0;
else j=0;
} CPU 5 i
11 k
13
FSM Representing the Program
k=i+j
2 k<=0
1
V
k>0 4
14
Compare with Flow Chart
k=i+j
yes no
k>0
i=0 j=0
15
Some Questions
Can FSM be used to represent any program?
Any algorithm?
What is the “state” of a program/process? Or
a computer?
Why is this important?
系統更新的回復點
電腦休眠之後回復原來畫面
Context switch
Interrupt and resume
Is there a machine that can model any
computation?
Turing machine
16
State of a Procedure?
Fibonacci number (iterative version)
17
Why Is It Important?
Fibonacci number (recursive version)
18
How to Solve for Recursion?
If we could save the “state” of the calling
procedure in a safe/separate place that the
called procedure will not overwrite, then the
recursion can be executed correctly
Big idea: “procedure state” in run-time stack
Allocate the “state” of each called procedure in the
run-time stack procedure frame
All references to the “state” go to the stack
Each invocation will push “state” down the stack
How to write assembly code to do that?
Memory reference relative to stack pointer
Chapter 8 of CS2422
19
Summary: A View of a “State”
Storage contents of the code/system that
must be saved away when the code/system is
suspended and resumed later, as if nothing
had happened
State of a procedure on procedure calls
State of a thread/process on context switch
make time-sharing possible
State of a process on migration to another PC
State of a computer on error recovery
State of a computer on hibernation
...
20
Implication of the View
We can do anything as long as we do not
disturb the “state”
z = x * 3.1412;
if (z > 10)
j = j + k;
21
Outline
What is finite state machine?
Finite state machine for program modeling
The important concept of “state”
Finite state machine for circuit control
Finite state machine for embedded control
Finite state machine for recognition
Modeling with finite state machine
22
FSM Also Good for Hardware
Processor
Register Memory
AX
x
BX a
…
data
b
Controller
01 0000001 1000010 MOV AX, a
11 0000001 1000110 ADD AX, b
01 1000011 0000001 MOV x, AX
ALU …
clock
IR PC
PC: program counter
address
IR: instruction register
23
Step 1: Fetch (MOV AX, a)
Register Memory
AX
x
BX a
…
data
b
Controller
01 0000001 1000010 MOV AX, a
11 0000001 1000110 ADD AX, b
01 1000011 0000001 MOV x, AX
ALU …
clock
IR PC
01 0000001 1000010 0000111
address
24
Step 2: Decode (MOV AX,a)
Register Memory
AX
x
BX a
…
data
b
Controller
01 0000001 1000010 MOV AX, a
11 0000001 1000110 ADD AX, b
01 1000011 0000001 MOV x, AX
ALU …
clock
IR PC
01 0000001 1000010 0000111
address
25
Step 3: Execute (MOV AX,a)
Register Memory
00000000 00000001
AX
x
BX 00000000 000000001 a
…
data
b
Controller
01 0000001 1000010 MOV AX, a
11 0000001 1000110 ADD AX, b
01 1000011 0000001 MOV x, AX
ALU …
clock
IR PC
01 0000001 1000010 0000111
address
26
Concept of the State Machine
Computer Hardware = Datapath + Control
Qualifiers
Registers FSM generating sequences
Combinational Functional of control signals
Units (e.g., ALU) Instructs datapath what to
Busses do next
Control
Control
State
Qualifiers Control
and Signal
Inputs Outputs
Datapath
27
FSM of the Computer
For this highly simplified computer, the
controller can be described by a FSM
Fetch Decode
State Register
Combinational
circuit
Output
Clk To datapath
Controller
31
Two Types of FSMs
Differ in how outputs are produced
Moore Machine:
Outputs are independent of the inputs, i.e.
outputs are produced from within the state of the
state machine
Mealy Machine:
Outputs can be determined by the present state
alone, or by the present state and the present
inputs, i.e. outputs are produced as the machine
makes a transition from one state to another
Any Moore machine can be turned into a
Mealy machine (and vice versa)
32
Moore Machine
33
Mealy Machine
The Mealy State Machine
generates outputs based on:
The Present State, and
The Inputs to the M/c.
So, same state can generate
a,b
many different patterns of State 1 q,r
output signals, depending on
the inputs.
Input condition that
Outputs are shown on i,j must exist in order
transitions since they are x,y to execute these
determined in the same way transitions from
as is the next state. State 1
Output condition that
results from being in State 2
a particular present state
34
Outline
What is finite state machine?
Finite state machine for program modeling
The important concept of “state”
Finite state machine for circuit control
Finite state machine for embedded control
Finite state machine for recognition
Modeling with finite state machine
35
Safety Belt Control
We want to design a controller for safety belt
If the seat is seated and the belt is not buckled
within a set time, a buzzer will sound until the belt
is buckled event driven
Inputs: seat sensor, timer, belt sensor
Output: buzzer, timer
System: specialized computer for reacting
according to events
sensed by the sensors
36
FSM for Event-driven Systems
no seat/-
no seat/ idle
buzzer off seat/timer on
no belt and
no seat/- no timer/-
buzzer seated
Belt/buzzer on
belt/-
belt/
buzzer off no belt/timer on
belted
37
C Code Structure
Current state is kept in a variable
State table is implemented as a switch
Cases define states
States can test inputs and produce outputs
while (TRUE) {
switch (state) {
case state1: …
}
}
in1=1/x=a
A B r=0/out2=1
r=1/out1=0
in1=0/x=b
s=0/out1=0 C D
s=1/out1=1
40
C State Table
switch (state) {
case A: if (in1==1) { x = a; state = B; }
else { x = b; state = D; }
break;
case B: if (r==0) { out2 = 1; state =
B; }
else { out1 = 0; state = C; }
break;
case C: if (s==0) { out1 = 0; state =
C; }
else { out1 = 1; state = D; }
break;
41
Outline
What is finite state machine?
Finite state machine for program modeling
The important concept of “state”
Finite state machine for circuit control
Finite state machine for embedded control
Finite state machine for recognition
Modeling with finite state machine
42
FSM as Recognizer/Translator
Outputs a ‘0’ if an even # of 1’s is received
and outputs a ‘1’ otherwise
What is state?
Two states: whether an even # of 1s have been
received, or an odd # of 1s have been received
Mealy Machine Moore Machine
0/0 0
Even Even
Input [0] Output
1/0 1/1
1 1
Output Input
Odd Odd
[1]
0/1 0
43
Finite State Machines
Language recognizer (acceptor)
y recognizer of yes, y in L
L
no
Problem solver
44
FSM as Recognizer/Translator
How to recognize words in a document?
Assume characters in the document are input
sequentially
What is state?
others/[word]
State 1 State 2
[a-z, A-Z]
others
[a-z, A-Z]
45
Example
A FSM that outputs a ‘1’ whenever it receives
a multiple of 3 # of 1’s on a serial input
Relevant information to solve the problem:
(A) A multiple of 3 # is received
(B) A non-multiple of 3 # is received
Questions to consider:
(1) How do we go from (A)(B)
Ans.: If a ‘0’ is received
(2) How do we go from (B)(A)
Ans.: Not clear; need to consider
(B1): 3y+1 # of 1’s received. y is an integer 0
(B2): 3y+2 # of 1’s received.
46
Example
Transitions between 3 classes of information:
(A) (B1) (B2) (A)
1 received 1 received 1 received
0/0 0
Mealy Machine Moore Machine
47
Outline
What is finite state machine?
Finite state machine for program modeling
The important concept of “state”
Finite state machine for circuit control
Finite state machine for embedded control
Finite state machine for recognition
Modeling with finite state machine
48
Scenario 1
入侵偵測系統:
某一房間的麥克風偵測到有不正常的聲音,即
提高偵測頻率,並通知房間內的攝影機追蹤聲
音的來源,且將影像傳到管理員手機。
同時,通知走廊及其他房間內的麥克風提高偵
測頻率,攝影機追蹤移動物體。
49
Scenario 2
大富翁:
What are the states?
50
Quiz
每天早上小明的爸爸開車送他到學校,然後到
公司上班。小明的媽媽處理家務。
每天下午下課時,小明的媽媽用機車來接他,
並送他去補習。
小明補完習,媽媽再來接他回家。
爸爸下班時間比較不固定,他直接開車回家。
51