3.4.10 · D3Sequential Circuits

Worked examples — Finite state machines (Mealy and Moore)

4,049 words18 min readBack to topic

Before anything, a tiny reminder of the notation we will lean on, so no symbol appears unearned:


The scenario matrix

Every FSM exam question is one of these cells. The examples below each carry a tag like [Cell A] so you can see the whole space is covered.

Cell Case class What makes it tricky
A Trace a Moore machine over a bit stream output is delayed one cycle
B Trace a Mealy machine over the same stream output appears same cycle; watch the last symbol
C Flip-flop count from the ceiling and its degenerate values
D Degenerate / edge inputs empty stream, all-zeros, all-ones, reset behaviour
E Convert Mealy Moore states multiply; where the extra state comes from
F Convert Moore Mealy states merge; output moves onto arrows
G Real-world word problem turnstile / vending controller
H Exam twist — overlapping vs non-overlapping detection one subtle transition arrow changes everything

Example 1 — Trace a Moore "detect 11" machine [Cell A]

Forecast: Guess when the first 1 appears in the output — will it line up with the second input 1, or lag behind it?

  1. Start in the reset state , output = 0. Why this step? Real hardware powers up unknown; the reset forces the reset state (defined in the symbols box above). Every trace must begin here or the answer is undefined.
  2. Read input, apply , then read the new state's output. Why this step? Moore output is — but the table convention is: we clock into the next state, then that state's bubble decides the output for the following cycle. Follow the arrows in the figure.

Figure — Finite state machines (Mealy and Moore)
Figure s01 — The Moore "detect 11" diagram. Read the picture like this: three violet bubbles ; the output digit sits inside each bubble (0 in and , and a bold 1 in the double-ringed magenta — the double ring is the standard "accepting/output-high" mark). The orange arrows labelled "in=1" march you rightward as consecutive 1s arrive; the magenta arrows labelled "in=0" all sweep you back to because any 0 breaks the run. The small orange self-loop on says "another 1 keeps you saturated in ." The navy arrow into marks it as the reset state. When you trace below, physically hop bubble-to-bubble along these arrows.

Walking it (state shown after each input, output is that state's bubble value):

step input state after output
1 1 0
2 0 0
3 1 0
4 1 1
5 1 1
  1. Read off the output stream: . Why this step? Once two 1s are seen we stay in as long as more 1s keep arriving (the self-loop in the figure), so the output stays high.

Verify: The input had its first 11 completing at position 4 (the pair at positions 3–4). Output first goes high at step 4 — the cycle the second 1 lands in . Sanity check: three total consecutive 1s (positions 3,4,5) should keep output high for the two "completed pairs" cycles (steps 4 and 5). ✓


Example 2 — Same stream, Mealy machine [Cell B]

Forecast: Will the Mealy 1 appear one cycle earlier than the Moore 1?

  1. Start in reset . Why? Same reason — known start state .
  2. For each input, the output is decided by the arrow we traverse, not by the state we land in. Why this step? This is the entire Mealy idea: output rides the transition, so the current input is allowed to influence it this cycle.

Figure — Finite state machines (Mealy and Moore)
Figure s02 — The Mealy "detect 11" diagram, only two bubbles (, ). Crucially, the output now lives on the arrows, written as input / output. The orange arrow reads "input 1 gives output 0 and moves to "; the magenta arrows labelled 0 / 0 reset you to ; the magenta self-loop 1 / 1 on is the money arrow — "already saw a 1, now see another 1, so emit 1 right now." As you trace, the digit you write down is the second half of the label on the arrow you walk, not anything inside a bubble.

step state input arrow taken arrow output next state
1 1 0
2 0 0
3 1 0
4 1 1
5 1 1
  1. Output stream: .

Verify: Compare with Example 1. The pattern is identical, but for a subtle reason tied to our clocking convention. Recall the convention: our Moore trace already prints 's bubble value in the same cycle the second 1 clocks it into (i.e. right at the rising edge that ends step 4), so both machines number their first output-1 at step 4. The genuine timing difference is within a cycle: the Mealy 1 at step 4 is produced combinationally, the instant the input is 1 while in — before the rising edge fires, so it is available early in the cycle; the Moore 1 only exists after that rising edge parks the register in , so it settles late in the cycle and is glitch-free. Same step number, different moment inside the cycle. ✓


Example 3 — Flip-flops for various state counts [Cell C]

Forecast: Does 8 states cost the same as 9? Does 1 state need zero flip-flops?

The tool is . Why and not, say, directly? Each flip-flop is one bit with 2 possible values, so flip-flops give distinct codes. We need ; solving for the smallest whole gives the ceiling of .

  1. : , . Why? One state needs no bit to distinguish it from anything — but real designs still keep at least 1 register for reset, so this is the theoretical floor.
  2. : .
  3. : . Why the jump? 1 flip-flop only gives 2 codes; 3 states need at least 2 bits (4 codes, one unused).
  4. : .
  5. : . Exactly filled — all 8 codes used.
  6. : . The exam trap: one extra state past a power of 2 forces a whole new flip-flop.

Verify (units + monotonic): must be a non-negative integer and never decrease as grows: — monotone non-decreasing ✓. Powers of two () sit right at the boundary: , ✓.

This directly settles the parent-note mistake "more states → more flip-flops": going states costs the same 2 flip-flops after rounding.


Example 4 — Degenerate inputs: empty, all-zeros, all-ones [Cell D]

Forecast: Which one produces the longest run of output 1s?

  1. (a) Empty stream: no inputs arrive, so no clock edge processes a symbol and the machine simply sits in its reset state . Because 's bubble carries output 0, the output the machine holds is , but the output stream (one digit per input symbol) is empty since there were zero symbols. Why? No transitions fire, so we never leave the reset bubble; and with no input symbols there is nothing to produce a digit for.
  2. (b) : every 0 sends us to (or keeps us in) . States: . Output . Why? A 0 breaks any run of 1s — this is the "reset toward " rule.
  3. (c) : states . Output . Why? First 1 only reaches (no pair yet); every subsequent 1 keeps us saturated in , which outputs 1.

Verify: Count of output-1s in (c) is , matching "four 1s make three overlapping pairs." (a) and (b) have zero 1s since no pair ever forms ✓. This confirms the degenerate all-ones case is the max-output case.


Example 5 — Convert Mealy → Moore [Cell E]

Forecast: Will the state count go up, and by how much?

The tool: state splitting. Why this tool? A Moore state carries a single fixed output. A Mealy state like can be entered with different outputs (you reach producing a 0 via , or via producing a 1). Moore cannot let one bubble emit two different outputs, so we must split into two Moore states — one that outputs 0, one that outputs 1.

Naming convention for the new Moore states: we write each Moore state as the pair (old Mealy state, the fixed output that state carries). So:

  • means — "the memory , carrying output 0."
  • means — "the memory , entered emitting 0."
  • means — "the memory , entered emitting 1."

Figure — Finite state machines (Mealy and Moore)
Figure s03 — The splitting move drawn out. On the left, the single violet (output 0). On the right, the old Mealy has become two bubbles: violet (output 0, reached by the orange "1" arrow from ) and double-ringed magenta (output 1, reached by the orange "1" arrow from and by its own self-loop). Notice both and send a magenta "0" arrow back to — that identical behaviour is the visual proof they are the same memory ("last bit was a 1"), differing only in the output digit each carries. The side note reminds you: = "entered emitting 0", = "entered emitting 1".

  1. List each (state, incoming-output) pair. is always entered producing 0 → one Moore state . is entered producing 0 (from on 1) → , and producing 1 (from on 1) → . Why? Each distinct output that could be "carried into" a state becomes its own bubble.
  2. Assign each Moore state its fixed output: , , .
  3. Rebuild transitions so they land in the correctly-outputting copy:
    • : on 0 → , on 1 →
    • : on 0 → , on 1 →
    • : on 0 → , on 1 → Why? and are the same memory ("last bit was a 1") but differ only in the output they emit — so their outgoing arrows are identical (see the figure).

Verify: This is exactly the 3-state Moore machine from the parent note with , , . Trace : — matches Example 1's one-cycle-delayed 1 ✓. State count went , confirming "Moore needs more states." Flip-flops: (Example 3), while Mealy needs — so here Moore actually costs one more flip-flop, because crosses the boundary. Good corner case! ✓


Example 6 — Convert Moore → Mealy [Cell F]

Forecast: Which two states collapse into one?

The tool: state merging — the reverse of splitting. Why? and have identical outgoing transitions (both go to on 0 and to on 1); they differ only in the constant output printed in their bubble. In Mealy we can encode that output difference on the arrow that entered them, freeing us to merge and into one memory state " = last bit was 1."

Figure — Finite state machines (Mealy and Moore)
Figure s04 — The merge drawn out. On the left the three Moore bubbles with the two right-hand ones highlighted; a violet dashed lasso groups and to show "same memory, different bubble output." On the right the collapsed Mealy machine: violet (was ) and violet (the merged ). The output that used to sit inside has slid onto the magenta self-loop — that is the arrow which formerly carried you into the output-1 bubble. The orange (was ) and the magenta reset arrows keep output 0.

  1. Identify mergeable states. Compare rows: goes and goes — identical destinations. They mean the same fact, "the last bit I read was a 1," so merge ; keep . Why? Two states with the same next-state behaviour are the same memory; their only difference (bubble output) can be pushed onto arrows in Mealy form.
  2. Move each output onto the arrow that reaches the merged state. In Moore, landing in produced output 1. The arrow that lands you in and comes from a state already holding a 1 is the old ; in the merged machine that is , so we label it output 1: . The arrow (formerly , landing in the output-0 bubble ) keeps output 0: . Every 0 arrow returns to with output 0. Why? The output must be emitted exactly when the Moore machine would have been sitting in the output-1 bubble , i.e. right as we complete the second consecutive 1 — that is the on input 1 transition.
  3. Result: exactly the 2-state Mealy machine of Example 2 (Figure s02), with , , and both resets.

Verify: Trace on the merged Mealy: → outputs . The Moore original on gave as well (Example 1's delayed pattern). Language identical, state count ✓. And a longer check — feed : merged Mealy gives , matching Example 2 exactly ✓.


Example 7 — Real-world word problem: a turnstile [Cell G]

Forecast: How many of the five actions actually let someone through?

  1. Choose the two states and their outputs. Moore output = "gate is open to walk through": , . Why this step? The gate's open/closed status is a property of where we are, not of the button just pressed — that is precisely the Moore rule , so it belongs inside the state, and we pick Moore for exactly that reason.

  2. Write the transition table.

    • Locked: coin → Unlocked, push → Locked
    • Unlocked: coin → Unlocked, push → Locked

    Why this step? Each row must answer "for this state, where does each possible input send me?" — the definition of . coin always ends Unlocked (money always unlocks); push always ends Locked (a push either passes you through and re-locks, or does nothing while locked). A coin while Unlocked stays Unlocked because it is "wasted" — no state change.

  3. Trace from reset Locked. Why this step? Hardware powers up unknown, so we must anchor at the defined reset state ; each row below applies then reads the landed state's bubble output (our Moore convention).

step input state after
1 push Locked 0
2 coin Unlocked 1
3 push Locked 0
4 push Locked 0
5 coin Unlocked 1

Why each row is what it is: step 1 pushes a locked gate → stays Locked (); step 2 coin → Unlocked (); step 3 push while unlocked → passes through and re-locks (); step 4 push a now-locked gate → stays Locked (); step 5 coin → Unlocked again ().

Verify (real-world sanity): Output is high (gate open) exactly after a coin: steps 2 and 5 → two people got through, matching the two coins inserted. Wasted pushes at steps 1,3,4 correctly produce 0. Units check: one coin ⟶ at most one passage ✓.


Example 8 — Exam twist: overlapping vs non-overlapping "111" [Cell H]

Forecast: How many 1 outputs does each flavour produce on five 1s?

States: =zero 1s so far, =one 1 so far, =two 1s so far. Arrow labels are input / output.

  1. (a) Overlapping. On the third consecutive 1, output 1 and stay in (so the just-matched 1 can be the start of the next overlap). The money arrow is . Why stay in ? Because after 111, the last two 1s ("11") are already a partial next match; staying in remembers "I still have two 1s in a row behind me."

Figure — Finite state machines (Mealy and Moore)
Figure s05 — Both 111 detectors on one diagram; the single arrow leaving on input 1 is the whole exam trap. Three violet bubbles are chained by orange arrows labelled 1 / 0 (building up 1s, no output yet). Then two competing arrows from : the magenta self-loop 1 / 1 (overlap) keeps you in and emits 1 — that is flavour (a); the violet sweeping arc 1 / 1 (non-overlap) back to also emits 1 but throws you to the start — that is flavour (b). Pick exactly one of those two arrows for your machine. (For clarity the "input 0" arrows, which all send you back to with output 0, are omitted.)

Trace using the magenta self-loop:

step state input arrow taken output next state
1 1 0
2 1 0
3 1 1
4 1 1
5 1 1

Outputs: three matches (the windows at positions 1–3, 2–4, 3–5).

  1. (b) Non-overlapping. On the third 1, output 1 but reset to — discard the shared bits. The money arrow is . Why reset to ? We are told matches may not share bits, so once 111 completes we throw everything away and rebuild a fresh run from zero.

Trace using the violet arc back to :

step state input arrow taken output next state
1 1 0
2 1 0
3 1 1
4 1 0
5 1 0

Outputs: one match (the first 111); the remaining two 1s only rebuild toward but never complete a second, non-overlapping triple.

Verify: Overlapping count = number of length-3 windows in a length-5 all-ones string = , all of which match ✓. Non-overlapping count = ✓. This is the classic exam trap: a single transition arrow ( vs ) flips the answer from 3 to 1.


Recall Quick self-test

Moore output for input 11 starting reset ::: (delayed one cycle). Flip-flops for 9 states ::: . Mealy → Moore state count for the 2-state detector ::: grows to 3 (split ). Overlapping 111 on five 1s: number of matches ::: 3. Non-overlapping 111 on five 1s: number of matches ::: 1.


Connections

  • Finite state machines (Mealy and Moore) — the parent note these examples drill
  • Sequence detectors — Examples 1, 2, 8 are the canonical sequence-detector drills
  • State encoding — Example 3's flip-flop counting is the first step of encoding
  • State minimization — Example 6's state merging is minimization in action
  • Flip-flops — the state register holding in every trace
  • Clocking and timing — why Moore's output lags one cycle (Examples 1 vs 2)