2. FSM Reduction and Encoding

Question.7

A four-state one-hot FSM uses the following encoding:

StateQ3 Q2 Q1 Q0
A0001
B0010
C0100
D1000

When X=1, the FSM advances:

A → B → C → D → A

When X=0, it holds its present state. Since D2 = Q2+, which equation correctly drives flip-flop D2?

Need Help? Refer to the Quick Guide below

FSM Fundamentals and Architecture

What Is an FSM?

A Finite State Machine (FSM) is a sequential control circuit with a limited number of operating states. It remembers the present state and uses the inputs to choose the next state.

Example: a simple process controller.

IDLE → RUN → DONE
  • IDLE – waits for a start command.
  • RUN – performs the operation.
  • DONE – indicates completion.

FSMs are widely used in controllers, sequence detectors, interfaces, and control logic.

The state register changes only at the active clock edge except when an asynchronous reset is asserted. Between active edges, it keeps the same state.

FSM Architecture

An FSM has three main blocks:

  • State Register – stores the present state.
  • Next-State Logic – decides the next state.
  • Output Logic – generates outputs.
FSM Architecture

Key terms:

  • State: Current operating condition.
  • Transition: Change from one state to another.
  • Transition Condition: Input or event that causes a transition.
  • Present State: Current state stored in the register.
  • Next State: State selected for the next clock edge.

D flip-flops are normally used for the state register because the required next-state bit can be connected directly to the D input.

States are used for control. Use counters, registers, or flags to store values.

FSM Representation

State Diagram

A state diagram shows how an FSM behaves. Each node represents a state, and each arrow shows when the FSM moves to another state.

Consider a 2-bit sequence generator controlled by CTR:

CTR = 0: 00 → 10 → 11 → 01 → 00
CTR = 1: 00 → 01 → 11 → 10 → 00
FSM Architecture

The state bits also form the generated 2-bit output sequence.

State Table

A state table gives the same information in tabular form. It shows the next state selected for every relevant present-state and input combination.

Present StateCTRNext State
00010
00101
01000
01111
10011
10100
11001
11110

Transition Priority

When several active-high inputs are 1 together, priority decides which input controls the transition.

Example priority:

FAULT > DONE > START
FAULTDONESTARTInput Considered
000None
001START
01XDONE
1XXFAULT

Higher-priority inputs override lower-priority inputs.

Moore and Mealy FSM

Moore FSM

In a Moore FSM, the output depends only on the present state.

Output = f(Present State)

Example: a 3-state process with output COMPLETE active only in state 10.

Moore FSM
Present StateConditionNext StateCOMPLETE
00START=0000
00START=1010
01DONE=0010
01DONE=1100
10RESET=0101
10RESET=1001
Circuit diagram of a three-state Moore finite state machine with state register, next-state logic, and state-only output logic.
  • Timing: Output changes after the state changes.
  • Advantage: Stable state-controlled outputs.
  • Limitation: May require extra states.
  • Use when: Output stability is more important than immediate response.

Applications: process control, signalling, mode control, safety outputs.

Mealy FSM

In a Mealy FSM, the output depends on the present state and input.

Output = f(Present State, Input)

Example: a 3-state FSM with input X and output Z.

Output Z becomes 1 when State = 10 and X = 1

Mealy FSM
Present StateXNext StateZ
000000
001010
010100
011010
100000
101011
Circuit diagram of a three-state Mealy finite state machine with state register and input-dependent output logic.


Timing: Output can change immediately with the input.

  • Advantage: Fast response and often fewer states.
  • Limitation: Output can be input-sensitive or glitch-prone.
  • Use when: Immediate response is important.

Applications: sequence detection, handshaking, event detection, fast control decisions.

Hybrid FSM: An FSM containing both Moore-type and Mealy-type outputs is commonly called a hybrid FSM.

State Reduction

Unreachable and Equivalent States

  • Unreachable State: A defined state that cannot be reached from reset through legal transitions.
  • Equivalent States: States that produce the same required output and equivalent future behavior.

Example with six states:

A, B, C, D are reachable.
E and F are unreachable.
C and D are equivalent.

State Reduction Method

Consider this FSM:

State Reduction FSM
StateOutputX=0X=1Observation
A0BCReachable
B1BDReachable
C0ACEquivalent to D
D0ADEquivalent to C
E0EFUnreachable
F1FEUnreachable

Reduction:

  1. Remove unreachable states E and F.
  2. Merge equivalent states C and D.
  3. Verify that external behavior is unchanged.

Reduced FSM:

Reduced FSM

Important: Fewer states do not always mean smaller or faster hardware.

State Encoding

State encoding assigns a binary bit pattern to each symbolic FSM state so that the state can be stored in flip-flops.

Binary Encoding

Binary encoding uses the minimum number of state bits.

Number of flip-flops = ceil(log2(Number of states))

For four states:

ceil(log2(4)) = 2 flip-flops
State NameEncoded Value
IDLE00
LOAD01
RUN10
DONE11

Main benefit: low flip-flop count.

One-Hot Encoding

One-hot encoding uses one flip-flop for each state. Only one state bit is normally 1.

For five states:

Number of flip-flops = Number of states = 5
State NameEncoded Value
CLOSE00001
OPENING00010
OPEN00100
CLOSING01000
WAIT10000

One-hot encoding is commonly preferred in FPGA-oriented designs because state decoding and transition logic can be simple.

Gray Encoding

Gray encoding assigns codes so that selected adjacent states differ by only one bit.

For four states:

2 flip-flops are required.
State NameEncoded Value
IDLE00
LOAD01
RUN11
DONE10

Main benefit: reduced state-bit switching on suitable transition paths.

Encoding Comparison

FeatureBinaryOne-HotGray
DefinitionMinimum-bit state codesOne bit per stateAdjacent codes differ by one bit where possible
LogicMore decoding may be neededUsually simpleDepends on transition structure
AdvantageFew flip-flopsSimple decodingLower switching on selected transitions
LimitationUnused codes and more logic may occurUses more flip-flopsNot ideal for every FSM graph
When to UseCompact implementationFPGA-oriented controlSwitching reduction is useful

FSM Implementation Idea

This example implements a four-state Moore FSM using binary encoding.

Machine Requirements

States:

IDLE → BUSY → DONE
          ↓
        ERROR

Inputs:

  • run
  • fault

Outputs:

  • success – 1 only in DONE.
  • failure – 1 only in ERROR.

Behavior:

  • IDLE: run=0 → stay in IDLE; run=1 → BUSY.
  • BUSY: fault=1 → ERROR.
  • BUSY: if fault=0, run=0 → stay in BUSY; run=1 → DONE.
  • DONE: run=1 → stay in DONE; run=0 → IDLE.
  • ERROR: return to IDLE only when run=0 and fault=0; otherwise remain in ERROR.

State Diagram

FSM of Simple Machine

Binary State Encoding

StateQ1Q0
IDLE00
BUSY01
DONE10
ERROR11

Two D flip-flops are required.

Excitation Table

For D flip-flops:

D1 = Q1+
D0 = Q0+
Q1Q0runfaultQ1+Q0+D1D0successfailure
0000000000
0001000000
0010010100
0011010100
0100010100
0101111100
0110101000
0111111100
1000000010
1001000010
1010101010
1011101010
1100000001
1101111101
1110111101
1111111101

K-Maps

Rows use Q1Q0 = 00, 01, 11, 10.
Columns use run fault = 00, 01, 11, 10.

K-map for D1

Q1Q0 \ run fault00011110
000000
010111
110111
100011

K-map for D0

Q1Q0 \ run fault00011110
000011
011110
110111
100000

K-map for success

Q1Q0 \ run fault00011110
000000
010000
110000
101111

K-map for failure

Q1Q0 \ run fault00011110
000000
010000
111111
100000

Input and Output Equations

D1 = Q0·fault + Q0·run + Q1·run
D0 = Q0·fault + Q0·Q1·run + Q0·Q1'·run' + Q1'·Q0'·run

success = Q1·Q0'
failure = Q1·Q0
Complete two-bit binary encoded FSM circuit using D flip-flops, next-state logic, and success and failure output logic.

FSM State Integrity and Recovery

Illegal States

An illegal state is a state-register code that was not assigned to any valid state.

Example with three valid states:

A = 00
B = 01
C = 10
11 = Illegal

The FSM should define a safe action if 11 occurs instead of leaving its behavior unknown.

Lock-Up/Deadlock State

A lock-up occurs when the FSM becomes trapped and cannot make required progress.

Example:

START → RUN ↔ WAIT
             
DONE is never reached.

The RUN-WAIT loop becomes a lock-up if no legal exit exists.

Reset State

The reset state is the known and safe state entered after reset.

Example:

RESET → IDLE → RUN → DONE

With a synchronous reset, recovery occurs at the active clock edge. With an asynchronous reset, the state register changes as soon as reset is asserted. In both cases, reset must have clear priority over normal transitions.

State Explosion

State explosion occurs when too many states are created to represent counts, delays, or stored values.

Avoid:

WAIT0 → WAIT1 → WAIT2 → ... → WAIT63 → DONE

Prefer:

IDLE → WAIT → DONE
         +
      Counter

Safe-State Recovery

A safe FSM defines recovery from invalid operation.

Example:

IDLE → RUN → DONE
          ↑
Illegal State → SAFE → IDLE

During recovery, critical outputs should be driven to safe values.

Reliable and Scalable FSM Design

Input Synchronization

External inputs may not be aligned with the FSM clock.

Conceptually:

Asynchronous Input → D Flip-Flop → FSM

A clocked flip-flop aligns the signal with the FSM clock, but one flip-flop alone does not provide reliable metastability containment.

For practical metastability-safe synchronization, asynchronous control inputs are commonly passed through a proper synchronizer, often two cascaded D flip-flops.

Combinational and Registered Outputs

Output TypeMain BenefitLimitation
CombinationalImmediate responseCan glitch
RegisteredStable, clock-aligned outputAdds latency

Example with a three-state controller:

IDLE → RUN → DONE
  • A combinational Done output can respond immediately to logic conditions.
  • A registered Done output changes only on a clock edge.

Use registered outputs when stability is more important than immediate response.

FSM Partitioning

A large FSM can be split when one group of states performs a separate function.

Monolithic example:

FSM with Multiple States

The execution states can be moved into a sub-FSM.

Partitioned FSM

Partition when:

  • A group of states performs one clear sub-function.
  • The main FSM only needs simple Start, Done, or Error status.
  • Splitting makes the controller easier to understand and verify.

FSM Applications

Elevator Door Controller

The elevator door FSM controls opening, closing, obstacle handling, and a timed wait before reopening.

Circuit Requirements

The controller uses five states:

CLOSE, OPENING, OPEN, CLOSING, WAIT

Active-HIGH inputs:

obs, opn, cls

Input priority:

obs > opn > cls

CLOSE is the reset state. Only inputs relevant to the current state are evaluated. If more than one applicable input is HIGH, the priority order is followed.

State Diagram

Elevator Door Controller State Diagram

If no transition condition is active, the FSM remains in its current state.

One-Hot Encoding

Five states use five state flip-flops.

StateQ4 Q3 Q2 Q1 Q0
CLOSE00001
OPENING00010
OPEN00100
CLOSING01000
WAIT10000

WAIT-State Counter

A 2-bit T-flip-flop counter measures the three one-second intervals required in WAIT. The counter and state register use the same rising-edge clock.

~CLR0 = WAIT
~CLR1 = WAIT
T0 = 1
T1 = C0
cof = C1 · C0

Both T flip-flops use a 1-second clock tick.

  • WAIT = 0 → active-low clear resets the counter to 00.
  • WAIT = 1 releases the counter, which follows 00 → 01 → 10 → 11.
  • The FSM enters WAIT with the counter cleared to 00.
  • The next three rising edges load 01, 10, and 11.
  • After the third edge, cof = 1. The FSM samples it at the following rising edge and changes WAIT → OPENING.
Time in WAITCountercofFSM action
Entry000Remain in WAIT
1 s010Remain in WAIT
2 s100Remain in WAIT
3 s111Request OPENING at the next edge

This timing holds WAIT for three complete seconds. If the design must enter OPENING exactly on the third rising edge, generate the terminal-count request from count 10 instead.

The counter avoids creating separate WAIT1, WAIT2, and WAIT3 states, preventing state explosion.

Excitation Table for D Flip-Flops

X = either HIGH or LOW; the input does not affect that transition.

Current StateobsopnclscofNext StateD4D3D2D1D0
00001X0XX0000100001
00001X1XX0001000010
00010XXXX0010000100
00100XX0X0010000100
00100XX1X0100001000
010001XXX1000010000
0100001XX1000010000
0100000XX0000100001
10000XXX01000010000
10000XXX10001000010

D Flip-Flop Equations

For present-state bits Q4...Q0:

D4 = Q3 · (obs + opn) + Q4 · ~cof
D3 = Q2 · cls
D2 = Q1 + Q2 · ~cls
D1 = Q0 · opn + Q4 · cof
D0 = Q0 · ~opn + Q3 · ~obs · ~opn

Circuit Diagram

Elevator Door Controller Circuit

3-Digit Authentication System

The authentication FSM checks the entered digits one by one. The valid code is 784. A wrong digit resets the sequence, while the correct sequence unlocks the system.

Circuit Requirement

The system uses four states:

00 → Waiting for 1st digit
01 → 1st digit correct
10 → 2nd digit correct
11 → UNLOCKED

The circuit uses:

  • Two state D flip-flops
  • 10-to-4 encoder
  • Press detector
  • INVALID output
  • UNLOCK output
  • RESET

Valid code:

784

Only one digit button is pressed at a time, and it is released before the next digit is entered.

State Diagram

3-digit Authentication System State Diagram

No key press retains the current state. RESET returns the FSM to 00 and is omitted from the diagram for clarity.

Input-Output Logic

Correct digit → Next state
Incorrect digit → 00
RESET → 00
No press → Hold current state

INVALID becomes HIGH for one incorrect-digit press.

UNLOCK becomes HIGH when the FSM reaches state 11.

Once:

UNLOCK = 1

all digit presses are ignored.

Only RESET returns the system to the locked state.

Encoding

Digit buttons 1 to 9 use a 10-to-4 encoder. Input I0 is tied LOW.

The encoder produces an active-HIGH ordinary binary code. Only one digit button may be pressed at a time. The encoded digit must remain stable while PR_EDGE is HIGH. With no button pressed, 0000 means no press, not digit 0.

DigitD C B A
10001
20010
30011
40100
50101
60110
70111
81000
91001

All four encoder outputs are used.

Press Detector

The press detector converts a held button into one clock-cycle event.

First combine the encoder outputs:

PR = A + B + C + D

Pass PR through an active-HIGH rising-edge detector:

PR → Rising-Edge Detector → PR_EDGE

The encoded digit is passed only during PR_EDGE:

A_valid = A · PR_EDGE
B_valid = B · PR_EDGE
C_valid = C · PR_EDGE
D_valid = D · PR_EDGE

This prevents one long button press from being detected multiple times.

This FSM uses all four possible 2-bit state values:

00, 01, 10, 11

Therefore, no unused state exists.

nRESET always forces the FSM to:

00

Excitation Table

Present StateInput ConditionNext StateD1D0INVALIDUNLOCK
00No press000000
00Digit 7 (0111)010100
00Any other pressed digit000010
01No press010100
01Digit 8 (1000)101000
01Any other pressed digit000010
10No press101000
10Digit 4 (0100)111100
10Any other pressed digit000010
11Any input111101

D-Input Equations

For simpler implementation, define:

K7 = D_valid' · C_valid · B_valid · A_valid
K8 = D_valid · C_valid' · B_valid' · A_valid'
K4 = D_valid' · C_valid · B_valid' · A_valid'

NP = D_valid' · C_valid' · B_valid' · A_valid' //No Press

For present-state bits Q1 Q0:

D1 = Q1' · Q0 · K8
   + Q1 · (Q0 + NP + K4)
D0 = Q0' · (Q1' · K7 + Q1 · K4)
   + Q0 · (Q1 + NP)

Moore output:

UNLOCK = Q1 · Q0

A valid key press is detected by:

PRESS = NP'

Correct-digit logic:

CORRECT = Q1' · Q0' · K7
        + Q1' · Q0 · K8
        + Q1 · Q0' · K4

Mealy invalid output:

INVALID = PRESS · CORRECT' · (Q1 · Q0)'

Circuit Diagram

3-Digit Authentication System Circuit Alt Text: Binary encoded 3-digit authentication FSM for code 784 using a 10-to-4 encoder, press detector, two D flip-flops, INVALID output, UNLOCK output, and nRESET.

Automatic Subway Signalling Controller

Each signal uses the same FSM. A train passing the current signal makes it RED. Events from the next signal then move it through YELLOW and back to GREEN.

Circuit Requirement

States:

GREEN, RED, YELLOW

Active-HIGH inputs:

TR
NXT

Active-HIGH output:

PRV

GREEN is the reset state.

PRV of the current signal is connected to NXT of the previous signal.

For deterministic operation, TR_P is given priority if TR_P and NXT occur together.

State Diagrams

The transition label uses NXT / PRV because each diagram fixes the value of TR_P.

For TR_P = 0, the downstream event NXT advances RED → YELLOW → GREEN. NXT does not affect GREEN.

Signalling System State Diagram for TR_P = 0

For TR_P = 1, train detection has priority over NXT. The controller enters or remains in RED.

X = either HIGH or LOW; the value of NXT does not affect that transition.

One-Hot State Encoding

StateQ2 Q1 Q0
GREEN001
RED010
YELLOW100

Edge Detector

The digital TR input may remain HIGH for more than one clock cycle. A rising-edge detector converts it into a single pulse:

TR → Rising-Edge Detector → TR_P

The FSM uses TR_P, not the held TR level.

Invalid-State Recovery

Any invalid one-hot pattern is forced back to:

GREEN = 001

Excitation Table

X = either HIGH or LOW; the value does not affect that transition. Invalid encodings recover to 001.

Current StateTR_PNXTNext StateD2D1D0PRV
0011X0100101
0010X0010010
0101X0100100
010011001001
010000100100
1001X0100101
100010010011
100001001000
InvalidXX0010010

D-Input Equations

For valid one-hot states, let:

G = Q0
R = Q1
Y = Q2
T = TR_P
N = NXT

Then:

D0 = ~T · (G + Y · N)
D1 = T + ~T · R · ~N
D2 = ~T · (R · N + Y · ~N)

Mealy output:

PRV = T · (G + Y) + ~T · N · (R + Y)

Note: Before operating the circuit, press nRESET to initialize it to a valid state.

Circuit Diagram

One-hot subway signalling FSM with TR edge detector, NXT digital input, GREEN RED YELLOW state flip-flops, and PRV digital output.

This diagram contains one signal controller. NXT is provided using a digital input, and PRV is monitored using a digital output.

FSM Communication

For two adjacent controllers:

  • Initially, Signal 1 and Signal 2 are GREEN.
  • When a train passes Signal 1, TR1_P = 1 and Signal 1 → RED.
  • When the train later passes Signal 2, Signal 2 → RED and PRV2 = 1.
  • PRV2 is connected to NXT1, so Signal 1: RED → YELLOW.
  • A later NXT2 event advances Signal 2, generating another PRV2 pulse.
  • This pulse advances Signal 1: YELLOW → GREEN.

Connection:

NXT1 = PRV2

Both PRV1 and PRV2 can be monitored using digital outputs.

Complete Communication Circuit Diagram

Two one-hot subway signal FSM controllers connected so PRV2 drives NXT1, with NXT2 as the downstream input and PRV1 and PRV2 monitored on digital outputs.

Select Answer

Restart quiz!