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

The State Machine in RTL

Section 3.3 explained state machines using manual schematic methods: state diagrams, excitation tables, Boolean equations, and gate networks. This section implements identical finite state machines (FSMs) using Verilog RTL. The underlying hardware structures remain unchanged. The synthesis tool infers physical gates directly from behavioral code descriptions.

The Three Core Hardware Sub-Circuits

Every finite state machine contains three primary functional blocks (Figure 1):

  1. State register
  2. Next-state combinational logic
  3. Output combinational logic

Three circuits
Figure 1: State register, next-state logic, output logic

1. State Register

The state register contains edge-triggered flip-flops that store the current state. The register loads the evaluated next state on every active clock edge:

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

The register width depends on the encoding scheme. For four states, binary encoding requires two bits. One-hot encoding requires four bits.

2. Next-State Logic

Next-state logic is purely combinational. It computes next_state based on the current state and external inputs:

always @(*) begin
    next_state = state;      // Default condition: retain current state
    case (state)
        IDLE:    if (start) next_state = ACTIVE;
        ACTIVE:  if (done)  next_state = DONE;
        DONE:               next_state = IDLE;
        default:            next_state = IDLE;
    endcase
end

Assigning next_state = state; before the case statement prevents unwanted latch inference. Any branch that does not specify an explicit transition retains the current state.

3. Output Logic

The output logic generates control signals. Moore and Mealy architectures handle output decoding differently.

In a Moore machine, outputs depend only on the current state:

always @(*) begin
    active_led = (state == ACTIVE);
    done_led   = (state == DONE);
end

In a Mealy machine, outputs depend on both the current state and external inputs:

always @(*) begin
    grant = (state == ARBITRATE) && req;
end

Both architectures use the same three sub-circuits. A Moore machine simply omits the input feedforward connection into the output block.

State Encoding Definitions

Define state names using localparam rather than parameter. A parameter allows external module overrides during instantiation. Overriding internal state encodings damages FSM integrity:

localparam [1:0] IDLE   = 2'b00,
                 ACTIVE = 2'b01,
                 DONE   = 2'b10,
                 ERROR  = 2'b11;

Specify explicit bit widths (such as [1:0]) to ensure definitions match the state register declaration.

Common encoding styles include:

  • Binary encoding: Uses $\lceil \log_2 N \rceil$ registers for $N$ states. It minimizes flip-flop count, but requires multi-level decode logic. This style suits standard ASIC cell libraries.
  • One-hot encoding: Uses $N$ registers for $N$ states, where exactly one bit asserts at any time:
localparam [3:0] RED    = 4'b0001,
                 GREEN  = 4'b0010,
                 YELLOW = 4'b0100,
                 PED    = 4'b1000;

One-hot encoding decodes states using single-bit wire taps (such as state[1]). On FPGA architectures with abundant flip-flops, one-hot encodings reduce lookup-table levels and maximize clock frequency.

  • Gray encoding: Consecutive transitions change only one bit, reducing dynamic switching power in sequential counters.

Synthesis tools support attributes to guide state encoding selection:

(* fsm_encoding = "one_hot" *) reg [3:0] state, next_state;

Comparing Moore and Mealy Architectures in RTL

Parameter Moore Model Mealy Model
Output dependencies Current state only Current state and primary inputs
Output timing Updates following active clock edge Updates immediately when inputs change
Glitch vulnerability Glitch-free when registered Susceptible to combinational input glitches
State complexity Requires more states Requires fewer states

Moore machines are preferred in standard digital systems because registered outputs eliminate downstream glitch propagation. Mealy machines are selected when a system requires zero-cycle response time.

Practice Exercise

Analyze the pedestrian traffic-light controller in Figure 2:

Traffic light state diagram
Figure 2: A pedestrian traffic light

  1. Determine the required flip-flop count for binary encoding and one-hot encoding.
  2. Write the localparam declarations for binary encoding.
  3. List all input signals driving the next-state logic block.
  4. List the signals driving the output logic for Moore outputs red, green, yellow, and walk.
  5. An output signal hurry asserts in GREEN when input cross_traffic asserts. Identify if hurry is Moore or Mealy.
Solution
  1. Four states require two flip-flops in binary encoding and four flip-flops in one-hot encoding.
  2. Declarations:
localparam [1:0] RED      = 2'b00,
                 GREEN    = 2'b01,
                 YELLOW   = 2'b10,
                 PED_WAIT = 2'b11;

reg [1:0] state, next_state;
  1. The next-state logic receives state, timer_done, ped_req, and ped_timer_done.
  2. The Moore output logic receives only state.
  3. Signal hurry is a Mealy output because it depends on both state and external input cross_traffic:
assign hurry = (state == GREEN) && cross_traffic;

Designers implement these three sub-circuits using one, two, or three procedural always blocks. The next three articles examine each implementation style.

← Pipeline Registers and Valid Signals The One-Process Style →