Finite State Machines
Mealy vs Moore machines, state diagrams, state tables, state encoding, FSM design procedure, traffic light controller example, and FPGA implementation
1. What is a Finite State Machine?
A Finite State Machine (FSM) is a mathematical model of computation consisting of:
2. Mealy vs Moore Machines
Outputs depend only on current state:\( O = \lambda(q) \).
Outputs change only on clock edges. Glitch-free. One extra state may be needed compared to Mealy. Traffic lights are a classic Moore machine example.
Outputs depend on state AND current inputs:\( O = \lambda(q, i) \).
Faster response (combinational path from input to output). Fewer states required. Outputs may glitch if inputs change asynchronously.
3. State Encoding
States must be encoded as binary patterns for hardware implementation. The choice of encoding affects logic complexity, speed, and power:
Uses ceil(log₂N) flip-flops for N states. Minimal hardware but complex next-state logic.
One flip-flop per state, exactly one FF HIGH at any time. Fastest; simple logic; uses most FFs.
Adjacent states differ by one bit. Eliminates glitches in output during transitions.
4. Design Example: Traffic Light Controller
A traffic light is a Moore FSM with three states and a timeout-driven transition. The state table is:
| Current State | Duration | R | G | Y | Next State |
|---|---|---|---|---|---|
| RED (00) | 4 clk | 1 | 0 | 0 | GREEN |
| GREEN (01) | 4 clk | 0 | 1 | 0 | YELLOW |
| YELLOW (10) | 2 clk | 0 | 0 | 1 | RED |
5. FPGA Implementation
FPGAs (Field-Programmable Gate Arrays) implement FSMs in configurable logic blocks (CLBs) containing LUTs (Look-Up Tables) and flip-flops. A synthesis tool converts HDL code (VHDL/Verilog) into an FSM automatically:
// Verilog Moore FSM — Traffic Light
module traffic_light(
input clk, rst,
output reg red, green, yellow
);
localparam RED=2'd0, GREEN=2'd1, YELLOW=2'd2;
reg [1:0] state;
reg [2:0] cnt;
always @(posedge clk or posedge rst) begin
if (rst) begin state <= RED; cnt <= 0; end
else begin
cnt <= cnt + 1;
case (state)
RED: if (cnt==3) begin state<=GREEN; cnt<=0; end
GREEN: if (cnt==3) begin state<=YELLOW; cnt<=0; end
YELLOW: if (cnt==1) begin state<=RED; cnt<=0; end
endcase
end
end
always @(*) begin
{red,green,yellow} = 3'b000;
case (state)
RED: red = 1;
GREEN: green = 1;
YELLOW: yellow = 1;
endcase
end
endmodule6. Python: Traffic Light FSM Simulation
Simulate the traffic light FSM over 40 clock cycles, plot the state diagram, state transition color map, and output waveforms for the RED, GREEN, and YELLOW signals.
Click Run to execute the Python code
Code will be executed with Python 3 on the server