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

The One-Process Style

A finite state machine combines state storage, next-state evaluation, and output decoding. In the one-process coding style, a single sequential always block implements all three functions.

Implementation Architecture

In this style, all signal assignments occur within a clocked block using nonblocking (<=) operators:

always @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
        state <= IDLE;
        out   <= 1'b0;
    end else begin
        case (state)
            IDLE: begin
                out <= 1'b0;
                if (start) state <= ACTIVE;
            end
            ACTIVE: begin
                out <= 1'b1;
                if (done) state <= IDLE;
            end
        endcase
    end
end

The synthesis tool infers flip-flops for both state and out. Both signals update synchronously on active clock edges.

Architectural Advantages

The one-process style offers two distinct advantages:

  1. Compact source code: All state transitions and output assignments reside in one unified structure.
  2. Glitch-free registered outputs: Because outputs come directly from flip-flops, they cannot experience combinational hazards. These outputs can drive clock-domain crossing synchronizers or external chip pins safely.

Latency Characteristics

Consider the IDLE state in the example. When input start asserts, the machine schedules state to become ACTIVE at the active clock edge. At that identical clock edge, the IDLE branch assigns out <= 1'b0. Signal out transitions to 1 only at the subsequent clock edge when the ACTIVE branch executes.

Consequently, outputs update one clock cycle after the corresponding state transition occurs. The output reflects the originating state rather than the destination state.

This one-cycle latency is acceptable for standard Moore outputs. However, this style cannot describe Mealy outputs that must respond combinationally within the same clock cycle.

Sequence Detector Example

Figure 1 shows a state diagram that detects the two-bit binary sequence 10 on a serial line.

Two-state detector
Figure 1: The detector's two states

The following module implements this detector using the one-process style:

`timescale 1ns / 1ps
`default_nettype none

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

    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            state <= S0;
            found <= 1'b0;
        end else begin
            case (state)
                S0: begin
                    found <= 1'b0;
                    if (bit_in) state <= S1;
                end
                S1: begin
                    if (!bit_in) begin
                        found <= 1'b1;      // Registered: asserts one cycle after bit 0 arrives
                        state <= S0;
                    end else begin
                        found <= 1'b0;
                        state <= S1;
                    end
                end
            endcase
        end
    end
endmodule

module tb_packet_detector_1proc;
    reg  clk = 0, rst_n, bit_in;
    wire found;

    packet_detector_1proc dut (.clk(clk), .rst_n(rst_n), .bit_in(bit_in), .found(found));
    always #5 clk = ~clk;

    integer   i;
    reg [4:0] stream = 5'b10100;     // Contains two '10' sequences

    initial begin
        $dumpfile("tb.vcd");
        $dumpvars(0, tb_packet_detector_1proc);

        rst_n = 0; bit_in = 0;
        @(negedge clk); rst_n = 1;

        for (i = 4; i >= 0; i = i - 1) begin
            bit_in = stream[i];
            @(negedge clk);
            $display("t=%0t bit_in=%b state=%b found=%b", $time, bit_in, dut.state, found);
        end

        $finish;
    end
endmodule

In the simulation log, signal found asserts one clock cycle after the 0 bit completes the sequence.

Designers select the one-process style when timing closure and glitch elimination require registered outputs. The next article examines the two-process style, which separates sequential registers from combinational next-state logic.

← The State Machine in RTL The Two-Process Style →