SpinalHDL
1 / 1
View past submissionsLeaderboard

Description

The Rent Is Due in Kilowatts

Your landlord turned the building's circuit breakers into a "market-based power allocation system" after the penthouse sauna overloaded the service again. Now four circuits fight over a limited supply while the lease ranks exactly which tenants get power first.

The electrical service can supply at most limit power units. Four circuits request power through bits 0 through 3 of requests; their power costs are 1, 2, 4, and 8 units respectively.

Process requested circuits in strict priority order from bit 3 down to bit 0. Grant a circuit when enough capacity remains, subtract its cost, and continue to the next circuit. The four output bits indicate which requests were granted.

Hardware I/O Encoding

  • requests is a 4-bit request mask
  • limit is a 4-bit unsigned capacity from 0 through 15
  • the output is a 4-bit grant mask
  • bit 3 has highest priority and bit 0 has lowest priority

Example

For requests = 1111 and limit = 7, bit 3 cannot be granted, then bits 2, 1, and 0 are granted. The output is 0111.

Input

requests

4-bit Logic Vector
Requested circuits, from one-unit bit 0 through eight-unit bit 3.

limit

4-bit Unsigned Integer
Available power capacity.

Output

4-bit Logic Vector
Granted circuits after fixed-priority allocation.