DEV Community

olivermcmillen34
olivermcmillen34

Posted on Edited on

What I built

Speed Trap
Intro
Speed Trap is a reflex game I made in Verilog for Tiny Tapeout. It started as a simple 0 to 9 counter, until my reviewer pushed me to make it an actual game.
How to play
A 7-segment display spins through 0 to 9 and you have to hit the button right on 7. If you hit 7, it shows P until you press again and goes to the next level. Each level spins 1.25x faster, from 4 to 24 digits per second across Levels 1 to 9. If you miss, it shows F until you press again and resets to Level 1.
How it works
My code has five main parts:
Button press detector: Mechanical buttons stay pressed for thousands of clock cycles. Checking if the button is high now and was low on the last tick catches just one pulse.
wire press = ui_in [0] && !btn_last;
Speed table and prescaler: The chip runs at 10 MHz. At Level 1, 10,000,000 / 4 = 2,500,000, and each level divides that by 1.25. A 22-bit prescaler counts to the limit and fires a go pulse.
4'd1: limit = 22'd2500000;
wire go = (tick == limit);
Digit counter: Every time go fires, the digit q adds 1 and wraps back to 0 at 9.
else if (go && q == 4'd9) begin
State machine: Three states (2'd0 spin, 2'd1 pass, 2'd2 fail) check if you pressed on 7 and hold P or F until your next press.
if (q == 4'd7) state <= 2'd1;
Decoder: Maps each number or letter to 7 bits so the right LED segments light up.
4'd0: seg = 7'b0111111;
Testing
I wrote test.py to test the clock, reset, and button presses, then opened the waveform in Surfer. Reading the hex outputs was confusing at first, until I matched the bits to the segments. You can see uo_out go from 3f (0) to 71 (F) when you miss, and back to 3f (0) on the second press. Seeing all the checks go green felt awesome.
[Surfer waveform screenshot]
The chip
After the tests passed, the tools turned my Verilog into a chip layout. In the images, that tight cluster packed with wires is my logic.
[Chip layout images]
Failures and fixes
I hit a lot of real bugs over the past couple days:
• Used ui_in[7] instead of ui_in[0] and swapped the press order, which would have caught the button release instead.
• Pasted the counter inside the button's always block and had to move an end to separate them.
• Wrote state assignments backwards like 2'd0 <= state until I put the variable name on the left.
• Deleted the wrong copy while moving the digit counter below go and lost the go line.
• Changed module tb instead of the instance line when renaming my module, which broke tb.v until I fixed it.
What I learned + what's next
Hardware is way different from normal coding because everything happens at once on every clock edge. Next, I want to add a victory screen after Level 9 and test it on a real FPGA board.

Github - https://github.com/olivermcmillen34-debug

Top comments (0)