Lab10: State Machines
Time Estimate4-6 Hours Grade Impact2.5% DueNov 25 @ 3pm Required for Pass
Objective
Design, minimize, implement, and deploy a vending machine controller FSM. Compare Moore vs. Mealy, binary vs. one-hot, and D vs. JK flip-flop implementations, and learn which choices matter on an FPGA.
Warning
Before starting any lab, always read the entire lab description. You are working with expensive, fragile hardware.
Story
You are building the controller for a vending machine. It accepts nickels and dimes, dispenses a product when at least 15 cents has been inserted (no change), and shows the accumulated value on LEDs (or 7-segment display, if desired).
Before the Lab
Repository
Create a repository for this assignment and add your instructor and TA as collaborators.
Equipment
- Basys 3, USB cable, notebook.
Pre-Lab Work
Due in the first 30 minutes of lab.
Pre-Lab
1. Design the FSM
- States represent accumulated value: 0, 5, 10, 15+.
- Inputs: nickel, dime (one-hot, never both).
- Output: dispense (and value display).
- Draw Moore and Mealy versions; compare state counts.
2. Encodings
Produce binary and one-hot encodings.
3. Flip-Flop Choice
Derive next-state logic for D and JK flip-flops (excitation table). Predict which uses fewer LUTs.
Deliverable Checkpoint
Submit your pre-lab work (on Canvas or in-person).
In the Lab
Step 1: Implement All Four Variants
- Create a base module
VendingFSMwith portsclk,reset,nickel,dime,dispense, andvalue[3:0](accumulated cents / 5). - Implement four modules:
Vending_Binary_DVending_Binary_JKVending_OneHot_DVending_OneHot_JK
- For D variants, drive
Dinputs directly from next-state logic. - For JK variants, use the excitation table to derive
JandKfor each state bit. - Use two-always style and synchronous reset to the zero-cents state.
Step 2: Simulate
- Write a self-checking testbench with tasks
insert_nickelandinsert_dime. - Apply sequences: 5+5+5, 10+5, 5+10, 10+10.
- Check that
dispenseasserts on the clock edge after reaching 15 cents. - Check
valueoutputs for each partial sum. - Print pass/fail counts at the end.
Deliverable Checkpoint
Demonstrate a working testbench, explaining why the design is working.
Step 3: Synthesize and Compare
| Variant | FFs | LUTs | Max Freq |
|---|---|---|---|
| Binary / D | |||
| Binary / JK | |||
| One-hot / D | |||
| One-hot / JK |
Step 4: Deploy
Program the Basys 3. Use switches for coin inputs, button for clock, LEDs for value and dispense.
Step 5: Verify
Demonstrate to TA: 5+5+5 dispenses; 10+10 dispenses; partial sums display correctly.
Fun Additional Challenge
Add a “return change” feature (machine returns 5 cents when 20 cents inserted for a 15-cent product). Redesign and re-verify.
Post-Lab
Submit repository, waveforms, comparison table, and a short write-up: which variant would you use in a professional setting and why?
Deliverables
Indicate to your instructor that you’re ready to submit your work by leaving a comment on the Feedback PR and requesting a review from @mossbiscuits. Ensure that your submission is on the main branch. Create a tag named done to mark your submission:
git tag -a done -m "Lab10 submission"
git push origin doneTo receive 100 points, submissions must include evidence of the following:
- (20) Correct and timely completion of the pre-lab work
- (20) All four variants implemented
- (20) Self-checking testbench
- (20) Synthesis comparison table
- (20) Hardware demonstration and write-up