Knowledge / Electronics and Communication Engineering / Finite state machine design

Finite state machine design

Electronics and Communication Engineering (B.E./B.Tech) · Digital electronics · free study notes for exams, placements and interviews

In short

A finite state machine is a clocked sequential circuit whose behaviour is described by a finite set of states, a next-state function and an output function - it remembers "where it is" in a sequence rather than just the present input. FSM design is the formal procedure that converts a word statement (detect the pattern 1011, control a traffic signal, run a UART) into a state diagram, a state table, a state assignment and finally flip-flops plus combinational logic. It is the highest-scoring sequential-logic area in GATE EC/EE/CS, and in digital-design interviews at Intel, Qualcomm, Texas Instruments, Samsung, Nvidia, Micron, Bosch, Tessolve, Sasken, BEL, ISRO and DRDO almost every candidate is asked to draw one FSM on the whiteboard. Every protocol controller, arbiter, memory controller and datapath controller inside an ASIC or FPGA is an FSM.

Explained

Definition, model and the two machine types

Representations: state diagram, state table, ASM chart

The nine-step classical design procedure

  1. Read the specification - fix clock, inputs, outputs, reset, whether patterns may overlap, and Moore or Mealy.
  2. Draw the state diagram - one state per "amount of useful history remembered". Name states by the input prefix matched (∅, 1, 10, 101 …).
  3. Write the state table.
  4. State reduction - remove equivalent/redundant states (next section).
  5. State assignment - give each state a binary code; decide binary / Gray / one-hot.
  6. Choose the flip-flop (D in all modern synthesis; JK/T/SR appear in exams).
  7. Form the excitation (transition) table - for each flip-flop write the required input from the present → next state pair: D = next state; T = Q ⊕ Q⁺; JK from 0→0: J=0,K=X; 0→1: J=1,K=X; 1→0: J=X,K=1; 1→1: J=X,K=0.
  8. Minimise each flip-flop input and each output by K-map/QM, using unused states as don't-cares if a *minimal* machine is wanted.
  9. Draw the circuit and verify by simulation and by walking the original sequence through it.

State reduction and machine equivalence

State assignment, unused states and encoding styles

Timing, inputs and practical issues

Key points

Formulas and examples

n = ⌈log₂ S⌉ - minimum flip-flops for S states, binary encoding (n in bits). Unused states = 2ⁿ − S.
Number of distinct binary state assignments = (2ⁿ)! / (2ⁿ − S)! ; dividing by n! × 2ⁿ (flip-flop permutation and complementation) gives the functionally distinct ones.
One-hot: flip-flops = S; next-state logic Dᵢ = Σ (source state × arc condition) over all arcs entering state i.
Maximum states a machine remembering the last k input bits can need = 2k.
Overlapping k-bit sequence detector: Mealy states = k, Moore states = k + 1.
Mealy → Moore state bound: S_Moore ≤ S_Mealy × (number of distinct outputs per state).
T_min = t_cq + t_pd(next-state logic) + t_su + t_skew ; f_max = 1 / T_min (t in ns, f in GHz if T in ns).
Hold check: t_cq + t_pd,min ≥ t_h + t_skew.
Excitation: D = Q⁺ ; T = Q ⊕ Q⁺ ; JK: (0→0) 0,X · (0→1) 1,X · (1→0) X,1 · (1→1) X,0.

Example 1 - Moore "101" overlapping detector, with D and then JK flip-flops. States: S0 = nothing, S1 = "1", S2 = "10", S3 = "101" (Z = 1 only in S3). Codes S0=00, S1=01, S2=10, S3=11 (Q₁Q₀).

Q₁Q₀ (state)x = 0 → nextx = 1 → nextZ
00 (S0)00010
01 (S1)10010
10 (S2)00110
11 (S3)10011

Transition table for Q₁⁺ over (Q₁Q₀x): 1 at 010, 101, 110 only.

Example 2 - Mealy "1011" overlapping detector. States A = ∅, B = "1", C = "10", D = "101". After 1011 the longest useful suffix is "1", so D on x=1 outputs 1 and goes to B.

Statex=0 → next/Zx=1 → next/Z
A (00)A / 0B / 0
B (01)C / 0B / 0
C (10)A / 0D / 0
D (11)C / 0B / 1

Example 3 - state reduction by equivalence partitioning.

PresentN(x=0)N(x=1)Z
AAB0
BCB0
CAD0
DEB1
EAD0

Example 4 - flip-flop count, assignments and speed. A controller has 12 states.

Example 5 - Mealy vending controller (₹10 item, accepts ₹5 and ₹10). Inputs a = ₹5 coin, b = ₹10 coin (never together); outputs DISP = dispense, RET5 = return ₹5. States S0 = ₹0 credit (Q=0), S5 = ₹5 credit (Q=1).

StateInputNextDISPRET5
S0noneS000
S0₹5S500
S0₹10S010
S5noneS500
S5₹5S010
S5₹10S011
// two-block FSM template (Example 1, Moore 101 detector)
localparam S0=2'b00, S1=2'b01, S2=2'b10, S3=2'b11;
reg [1:0] cs, ns;
always @(posedge clk or negedge rstn)
  if (!rstn) cs <= S0; else cs <= ns;
always @(*) case (cs)
  S0: ns = x ? S1 : S0;
  S1: ns = x ? S1 : S2;
  S2: ns = x ? S3 : S0;
  S3: ns = x ? S1 : S2;
  default: ns = S0;        // safe FSM: invalid codes go to reset
endcase
assign z = (cs == S3);     // Moore output
FeatureMooreMealy
Output depends onState onlyState and input
States for k-bit detectork + 1k
Output timingSynchronous, one cycle lateSame cycle, follows input
Glitches on outputNone (if coded well)Possible
Safe to drive a clock/enableYesOnly if registered
Typical useProtocol control signals, countersHandshakes needing same-cycle reply
EncodingFlip-flops for 12 statesNext-state logicBest for
Binary4Deep, wideArea-critical ASIC, CPLD
Gray4Similar to binaryLow-power, glitch-sensitive outputs
One-hot12Shallow OR per bitFPGA, high-speed FSM
Johnson6ModerateRing-type sequences

Where it is used

Interview questions

Common mistakes

Related topics

Sequential circuits: flip-flops, counters and registers · Combinational circuits · Verilog and HDL basics · Programmable logic: PLDs, CPLDs and FPGAs · Number systems and Boolean algebra · Computer organisation: control unit design

Open in the Knowledge reader →Find jobs →