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

The Two-Process Style

Most major synthesis and FPGA vendor guidelines recommend the two-process state machine style. This architecture partitions logic into two procedural blocks: one sequential block for the state register, and one combinational block for transitions and outputs.

Structural Partitioning

The two blocks divide sequential and combinational logic clearly:

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

// 2. Combinational next-state and output decoding
always @(*) begin
    next_state = state;
    out        = 1'b0;
    case (state)
        IDLE:   if (start) next_state = ACTIVE;
        ACTIVE: begin
                    out = 1'b1;
                    if (done) next_state = IDLE;
                end
    endcase
end

The first block infers physical flip-flops for signal state. The second block uses always @(*) and blocking (=) assignments to describe pure combinational logic.

Advantages of the Two-Process Architecture

This style provides several architectural benefits:

  • Clear separation between clocked storage and combinational evaluation.
  • Simplified timing analysis of combinational decision paths.
  • Direct visibility of state transitions during behavioral simulation.
  • Support for both zero-latency Mealy outputs and state-based Moore outputs.

Implementing Moore and Mealy Outputs

Both machine types share the same two-process template. They differ only in how outputs are decoded.

Moore Output Implementation

In a Moore machine, output values depend exclusively on the current state:

// Moore: Output depends only on state
always @(*) begin
    next_state = state;
    grant      = 1'b0;
    case (state)
        ARBITRATE: if (winner) next_state = GRANTED;
        GRANTED:   grant = 1'b1;
    endcase
end

Mealy Output Implementation

In a Mealy machine, output values depend on both the current state and dynamic input signals:

// Mealy: Output depends on state and inputs
always @(*) begin
    next_state = state;
    grant      = 1'b0;
    case (state)
        ARBITRATE: begin
            grant = req;
            if (req) next_state = GRANTED;
        end
    endcase
end

Preventing Transparent Latches

To prevent latch inference, define default values at the beginning of the combinational block:

next_state = state;
out        = 1'b0;

These default assignments guarantee that every variable receives an evaluation across all branches and unlisted conditions.

Sequence Detector Example

The following module implements a 10 sequence detector using the two-process style:

`timescale 1ns / 1ps
`default_nettype none

module packet_detector_2proc (
    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;
        found      = 1'b0;
        case (state)
            S0: if (bit_in) next_state = S1;
            S1: if (!bit_in) begin
                    found      = 1'b1;      // Combinational: asserts during the same cycle as bit 0
                    next_state = S0;
                end else begin
                    next_state = S1;
                end
        endcase
    end
endmodule

Unlike the one-process implementation, output found is combinational. It asserts immediately within the same clock cycle that input bit_in = 0 arrives.

The two-process style represents the industry standard for finite state machines. The next article examines the three-process style, which isolates next-state logic and output decoding into separate blocks.

← The One-Process Style The Three-Process Style →