Practice

Cold Shower Token

The shower stalls at the Aspen Hollow campground take brass tokens. Drop one in, and the water starts. A big rubber soap button on the wall pauses the flow without eating your remaining time. The session clock stays frozen while the water is off, which is the whole reason campers accept token showers at all.

Since spring, the complaints book keeps filling with the same entry: token in, nothing out. It happens the morning after the attendant pressed the reset button on the panel. And it happens only when the previous camper walked off mid-shower with the water paused. In that case the reset gives the next camper a full session, then quietly refuses to open the valve. A second press of the soap button fixes it instantly. That is why maintenance has never caught it live.

Your task: the shipped firmware is in your editor. Find the bug and fix it in place.

Interface

PortDirectionTypeDescription
clk_iin1 bitClock
rst_iin1 bitSynchronous reset (active high), the panel button
token_iin1 bitToken accepted, 1 cycle wide
soap_iin1 bitSoap button pressed, 1 cycle wide
water_oout1 bitValve open, changes only at clock edges
hold_oout1 bitSession paused indicator, changes only at clock edges

Behavior

  • After reset the stall is idle: water_o = 0, hold_o = 0, no time on the clock
  • A token_i sampled while idle loads a session of exactly 8 cycles: water_o is high from the next cycle
  • A token_i sampled while a session has time left is ignored, paused or not
  • A soap_i pulse during a session toggles hold_o; while hold_o = 1 the water is off and the clock is frozen
  • A soap_i pulse while the stall is idle is ignored
  • While a session is running unheld, the clock counts down one cycle per cycle
  • Each token buys exactly 8 cycles of open valve, no matter how often the session is paused
  • rst_i = 1 at any rising edge returns the stall to idle, clearing the session time and the pause, whatever was in flight

What the bench checks

  • The testbench checks water_o and hold_o on every cycle
  • The testbench drives a plain session, an ignored mid-session token, a paused and resumed session, and a soap press while idle
  • It also drives the complaint-book case: a reset in the middle of a paused session, followed by a fresh token

Constraints

TIMING: everything is synchronous to clk_i; both outputs are decoded from registers and change only at clock edges.

SCOPE: this models the stall controller behind clean one-cycle token_i and soap_i pulses. The token mechanism, the valve hardware, and the water are out of scope.

The template contains the shipped controller: fix it in place rather than starting over.

Do not add ports.

Loading editor...

Click Run to execute your code. Output will appear here.