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
clk_iin1 bitClock
rst_iin1 bitSynchronous reset (active high)
trig_iin1 bitMotion trigger wants a photo, sampled every edge
fire_oout1 bitFlash fires, registered 1-cycle pulse
charges_oout3-bit vectorCharges banked right now, 0 to 4, registered

Behavior

  • After reset the bank is empty: charges_o = 0, fire_o = 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_i = 1 at a rising edge with at least one charge available: fire_o pulses high for the following cycle and the bank drops by one
  • trig_i = 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_i held high simply requests a photo on every edge: a full bank gives 4 back-to-back pulses
  • When trig_i stays high after that 4-pulse burst, fire_o follows the delivery cadence exactly
  • charges_o always shows the post-edge bank level; fire_o and charges_o are registers
  • rst_i = 1 at any rising edge empties the bank, clears fire_o, and restarts the cadence

What the bench checks

  • The testbench mirrors these rules with a reference model and checks fire_o and charges_o 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_i 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_i; 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.