Finite state machines

Download Report

Transcript Finite state machines

Finite state machines
Example -1
• Consider a very simple version of American vending
machine which provides chewing gum at a cost of 15
cents per packets, and candy bars at a cost of 20
cents each.
• The machine permits the use of nickels and dimes
only and does not give change.
• In addition to the obvious outputs, namely packets
of chewing gum and candy bars, we shall assume
that the machine makes reassuring clicks when
appropriate coins are inserted.
….. Example -1
• Let the input and output alphabets therefore can be
denoted as follows:
• Input alphabet = { 5, 10, Candy Bar Request, Gum
Request}
• Output alphabet = { Dispense Candy Bar, Dispense
Gum, click, 2 clicks}
• An FSM for the vending machine may now be
represented by the following state transition
diagram:
State Transition Diagram
Gum Request → Dispense Gum
Nickel
15 Cents
Zero
Dime
Candy bar Request → Dispense Candy bar
20 Cents
Example-2
• If you have ever used a lift, you have used a finite state machine
(FSM).
• The lift can be in one of a finite number of states at any moment.
Finite means countable. For example, one state for a lift serving a threestorey building is at the third floor with doors open. Another is
travelling between second and third floor with doors closed.
• The lift system has inputs and outputs. One input occurs when a
person calls the lift by pressing the lift request button on, say, the second
floor.
• One input occurs to indicate the position of the lift to the person
waiting on the second floor.
• For the lift to move in the desired floor, it must always know which
state it is currently in and what step to take next so it needs memory to
store the current state and a logic device to determine the next step.
Finite state machines
• When the lift arrives at the second floor, the logic device briefly turns on the door
motor open the doors. The logic device must also update the memory to reflect the
new state of lift stationary at the second floor with the doors open. Fig 1.3.1 shows a
model of this FSM.
Finite state machines
• A finite state machine is a machine that consists of a fixed set
of possible states, a set of allowable inputs that change the state, a
set of possible outputs. The outputs can depend only on the
current state, which depends only on the history of the sequence
of inputs.
• Many types of digitals machine, including general-purpose
computers, are FSMs. General-purpose computers advance their
state at regular intervals and the rate at which they advance is
called the clock rate of the machine. Each state is one step
towards the solution to a problem. Increasing the clock rate of a
computer should enable the computers to solve a problem more
quickly.
State transition diagram
• A ballpoint pen is example of an FSM:
 It has a finite number of states: ballpoint extended and ballpoint
retracted.
 It has set of allowable inputs: clicking the pen’s button.
 It has a set of possible outputs: retracting or extending the ballpoint.
• The behavior of ballpoint can be described by a state transition
diagram (fig1.3.2). The diagram consists of two states, 0 and 1, and two
transitions indicated by curved arrows. Each curved arrow is labeled
with the input that causes each transition, here button clicked.
• By repeatedly clicking the button (input), the pen alternates between
state 0 (ballpoint retracted) and state 1 (ballpoint extended); the outputs
is determined by which state the pen is in when the button is clicked.
State transition diagram
Worked example
• Consider a combination lock that opens only when it is
given the sequence326. Whether mechanical or electrical,
this is an FSM with the state transition diagram shown in
fig1.3.3. There are no outputs. The initial state of
combination lock is indicated by small entering arrow
pointing to the state ‘locked’ and it receives the input 3, the
lock moves to the state ‘first digit’. When it receives the
input 2, the lock moves to the state ‘second digit’. When it
receives the input 6, the lock, moves to the state ‘unlocked’.
At any stage, if a digit other than the required digit is
entered, the lock remains in its current state.
Worked example
• The final or halting state is reached when the combination lock is
unlocked. The state ‘unlocked’ is indicated by a double circle it is the
goal state. The goal state is known as the accepting stage.
FSM with outputs
• An FSN may produce a response when it makes a transition,
i.e. an output. This is represented as shown in Fig.1.3.4. If the
input is A when this machine is in state 1 then the machine
moves to state 2 and outputs “response”. This type of FSM is
known as a mealy machine. It does not have an accepting
state.
FSM with outputs
• This type of machine is used when we want to do more than
just recognize a sequence, such as the sequence for the
combination lock. We might want a machine starting in the
initial state S0 to translate a sequence of received 1s 0s and 2s
into plain text consisting of the letters A and B and
exclamation mark ! according to the following rule:
• The system begins receiving in the initial state S0.
• If the received data is 0 and system is in state S0’ output the
latter A.
• If the received digit is 1 and the system is in state is S0 change
state to S1 and output the latter B.
FSM with outputs
• If the received digit is 2 and the system is in state is S0,
change state to S2 and output the latter!.
• If the received digit is 0 and the system is in state is S1,
change state to S0 and output the latter A.
• If the received digit is 1 and the system is in state is S1,
and output the latter B.
• If the received digit is 2 and the system is in state is S1,
change state to S2 and output the character !.
• Figure 1.3.5 shows the corresponding state transition
diagram with outputs for this system.
FSM with outputs
The sequence 00101102 produces as output AABABBA! Check
this for yourself. What does the sequence 101012 produce as
output? Answer BABAB!
State Transition Table
• We can use a table that shows the state that follows for every
state and every input. For example, Table 1 summarizes the
ballpoint pen operation.
Sometimes the state transition table is just called the state table.
State Transition Table
• If an FSM has outputs, then the state transition table can also
shows the outputs. Table 2 shows the state transition diagram
in fig.1.3.5 check the table entries for yourself.
Decision Table
• A decision table is a precise yet compact way to model
complicated logic. Decision table make it easy to observe that
all possible conditions are accounted for.
• For example, some logic is written as follows:
• If X is greater than 6 and Y is less than 7
• Then output “Pass”
• Else Output “Fail”
• Table 3 shows the corresponding decision table.
Decision Table
Note that there are two subconditions in the conditions section of Table 3:
X>6
X<7
Decision Table
• When both conditions are true at the same time, the output is
pass .when either of the conditions is false or both are false,
the output is fail. The pass or fail output is indicated by X in
the corresponding table cell.
• Note that the rules rows are constructed in a systematic way.
For example, if there are two condition alternatives, then the
first row is constructed with YYNN and the second row with
YNYN.
Decision Table