4. One-hot decoder and encoder
Build two independent combinational blocks in one module.
The decoder turns a 3-bit number sel into an 8-bit one-hot word: bit sel of onehot is 1 and the others are 0. When en is 0, onehot is all zeros.
The encoder goes the other way: hot_in should have exactly one bit set, and bin reports which one. When hot_in does not have exactly one bit set (no bits, or two or more), error is 1 and bin is 0. Otherwise error is 0. There is no clock and no reset.
Ports
| Port | Dir | Bits | What it is |
|---|---|---|---|
sel | in | 3 | Number to decode. |
en | in | 1 | Decoder enable; 0 forces onehot to zero. |
hot_in | in | 8 | One-hot word to encode. |
onehot | out | 8 | Decoder output. |
bin | out | 3 | Index of the set bit of hot_in. |
error | out | 1 | 1 when hot_in is not exactly one-hot. |
Example
sel = 5, en = 1 gives onehot = 0010_0000. hot_in = 0000_1000 gives bin = 3, error = 0. hot_in = 0001_1000 gives bin = 0, error = 1.
Written hints
AI hint
Submissions
No submissions yet. Submit your answer and it shows here. Sign in to keep them.
Submit your answer to have it graded. The hidden test runs on the server; you see whether it passes and, if not, the first case it gets wrong.
Press Build to check your code with Yosys. Errors and warnings show here, and a clean build draws the logic diagram.
Build your design to drive it by hand and see its signals here.