When to use it
Given a bunch of request lines, which one do you service? A priority encoder returns
the index of the lowest set bit plus a valid flag, the building block of
interrupt controllers, allocators and fixed-priority arbiters.
How it works
Scan the bits from high to low and keep the index of each one that's set; the last one
written wins, so you end up with the lowest. valid is just the OR of all the inputs
(is anything requesting at all?). A synthesizer maps this to a small tree of gates.
Gotchas
- Decide your tie-break: this returns the lowest index. Flip the scan for highest-priority-is-MSB.
- Fixed priority can starve low-priority requesters. If that matters, rotate the starting point, that's a round-robin arbiter.