Lab9: Bridge to State Machines

Time Estimate3-4 Hours   Grade Impact2%   DueNov 18 @ 3pm   Required for Pass  

Objective
Bridge the sequential circuits you built in M4 (registers, counters, flip-flops) into your first finite state machine. You will build the same behavior two ways – once as a shift-register datapath and once as an FSM – and discover why state machines are the cleaner abstraction.

Warning
Before starting any lab, always read the entire lab description. You are working with expensive, fragile hardware.


Story

You are building a pattern lock for a treasure chest. The chest opens when the last three button presses are 1-0-1. You will first build this with the shift-register techniques from M4, then rebuild it as a proper FSM, and compare the two implementations.

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

Part 1: Shift-Register Detector (M4 style)

Using a 3-bit shift register (M4 L23) and a comparator, design a circuit that outputs 1 when the stored bits equal 101. Draw the block diagram.

Part 2: FSM Detector (M5 style)

Design a 4-state Moore FSM that detects 101 with overlapping allowed. Draw the state diagram and state table.

Part 3: Prediction

Predict which design uses fewer flip-flops and which is easier to modify to detect 1101 instead. Write your prediction down – you will check it later.

Deliverable Checkpoint
Submit your pre-lab work (on Canvas or in-person).

In the Lab

Step 1: Build the Shift-Register Detector

  1. Create module ShiftDetect with ports clk, reset, serial_in, unlock.
  2. Declare reg [2:0] shift; and update it on each clock: shift <= {shift[1:0], serial_in};.
  3. Add combinational output: assign unlock = (shift == 3'b101);.
  4. Write a testbench that applies reset, then feeds 1 0 1 0 1 0 1 (one bit per clock), and prints unlock each cycle.
  5. Run the simulation and record the cycle on which unlock first asserts.

Verilog hint
Use non-blocking assignments (<=) in the clocked block. Keep the comparison combinational (assign unlock = (shift == 3'b101);).

Step 2: Build the FSM Detector

  1. Define states S0 (no progress), S1 (saw 1), S2 (saw 10), S3 (saw 101, unlock).
  2. Implement FsmDetect as a Moore machine using the two-always style from L30: one combinational block for next_state and unlock, one clocked block for state.
  3. Use binary state encoding and D flip-flops (state register updates on posedge clk).
  4. Simulate with the same testbench used in Step 1 and compare waveforms.

Verilog hint
Include a default case mapping to S0. Provide synchronous reset to S0.

Step 3: Compare

Record:

Design Flip-Flops LUTs Lines of Code Easy to change pattern?
Shift-register
FSM

Deliverable Checkpoint
Demonstrate both testbenches passing, explaining why each design works.

Step 4: Deploy

Program the Basys 3 with the FSM version. Use a switch for serial input and a button for clock; LED lights on detection.

Step 5: Verify

Demonstrate to the TA: overlapping detection (10101 unlocks twice).

Fun Additional Challenge

Modify both designs to detect 1101 and re-run the comparison. Did your prediction hold?

Post-Lab

Submit repository, waveforms, comparison table, and a short reflection: which abstraction would you use for a 10-bit pattern, 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 "Lab9 submission"
git push origin done

To receive 100 points, submissions must include evidence of the following:

  • (20) Correct and timely completion of the pre-lab work
  • (20) Shift-register detector simulation
  • (20) FSM detector simulation
  • (20) Hardware demonstration
  • (20) Comparison table and reflection