Finite State Machine: Varshini Narayana
Finite State Machine: Varshini Narayana
A Finite State Machine (FSM) is a computational model used to design both computer programs
and sequential logic circuits. It is called "finite" because it consists of a finite number of states.
An FSM is an abstract machine that can be in exactly one of a finite number of states at any given
time. The FSM can change from one state to another in response to some inputs; the change from
one state to another is called a transition. An FSM is defined by a list of its states, its initial state,
and the conditions for each transition.
Varshini Narayana
DIFFERENT TYPES OF FSMS
Deterministic Finite Automaton (DFA): In a DFA, for each state, there is exactly one transition for
each possible input. This means that, given the current state and input, there is no ambiguity as to
what the next state will be. This is the simpler type of FSM, used in many applications such as
regular expressions and lexical analysis.
Nondeterministic Finite Automaton (NFA): An NFA allows for multiple (or zero) transitions for a
specific state and input combination. This means that, given the current state and input, the
machine could move to any of several possible next states. NFAs are useful in theoretical computer
science for proving theorems and properties of languages.
FSMs are widely used in various fields, such as computer science, linguistics, computer
engineering, mathematics, and logic. They are fundamental in many computational tasks like
parsing text, network protocols, and the design of digital circuits.
FSMs are a fundamental concept in computer science and provide a simple and powerful way to
model and analyze the behavior of systems. They are particularly useful in situations where the
system must make a series of decisions based on a sequence of events.
Varshini Narayana
DIFFERENCE BETWEEN MOORE & MEALY MACHINE
§ State-Based Outputs: In a Moore machine, outputs depend only on the current state. In
contrast, Mealy machine outputs depend on both the current state and the current input.
§ Output Stability: Moore machines exhibit more output stability as outputs change only at
state transitions. Mealy machines can have outputs that change abruptly in response to
input changes.
§ Number of States: Moore machines often require more states to achieve the same
functionality as a Mealy machine because the output is state-dependent.
§ Speed of Response: Mealy machines can have a quicker response to changes in input as
their outputs can change without needing state transitions.
§ Output Signal: In Moore machines, each state has a fixed output, whereas, in Mealy
machines, outputs can differ for the same state based on different inputs.
Varshini Narayana
§ Complexity of Output Logic: The output logic in Moore machines is generally simpler
compared to Mealy machines.
§ Memory Requirement: Moore machines might require more memory due to the increased
number of states.
§ Design: The state diagram of a Moore machine is typically simpler than that of a Mealy
machine.
§ Predictability: Moore machines offer more predictability in output behavior as the output
is only state-dependent.
§ Implementation: Moore machines are generally easier to implement in hardware due to
their simpler logic and predictability. Mealy machines, however, may require more
complex hardware implementation.
Varshini Narayana
APPLICATIONS OF FSM
§ FSMs are used in software development for managing states in applications, such as user
interface control, game development, language parsing, and protocol design.
§ In embedded systems, FSMs control operational sequences, like in washing machines,
elevators, or traffic lights.
§ They are fundamental in compilers and interpreters, particularly for lexical analysis and
parsing, where regular expressions are often modeled as FSMs.
§ FSMs model the behavior of network protocols, managing different states of a connection
like establishment, data transfer, and termination.
§ In hardware, FSMs are used for designing sequential logic circuits, such as counters, shift
registers, and control units in microprocessors.
§ FSMs are used in the design and implementation of protocols and signal processing.
§ Simple AI in video games, like enemy behavior, can be managed using FSMs.
§ Basic robot behaviors and decision-making processes are often modeled using FSMs.
§ FSMs are used for parsing text inputs and validating formats like email addresses or phone
numbers.
§ They can be used to manage business processes and workflows, ensuring that each step is
executed in the correct order.
Varshini Narayana
LIMITATIONS OF FSM
§ FSMs can become overly complex and hard to manage with an increasing number of states
and transitions, leading to state explosion.
§ Traditional FSMs have no memory of past states, except for the current state, which limits
their use in applications requiring a history of state changes.
§ FSMs may not scale well for complex systems with numerous states and transitions.
§ FSMs are not suitable for systems requiring complex decision-making capabilities beyond
simple state transitions.
§ Once an FSM is designed and implemented, making changes can be challenging, especially
in hardware implementations.
§ FSMs are less effective in scenarios where outcomes are probabilistic or non-deterministic.
§ FSMs traditionally handle one state at a time, making them less suitable for systems
requiring parallel state handling.
§ For certain applications, other computational models may be more efficient than FSMs.
§ FSMs do not naturally support hierarchical state organization, which can be a limitation
for complex systems.
§ Traditional FSMs do not have an inherent concept of time, which can be a limitation for
time-dependent applications.
Varshini Narayana
HANDLING EXCEPTIONS OR ERROR STATES IN A FINITE STATE MACHINE
§ Define Error States: Specifically define error or exception states in the FSM. These states
represent situations where something unexpected or undesirable has happened.
§ Transition to Error States: Create transitions that lead to these error states when an
exception or error condition is detected. These transitions can be triggered by various
events or conditions that are considered erroneous.
§ Error Handling Logic: In the error states, implement logic to handle the error. This could
involve logging the error, performing cleanup tasks, resetting certain parts of the system,
or triggering alerts.
§ Recovery Mechanisms: Provide a way for the FSM to recover from the error state. This
could be a transition back to a safe or initial state, or to a state that attempts to correct the
error.
§ Guard Conditions: Use guard conditions on transitions to prevent the FSM from entering
invalid states. These conditions check for certain criteria before allowing a state transition.
§ Timeouts: Implement timeouts for states where the FSM might get stuck. If the FSM
remains in a specific state longer than expected, it can transition to an error state.
§ Monitoring and Alerts: Continuously monitor the FSM's performance and state. If it enters
an error state, alerts can be triggered to notify relevant personnel or systems.
§ State Consistency Checks: Regularly perform consistency checks within states to ensure
the FSM is operating as expected. If inconsistencies are found, transition to an error state.
§ Testing and Validation: Thoroughly test the FSM, including its error handling capabilities,
to ensure it behaves correctly under various scenarios, including failure conditions.
§ Documentation: Document error states, transitions, and handling logic clearly, so that
anyone working with the FSM can understand how errors are managed.
By incorporating these strategies, an FSM can be made robust and capable of handling exceptions
and errors in a controlled and predictable manner.
Varshini Narayana
IMPORTANT TERMS RELATED TO FINITE STATE MACHINES (FSM)
1. Trigger: A trigger in an FSM is an event or condition that causes the state machine to
transition from one state to another. Triggers are usually external inputs or actions that
initiate a state change.
2. Transition: A transition in an FSM is the movement from one state to another. Transitions
are defined by a specific trigger and possibly additional conditions. They represent the
reaction of the FSM to different inputs or events.
3. State Diagram: A state diagram is a graphical representation of an FSM. It shows all the
states, the transitions between them, and the triggers that cause these transitions. State
diagrams are useful for visualizing the behavior and possible states of an FSM.
4. Superstate: A superstate (or composite state) is a state that encompasses multiple other
states. It represents a higher level of abstraction and is used to simplify complex state
diagrams by grouping related states. Actions and transitions defined at the superstate level
apply to all its substates.
5. Guard Conditions: Guard conditions are Boolean expressions or criteria that must be met
for a transition to occur. They are used to add more control over when a transition should
happen, ensuring that certain conditions are satisfied before the FSM can move to the next
state.
6. Deadlock: Deadlock in an FSM occurs when it reaches a state or a set of states from which
no further transitions are possible, and it cannot progress any further. This situation is
usually undesirable and indicates a problem in the design of the FSM.
7. Determinism: Determinism in an FSM means that for each state and input, there is a clearly
defined next state. A deterministic FSM has no ambiguity in its state transitions - given the
current state and input, the next state is always uniquely determined.
8. Epsilon Transitions: Epsilon transitions are transitions that occur without any external
triggers or inputs. They allow the FSM to change its state spontaneously. In formal FSM
notation, these transitions are often represented by the Greek letter epsilon (ε). They are
useful for modeling situations where a state change occurs internally within the system
without any external event.
Varshini Narayana
EXAMPLES FOR DESIGNS OF A FINITE STATE MACHINE
Varshini Narayana
4. Detect a sequence 11001100 (non-overlapping)
Varshini Narayana