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 | in | 1 bit | Clock |
rst | in | 1 bit | Synchronous reset (active high) |
inc | in | 1 bit | Entry beam, one pulse per person in |
dec | in | 1 bit | Exit beam, one pulse per person out |
count | out | 4-bit vector | Current headcount (registered) |
full | out | 1 bit | High when count is 12 or more |
Behavior
incanddecare sampled at each rising edge;countupdates at the following cycleincanddechigh on the same edge cancel:countdoes not change, at any value including 0 and 15incalone adds one, except thatcountholds at 15 (no wrap to 0)decalone subtracts one, except thatcountholds at 0 (no wrap to 15)fullis high exactly whencountis 12 or greaterrst = 1at a rising edge clearscountto 0, whatever the beams are doing
What the bench checks
- The testbench checks
countandfullon every cycle - Exits at zero and entries at 15 must clamp instead of wrapping the count
- Simultaneous
incanddecpulses must cancel at ordinary counts, the full threshold, zero, and the ceiling - The
fulloutput 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
everything is synchronous to clk and count is registered; full may be a plain decode of the count.
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.