CPEN 311

State Machine

Updated 2019-04-04

A finite state machine (FSM) is a system of circuit made up of sequential and combinational logic that has multiple states. The output of a FSM depends on the current input as well as the current state. This is a Mealy machine. If the FSM only depends on the current state, it is a Moore machine. 1

State diagrams are used to visualize and describe the behavior of a state machine. Each bubble is a state, which may also describe the output. The arrow between the bubbles are transitions. Alongside are the input condition for which the transition occur.

fsm diagram


There are three parts to any FSM:

fsm diagram

  1. The next-state logic that takes current input and current state and determine what the next state should be.
  2. The registers that holds the memory of the current state.
  3. The output logic that takes current state (and current input sometimes) and produces an appropriate output.

In Verilog, it should look like:

module FSM(...);

    // Use enumerations for states
    enum {
        // ...
    } current, next;

    // Combinational next state logic
    always_comb begin
        case (current)
            // Assign next state based on input and current state

    // Non blocking sequential logic
    always_ff @(posedge clk, negedge rst) begin
        current <= rst == 0 ? INIT : next;

    // Combinational output logic
    always_comb begin
        // Logic for output here

(Note: this isn’t proper Verilog and it won’t synthesize)

  1. Mealy Machine