Overview of Mealy and Moore Machines
In a finite state machine, state is a combination of local data and chart activity. "Computing state" means updating local data and making transitions from a currently active state to a new state. In state machine models, the next state is a function of the current state and its inputs:
In this equation:
X(n) represents the state at time step n.
X(n+1) represents the state at the next time step n+1.
u represents inputs.
State persists from one time step to the next time step.
Semantics of Mealy and Moore Machines
Mealy and Moore machines are often considered the basic, industry-standard paradigms for modeling finite-state machines. You can create charts that implement pure Mealy or Moore semantics as a subset of Stateflow® chart semantics. You can use Mealy and Moore charts in simulation and code generation with Embedded Coder®, Simulink® Coder™, and HDL Coder™ software. Mealy and Moore semantics are not supported in standalone Stateflow charts in MATLAB®.
Semantics of Mealy Charts
Mealy machines are finite state machines in which transitions occur on clock edges. The output of a Mealy chart is a function of inputs and state:
At every time step, a Mealy chart wakes up, evaluates its input, and then transitions to a new configuration of active states, also called its next state. The chart computes its output as it transitions to the next state.
To ensure that output is a function of input and state, Mealy state machines enforce these semantics:
Outputs do not depend on the next state.
The chart computes outputs only in transitions, not in states.
The chart wakes up periodically based on a system clock.
Mealy machines compute their output on transitions. Therefore, Mealy charts can compute their first output at the time that the default path for the chart executes. If you enable the chart property Execute (enter) Chart At Initialization for a Mealy chart, this computation occurs at t = 0 (first time step). Otherwise, it occurs at t = 1 (next time step). For more information, see Execute (enter) chart at initialization.
Semantics of Moore Charts
Moore machines are finite state machines in which output is modified at clock edges. The output of a Moore chart is a function only of state:
At every time step, a Moore chart wakes up, computes its output, and then evaluates its input to reconfigure itself for the next time step. For example, after evaluating its input, the chart can transition to a new configuration of active states. The chart computes its output before evaluating its input and updating its state.
To ensure that output is a function only of the current state, Moore state machines enforce these semantics:
Outputs do not depend on inputs.
Outputs do not depend on previous outputs.
Outputs do not depend on temporal logic.
Moore machines compute their output in states. Therefore, Moore machines can compute outputs only after the default path executes. Until then, outputs take the default values.
Create Mealy and Moore Charts
When you create a Stateflow chart, the default type is a hybrid state machine model called a Classic chart. Classic charts combine the semantics of Mealy and Moore charts with the extended Stateflow chart semantics.
To create a Mealy chart, at the MATLAB command prompt, enter:
sfnew -Mealy
To create a Moore chart, at the MATLAB command prompt, enter:
sfnew -Moore
Alternatively, after adding a Stateflow chart block to a Simulink model, you can choose the type of semantics for the chart by setting the State Machine Type chart property. For more information, see State Machine Type.
Advantages of Mealy and Moore Charts
Mealy and Moore charts offer these advantages over Classic Stateflow charts:
You can verify that the Mealy and Moore charts you create conform to their formal definitions and semantic rules. Error messages appear at compile time (not at design time).
Moore charts provide a more efficient implementation than Classic charts for C/C++ and HDL targets.
You can use a Moore chart to model a feedback loop. In Moore charts, inputs do not have direct feedthrough. You can design a loop with feedback from the output port to the input port without introducing an algebraic loop. Mealy and Classic charts have direct feedthrough and produce an error in the presence of an algebraic loop.