Problems

4. One-hot decoder and encoder

EasyWrite the moduleAbout 15 minFree
combinationaldecoderone-hot

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

Module onehot_codec

PortDirBitsWhat it is
selin3Number to decode.
enin1Decoder enable; 0 forces onehot to zero.
hot_inin8One-hot word to encode.
onehotout8Decoder output.
binout3Index of the set bit of hot_in.
errorout11 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.

Stuck? . Sign in to save your progress. You can solve the free problems without an account.

Verilogonehot_codec.v
14px
Loading the editor…

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.

One-hot decoder and encoder: Verilog interview question | breadboardhub