# Documentation

## Overview of Mealy and Moore Machines

### Semantics of Mealy and Moore Machines

Mealy and Moore are often considered the basic, industry-standard paradigms for modeling finite-state machines. Generally in state machine models, the next state is a function of the current state and its inputs, as follows:

`$X\left(n+1\right)=f\left(X\left(n\right),u\right)$`

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 is a combination of local data and chart activity. Therefore, computing state means updating local data and making transitions from a currently active state to a new state. State persists from one time step to another.

In this context, Mealy and Moore machines each have well-defined semantics.

Type of MachineSemanticsApplications
MealyOutput is a function of inputs and state:
`$y=g\left(X,u\right)$`
Clocked synchronous machines where state transitions occur on clock edges
MooreOutput is a function only of state:
`$y=g\left(X\right)$`
Clocked synchronous machines where outputs are modified at clock edges

You can create charts that implement pure Mealy or Moore semantics as a subset of Stateflow® chart semantics (see Create Mealy and Moore Charts). Mealy and Moore charts can be used in simulation and code generation with Embedded Coder®,Simulink® Coder™, and HDL Coder™ software, which are available separately.

### Model with Mealy and Moore Machines

Stateflow software ships with a model that shows how to use Mealy and Moore machines for sequence recognition in signal processing. To open the model, enter `sf_seqrec` at the MATLAB® prompt.

### Default State Machine Type

When you create a Stateflow chart, the default type is a hybrid state machine model that combines the semantics of Mealy and Moore charts with the extended Stateflow chart semantics. This default chart type is called Classic.

### Availability of Output

Mealy machines compute output on transitions, while Moore machines compute outputs in states. Therefore, Mealy charts can compute output earlier than Moore charts — that is, at the time the chart's default path 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). By contrast, Moore machines can compute outputs only after the default path executes. Until then, outputs take the default values.

### Advantages of Mealy and Moore Charts

Mealy and Moore charts offer the following advantages over Classic Stateflow charts:

• You can verify the Mealy and Moore charts you create to ensure that they 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, both 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. Therefore, 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 error with an algebraic loop.