Volume 01 Beginner 5 sub-modules ~25 min read

What Is a State Machine?

A gate forgets. A state machine remembers. In this volume you meet your first state machines - a turnstile and a traffic light - and learn the two ways engineers write them down: the state diagram and the state table. Then you see why, in hardware, every move happens on the tick of a clock.

You will learn
  • What makes a circuit a state machine
  • The four parts of every state machine: states, inputs, outputs and transitions
  • How to draw a state diagram, and check that it is complete
  • How to write a state table, and count its rows
  • The three blocks inside every clocked state machine
You need
  • Volume 00: bits, gates, flip-flops and timing diagrams

1.1 Machines that remember: everyday examples

A state machine is anything that is always in one of a few situations, and moves between them when something happens.

A turnstile

Think of the turnstile at a metro station or a stadium gate. It has a rotating arm, a slot for a coin or a card, and a lock. It can be in only two situations:

Two things can happen to it. You can put in a coin, or you can push the arm. Here is what the turnstile does in each case:

  1. It is locked, and you put in a coin. It unlocks.
  2. It is unlocked, and you push the arm. You walk through, and it locks again behind you.
  3. It is locked, and you push the arm. Nothing happens. It stays locked.
  4. It is unlocked, and you put in another coin. Nothing changes. It stays unlocked.

Now look closely at "you push the arm". Sometimes a push lets you through. Sometimes it does nothing. The push is the same. What differs is the situation the turnstile was already in. To behave correctly, the turnstile must remember whether someone has paid.

That memory is what makes it a state machine. Each situation it can be in is called a state. The turnstile has two states: LOCKED and UNLOCKED.

In plain words

A state machine remembers which situation it is in. What it does next depends on two things: what just happened, and which situation it was already in.

State machines are everywhere

Once you know what to look for, you see state machines all around you:

Machine Its states What makes it change state
Traffic light GREEN, YELLOW, RED A timer running out
Lift Stopped at a floor, moving up, moving down, doors open Buttons, floor sensors, a door timer
Washing machine Idle, filling, washing, rinsing, spinning The start button, a water-level sensor, timers
Phone screen Locked, unlocked, asleep The right PIN, a timeout, the power button
Push-button lamp OFF, ON Each press of the button

The last one is worth a second look. A lamp with a single push-button turns on when you press it, and off when you press it again. The same press does opposite things. That only works because the lamp remembers whether it is on.

Think of it like this

The state is like a bookmark in a book. The bookmark does not hold the story - it just remembers where you are, so you know what to read next.

The exact version

Engineers call these circuits finite state machines, or FSMs for short. Finite means the number of states is fixed and countable - two for the turnstile, three for the traffic light. A finite state machine is always in exactly one of its states. It moves between them only when its inputs tell it to.

Common mistake

Not every circuit with inputs and outputs is a state machine. An AND gate has inputs and an output, but no memory: the same inputs always give the same output. A state machine is different. The same input can give different results, depending on the state it is in.

Quick check

Which of these needs to remember something, and so must be a state machine?

Show the answer

Answer: B. The toggling lamp does opposite things for the same press, so it must remember whether it is on. The first lamp just copies the button, like a wire. The AND gate has no memory at all.

1.2 States, inputs, outputs and transitions

Every state machine is described by four things: its states, its inputs, its outputs, and its transitions - the rules for moving between states.

Let us take the turnstile apart, one piece at a time.

States

The states are the situations the machine can be in. The turnstile has two: LOCKED and UNLOCKED. At every moment it is in exactly one of them - never both, never neither.

One state is special: the reset state, where the machine starts. A turnstile should start LOCKED. Otherwise, the first person after a power cut would walk through for free.

Inputs

The inputs are the things that happen to the machine from outside. The turnstile has two inputs: coin (a coin goes in) and push (someone pushes the arm).

Outputs

The outputs are what the machine does to the world. The turnstile has one output, locked. When locked is 1, a small motor holds the arm still. When locked is 0, the arm is free to turn. Some turnstiles also have a green "go" lamp, which is simply the opposite of locked.

Transitions

A transition is a move from one state to another. Each transition has a condition: "if I am in this state, and this input happens, go to that state." The turnstile has four transitions:

If the turnstile is and this happens it moves to
LOCKED coin UNLOCKED
LOCKED push LOCKED
UNLOCKED coin UNLOCKED
UNLOCKED push LOCKED

Two of these rows do not change the state at all: LOCKED + push stays LOCKED, and UNLOCKED + coin stays UNLOCKED. These still count as transitions. A transition that returns to the same state is called a self-loop, and it means "stay where you are".

Think of it like this

Think of a board game. Your piece stands on one square - that is the state. The dice decide where you go next - those are the inputs. The rules printed on the board are the transitions. And the instruction written on each square, such as "miss a turn", is the output.

Remember

To describe any state machine, write down four lists: the states (and which one is the reset state), the inputs, the outputs, and the transitions.

Common mistake

The most common mistake is forgetting a case. What should the turnstile do if someone pushes while it is locked? It is tempting to leave that out, because "nothing happens". But a real circuit always does something. If your design does not say what, the hardware will decide for you - and it may decide badly. Every state needs an answer for every input.

Going deeper: the textbook definition

Textbooks define a finite state machine as six things. Four of them are lists: the states, the inputs, the outputs and the start state. The other two are rules: a next-state function and an output function. The next-state function takes the current state and the inputs, and gives the next state - it is the transition table. The output function gives the outputs. It is exactly the four lists above, written as mathematics. You will meet the two functions again as real circuits in sub-module 1.5.

Quick check

The turnstile is UNLOCKED, and someone puts in another coin. What happens?

Show the answer

Answer: A. The table says UNLOCKED + coin stays UNLOCKED. The machine has only two states, so there is no "double unlocked" - the second coin is simply wasted. A real ticket gate would need extra states, or a counter, to remember that two people have paid.

1.3 Drawing a state diagram

A state diagram draws each state as a circle and each transition as an arrow, labelled with the input that causes it.

Tables are precise, but they are hard to take in at a glance. So engineers usually draw a state machine first. The drawing is called a state diagram. It has only four ingredients:

  1. A circle for each state, with the state's name inside.
  2. An arrow for each transition, from the old state to the new one. The arrow is labelled with the input that makes it happen.
  3. A looping arrow for each self-loop - an arrow that leaves a state and comes back to it.
  4. A short arrow marked reset, pointing at the state where the machine starts.

Figure 1.1 is the turnstile, drawn this way.

State diagram of a coin-operated turnstile with states LOCKED and UNLOCKED LOCKED UNLOCKED coin push push coin reset
Figure 1.1 - The turnstile. A coin moves it from LOCKED to UNLOCKED, and a push moves it back. The two looping arrows show the inputs that change nothing. The gold arrow marks the reset state.

To read a state diagram, put your finger on the reset state. Then follow the arrow that matches each input. Start at LOCKED. A coin arrives, so follow the coin arrow - you are now at UNLOCKED. Someone pushes, so follow the push arrow - you are back at LOCKED. Someone pushes again, so follow the push loop - you stay at LOCKED.

A second example: a traffic light

A simple traffic light has three states: GREEN, YELLOW and RED. It has one input, called done. A timer sets done to 1 when the current colour has been on for long enough. Until then, done is 0.

The rules are:

State diagram of a traffic light cycling through GREEN, YELLOW and RED GREEN YELLOW RED done=1 done=1 done=1 done=0 done=0 done=0 reset
Figure 1.2 - A traffic light. Each colour waits while done = 0, and moves on when done = 1. Notice that every state has exactly two arrows leaving it - one for done = 0 and one for done = 1.

How to check a state diagram

A good state diagram passes two tests. Check them for every state:

  1. Nothing is missing. The arrows leaving the state cover every possible input. In Figure 1.2, each state has an arrow for done = 0 and an arrow for done = 1.
  2. Nothing clashes. No input value appears on two arrows leaving the same state. The machine cannot go to two places at once.
Common mistake

Beginners often draw only the "interesting" arrows - the ones that change the state - and leave out the self-loops. The diagram then looks cleaner, but it is incomplete. When you are learning, draw every arrow. Later, you may agree with your team that a missing arrow means "stay", but that must be a stated rule, never an accident.

Quick check

In Figure 1.2, the light is YELLOW and done = 0. Where is it after the next move?

Show the answer

Answer: C. With done = 0, follow the loop on YELLOW: it points back to YELLOW. The light stays yellow until the timer says done = 1.

Quick check

A state has three arrows leaving it: one for x = 0, one for x = 1, and another for x = 1. What is wrong?

Show the answer

Answer: B. x = 1 appears on two arrows, so the diagram does not say which way to go when x = 1. That is a clash. Nothing is missing, because x = 0 and x = 1 are both covered.

1.4 The state table

A state table holds the same information as a state diagram, in rows. The current state and the inputs go in; the next state and the outputs come out.

A diagram is easy to read. A table is easy to check, and it is the form you will turn into gates in Volume 03 and into code in Volume 04. So engineers keep both.

A state table has one row for every combination of state and input. Each row gives the next state and the outputs. Here is the turnstile:

Current state Input Next state locked
LOCKED coin UNLOCKED 1
LOCKED push LOCKED 1
UNLOCKED coin UNLOCKED 0
UNLOCKED push LOCKED 0

The state the machine is in now is called the current state. The output, locked, depends only on the current state here: it is 1 in LOCKED and 0 in UNLOCKED.

In hardware, every input is a bit

So far we have treated "coin" and "push" as events that happen one at a time. In a circuit, they are two wires, and each is a bit. At any moment, coin can be 0 or 1, and push can be 0 or 1. So there are four input combinations: nothing, push only, coin only, and both at once.

"Both at once" sounds unlikely, but in hardware it will happen sooner or later - and the table must say what to do. Here is the full table, with a row for every combination:

Current state coin push Next state locked
LOCKED 0 0 LOCKED 1
LOCKED 0 1 LOCKED 1
LOCKED 1 0 UNLOCKED 1
LOCKED 1 1 UNLOCKED 1
UNLOCKED 0 0 UNLOCKED 0
UNLOCKED 0 1 LOCKED 0
UNLOCKED 1 0 UNLOCKED 0
UNLOCKED 1 1 LOCKED 0

Two rows were design decisions. In LOCKED with both inputs, we unlock, because a coin was paid. In UNLOCKED with both inputs, we lock, because someone walked through. That second choice loses the new coin - a real ticket gate would need to count coins. Writing the full table forced us to notice the problem. That is exactly what tables are for.

How many rows?

Remember

A full state table has (number of states) × 2(number of input bits) rows. The turnstile has 2 states and 2 input bits, so it needs 2 × 4 = 8 rows.

If your table has fewer rows than this, some case is missing.

A shorter layout

When there are only a few inputs, a table can put one column per input value. Each cell is then the next state. Here is the traffic light from Figure 1.2 in this shorter form:

Current state Next state if done = 0 Next state if done = 1 Lamp that is on
GREEN GREEN YELLOW green
YELLOW YELLOW RED yellow
RED RED GREEN red

Both layouts carry the same facts. Use whichever is easier to read for the machine in front of you.

Common mistake

A state table with a missing row is not "mostly right". The missing row is a situation the hardware will meet one day, with no rule for what to do. Always count your rows with the formula above.

Quick check

A machine has 4 states and 2 input bits. How many rows does its full state table have?

Show the answer

Answer: D. 2 input bits make 22 = 4 input combinations. Each of the 4 states needs one row per combination, so the table has 4 × 4 = 16 rows.

Try it in FSM StudioThe turnstile is waiting for you in FSM Studio. Change a next state in its table and watch the diagram redraw itself, then press Clock edge to walk it through coins and pushes.
Open FSM Studio

1.5 Why hardware needs a clock

In hardware, a state machine moves only at clock edges. The clock keeps every part of the machine in step.

The three blocks inside every state machine

Every clocked state machine is built from the same three blocks. Figure 1.3 shows them.

Block diagram of a clocked state machine: next-state logic, state register and output logic, with the state fed back Next-state logic State register Output logic gates flip-flops gates inputs next state state outputs clk the current state feeds back
Figure 1.3 - Every clocked state machine has this shape. The state register remembers. The next-state logic decides where to go. The output logic decides what to show. The purple line carries the current state back into the next-state logic.
  1. The state register is a group of flip-flops. It holds the current state as a pattern of bits.
  2. The next-state logic is made of gates. It looks at the current state and the inputs, and works out the next state.
  3. The output logic is also made of gates. It looks at the current state and works out the outputs.

Notice the purple line. The current state leaves the register and goes back into the next-state logic. A path that brings a result back to its own start is called feedback. Feedback is how the machine's past affects its future.

What happens in one clock cycle

  1. During the cycle, the state register holds the current state steady.
  2. The next-state logic sees the current state and the inputs, and works out the next state. Its answer waits at the register's input.
  3. At the rising clock edge, the register copies that answer. The machine has moved.
  4. The new state flows back through the feedback line, and the next-state logic starts working on the move after that.

Why not leave the clock out?

Imagine the same circuit with no clock - the gates connected straight back to themselves. The next-state logic would compute a new state, which would feed straight back in, which could change the answer again, and again, as fast as the gates can switch. The machine would race through states with no control. And if the state has several bits, some bits would change a little before others. For a short moment, the state would be a mix of old and new bits - a state that should not exist.

The clock fixes all three problems:

A circuit that moves in step with a clock like this is called synchronous. Every state machine in this course is synchronous.

Think of it like this

Think of a group of dancers who may change position only on the drum beat. Between beats, everyone decides where to go next. On the beat, everyone steps together. Nobody bumps into anybody, because nobody moves early.

The turnstile on a clock

Figure 1.4 shows the turnstile running on a clock. Coin and push are now two input wires, and each is 1 during the cycle when that event happens.

Timing diagram of the turnstile: coin and push inputs, the state, and the locked output over eight clock cycles 0 1 2 3 4 5 6 7 clk coin push state LOCKED UNLOCKED LOCKED UNLOCKED locked
Figure 1.4 - The turnstile on a clock. Each input is looked at only at the next rising edge. A push while locked (before edge 5) and a second coin while unlocked (before edge 7) change nothing - these are the self-loops.
  1. Edge 2. Coin was 1 during the cycle before this edge. The state becomes UNLOCKED, and locked drops to 0.
  2. Edge 4. Push was 1. The state becomes LOCKED again.
  3. Edge 5. Push was 1 again, but the turnstile was already locked. It stays LOCKED - the push self-loop.
  4. Edge 6. A coin: the state becomes UNLOCKED.
  5. Edge 7. A second coin while unlocked. It stays UNLOCKED - the coin self-loop.

A first look at the code

You will learn to write state machines in Volume 04. But it is worth seeing now how closely the code follows the diagram. Each else if line below is one arrow of Figure 1.1:


module turnstile (
  input  wire clk, rst, coin, push,
  output wire locked
);
  localparam LOCKED = 1'b0, UNLOCKED = 1'b1;   // the two states
  reg state;                                    // the state register

  always @(posedge clk) begin                   // move only at a rising edge
    if (rst)                            state <= LOCKED;    // the reset arrow
    else if (state == LOCKED   && coin) state <= UNLOCKED;  // coin arrow
    else if (state == UNLOCKED && push) state <= LOCKED;    // push arrow
  end                                           // no match: stay (the self-loops)

  assign locked = (state == LOCKED);            // the output logic
endmodule
Common mistake

A state machine does not react the instant an input changes. It reacts at the next rising clock edge. If you expect the state to change "when coin goes to 1", you will misread every timing diagram. Always look for the next edge.

Quick check

The turnstile is LOCKED. Coin goes to 1 halfway through a clock cycle and stays 1 until after the next edge. When does the state become UNLOCKED?

Show the answer

Answer: C. The state register changes only at a rising edge. Coin is 1 when the next edge arrives, so the register copies UNLOCKED at that edge. It would never unlock only if coin had dropped back to 0 before the edge.

Quick check

Which block in Figure 1.3 remembers the state?

Show the answer

Answer: A. Only the state register contains flip-flops, and only flip-flops remember. The two logic blocks are gates, which have no memory - they recompute their answers all the time.

What you learned

Key words from this volume

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

Practice

Practice 1

A washing machine

A simple washing machine has four states: IDLE, FILL, WASH and DRAIN. It starts in IDLE. It has four inputs, and each is 1 when its event happens:

  • start - the start button is pressed
  • full - the drum is full of water
  • done - the wash timer has run out
  • empty - the drum is empty

In IDLE it waits for start, then fills. In FILL it waits until full, then washes. In WASH it waits for done, then drains. In DRAIN it waits until empty, then returns to IDLE. Draw the state diagram. Assume each state looks at only its own input.

Show the solution

Each state has two arrows: one that moves on when its input is 1, and a self-loop while it is 0.

State diagram of a washing machine with states IDLE, FILL, WASH and DRAIN IDLE FILL WASH DRAIN start=1 full=1 done=1 empty=1 start=0 full=0 done=0 empty=0 reset
Figure 1.5 - The washing machine. Each state waits on its own input, then moves on.

In a real design, a state such as WASH would also react to the lid opening, or to a stop button. Every extra input adds rows to the table - which is why engineers keep machines small.

Practice 2

Counting rows

A machine has 5 states and 3 input bits. How many rows does its full state table need?

Show the solution

3 input bits make 23 = 8 combinations. So the table needs 5 × 8 = 40 rows. This is why real tables are often written in a shorter form, with "any value" marks for inputs that do not matter in a state. You will learn that trick in Volume 03.

Practice 3

Find the two problems

Here is a state table for a machine with states A and B, and one input x:

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

What is wrong with it?

Show the solution

The table should have 2 × 21 = 4 rows, one per combination, and each combination exactly once.

  • A clash. A with x = 1 appears twice, with two different next states: B and A. The machine cannot go both ways.
  • A missing row. B with x = 1 is not listed at all, so nothing says what the machine does there.

Counting the rows first would have shown that something was wrong, even before reading them.

Interview corner

Interview question 1

What is a finite state machine?

"Explain what a finite state machine is, with an example." This is often the first question in a digital design interview.

Show the solution

A strong answer is short, and has an example:

"A finite state machine is a sequential circuit that is always in one of a fixed number of states. It is built from a state register, which holds the current state, and combinational logic that computes the next state and the outputs from the current state and the inputs. It moves to the next state on each clock edge. For example, a turnstile has two states, LOCKED and UNLOCKED. A coin moves it from LOCKED to UNLOCKED, and a push moves it back."

Mentioning the three blocks - register, next-state logic, output logic - shows that you understand the hardware, not just the diagram.

Interview question 2

Why must every state handle every input?

"Your state diagram has no arrow for input x = 1 in state S3. Is that a problem?"

Show the solution

Yes. The hardware will still do something when x = 1 in S3 - the gates always produce some next state. If the design does not say what, the result depends on how the logic happened to be built, and it may be a jump to the wrong state. Either add the missing arrow, or make the convention explicit ("no arrow means stay") and code it that way, for example with a default "stay in the current state" line.

Next, Volume 02 looks closely at the outputs - and shows that there are two quite different ways for a state machine to make them.