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
clkin1 bitClock
rstin1 bitSynchronous reset (active high), the panel button
tokenin1 bitToken accepted, 1 cycle wide
soapin1 bitSoap button pressed, 1 cycle wide
waterout1 bitValve open, changes only at clock edges
holdout1 bitSession paused indicator, changes only at clock edges

Behavior

  • After reset the stall is idle: water = 0, hold = 0, no time on the clock
  • A token sampled while idle loads a session of exactly 8 cycles: water is high from the next cycle
  • A token sampled while a session has time left is ignored, paused or not
  • A soap pulse during a session toggles hold; while hold = 1 the water is off and the clock is frozen
  • A soap 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 = 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 and hold 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; both outputs are decoded from registers and change only at clock edges.

SCOPE

this models the stall controller behind clean one-cycle token and soap 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.