Worked Example: A Vending Machine Controller
This article examines the scalability limits of pure finite state machines. It demonstrates why arithmetic operations and numeric accumulation are better implemented using a hybrid architecture that separates control logic from datapath registers.
Functional Specification
Design a controller for a vending machine:
- Product Price: 30 cents.
- Coin Inputs: Accepts nickels (5 cents), dimes (10 cents), and quarters (25 cents). At most one coin input asserts per clock cycle.
- Control Input:
cancelreturns all accumulated currency. - Outputs:
dispenseasserts when payment meets or exceeds 30 cents. Change outputs indicate refund amounts:change_5,change_10,change_15,change_20. The signalreturn_coinactivates on cancel.
Because the maximum balance before dispensing is 25 cents and the largest single coin is 25 cents, the maximum accumulated total is 50 cents. The maximum change returned is 20 cents ($50 - 30$).
Approach A: Pure Finite State Machine
In a pure FSM, every valid monetary balance forms an individual state:
- Accumulation states:
S0,S5,S10,S15,S20, andS25(6 states). - Dispense states: In a Moore machine, every distinct change amount requires a dedicated state:
| Origin State | Inserted Coin | Total Balance | Change Returned | Destination State |
|---|---|---|---|---|
| S5 | Quarter (25¢) | 30¢ | 0¢ | DISP_0 |
| S20 | Dime (10¢) | 30¢ | 0¢ | DISP_0 |
| S25 | Nickel (5¢) | 30¢ | 0¢ | DISP_0 |
| S25 | Dime (10¢) | 35¢ | 5¢ | DISP_5 |
| S15 | Quarter (25¢) | 40¢ | 10¢ | DISP_10 |
| S20 | Quarter (25¢) | 45¢ | 15¢ | DISP_15 |
| S25 | Quarter (25¢) | 50¢ | 20¢ | DISP_20 |
The machine requires 11 states: six accumulation states and five dispense states (Figure 1). Each dispense state persists for one cycle and returns to S0.
Eleven states require four flip-flops ($Q_3, Q_2, Q_1, Q_0$).
Assign output-coded states:
- Accumulation states:
4'b0000through4'b0101($Q_3 = 0$) - Dispense states: Set $Q_3 = 1$ (
DISP_0 = 1000,DISP_5 = 1001,DISP_10 = 1010,DISP_15 = 1011,DISP_20 = 1100).
Output dispense connects directly to $Q_3$. The change outputs decode the lower state bits when $Q_3 = 1$:
$$\text{change_5} = Q_3 \cdot \overline{Q_2},\overline{Q_1} \cdot Q_0 \qquad \text{change_10} = Q_3 \cdot \overline{Q_2} \cdot Q_1 \cdot \overline{Q_0}$$
$$\text{change_15} = Q_3 \cdot \overline{Q_2} \cdot Q_1 \cdot Q_0 \qquad \text{change_20} = Q_3 \cdot Q_2 \cdot \overline{Q_1},\overline{Q_0}$$
The pure FSM operates correctly. However, modifying the price or adding products causes state count to expand rapidly. For example, pricing an item at $1.25 with nickels requires over 30 states and complex next-state decoders.
Approach B: Hybrid FSM-plus-Datapath Architecture
The hybrid architecture separates control sequencing from numerical computation (Figure 2):
- Datapath: Contains a 6-bit accumulator register, an input multiplexer to select coin values (5, 10, 25, or 0), an adder, a subtractor for change calculation, and a threshold comparator ($A \ge 30$).
- Control FSM: Requires only three states (
IDLE,ACCUMULATING,DISPENSING).
FSM State Definitions
IDLE: Register cleared to zero. Waiting for coin insertion.ACCUMULATING: Coins accumulated. Total balance remains below 30 cents.DISPENSING: Balance reaches or exceeds 30 cents. Assertdispenseand output change ($\text{total} - 30$). Return toIDLEon the next clock cycle.
When input cancel asserts, the FSM clears the accumulator register and returns to IDLE.
Architectural Comparison
| Characteristic | Pure FSM Architecture | Hybrid FSM-plus-Datapath |
|---|---|---|
| Total States | 11 states | 3 states |
| Flip-Flop Count | 4 flip-flops | 2 flip-flops (state) + 6 flip-flops (accumulator) |
| Next-State Combinational Logic | Complex 8-input multi-output logic | Minimal logic gates |
| Output Logic | Multi-variable state decoder | Simple arithmetic subtractor |
| Price Modification Flexibility | Requires complete redesign of state diagram | Requires changing one digital comparator threshold |
| Multi-Product Scalability | Causes exponential state explosion | Requires additional comparator registers |
The hybrid architecture uses more register bits, but significantly reduces combinational logic complexity. Most practical digital controllers use the FSM-plus-datapath architecture.
Summary of the Three Case Studies
- Traffic Light Controller: A classic Moore state machine with strict cyclic progression. States map directly to output lamps, and transitions depend on timer events.
- Serial Pattern Detector: Focuses on state transitions rather than outputs. Preserving overlapping prefixes is essential for detecting continuous data patterns.
- Vending Machine Controller: Demonstrates the division between control and data. Use pure state machines for sequencing and handshaking. Use datapath registers and arithmetic units for counting and numeric comparisons.
Practice Exercises
Problem 1
Suppose the vending machine increases product price to 50 cents and accepts 50-cent half-dollar coins. Compare the structural changes required for Approach A and Approach B.
Solution
- Approach A (Pure FSM): Accumulation states require every 5-cent increment from 0 to 45 (10 states). Dispense states require every change combination from 0 to 45 cents (up to 10 states). Total states expand to approximately 20 states, requiring 5 flip-flops and complex next-state decoders.
- Approach B (Hybrid): The control FSM remains unchanged with 3 states. The comparator threshold changes from 30 to 50. The accumulator register expands from 6 bits to 7 bits to accommodate maximum totals up to 95 cents.
Problem 2
Derive next-state equations for the three-state hybrid FSM using binary encoding: IDLE = 2'b00, ACCUMULATING = 2'b01, and DISPENSING = 2'b10.
Solution
Let coin be the logical OR of all coin inputs. Let enough be the comparator output ($A \ge 30$).
- Bit $D_1$ asserts when entering
DISPENSING(10):
$$D_1 = Q_0 \cdot \text{coin} \cdot \text{enough}$$
- Bit $D_0$ asserts when transitioning into or remaining in
ACCUMULATING(01):
$$D_0 = (\overline{Q_1},\overline{Q_0} \cdot \text{coin}) + (\overline{Q_1}, Q_0 \cdot \overline{\text{cancel}} \cdot \overline{\text{coin} \cdot \text{enough}})$$
State DISPENSING (10) returns to IDLE (00) unconditionally on the next clock cycle.
Problem 3
A designer states that the hybrid architecture violates the formal definition of a state machine because it uses an arithmetic adder. Is this statement correct?
Solution
No. The total system state consists of the FSM state register combined with the datapath accumulator register.
All registers are edge-triggered D flip-flops that update simultaneously on the system clock. The three-state FSM serves as the control unit, while the accumulator acts as an extended sequential state register.
This hybrid architecture conforms to synchronous sequential design principles.
Section 3.4 explores registers, shift registers, and parallel-serial conversion circuits.