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
clk_iin1 bitClock
rst_iin1 bitSynchronous reset (active high)
inc_iin1 bitEntry beam, one pulse per person in
dec_iin1 bitExit beam, one pulse per person out
count_oout4-bit vectorCurrent headcount (registered)
full_oout1 bitHigh when count_o is 12 or more

Behavior

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

What the bench checks

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