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

PortDirectionTypeDescription
clkin1 bitClock
rstin1 bitSynchronous reset (active high)
trigin1 bitMotion trigger wants a photo, sampled every edge
fireout1 bitFlash fires, registered 1-cycle pulse
chargesout3-bit vectorCharges 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 = 1 at a rising edge with at least one charge available: fire pulses high for the following cycle and the bank drops by one
  • trig = 1 with 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
  • trig held high simply requests a photo on every edge: a full bank gives 4 back-to-back pulses
  • When trig stays high after that 4-pulse burst, fire follows the delivery cadence exactly
  • charges always shows the post-edge bank level; fire and charges are registers
  • rst = 1 at any rising edge empties the bank, clears fire, and restarts the cadence

What the bench checks

  • The testbench mirrors these rules with a reference model and checks fire and charges on every cycle
  • The testbench drives the fill cadence from empty, the cap at 4, the 4-flash burst, and a long stretch with trig held 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

TIMING

everything is synchronous to clk; both outputs are registers.

SCOPE

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.

CADENCE

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.

Loading editor...

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