Skip to content

Digital Logic: Finite State Machines

Objectives

  • Understand the fundamental concepts of a finite state machine (FSM).
  • Understand the difference between Moore and Mealy state machines.
  • Master describing a state machine in Verilog.

1. Finite State Machines

1.1 What Is a Finite State Machine?

A finite state machine (FSM) is a sequential logic circuit model that:

  • Has a finite number of states.
  • Moves from its current state to a next state under clock control, according to the current state and input signals.
  • Produces outputs based on the state (and inputs).

State machines are fundamental tools in digital-system design and are widely used for control logic, protocol processing, sequence detection, and other applications.

1.2 Components of a State Machine

image-20260409201210216

1.3 Moore Versus Mealy Machines

Characteristic Moore machine Mealy machine
Output depends on Current state only Current state + inputs
Output changes After a state transition May change immediately when an input changes
Number of states Usually greater Usually smaller
Output stability More stable and synchronized to the clock May produce glitches
Typical use Control logic and outputs that must be stable Logic that must respond quickly to inputs

image-20260409201148039

This course recommends Moore machines because their outputs are more stable and they are easier to understand and debug.

2. Three-Process State-Machine Template

The three-process style places the three parts of a state machine in three separate always blocks. This structure is clear, maintainable, and is the recommended standard style.

2.1 Process 1: State Register (Sequential Logic)

// Process 1: State register - update the state on the clock edge
always @(posedge clk or negedge rst_n) begin
    if (!rst_n)
        state <= IDLE;       // Reset to the initial state
    else
        state <= next_state; // Update to the next state
end

2.2 Process 2: Next-State Logic (Combinational Logic)

// Process 2: Next-state logic - determine the next state from the current state and inputs
always @(*) begin
    case (state)
        IDLE: begin
            if (start)
                next_state = STATE_A;
            else
                next_state = IDLE;
        end
        STATE_A: begin
            if (condition)
                next_state = STATE_B;
            else
                next_state = STATE_A;
        end
        STATE_B: begin
            next_state = IDLE;
        end
        default: next_state = IDLE;
    endcase
end

2.3 Process 3: Output Logic (Combinational Logic)

// Process 3: Output logic - determine outputs from the current state only (Moore type)
always @(*) begin
    // Default outputs ensure an assignment on every combinational logic path
    led = 1'b0;
    buzzer = 1'b0;
    case (state)
        IDLE: begin
            led = 1'b0;
        end
        STATE_A: begin
            led = 1'b1;
        end
        STATE_B: begin
            buzzer = 1'b1;
        end
        default: begin
            led = 1'b0;
            buzzer = 1'b0;
        end
    endcase
end

2.4 State Encoding

Define state constants using localparam or parameter:

// State definitions
localparam IDLE    = 2'd0;
localparam STATE_A = 2'd1;
localparam STATE_B = 2'd2;

reg [1:0] state, next_state;

Three-state finite state machine: transitions between IDLE, STATE_A, and STATE_B

Each circle shows the state name and the outputs led and buzzer in that state; arrow labels specify transition conditions, with 1 denoting an unconditional transition. When rst_n = 0, the machine asynchronously resets to IDLE; all other transitions occur on the rising edge of the clock.

Note: Both state and next_state are declared as type reg, but state is assigned in a sequential block (synthesized as a register), while next_state is assigned in a combinational block (synthesized as wires).


3. Two-Process State-Machine Template

The two-process style differs from the three-process style as follows:

  1. Process 1: State register (sequential logic)
  2. Process 2: Combined next-state and output logic (combinational logic)

3.1 Process 1: State Register (Sequential Logic)

// Process 1: State register - update the state on the clock edge
always @(posedge clk or negedge rst_n) begin
    if (!rst_n)
        state <= IDLE;       // Reset to the initial state
    else
        state <= next_state; // Update to the next state
end

3.2 Process 2: Next-State and Output Logic (Combinational Logic)

// Process 2: Next-state and output logic (combined combinational logic)
always @(*) begin
    // Hold the current state and clear outputs by default to avoid inferring latches
    next_state = state;
    led = 1'b0;
    buzzer = 1'b0;

    // Determine the next state and outputs from the current state and inputs
    case (state)
        IDLE: begin
            if (start) begin
                next_state = STATE_A;
            end
        end
        STATE_A: begin
            led = 1'b1;              // Moore-type output
            if (condition)
                next_state = STATE_B;
        end
        STATE_B: begin
            buzzer = 1'b1;
            next_state = IDLE;
        end
        default: begin
            next_state = state;
            led = 1'b0;
            buzzer = 1'b0;
        end
    endcase
end

The state transitions and outputs in this example are the same as those in the state diagram in Section 2.4. The two-process and three-process styles differ in how the always blocks are organized.

Both this example and the three-process example in the previous section are Moore machines: outputs depend only on the current state. While in IDLE, led remains 0 even when start = 1; it becomes 1 only after the machine enters STATE_A on the rising edge of the clock. Two-process and three-process refer to code structure, whereas Moore or Mealy behavior depends on whether outputs depend on the current inputs.

Note: Default assignments before case cover every execution path; assignments inside case override the corresponding defaults. A default branch alone does not provide default values for signals left unassigned in a matched state.

Recommendation: Prefer the three-process style. It requires slightly more code but provides a clearer structure and is easier to maintain and debug.

4. Laboratory Exercise: Count Detection

4.1 Requirements

Use both two-process and three-process styles to design a count-detection circuit with the following characteristics.

Inputs:

  • clk: 100 MHz system clock
  • x_in[4:0]: Five-bit input sampled on each rising edge of clk

Output:

  • y_out: Validity indicator

Function:

On each rising edge of the clock, the circuit samples the bits in x_in. It accumulates the total number of all 1s received.

  • When the accumulated number of 1s is a multiple of 5, y_out = 1 (valid).
  • Otherwise, y_out = 0 (invalid).

Example:

Time x_in 1s in this sample Accumulated 1s y_out
0 0b00001 1 1 0
1 0b00001 1 2 0
2 0b00001 1 3 0
3 0b00011 2 5 1 ✓
4 0b00111 3 8 0
5 0b00010 1 9 0
... ... ... 15 1 ✓

4.2 Moore or Mealy?

  • y_out depends only on the accumulated state, representing the total number of 1s received, and not on the current input.
  • The design is therefore a Moore state machine.

4.3 Simulation and Verification

Use a testbench to verify that the two-process and three-process implementations behave identically:

`timescale 1ns/1ps
module tb_bit_counter();
    reg clk, rst_n;
    reg [4:0] x_in;
    wire y_out_2stage, y_out_3stage;

    bit_counter_2stage u1(...);  // Two-process implementation
    bit_counter_3stage u2(...);  // Three-process implementation

    initial clk = 0;
    always #5 clk = ~clk;

    initial begin
        rst_n = 0; #50; rst_n = 1;

        // Test case: Repeat a single 1 five times
        repeat(5) begin
            x_in = 5'b00001;  #100;
            x_in = 5'b00000;  #100;
        end

        // At this point, the state should return to state=0, with y_out=1
        $finish;
    end
endmodule

4.4 On-Board Verification

Since testing directly with the system clock (100 MHz) is difficult, a button + debouncing + edge detection can be used to emulate clock pulses:

Procedure:

  1. Program the bitstream.
  2. Set five switches to configure x_in[4:0].
  3. Press the button once to sample x_in once.
  4. Observe the result:
  5. Optionally show the current state on the seven-segment display.
  6. Show y_out on an LED.
  7. y_out is asserted when the accumulated number of sampled 1s reaches a multiple of 5.

5. Questions

Q1: Can a Mealy machine be used?

Approach:

  • Compute y_out = (next_state == S0) ? 1'b1 : 1'b0 in combinational logic.
  • The output will then respond immediately to an input change.
  • This approach can produce glitches and must be used carefully.

Q2: Why is a single-process style discouraged?

In a single-process design, state transitions and outputs are placed together in one always @(posedge clk) block.

Disadvantages:

  1. Poor readability: Sequential and combinational logic are mixed and difficult to distinguish.
  2. Error-prone: It is easy to confuse blocking assignments (=) with non-blocking assignments (<=).
  3. Difficult maintenance: Logic changes can easily introduce bugs.
  4. Poor conformity: It does not conform to Verilog HDL design specifications.
  5. Synthesis uncertainty: Different tools may interpret the description differently and produce different synthesis results.