4-to-2 Priority Encoder
Four-input priority encoder creating 2-bit output. Learn data compression and priority encoding schemes.
您将学到什么
- Encode the highest-priority active input into a binary code.
- Distinguish a priority encoder from a plain encoder.
- Read the priority truth table — multiple inputs active resolves to the highest one.
- Write the priority Boolean expressions: each higher input masks lower ones.
- Apply priority encoders in interrupt controllers and bus arbitration.
工作原理
An encoder is the inverse of a decoder: it converts a one-hot input pattern into a binary code. A priority encoder handles the case where more than one input might be active — it returns the binary index of the highest-priority active input, ignoring lower-priority ones.
This 4-to-2 priority encoder takes 4 inputs (I0–I3, where I3 has highest priority) and produces a 2-bit output (Y1, Y0): - If I3 is active: output 11 (decimal 3) - Else if I2 is active: output 10 (decimal 2) - Else if I1 is active: output 01 (decimal 1) - Else if I0 is active: output 00 (decimal 0) - If all inactive: output 00 plus a separate "valid" bit goes low
A "valid" output (sometimes called V or GS — Group Select) signals whether any input was active. Without it, an output of 00 is ambiguous: it could mean I0 is active or it could mean none are.
Boolean expressions use priority masking: Y1 = I3 + I2 (high if either of the upper two inputs is active); Y0 = I3 + (¬I2 · I1) (high if I3 is active, OR if I1 is active and I2 isn't masking it).
真值表
X = don't care. The encoder picks the highest-numbered active input. V (valid) signals whether any input is active.
| 输入 | 输出 | ||||||
|---|---|---|---|---|---|---|---|
| I3 | I2 | I1 | I0 | Y1 | Y0 | V | |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | Nothing active — V=0, Y meaningless |
| 0 | 0 | 0 | 1 | 0 | 0 | 1 | Only I0 — code 00, V=1 |
| 0 | 0 | 1 | 0 | 0 | 1 | 1 | Only I1 — code 01 |
| 0 | 0 | 1 | 1 | 0 | 1 | 1 | I1 wins over I0 |
| 0 | 1 | 0 | 0 | 1 | 0 | 1 | Only I2 — code 10 |
| 0 | 1 | 1 | 1 | 1 | 0 | 1 | I2 wins over I1, I0 |
| 1 | 0 | 0 | 0 | 1 | 1 | 1 | Only I3 — code 11 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | I3 wins over all — highest priority |
布尔表达式
High bit of code: set if either of the upper two inputs is active.
Low bit: set if I3 is active, OR if I1 is active without I2 masking it.
Valid = OR of all inputs. Tells the consumer whether the encoded output is meaningful.
逐步尝试
在上方嵌入式电路中设置输入,然后阅读预期结果并验证。
- 1I3 = 0 I2 = 0 I1 = 0 I0 = 0预期:
Y1Y0=00, V=0您将看到: No inputs active — V is low. The output code is 00 but you ignore it because V=0. - 2I3 = 0 I2 = 0 I1 = 0 I0 = 1预期:
Y1Y0=00, V=1您将看到: Only I0 → code 00 (= 0) and V=1. The encoder confirms valid output even though Y is all zeros. - 3I3 = 0 I2 = 1 I1 = 1 I0 = 1预期:
Y1Y0=10, V=1您将看到: I0, I1, and I2 all active. I2 has the highest priority so the encoder outputs 10 (= 2). I0 and I1 are ignored. - 4I3 = 1 I2 = 1 I1 = 1 I0 = 1预期:
Y1Y0=11, V=1您将看到: All inputs active → I3 wins, output 11. Priority encoders always tell you about the highest-numbered active input.
使用的组件
实际应用
Interrupt controllers. Multiple peripheral interrupts arrive simultaneously; a priority encoder picks the highest-priority one and presents its vector to the CPU. Crucial for pre-emptive scheduling.
Cache way replacement. When a cache evicts a line, a priority encoder selects which way to replace based on LRU bits or replacement policy.
Bus arbitration. Multiple bus masters request simultaneously; a priority encoder selects the highest-priority requester to grant the bus to.
Floating-point normalize. After arithmetic, the leading-1 detection of the mantissa uses a priority encoder to determine the shift count for normalization.
Resource allocation. Any "first available slot" search uses a priority encoder to find the lowest-numbered free resource quickly.