Decoders, Encoders & casez

One-hot decoders, priority encoders and the casez statement with don't-care bits.

Decoders turn a binary number into a one-hot pattern (exactly one line high), used for chip selects and memory word lines. Encoders do the reverse. A priority encoder answers 'which is the highest request?' — the basis of interrupt controllers and arbiters.

Decoder and priority encoder

y = 4'b0000; first gives every bit a value (no latch).

y[a] = 1'b1; sets the one bit selected by a.

casez branches are checked in order, so 4'b1??? (bit 3 set) wins over everything below it.

The default branch handles req = 0000: no request, valid = 0.

case, casez and casex

casex (instr) 8'b1xxx_xxxx: ...

casez (instr) 8'b1???_????: ...

casex also treats x on the selector as don't care, so an uninitialized selector silently takes a branch in simulation. casez only ignores the bits you mark with ?.

What does a 3-to-8 decoder output for input 3'd5?

8'b0010_0000: only bit 5 is high.

Why does a priority encoder need priority, unlike a plain encoder?

A plain encoder assumes exactly one input is active. When several requests are active at once, a priority encoder defines which one wins (here the highest-numbered), so the output is always well defined.