Volume 02 Beginner 5 sub-modules ~25 min read

Moore, Mealy and Medvedev Machines

In Volume 01 the outputs came straight from the state. That is one way to make outputs, but not the only one. This volume builds one small circuit - a detector that pulses once when a button is pressed - as a Moore machine and as a Mealy machine. You will watch both on a timing diagram, convert one into the other, and finish with the Medvedev machine, whose outputs need no logic at all.

You will learn
  • How Moore, Mealy and Medvedev machines make their outputs
  • How to write outputs on a state diagram for each type
  • Why a Mealy output reacts a cycle earlier, and why it can glitch
  • How to convert a Mealy machine into a Moore machine, and back
  • How to choose the right type for a job
You need
  • Volume 01: states, transitions, diagrams and tables
  • Reading a timing diagram, from sub-module 0.4

2.1 Moore: output depends only on the state

In a Moore machine, the outputs depend only on the current state. Each state has its own fixed outputs.

One job, two designs

All through this volume we will build the same small circuit. It watches a button, and gives a pulse - a 1 for exactly one clock cycle - each time the button goes from not pressed (0) to pressed (1). It must not keep pulsing while the button is held down. A circuit like this is called a rising-edge detector. You will find one in almost every design that reads a button or a flag.

The input is btn. The output is pulse.

The Moore way

The turnstile and the traffic light in Volume 01 had something in common. Their outputs came from the state alone. LOCKED meant locked = 1. GREEN meant the green lamp was on. A machine built this way is called a Moore machine, after Edward F. Moore, who described it in 1956.

In plain words

In a Moore machine, you can read the outputs just by knowing the state. The inputs decide where the machine goes next - but they never change the outputs directly.

Now think about what the edge detector must remember. It needs to tell apart three situations:

  1. ZERO - the button is up. Output 0.
  2. EDGE - the button has just gone down, one cycle ago. Output 1. This state exists only to make the pulse.
  3. ONE - the button has been held for more than one cycle. Output 0, so there is no second pulse.

Because the output belongs to the state, a Moore diagram writes it inside the circle, under the state's name. Figure 2.1 shows the edge detector as a Moore machine.

Moore state diagram of a rising-edge detector with states ZERO, EDGE and ONE ZERO pulse=0 EDGE pulse=1 ONE pulse=0 1 0 1 0 0 1 reset
Figure 2.1 - The edge detector as a Moore machine. Each state shows its output under its name. The green circle, EDGE, is the only state where pulse = 1. The arrows carry only the input value of btn.

Follow a press through the diagram. The machine sits in ZERO. The button goes down, so btn = 1, and at the next edge the machine moves to EDGE - pulse is now 1. The button is still down, so at the next edge the machine moves to ONE - pulse drops to 0. It stays in ONE until the button comes up, then returns to ZERO.

The Moore state table

In a Moore table, the output column depends only on the current state, so it is the same in every row for that state:

Current state btn Next state pulse
ZERO 0 ZERO 0
ZERO 1 EDGE 0
EDGE 0 ZERO 1
EDGE 1 ONE 1
ONE 0 ZERO 0
ONE 1 ONE 0
Remember

In a Moore machine, outputs change only when the state changes - that is, only just after a clock edge.

Common mistake

Do not write an output on an arrow of a Moore diagram. The arrow is taken during a move, but a Moore output belongs to the state you are in. If you find yourself wanting to put an output on an arrow, you are designing a Mealy machine - the next sub-module.

Quick check

Why does the Moore edge detector need the EDGE state?

Show the answer

Answer: B. A Moore output belongs to a state. The pulse must be 1 for exactly one cycle, so the machine needs one state where pulse = 1, and it must pass through that state for exactly one cycle. That state is EDGE.

2.2 Mealy: output depends on state and input

In a Mealy machine, the outputs depend on the current state and on the current inputs. So the output is written on the arrow, next to the input that causes it.

A second way to make outputs

In 1955, George H. Mealy described another kind of state machine. In a Mealy machine, the output logic looks at the inputs as well as the state. The same state can then give different outputs for different inputs.

That changes what the edge detector needs to remember. It only has to know one thing: was the button down last cycle, or not? Two states are enough:

The pulse comes from a simple rule: if the machine is in ZERO and btn is 1, then pulse is 1. "In ZERO" means the button was up; "btn is 1" means it is down now. Together, that is exactly a rising edge.

Outputs on the arrows

A Mealy output depends on the state and the input together - which is exactly what an arrow stands for. So a Mealy diagram labels each arrow as input / output. The label 1 / 1 means: "if btn is 1, go this way, and make pulse 1 while you do".

Mealy state diagram of a rising-edge detector with states ZERO and ONE ZERO ONE 1 / 1 0 / 0 0 / 0 1 / 0 reset
Figure 2.2 - The edge detector as a Mealy machine. Each arrow reads input / output. Only the arrow from ZERO to ONE has pulse = 1 - that is the rising edge.

The Mealy table has an output column that can differ from row to row within one state:

Current state btn Next state pulse
ZERO 0 ZERO 0
ZERO 1 ONE 1
ONE 0 ZERO 0
ONE 1 ONE 0

In ZERO, pulse is 0 in one row and 1 in the other. That could never happen in a Moore table.

What changes in the hardware

Figure 2.3 puts the two kinds side by side. They are identical except for one wire.

Block diagrams of a Moore machine and a Mealy machine; the Mealy machine has an extra path from the inputs to the output logic MOORE outputs depend on the state only MEALY outputs depend on the state and the inputs Next-state logic State register Output logic Next-state logic State register Output logic inputs inputs outputs outputs state state clk clk the input also reaches the output logic
Figure 2.3 - Moore (top) and Mealy (bottom). The only difference is the red dashed wire: in a Mealy machine, the inputs also reach the output logic, skipping the state register.

That one extra wire gives a Mealy machine two strengths and two weaknesses:

Moore Mealy
Outputs depend on the state only the state and the inputs
States for the edge detector 3 2
When the output reacts one cycle after the input in the same cycle as the input
Can an input glitch reach the output? No Yes
Path from input to output through the state register straight through gates

The last row matters most. A path that goes only through gates, with no flip-flop on the way, is called a combinational path. In a Mealy machine, a change on an input races straight through to the output within the same cycle.

Think of it like this

A Moore machine is like a notice board that is updated only at the top of each hour. Whatever happens during the hour, the board shows the same message until the next update. A Mealy machine is like a person at a help desk. They answer you the moment you speak, based on what they already know and what you just said. The answer comes faster - but if you say something by mistake, they react to that too.

Common mistake

It is easy to think a Mealy output is stored somewhere. It is not. It is computed by gates, all the time, from the state and the inputs. If an input wobbles, the output wobbles with it.

Quick check

In Figure 2.2, the machine is in ONE and btn = 1. What are the next state and the output?

Show the answer

Answer: C. Look at the arrows leaving ONE. The one for btn = 1 is the loop labelled 1 / 0. So the machine stays in ONE, and pulse is 0 - the button is being held, which is not a new edge.

2.3 Seeing the difference on a waveform

On a timing diagram, a Mealy output reacts in the same clock cycle as its input. A Moore output reacts one cycle later.

Figure 2.4 runs both edge detectors on the same button press. The top half is the Moore machine from Figure 2.1. The bottom half is the Mealy machine from Figure 2.2.

Timing diagram comparing the Moore and Mealy rising-edge detectors on the same button press 0 1 2 3 4 5 6 7 clk btn moore_state ZERO EDGE ONE ZERO moore_pulse mealy_state ZERO ONE ZERO mealy_pulse
Figure 2.4 - The same press, two machines. The Mealy pulse rises as soon as btn rises, in cycle 2. The Moore pulse appears one cycle later, in cycle 3, after the state has moved to EDGE.

Walk through it:

  1. Just after edge 2, btn rises. The Mealy machine is in ZERO and now sees btn = 1, so its gates make mealy_pulse = 1 at once, in the same cycle.
  2. At edge 3, both machines see btn = 1. The Mealy machine moves to ONE, so mealy_pulse drops back to 0. The Moore machine moves to EDGE, so moore_pulse rises now.
  3. At edge 4, the Moore machine moves on to ONE, and moore_pulse drops back to 0.
  4. At edge 6, btn has been released. Both machines return to ZERO, ready for the next press.

Both pulses are exactly one cycle long. The only difference is when they happen: the Mealy pulse comes one cycle earlier. The number of cycles a circuit takes to respond is called its latency. Here the Mealy detector has a latency of zero cycles, and the Moore detector has a latency of one.

The price of speed: glitches

Now suppose the button signal flickers for a moment, in the middle of a cycle. Perhaps a wire picked up some noise. Figure 2.5 shows what each machine does.

Timing diagram showing a short input flicker between clock edges: the Mealy output copies it and the Moore output ignores it 0 1 2 3 4 5 clk btn state ZERO mealy_pulse moore_pulse
Figure 2.5 - A short flicker on btn between two edges. The Mealy output copies it, because gates pass it straight through. The Moore output never sees it, because no clock edge happens during the flicker.

The flicker never lasts until a clock edge, so neither machine changes state. The Moore output comes only from the state, so it stays at 0. But the Mealy output is pulse = (state is ZERO) AND btn. The machine is in ZERO, so pulse simply copies btn - flicker and all. That short, unwanted pulse is a glitch. If pulse drives another circuit that reacts at once - a counter with its own clock, say - the glitch can do real damage.

Remember

Mealy: earlier, but it passes on input glitches. Moore: one cycle later, but steady between clock edges.

Going deeper: the best of both - a registered Mealy output

Designers often take a Mealy output and pass it through one extra flip-flop. The flip-flop only copies the value at a clock edge, so the glitch disappears. The cost is one cycle of delay - which brings the timing back to the Moore timing. An output that comes straight from a flip-flop is called a registered output. Volume 04 shows how to code one, and Volume 13 explains why industry prefers them.

Quick check

A Moore machine and a Mealy machine do the same job. The input changes just after clock edge 5. When does each output react?

Show the answer

Answer: A. The Mealy output logic sees the input directly, so it reacts in the same cycle. The Moore output waits for the state to change, and the state changes only at the next edge - edge 6.

2.4 Converting Mealy to Moore and back

Any Mealy machine can be rebuilt as a Moore machine that gives the same outputs one cycle later, and any Moore machine can be rebuilt as a Mealy machine.

You do not have to choose the type before you start. You can design in whichever form feels natural, then convert. The conversion is a fixed recipe, and it explains exactly why the two types differ.

Mealy to Moore: split the states

In a Moore machine every state has one fixed output. So look at each Mealy state, and at the outputs on the arrows that enter it. If they all carry the same output, the state can keep it. If they carry different outputs, the state must be split into copies - one per output value.

  1. List each state, and the output value on every arrow that enters it.
  2. If all those outputs are equal, keep the state and give it that output.
  3. If they differ, make one copy of the state for each output value.
  4. Each old arrow now points to the copy whose output matches the arrow's output.
  5. Give every copy the same outgoing arrows as the original state had.

Let us convert the Mealy edge detector from Figure 2.2:

State Arrows entering it, with their outputs Result
ZERO from ZERO: 0 / 0, and from ONE: 0 / 0 all outputs are 0 - keep ZERO, output 0
ONE from ZERO: 1 / 1, and from ONE: 1 / 0 outputs differ - split into two copies

ONE splits into a copy with output 1 and a copy with output 0. Call the first copy EDGE and keep the name ONE for the second. The arrow ZERO --1 / 1--> ONE now goes to EDGE, and the loop ONE --1 / 0--> ONE goes to the copy named ONE. Both copies keep ONE's outgoing arrows: on btn = 1 go to ONE, and on btn = 0 go to ZERO.

The result has three states - ZERO, EDGE and ONE - with exactly the arrows of Figure 2.1. The recipe has rebuilt, step by step, the Moore machine we designed by hand.

Remember

Mealy to Moore can only add states. In the worst case, each Mealy state splits into as many copies as there are different output values.

Moore to Mealy: move each output onto the arrows

Going the other way is simpler. Keep all the states. Then, on every arrow, write the output of the state the arrow points to. Here is the Moore edge detector from Figure 2.1, converted:

From btn To Output written on the arrow
ZERO 0 ZERO 0 (the output of ZERO)
ZERO 1 EDGE 1 (the output of EDGE)
EDGE 0 ZERO 0
EDGE 1 ONE 0
ONE 0 ZERO 0
ONE 1 ONE 0

Now look at EDGE and ONE. On btn = 0 both go to ZERO with output 0. On btn = 1 both go to ONE with output 0. They behave exactly alike, so they can be merged into one state. Merge them, and you get the two-state Mealy machine of Figure 2.2. Finding states that behave alike, and merging them, is called state minimisation - it has a whole volume of its own, Volume 10.

Common mistake

The two versions are not identical in time. The Moore version gives each output one cycle later than the Mealy version. If another circuit expects the answer in a particular cycle, converting one type to the other changes your design. Always check the timing after a conversion.

Quick check

In a Mealy machine, three arrows enter state S. Their outputs are 0, 1 and 1. When you convert to Moore, how many states does S become?

Show the answer

Answer: C. Count the different output values on the entering arrows, not the arrows. There are two values, 0 and 1, so S splits into two copies: one with output 0 and one with output 1.

2.5 Medvedev, and choosing the right type

A Medvedev machine is a Moore machine whose outputs are the state bits themselves, with no output logic at all.

Outputs with no logic

A Moore machine still needs output logic: gates that turn the state bits into outputs. Sometimes you can remove even those gates. If you choose the state's bit patterns cleverly, the state is the output. A machine like this is called a Medvedev machine.

Take the traffic light from Volume 01. It has three states and three lamps. Give it a state register of three flip-flops, one per lamp, and choose these bit patterns:

State State bits (red, yellow, green) Lamps that are on
RED 100 red
YELLOW 010 yellow
GREEN 001 green

Now wire each flip-flop straight to its lamp, as in Figure 2.6. There is nothing left to decode.

A Medvedev traffic light: three state flip-flops each drive one lamp directly, with no output logic Next-state logic red bit yellow bit green bit done clk red yellow green no output logic
Figure 2.6 - A Medvedev traffic light. Each bit of the state register drives one lamp directly. Choosing one bit per state like this is called one-hot encoding - Volume 05 covers it in full. The feedback path to the next-state logic is not drawn.

A Medvedev machine has the cleanest possible outputs. They change exactly at the clock edge, together, and they can never glitch - there are no gates in between to cause one. The price is that the outputs choose your state encoding for you, and that may cost extra flip-flops. Three flip-flops for three states is more than the two a counting code would need.

Choosing the right type

Moore Mealy Medvedev
Outputs come from the state, through gates the state and the inputs, through gates the state bits directly
Output timing one cycle after the input same cycle as the input one cycle after the input
Glitches on outputs rare possible never
Number of states sometimes more fewest as Moore
Good for most control logic fast replies within one cycle outputs that leave the chip or feed other clocks

Some simple rules to start with:

Common mistake

"Mealy is always better, because it is faster and smaller." It is faster by one cycle, and it is often smaller. But it also joins the input to the output with a combinational path. Put several Mealy machines in a row, and those paths join into one long chain of gates - which can make the whole chip run slower. Speed has to be judged for the whole design, not one machine.

Quick check

Which kind of state machine has no output logic at all?

Show the answer

Answer: D. In a Medvedev machine, the state bits are the outputs, wired straight out of the state register. A Moore machine usually needs gates to decode its state into outputs, and a Mealy machine also needs gates to mix in the inputs.

Quick check

An output leaves the chip and drives a motor controller. Which is the safest choice?

Show the answer

Answer: B. A glitch on a signal that leaves the chip can reach another device, which may react to it. An output that comes straight from a flip-flop changes only at clock edges, so it cannot glitch.

What you learned

Key words from this volume

Every word below has a plain-English entry in the glossary.

Practice

Practice 1

Mealy to Moore

This Mealy machine gives z = 1 when it sees two 1s in a row on its input x:

Current state x Next state z
A 0 A 0
A 1 B 0
B 0 A 0
B 1 B 1

Convert it to a Moore machine, and draw the diagram.

Show the solution

Look at the arrows entering each state:

  • A is entered by A --0/0--> A and B --0/0--> A. All outputs are 0, so A stays as it is, with z = 0.
  • B is entered by A --1/0--> B and B --1/1--> B. The outputs differ, so B splits into B0 (z = 0) and B1 (z = 1).

The arrow from A on x = 1 carried output 0, so it goes to B0. The loop on B for x = 1 carried output 1, so it goes to B1 - from both copies. On x = 0, both copies return to A.

Moore state diagram of a detector for two 1s in a row, with states A, B0 and B1 A z=0 B0 z=0 B1 z=1 1 0 1 0 0 1 reset
Figure 2.7 - The Moore version has three states. B1 is the only state with z = 1.

As always, the Moore version answers one cycle later: z rises one edge after the second 1 is seen, not during it.

Practice 2

Which type is it?

For each machine, say whether it is Moore or Mealy.

  1. A vending machine lights "Thank you" in the same cycle as the last coin drops in.
  2. A washing machine's "door locked" lamp is on in the states FILL, WASH and DRAIN.
  3. A diagram has arrows labelled 0 / 1 and 1 / 0.
Show the solution
  1. Mealy. The output reacts in the same cycle as the input, so the output logic must see the coin input directly.
  2. Moore. The lamp depends only on which state the machine is in.
  3. Mealy. Outputs are written on the arrows, as input / output.
Practice 3

Predict both outputs

Use the two edge detectors of this volume. The input btn is 0 in cycles 0 and 1, and 1 in cycles 2 and 3. It is 0 in cycle 4, and 1 again in cycles 5, 6 and 7. In which cycles is each pulse output 1?

Show the solution

There are two rising edges: btn rises in cycle 2, and again in cycle 5.

  • Mealy: pulse is 1 in the same cycle as each rise - cycles 2 and 5.
  • Moore: pulse is 1 one cycle later, while the machine sits in EDGE - cycles 3 and 6.

In both machines the pulse lasts exactly one cycle, even though the second press is held for three cycles.

Interview corner

Interview question 1

Moore or Mealy?

"What is the difference between a Moore and a Mealy machine, and which would you use?"

Show the solution

"In a Moore machine the outputs are a function of the current state only. In a Mealy machine they are a function of the current state and the current inputs. So a Mealy machine reacts one cycle earlier, and often needs fewer states. But it creates a combinational path from its inputs to its outputs, so input glitches can reach the outputs, and the paths can make timing harder. I default to Moore, or to a Mealy machine with a registered output. I use a plain Mealy output only when I need the answer in the same cycle, and the inputs are clean."

Interview question 2

An edge detector without a state machine?

"Design a rising-edge detector. Do you even need a state machine?"

Show the solution

Look again at the two-state Mealy machine. Its state is simply "was btn 1 last cycle?" - so its state register is one flip-flop that holds the previous value of btn. The whole design is:


reg btn_prev;                        // the state: btn one cycle ago
always @(posedge clk)
  btn_prev <= btn;                   // remember this cycle's value
assign pulse = btn & ~btn_prev;      // 1 only when btn is 1 now and was 0 before

So the answer is: yes, it is a state machine - a two-state Mealy machine - but it is so small that it is written as one flip-flop and one AND gate. Saying this out loud shows the interviewer that you see the state machine hiding inside a common circuit.

Next, Volume 03 takes a state machine all the way down to gates and flip-flops, by hand - the skill behind every state machine question in a written exam.