Practice
Gallery Headcount
The fire officer's limit for the map room at Aldercote House is 12 visitors. Infrared beams in the archway count them. The inner beam pulses once per person walking in, and the outer beam once per person walking out. A lamp at the attendant's desk lights when the room is at capacity.
The counter fitted during the renovation lasted one open day. Two staff members walked out of the empty room, the count wrapped below zero, and the lamp lit with nobody inside. A week later a school group came through side by side. Entries and exits landed on the same clock cycle, and the count drifted low.
The old board is in the scrap bin, and the sensors are yours now.
Your task: build the counter that keeps the headcount honest. It adds 1 for each entry pulse and subtracts 1 for each exit pulse. Simultaneous pulses cancel each other. At the limits the count sticks instead of wrapping.
Interface
| Port | Direction | Type | Description |
|---|---|---|---|
clk_i | in | 1 bit | Clock |
rst_i | in | 1 bit | Synchronous reset (active high) |
inc_i | in | 1 bit | Entry beam, one pulse per person in |
dec_i | in | 1 bit | Exit beam, one pulse per person out |
count_o | out | 4-bit vector | Current headcount (registered) |
full_o | out | 1 bit | High when count_o is 12 or more |
Behavior
inc_ianddec_iare sampled at each rising edge;count_oupdates at the following cycleinc_ianddec_ihigh on the same edge cancel:count_odoes not change, at any value including 0 and 15inc_ialone adds one, except thatcount_oholds at 15 (no wrap to 0)dec_ialone subtracts one, except thatcount_oholds at 0 (no wrap to 15)full_ois high exactly whencount_ois 12 or greaterrst_i = 1at a rising edge clearscount_oto 0, whatever the beams are doing
What the bench checks
- The testbench checks
count_oandfull_oon every cycle - Exits at zero and entries at 15 must clamp instead of wrapping the count
- Simultaneous
inc_ianddec_ipulses must cancel at ordinary counts, the full threshold, zero, and the ceiling - The
full_ooutput must rise at 12, stay high through 15, and fall when the count returns to 11 - Reset during an entry must override the beam, clear the count, and allow counting to resume on the next cycle
Constraints
TIMING: everything is synchronous to
clk_iandcount_ois registered;full_omay be a plain decode of the count.
SCOPE: this models the headcount logic behind the two beams. The infrared sensors, the archway optics, and the attendant's lamp are out of scope.
Do not add ports.
Click Run to execute your code. Output will appear here.