Practice
Camera Trap Flash
The wildlife unit's newest camera traps in the Herrick oak woods carry a xenon flash, which fires from a stored electric charge. The first firmware treated it like an LED that can fire any time. One busy night, a badger family filed past the lens, and the trigger fired 9 times in a minute. Recharging that fast dragged the power supply down until the whole camera lost power and rebooted. The night's 9 triggers gave 1 usable frame and a dead trap until morning.
The hardware fix is already on the board: a bank that stores up to 4 flash charges, topped up at 1 charge every 8 cycles.
Your task: build the block between the motion trigger and the flash tube. Spend 1 banked charge per photo, and ignore triggers while the bank is empty. A missed photo is fine; a crashed camera is not.
Interface
| Port | Direction | Type | Description |
|---|---|---|---|
clk | in | 1 bit | Clock |
rst | in | 1 bit | Synchronous reset (active high) |
trig | in | 1 bit | Motion trigger wants a photo, sampled every edge |
fire | out | 1 bit | Flash fires, registered 1-cycle pulse |
charges | out | 3-bit vector | Charges banked right now, 0 to 4, registered |
Behavior
- After reset the bank is empty:
charges = 0,fire = 0, and the delivery cadence restarts - A charge is delivered on every eighth rising edge after reset (the 8th, 16th, 24th, ...), regardless of traffic
- A delivery with the bank already at 4 is discarded
trig = 1at a rising edge with at least one charge available:firepulses high for the following cycle and the bank drops by onetrig = 1with the bank empty is ignored: no pulse, no debt, no queue- On an edge where a delivery and a trigger coincide, the delivery lands first and the trigger draws second
- On a coincident edge, an empty bank still yields a flash, and a full bank ends the edge at 3 charges
trigheld high simply requests a photo on every edge: a full bank gives 4 back-to-back pulses- When
trigstays high after that 4-pulse burst,firefollows the delivery cadence exactly chargesalways shows the post-edge bank level;fireandchargesare registersrst = 1at any rising edge empties the bank, clearsfire, and restarts the cadence
What the bench checks
- The testbench mirrors these rules with a reference model and checks
fireandchargeson every cycle - The testbench drives the fill cadence from empty, the cap at 4, the 4-flash burst, and a long stretch with
trigheld high across many deliveries - It also drives coincident delivery-and-trigger edges on both an empty and a full bank, a random storm, and a mid-run reset
Constraints
everything is synchronous to clk; both outputs are registers.
this models the charge bookkeeping between the motion trigger and the flash tube. The charger electronics, the xenon tube, and the camera's power supply are out of scope.
the delivery cadence is fixed by the charger and counts from reset, not from the last delivery or the last flash.
Do not add ports.
Click Run to execute your code. Output will appear here.