Easy · Combinational Logic
Implement an 8-to-3 priority encoder.
Eight requesters share one resource. Each one raises its bit of req to ask for it. The encoder reports the index of the highest-numbered set bit of req (bit 7 has the highest priority, bit 0 the lowest) and whether any bit is set at all.
| Port | Dir | Width | Meaning |
|---|---|---|---|
req | input | 8 | Request vector, unsigned. Bit i set means requester i is asking. |
idx | output | 3 | Index (0–7) of the most significant 1 in req. |
valid | output | 1 | 1 if at least one bit of req is set, otherwise 0. |
When req is all zeros, there is no winner: valid must be 0 and idx must be 0 (a defined value, not "don't care").
The circuit is purely combinational: outputs follow req in the same time step, with no clock and no state.
req (binary) | idx | valid | Why |
|---|---|---|---|
0000_0000 | 0 | 0 | no request |
0000_0001 | 0 | 1 | only bit 0 is set |
0000_0110 | 2 | 1 | bits 2 and 1 are set; bit 2 wins |
1000_0001 | 7 | 1 | bit 7 wins over bit 0 |
0101_0000 | 6 | 1 | bit 6 wins over bit 4 |
always_comb block with a for loop is a clean way to write this. Think about theorder of the loop: in a procedural block, the last assignment wins.
casez with ? wildcards also works (8'b1???_????: idx = 3'd7; ...).always_comb block so no latch isinferred and the all-zero case is defined.
Sign up free to solve 8-to-3 Priority Encoder in your browser.