Seyed Masoud Hosseini · Overview · Study log · Ideas · Transcript · RSS feed

FPGA & Verilog Design · Lecture 5 of 12 · 24:24

Part 5: Finite State Machines

Introduction to FPGA Part 5 - Finite State Machines | Digi-Key Electronics on YouTube

Study guide

What this lecture covers

This lecture introduces finite state machines (FSMs) as a way to organize sequential hardware behavior in Verilog, answering how a design moves through discrete states based on inputs and a clock. It builds the same circuit - a counter that pulses an LED when done - as both a Moore machine (outputs tied to state) and a Mealy machine (outputs tied to state and input).

By the end you can describe a state machine using localparam constants, a case statement, and multiple always blocks, and you understand the practical difference between blocking (=) and non-blocking (<=) assignment. The lecture closes with a challenge to use a state machine to debounce the button counter built in Part 4.

Key ideas

  • Finite state machine: a model with a fixed set of states, transitions between them, and an initial state, used here to structure hardware behavior.
  • Moore machine: outputs depend only on the current state; in this design the "done" LED is high only while the state equals done.
  • Mealy machine: outputs depend on both current state and inputs, which can let a design use fewer states than the equivalent Moore machine.
  • case statement: Verilog's equivalent of switch/case, used here to define per-state behavior and transitions, always paired with a default branch for safety.
  • Blocking vs non-blocking assignment: = (blocking) executes assignments in sequence within a clock cycle; <= (non-blocking) evaluates all right-hand sides using values from before the clock edge, so lines effectively execute together - the recommended choice for clocked sequential logic.
  • Combinational always @(*): using * in the sensitivity list tells the tool to infer which signals should trigger re-evaluation, typically paired with blocking assignments for unclocked logic.
  • Splitting logic across multiple always blocks: separate blocks handle the clock divider, state transitions, the LED counter, and the output logic, which the lecture presents as easier to read even though it could be combined.

Walkthrough

Why state machines, and the Moore design (0:00)

The lecture defines idle, counting, and done states for a circuit that starts counting when a "go" button is pressed, and pulses a "done" LED for one clock cycle once the counter reaches its maximum value before returning to idle. It explains that in a Moore machine, each state directly determines the outputs.

Implementing the Moore FSM in Verilog (3:08)

States are defined as localparam constants, and the design reuses the clock-divider pattern from the previous episode to slow the 12 MHz oscillator down to a visible rate. Separate always blocks handle the clock divider, the state-transition logic (a case statement keyed on the current state register), and the LED counter, deliberately kept apart from the state-transition logic because everything executes concurrently in hardware.

Blocking versus non-blocking assignment (13:22)

Using a worked example with two signals swapping values, the lecture shows that blocking assignment (=) lets each line see the result of the one before it within the same clock edge, while non-blocking assignment (<=) evaluates all right-hand sides first and applies updates simultaneously. The guidance given is to use non-blocking assignment for clocked sequential logic and blocking assignment for unclocked, combinational always blocks.

Building and testing the Moore machine (17:29)

After apio verify, apio build, and apio upload, pressing reset then go starts the counter, which displays on the LEDs and flashes the green LED for one cycle when it finishes.

Converting to a Mealy machine (18:32)

By copying the Moore project and merging the "done" output into the counting-state logic, the design drops the separate done state entirely: the green LED turns on directly when the counter reaches its maximum and the machine returns straight to idle. The lecture notes this trade-off - fewer states and less hardware, at some cost to readability.

Testing the Mealy machine and the debounce challenge (22:39)

The Mealy version is verified, built, and uploaded, and behaves the same way externally. The lecture closes with a challenge: use a Moore or Mealy state machine to debounce the button counter from Part 4 so each press increments the count exactly once.

Before you watch

  • Complete Part 4 ("Clocks and Procedural Assignments"); this lecture reuses its clock-divider design and assumes familiarity with always blocks and registers.
  • Review the button-debounce issue mentioned in Part 4, since it becomes this episode's challenge.

Check your understanding

  1. What is the key difference between how a Moore machine and a Mealy machine produce their outputs?
  2. Why does the lecture put the LED counter logic in a separate always block from the state-transition logic?
  3. What is the practical difference between blocking and non-blocking assignment inside a clocked always block?
  4. Why does the Mealy version of the design need one fewer state than the Moore version?
  5. Why does the design include a default case in the state case statement even though every defined state is handled?

From the YouTube description

A field-programmable gate array (FPGA) is an integrated circuit (IC) that lets you implement custom digital circuits. You can use an FPGA to create optimized digital logic for things like digital signal processing (DSP), machine learning, and cryptocurrency mining. Because of the FPGA’s flexibility, you can often implement entire processors using its digital logic. You can find FPGAs in consumer electronics, satellites, and in servers used to perform specialized calculations.

In this series, we will see how an FPGA works and demonstrate how to create custom digital logic using the Verilog hardware description language (HDL).

Previously, we demonstrated procedural assignments and how to feed a clock signal to such statements (https://youtu.be/LwQsyeuf9Sk). In this episode, we show how to create finite state machines (FSMs) in Verilog.

The solution to the challenge at the end of the episode can be found here: https://www.digikey.com/en/maker/projects/introduction-to-fpga-part-5-finite-state-machine-fsm/4d83e63da76044af9acc8aa7dcf07c22

All code examples and solutions for this series can be found here: https://github.com/ShawnHymel/introduction-to-fpga

A finite state machine (FSM or sometimes just “state machine”) is a mathematical model used to express how something (e.g. an abstract “machine”) can move sequentially through a series of states to tackle various problems. FSMs are used often in software, and we can create FSMs in hardware logic circuits.

While many problems can be tackled without the use of an FSM, they provide a wonderful way to organize code (whether that’s hardware or software).

A machine (or process) takes on certain properties when in a particular state, which often includes changing some kind of output. Such output might be toggling a bit, incrementing a counter, or opening a network connection. The machine moves to another state when certain criteria are met (known as the “inputs”). For example, a button might be pressed, a timer might expire, or a client might close the network connection, which will cause the state machine to move to a different state.

State machines are often depicted in diagram form as a series of connected circles or rectangles. The arrows show how the machine moves between states and list the input criteria required for the transition.

A Moore state machine has its outputs associated with each state. As a result, a Moore FSM must change states in order to change its outputs.

A Mealy state machine, on the other hand, associates its outputs with states and inputs. This means the outputs in a Mealy state machine may change on the transition. While a Mealy state machine may be harder to understand at times, they often have fewer states than the equivalent Moore FSM (thus saving you precious logic cells or code space).

Your challenge is to create a state machine that debounces a button to fix the counter from the previous episode. Instead of skipping counts on some button presses, the counter should increment by 1 (and only 1) each time you press the INCREMENT button. You are welcome to use a Mealy or a Moore machine.

Product Links:
https://www.digikey.com/en/products/detail/lattice-semiconductor-corporation/ICE40HX1K-STICK-EVN/4289604

Related Videos:
https://www.youtube.com/watch?v=z8Oldd-nrfs
https://www.youtube.com/watch?v=5kNXX67mchE
https://www.youtube.com/watch?v=iwcxLQ6AB88

Related Project Links:
https://www.digikey.com/en/maker/projects/introduction-to-fpga-part-5-finite-state-machine-fsm/4d83e63da76044af9acc8aa7dcf07c22

Related Articles:
https://www.digikey.com/en/pdf/r/renesas-electronics-america/powering-fpga-applications
https://www.digikey.com/en/videos/d/dsp/edge-machine-deep-learning-on-fpga
https://forum.digikey.com/t/debounce-logic-circuit-verilog/13196

Learn more:
Maker.io - https://www.digikey.com/en/maker
Digi-Key’s Blog – TheCircuit https://www.digikey.com/en/blog
Connect with Digi-Key on Facebook https://www.facebook.com/digikey.electronics/
And follow us on Twitter https://twitter.com/digikey

← Part 4: Clocks and Procedural Assignments · Part 6: Verilog Modules and Parameters →