Practice
Lane Rack Button
Star Lanes is a small bowling alley with pinsetter machines from the 1970s. Each lane has one reset button on the ball return. A press tells the pinsetter to clear the fallen pins and set a fresh rack.
The button is a bare mechanical contact, and its metal leaf bounces on every press. One press reaches the controller as a burst of pulses. The old controller counted each pulse as a new press, so the machine cycled 2 or 3 times and jammed. League nights stalled while staff climbed into the machine to free the pins. Rolling balls shake the contact too, so short rattles arrive with no press behind them.
Your task: build the debouncer that sits between the button and the controller. Pass raw through 2 flip-flops first, because the contact is not lined up with clk. Then filter it: clean may take a new value only after the contact has held that value for 8 samples in a row. Pulse pressed high for 1 cycle when clean rises. Pulse released high for 1 cycle when clean falls.
Interface
| Port | Direction | Type | Description |
|---|---|---|---|
clk | in | 1 bit | System clock |
rst | in | 1 bit | Synchronous reset (active high) |
raw | in | 1 bit | Raw contact from the button, bouncy |
clean | out | 1 bit | Debounced button level |
pressed | out | 1 bit | One-cycle pulse when clean rises |
released | out | 1 bit | One-cycle pulse when clean falls |
Behavior
- After reset,
cleanis low: the block treats the button as not pressed - The block samples
rawat every rising edge ofclk rawpasses through a 2-flop synchronizer: the filter sees each sample 2 cycles after the edge that took it- A qualifying run is an unbroken row of samples with the same value, and that value differs from
clean - When a run reaches 8 samples,
cleantakes the run's value at the 2nd rising edge after the edge with the 8th sample - One sample equal to
cleanends the run; the count starts over at 0 - A run of 7 or fewer samples never changes
clean, no matter how many such runs arrive pressedis high for exactly the 1 cycle on whichcleanrises, and low at all other timesreleasedis high for exactly the 1 cycle on whichcleanfalls, and low at all other timesrsthigh at a rising clock edge forcesclean,pressed, andreleasedlow from that edge- Reset fires no pulse:
releasedstays low even when reset drops a highclean - Reset also clears the synchronizer and the count, so a contact still closed after reset must qualify again with a fresh 8-sample run
What the bench checks
- The testbench checks
clean,pressed, andreleasedon every cycle of every scenario - The testbench drives a press as a bounce burst that settles high, and checks the exact edge where
cleanrises - The testbench drives a release as a bounce burst that settles low, and checks the exact edge where
cleanfalls - The testbench drives a long rattle in which no value holds for 8 samples, and checks that no output moves
- The testbench holds the opposite value for exactly 7 samples, on both sides, and checks
cleandoes not move - The testbench holds the contact closed for exactly 8 samples, then opens it, and checks the rise and the fall both land on schedule
- The testbench checks
pressedandreleasedreturn low exactly 1 cycle after they rise - The testbench asserts
rstmid-rattle whilecleanis high and checks all three outputs drop with noreleasedpulse - The testbench keeps
rawhigh through that reset and checkspressedfires on the 10th edge afterrstgoes low - The testbench asserts
rston the exact edge wherecleanwould rise and checks that no rise and no pulse happen
Constraints
everything is synchronous to clk; all outputs are registered.
this models the debouncer between the bouncy contact and the pinsetter controller. The button's mechanics and the pinsetter machine are out of scope.
raw must pass through 2 flip-flops before any other logic reads it. The bench's edge counts include this 2-cycle lag, so a skipped stage fails the timing checks.
Do not add ports.
Click Run to execute your code. Output will appear here.