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

PortDirectionTypeDescription
clkin1 bitClock
rstin1 bitSynchronous reset (active high)
incin1 bitEntry beam, one pulse per person in
decin1 bitExit beam, one pulse per person out
countout4-bit vectorCurrent headcount (registered)
fullout1 bitHigh when count is 12 or more

Behavior

  • inc and dec are sampled at each rising edge; count updates at the following cycle
  • inc and dec high on the same edge cancel: count does not change, at any value including 0 and 15
  • inc alone adds one, except that count holds at 15 (no wrap to 0)
  • dec alone subtracts one, except that count holds at 0 (no wrap to 15)
  • full is high exactly when count is 12 or greater
  • rst = 1 at a rising edge clears count to 0, whatever the beams are doing

What the bench checks

  • The testbench checks count and full on every cycle
  • Exits at zero and entries at 15 must clamp instead of wrapping the count
  • Simultaneous inc and dec pulses must cancel at ordinary counts, the full threshold, zero, and the ceiling
  • The full output 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 and count is registered; full may 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.

Loading editor...

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