Module 5: Behavioral Verilog for Synthesis · 5.6 Modeling Finite State Machines in Verilog

The Three-Process Style

The two-process style isolates sequential storage from combinational logic. The three-process style divides combinational logic further into two independent blocks: one for next-state transition calculation and one for output signal decoding.

Structural Partitioning Into Three Blocks

The three-process architecture assigns each functional responsibility to a dedicated block:

// 1. Sequential state register
always @(posedge clk or negedge rst_n) begin
    if (!rst_n) state <= IDLE;
    else        state <= next_state;
end

// 2. Next-state combinational transitions only
always @(*) begin
    next_state = state;
    case (state)
        IDLE:   if (start) next_state = ACTIVE;
        ACTIVE: if (done)  next_state = DONE;
        DONE:              next_state = IDLE;
    endcase
end

// 3. Combinational output decoding only (Moore model)
always @(*) begin
    out = 1'b0;
    case (state)
        ACTIVE: out = 1'b1;
    endcase
end

The state register contains physical flip-flops. The second block contains only state transition conditions without output signal assignments. The third block decodes output signals without modifying state transitions.

Architectural Advantages

The three-process style provides several design advantages:

  • Modular clarity: Engineers can review transition conditions independently from output behaviors.
  • Maintenance safety: Modifying output decoding logic cannot introduce unintended bugs into state transition paths.
  • Formal verification support: Model checkers and formal verification tools analyze transition reachability directly from the transition block.

Design Trade-Offs

The three-process style requires maintaining three separate procedural blocks. For simple state machines with two or three states, this structure adds code overhead. Furthermore, when adding a new state, you must update both the transition block and the output decoding block.

Comprehensive Comparison of FSM Styles

Attribute One-Process Style Two-Process Style Three-Process Style
Procedural blocks 1 (sequential) 2 (sequential + combinational) 3 (sequential + 2 combinational)
Output timing Registered (one-cycle latency) Combinational (same cycle) Combinational (same cycle)
Mealy output support No Yes Yes
Transparent latch risk Zero Low (using default assignments) Low (requires defaults in both combinational blocks)
Recommended application Compact control logic; clock domain crossing General industry standard Complex control units; formal verification

Sequence Detector Example

The following code implements the 10 sequence detector using the three-process style:

`timescale 1ns / 1ps
`default_nettype none

module packet_detector_3proc (
    input  wire clk, rst_n, bit_in,
    output reg  found
);
    localparam S0 = 1'b0, S1 = 1'b1;
    reg state, next_state;

    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) state <= S0;
        else        state <= next_state;
    end

    always @(*) begin
        next_state = state;
        case (state)
            S0: if (bit_in)  next_state = S1;
            S1: if (!bit_in) next_state = S0;
                else         next_state = S1;
        endcase
    end

    always @(*) begin                       // Mealy decoding: depends on state and bit_in
        found = 1'b0;
        if (state == S1 && !bit_in) found = 1'b1;
    end
endmodule

The synthesized hardware across all three styles remains functionally identical. The primary difference lies in code maintainability and verification convenience.

Practice Exercise

Consider a four-state vending machine controller with states IDLE, COIN_RECEIVED, DISPENSE, and CHANGE_BACK. Evaluate which coding style makes the output logic for dispense and change_out easiest to verify.

Solution

The three-process architecture provides the clearest separation for output verification. A code reviewer can examine the output decoding block directly without reading complex transition conditions.

In the one-process and two-process architectures, output statements interleave with transition checks. Verifying that dispense asserts only during DISPENSE requires reading unrelated state transition branches. Article 5.6.6 presents a complete vending machine design with datapath integration.

The next article examines state encoding methods and safe FSM design techniques for fault recovery.

← The Two-Process Style Encoding and Safe State Machines →