Designing a State Machine by Hand
A state diagram says what a machine should do. This volume shows how to build it from real parts - flip-flops and gates - with nothing but pencil and paper. It is the method behind every state machine question in a written exam, and it shows you what your Verilog code turns into.
- How many flip-flops a machine needs, and how to give each state a code
- The excitation tables of the D, T, JK and SR flip-flops
- How to fill in and group a Karnaugh map, from zero
- How to find the output logic of Moore and Mealy machines
- A full design from a story to gates, done two ways
- Volume 02: Moore and Mealy machines
- Truth tables and gates, from sub-module 0.2
3.1 Giving states binary codes
To build a state machine from flip-flops, give each state its own pattern of bits, called its code. The number of flip-flops is the number of bits in the code.
A flip-flop stores one bit. So a state register can only hold patterns of bits - it cannot hold the word "LOCKED". Before we can build anything, every state needs a bit pattern of its own. Choosing those patterns is called state assignment.
How many flip-flops?
In Volume 00 you learned that n bits make 2n patterns. We need at least one pattern per state:
| Number of states | Flip-flops needed | Why |
|---|---|---|
| 2 | 1 | 1 bit gives 2 patterns |
| 3 or 4 | 2 | 2 bits give 4 patterns |
| 5 to 8 | 3 | 3 bits give 8 patterns |
| 9 to 16 | 4 | 4 bits give 16 patterns |
With binary codes, the number of flip-flops is the smallest n for which 2n is at least the number of states.
Naming the bits
We call the flip-flops Q1 and Q0, and write a code with Q1 on the left. So the code 10 means Q1 = 1 and Q0 = 0. The value a bit will have after the next clock edge is written with a small plus: Q1+, spoken "Q1 next".
An example: the edge detector
The Moore edge detector from Volume 02 has three states, so it needs two flip-flops. Here is one choice of codes:
| State | Code (Q1 Q0) |
|---|---|
| ZERO | 00 |
| EDGE | 01 |
| ONE | 10 |
| no state | 11 |
Now rewrite the state table with codes in place of names. This is the encoded state table:
| Q1 Q0 | btn | Q1+ Q0+ | pulse |
|---|---|---|---|
| 00 | 0 | 00 | 0 |
| 00 | 1 | 01 | 0 |
| 01 | 0 | 00 | 1 |
| 01 | 1 | 10 | 1 |
| 10 | 0 | 00 | 0 |
| 10 | 1 | 10 | 0 |
| 11 | 0 | XX | X |
| 11 | 1 | XX | X |
The code 11 belongs to no state. It is an unused code. The machine should never be there, so we do not care what the logic does with it. We write X, which you will learn to use in sub-module 3.3.
Does the choice of codes matter?
Yes. Every choice works, but different codes give different amounts of logic. Two rules of thumb help when designing by hand:
- Give the reset state the all-zeros code. Resetting then means "clear every flip-flop".
- Where the machine moves from one state to the next along its main path, choose codes that differ in only one bit.
Volume 05 is all about encodings, including the one-hot code you met with the Medvedev traffic light.
Do not forget the unused codes. Three states in two flip-flops leave one code - here 11 - that belongs to no state. It still exists in the hardware, and a strong glitch could one day put the machine there. For now we mark it "don't care". Volume 11 shows how to make sure the machine can never get stuck in it.
A machine has 6 states. With binary codes, how many flip-flops does it need, and how many codes are unused?
Show the answer
Answer: A. 2 flip-flops give only 4 codes, too few for 6 states. 3 flip-flops give 8 codes, and 6 of them are used, so 2 are left over.
3.2 Flip-flop excitation tables (D, T, JK, SR)
An excitation table answers a reverse question: "Q must change from this value to that value - what must the flip-flop's inputs be?"
Volume 00 used only the D flip-flop. There are four classic kinds, shown in Figure 3.1. They have different inputs, but all of them store one bit and change only at the clock edge.
Two ways to read a flip-flop
The usual question is: "given the inputs now, what will Q be after the edge?" The table that answers it is the flip-flop's characteristic table.
When we design, we ask the opposite. We already know how each state bit must change - the encoded state table tells us. We want to know which inputs will cause that change. The table that answers this reverse question is the excitation table.
The D flip-flop
Q+ = D. Whatever you want Q to become, put it on D. The excitation table is almost too simple to write:
| Q now | Q+ wanted | D must be |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
The T flip-flop
A T flip-flop ("T" for toggle) keeps its value when T = 0, and flips it when T = 1. So T must be 1 exactly when the bit has to change:
| Q now | Q+ wanted | T must be |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
A T flip-flop is the push-button lamp from Volume 01. Pressing (T = 1) changes the lamp, whatever state it was in. Not pressing (T = 0) leaves it alone.
The JK flip-flop
A JK flip-flop has two inputs. Its characteristic table is:
| J | K | Q+ | In words |
|---|---|---|---|
| 0 | 0 | Q | keep |
| 0 | 1 | 0 | reset |
| 1 | 0 | 1 | set |
| 1 | 1 | Q' | toggle |
Now read it backwards. Suppose Q is 0 and must stay 0. "Keep" (J = 0, K = 0) works. "Reset" (J = 0, K = 1) also works. So J must be 0, but K can be either value. We write K = X, which means don't care: either value gives the right result. Doing the same for all four changes gives the excitation table:
| Q now | Q+ wanted | J | K | Why |
|---|---|---|---|---|
| 0 | 0 | 0 | X | keep or reset both give 0 |
| 0 | 1 | 1 | X | set or toggle both give 1 |
| 1 | 0 | X | 1 | reset or toggle both give 0 |
| 1 | 1 | X | 0 | keep or set both give 1 |
Those X values are a gift. As you will see, every X is a place where the logic may be made smaller.
The SR flip-flop
An SR flip-flop has a set input S and a reset input R. S = 1 sets Q to 1, R = 1 resets it to 0, and both 0 keep it. Both 1 at once is not allowed. Its excitation table:
| Q now | Q+ wanted | S | R |
|---|---|---|---|
| 0 | 0 | 0 | X |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | X | 0 |
All four on one page
| Change in Q | D | T | J K | S R |
|---|---|---|---|---|
| 0 to 0 | 0 | 0 | 0 X | 0 X |
| 0 to 1 | 1 | 1 | 1 X | 1 0 |
| 1 to 0 | 0 | 1 | X 1 | 0 1 |
| 1 to 1 | 1 | 0 | X 0 | X 0 |
Real designs - in Verilog, on FPGAs and on chips - use D flip-flops almost every time. JK, T and SR still matter for two reasons: written exams ask about them, and the don't-cares of the JK flip-flop often give smaller logic when you design by hand.
Do not mix up the two tables. The characteristic table goes forwards: inputs in, next Q out. The excitation table goes backwards: the change you want in, the inputs out. When designing, you always use the excitation table.
A JK flip-flop holds Q = 1, and Q must become 0 at the next edge. What must J and K be?
Show the answer
Answer: B. Two actions give 0 from Q = 1: reset (J = 0, K = 1) and toggle (J = 1, K = 1). K is 1 in both, and J can be either value. So J = X and K = 1.
A T flip-flop holds Q = 0, and Q must stay 0. What must T be?
Show the answer
Answer: A. T = 1 would flip Q to 1. To keep Q at 0, T must be 0. A T flip-flop has no don't-cares in its excitation table.
3.3 Next-state logic with K-maps
A Karnaugh map is a truth table redrawn as a grid, so that circling groups of 1s gives you the simplest gate formula.
The encoded state table tells us what each flip-flop input must be, for every combination of state bits and inputs. Each flip-flop input is therefore a truth table. To build it, we need a formula of AND, OR and NOT gates - a Boolean expression - and we want the smallest one.
How formulas are written
| Written | Read as | Meaning | In Verilog |
|---|---|---|---|
| A·B, or AB | A and B | 1 only when both are 1 | A & B |
| A + B | A or B | 1 when at least one is 1 | A | B |
| A' | not A | the opposite of A | ~A |
The plus sign means OR here, not adding. So 1 + 1 = 1.
From a truth table to a formula
Any truth table can be written in one standard shape. For every row where the output is 1, write the AND of all the inputs, with a ' on each input that is 0 in that row. Then join these terms with OR. The result is a sum of products. Each AND term for a single row is called a minterm, and minterms are numbered by the row's binary value.
Take a function F of A and B that is 1 in rows 01 and 11:
- Row 01 (A = 0, B = 1) gives the minterm A'·B - minterm number 1.
- Row 11 (A = 1, B = 1) gives the minterm A·B - minterm number 3.
So F = A'·B + A·B. But look closer. B is 1 in both rows, while A is 0 in one and 1 in the other. The output does not depend on A at all, so F is simply B. That is the whole secret of simplifying:
When two minterms differ in just one input, that input does not matter. Join them, and drop it.
In a big truth table, pairs like this are hard to spot. A Karnaugh map puts them side by side.
The Karnaugh map
A Karnaugh map, or K-map, is a grid with one cell per row of the truth table. The rows and columns are labelled in Gray code order - 00, 01, 11, 10 - and not in counting order. In Gray order each label differs from its neighbour in one bit, so each cell differs from the cell beside it in exactly one input. The map also wraps around: the left column is a neighbour of the right column.
Here is how to group the 1s:
- Circle only 1s. Never include a 0.
- Each group must be a rectangle of 1, 2, 4, 8 or 16 cells.
- Make each group as large as you can.
- Cover every 1 at least once, using as few groups as you can. Groups may overlap.
- Groups may wrap around the edges of the map.
- A don't care (X) may join a group if it makes the group bigger. You never have to cover an X.
To read a group, look at each input. If it has the same value in every cell of the group, keep it - plain if it is 1, with a ' if it is 0. If it changes inside the group, drop it.
Our edge detector: flip-flop Q1
From the encoded table in sub-module 3.1, Q1+ is 1 in two rows: Q1 Q0 btn = 011 (minterm 3) and 101 (minterm 5). The unused code gives don't-cares at minterms 6 and 7. Figure 3.2 shows the map. We write b for btn to keep the labels short.
Read the green group, down the 11 column. Q1 is 0 in one cell and 1 in the other, so it drops out. Q0 = 1 and b = 1 in both, so they stay: Q0·b. The blue group, along the bottom row, gives Q1·b in the same way. So:
Q1+ = Q0·b + Q1·b
Flip-flop Q0
Q0+ is 1 in only one row: 001, minterm 1. Its neighbours - minterms 0, 3 and 5 - are all 0, and the don't-cares at 6 and 7 are not next to it. So it stands alone, as a group of one:
A group of one cell keeps every input: Q0+ = Q1'·Q0'·b. With D flip-flops, the inputs are simply D1 = Q1+ and D0 = Q0+, so the next-state logic is done.
The most common K-map mistake is labelling the columns 00, 01, 10, 11 - counting order. Then the middle two columns differ in two bits, not one, and your groups give wrong formulas. Always use 00, 01, 11, 10.
Which of these groups is not allowed in a K-map?
Show the answer
Answer: A. Groups must have 1, 2, 4, 8 or 16 cells, so three is not allowed. Split it into two overlapping groups of two instead. Wrapping around the edges and including an X are both fine.
3.4 Output logic
The output logic is found the same way as the next-state logic: write a truth table for each output, then simplify it with a K-map.
Moore outputs: from the state bits only
In a Moore machine, the outputs depend only on the state. So each output's K-map has only the state bits as inputs. For the edge detector, pulse is 1 in EDGE (code 01) and don't-care in the unused code 11:
pulse = Q0. The output logic is a single wire. That is no accident: EDGE is the only state whose code has Q0 = 1. A good choice of codes can make the output logic vanish - which is the Medvedev idea from Volume 02.
The whole Moore edge detector, built from the three formulas, looks like this in Verilog:
module edge_moore_gates (
input wire clk, rst, b,
output wire pulse
);
reg q1, q0; // the state register: 2 flip-flops
wire d1 = (q0 & b) | (q1 & b); // Q1+ from Figure 3.2
wire d0 = ~q1 & ~q0 & b; // Q0+ from Figure 3.3
always @(posedge clk)
if (rst) {q1, q0} <= 2'b00; // reset to ZERO
else {q1, q0} <= {d1, d0}; // move to the next state
assign pulse = q0; // output logic from Figure 3.4
endmodule
Mealy outputs: the inputs join in
In a Mealy machine, the outputs depend on the inputs too, so the inputs appear in the output's truth table. Take the two-state Mealy edge detector. Give ZERO the code 0 and ONE the code 1, so one flip-flop Q is enough:
| Q | btn | Q+ | pulse |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 |
| 1 | 1 | 1 | 0 |
Two things fall straight out of this table. First, Q+ always equals btn, so the flip-flop just stores the button's last value. Second, pulse is 1 in one row only, where Q = 0 and btn = 1, so pulse = Q'·btn. That is exactly the one-flip-flop circuit from the interview question at the end of Volume 02.
| Moore version | Mealy version | |
|---|---|---|
| Flip-flops | 2 | 1 |
| Next-state logic | Q0·b + Q1·b, and Q1'·Q0'·b | none - Q+ = b |
| Output logic | a wire: pulse = Q0 | one AND gate: pulse = Q'·b |
When you build the K-map for a Mealy output, include the inputs. If you use only the state bits, the map cannot show that the same state gives different outputs for different inputs, and your formula will be wrong.
A Moore machine uses the codes A = 00, B = 01, C = 11 and D = 10. Its output z is 1 in states B and C only. What is z?
Show the answer
Answer: A. B (01) and C (11) both have Q0 = 1, and A (00) and D (10) both have Q0 = 0. So z is 1 exactly when Q0 is 1. On a K-map, B and C sit side by side, and their group drops Q1.
3.5 Worked design: from story to gates
Every hand design follows the same seven steps, from a story in words to a circuit of flip-flops and gates.
- Understand the story. List the inputs and outputs, and decide Moore or Mealy.
- Draw the state diagram.
- Write the state table. Check that it has every row.
- Give each state a code.
- Write the encoded table. Add each flip-flop's inputs from the excitation table.
- Simplify each flip-flop input and each output with a K-map.
- Build it, and check it. Write the circuit, then test it against the story.
Step 1: the story
A drink machine sells a drink for three coins. It takes one coin at a time. When the third coin goes in, it releases one drink, and its count starts again from zero.
- Input: coin - 1 during the clock cycle in which a coin arrives.
- Output: vend - 1 for one cycle, to release a drink.
We choose a Moore machine, so that vend is steady for its whole cycle.
Step 2: the state diagram
The machine must remember how many coins it has: none, one or two. It also needs a state in which vend = 1. That gives four states: C0, C1, C2 and VEND. A coin during VEND counts as the first coin of the next drink.
Step 3: the state table
| State | Next if coin = 0 | Next if coin = 1 | vend |
|---|---|---|---|
| C0 | C0 | C1 | 0 |
| C1 | C1 | C2 | 0 |
| C2 | C2 | VEND | 0 |
| VEND | C0 | C1 | 1 |
Four states and one input bit need 4 × 2 = 8 cases, and the table covers all of them.
Step 4: the codes
Four states need two flip-flops, and all four codes are used. Along the main path C0, C1, C2, VEND we choose codes that change one bit at a time: C0 = 00, C1 = 01, C2 = 11, VEND = 10.
Step 5: the encoded table
With D flip-flops, each D input is simply the next value of its bit. The last column numbers each row as a minterm of Q1, Q0 and coin:
| Q1 Q0 | coin | Q1+ Q0+ | vend | minterm |
|---|---|---|---|---|
| 00 | 0 | 00 | 0 | 0 |
| 00 | 1 | 01 | 0 | 1 |
| 01 | 0 | 01 | 0 | 2 |
| 01 | 1 | 11 | 0 | 3 |
| 11 | 0 | 11 | 0 | 6 |
| 11 | 1 | 10 | 0 | 7 |
| 10 | 0 | 00 | 1 | 4 |
| 10 | 1 | 01 | 1 | 5 |
Step 6: simplify
D1 = Q1+ is 1 at minterms 3, 6 and 7:
D0 = Q0+ is 1 at minterms 1, 2, 3, 5 and 6:
The output vend is 1 only in VEND, code 10, so vend = Q1·Q0'. The full answer:
D1 = Q0·coin + Q1·Q0
D0 = Q0'·coin + Q0·coin' + Q1'·coin
vend = Q1·Q0'
Minterm 3 could have been covered by Q1'·Q0 instead of Q1'·coin. A K-map can have more than one answer of the same size, and all of them are correct.
Step 7: build it and check it
module drink_gates (
input wire clk, rst, coin,
output wire vend
);
reg q1, q0; // C0=00 C1=01 C2=11 VEND=10
wire d1 = (q0 & coin) | (q1 & q0);
wire d0 = (~q0 & coin) | (q0 & ~coin) | (~q1 & coin);
always @(posedge clk)
if (rst) {q1, q0} <= 2'b00; // reset to C0
else {q1, q0} <= {d1, d0};
assign vend = q1 & ~q0; // 1 only in VEND
endmodule
Check it against the story, by hand. Start in C0 = 00 and put in three coins. With q1 = 0, q0 = 0 and coin = 1: d1 = 0 and d0 = 1, so the next state is 01 (C1). From 01 with coin = 1: d1 = 1 and d0 = 1, giving 11 (C2). From 11 with coin = 1: d1 = 1 and d0 = 0, giving 10 - VEND, where vend = 1. The drink comes out after the third coin, as the story says.
The same machine with JK flip-flops
Now build it again, with JK flip-flops. The states, codes and next values stay the same. Only the flip-flop inputs change. For each row, look up the change in each bit in the JK excitation table:
| Q1 Q0 | coin | Q1 changes | J1 K1 | Q0 changes | J0 K0 |
|---|---|---|---|---|---|
| 00 | 0 | 0 to 0 | 0 X | 0 to 0 | 0 X |
| 00 | 1 | 0 to 0 | 0 X | 0 to 1 | 1 X |
| 01 | 0 | 0 to 0 | 0 X | 1 to 1 | X 0 |
| 01 | 1 | 0 to 1 | 1 X | 1 to 1 | X 0 |
| 11 | 0 | 1 to 1 | X 0 | 1 to 1 | X 0 |
| 11 | 1 | 1 to 1 | X 0 | 1 to 0 | X 1 |
| 10 | 0 | 1 to 0 | X 1 | 0 to 0 | 0 X |
| 10 | 1 | 1 to 0 | X 1 | 0 to 1 | 1 X |
Half of every column is X. Watch what that does to the K-maps:
So J1 = Q0·coin and K1 = Q0'. In the same way - try it in the practice section - you will find J0 = coin and K0 = Q1·coin. Compare the two designs:
| D flip-flops | JK flip-flops | |
|---|---|---|
| Logic for bit 1 | Q0·coin + Q1·Q0 | J1 = Q0·coin, K1 = Q0' |
| Logic for bit 0 | Q0'·coin + Q0·coin' + Q1'·coin | J0 = coin, K0 = Q1·coin |
| AND gates | 5 | 2 |
| OR gates | 2 | 0 |
The don't-cares in the JK excitation table made the logic much smaller. That is why exams love JK designs. On a real chip, though, the D flip-flop still wins. A JK flip-flop is itself built from a D flip-flop plus gates, so the gates you save come back inside the flip-flop.
Do not skip step 7. Hand designs are easy to get slightly wrong - one misread cell in a K-map is enough. Always check the finished circuit against the story, by hand or in a simulator, before you trust it.
In the drink machine, why were the codes chosen as C0 = 00, C1 = 01, C2 = 11, VEND = 10?
Show the answer
Answer: C. Any assignment works, but codes that change one bit per step - Gray order - often give smaller groups in the K-maps. The only firm rule used here was to give the reset state the code 00.
What you learned
- A machine with S states needs the smallest n flip-flops for which 2n is at least S. Codes left over are unused, and marked don't-care.
- An excitation table says which inputs make a flip-flop change as you want. For D, it is simply D = Q+.
- JK and SR excitation tables contain don't-cares, which often make hand designs smaller.
- A K-map lays a truth table out in Gray-code order. Circle groups of 1, 2, 4 or 8, as large and as few as possible, and drop the inputs that change inside a group.
- Moore outputs are simplified from the state bits alone; Mealy outputs need the inputs in their map too.
- The seven steps: story, diagram, table, codes, encoded table, K-maps, build and check.
Key words from this volume
Every word below has a plain-English entry in the glossary.
- State assignment
- Unused state code
- Characteristic table
- Excitation table
- T flip-flop
- JK flip-flop
- SR flip-flop
- Boolean expression
- Sum of products
- Minterm
- Karnaugh map (K-map)
- Gray code
- Don't care (X)
Practice
Finish the JK design
Using the JK table in sub-module 3.5, draw the K-maps for J0 and K0 of the drink machine, and find their formulas.
Show the solution
J0 matters only where Q0 = 0 (minterms 0, 1, 4 and 5); everywhere else it is X. The 1s are at minterms 1 and 5. The X at minterms 3 and 7 let them grow into a group of four, which is the whole half of the map where coin = 1. Q1 and Q0 both change inside that group, so only coin is left: J0 = coin.
K0 matters only where Q0 = 1 (minterms 2, 3, 6 and 7). It is 1 only at minterm 7, where C2 moves to VEND. The X at minterm 5 is next to it, so they form a group of two in the bottom row, with Q1 = 1 and coin = 1: K0 = Q1·coin.
The same machine with T flip-flops
Redesign the drink machine with T flip-flops. Remember that T must be 1 exactly when the bit has to change.
Show the solution
From the encoded table, T1 = 1 where Q1 changes: minterms 3 (01 to 11), 4 (10 to 00) and 5 (10 to 01). T0 = 1 where Q0 changes: minterms 1, 5 and 7.
So T1 = Q1·Q0' + Q1'·Q0·coin and, from a second map, T0 = Q0'·coin + Q1·coin. T flip-flops have no don't-cares, so this design is larger than the JK one.
Count the flip-flops
A controller has 11 states. How many flip-flops does it need with binary codes, and how many codes are unused? How many flip-flops would it need with one flip-flop per state?
Interview corner
Excitation versus characteristic
"What is the difference between the characteristic table and the excitation table of a flip-flop? Write the excitation table of a JK flip-flop."
Show the solution
"The characteristic table gives the next state from the inputs and the present state. The excitation table goes the other way: from the present state and the required next state, it gives the inputs needed - and that is the one used in design. For a JK flip-flop, 0 to 0 needs J = 0 and K = X. 0 to 1 needs J = 1 and K = X. 1 to 0 needs J = X and K = 1. 1 to 1 needs J = X and K = 0." Mention the don't-cares and why they help - that is the point the question is really testing.
Why Gray order in a K-map?
"Why are the rows and columns of a Karnaugh map in the order 00, 01, 11, 10?"
Show the solution
"So that any two neighbouring cells differ in exactly one input. Two neighbouring 1s then differ only in that input, which means the output does not depend on it, and the pair simplifies to a term without it. In counting order, 01 and 10 would sit side by side while differing in two bits, so groups would give wrong answers. The same reason makes the edges wrap around: 10 and 00 also differ in one bit."
Next, Volume 04 writes state machines the way engineers really do - in Verilog and SystemVerilog - and lets the tools do the K-maps for you.