Datapaths steer with three blocks. A mux picks one input, a decoder wakes one line, and an encoder reports which line is active.
Block | Function | Inputs → outputs | Typical use |
|---|---|---|---|
Multiplexer (mux) | Chooses one of N inputs | N data + select → 1 | Register-file read, ALU result select |
Decoder | Converts a binary code to one active line | log₂N → N (one-hot) | Address decode, write enables |
Encoder | Converts one-hot lines back to a binary code | N → log₂N | Interrupt number, request index |
Priority encoder | An encoder that tolerates several active lines | N → log₂N + valid | Arbitration, find-first-set |
4 lessons, about 18 min
With the decoder enabled, the encoder returns the original 3-bit code. With the decoder disabled, all lines are 0 and a plain encoder has no valid answer: a priority encoder with a valid output flags that case explicitly.
A mux picks the word. Selects 5 to 7 are out of range, so the mux must define them: later lessons return 0 or assert they never happen.
The whole topic on one page, taken from each lesson's Remember box. Read it before an interview, or print it.
Mux Theory and Design Patterns
An N:1 mux is a tree of 2:1 muxes with depth ceil(log2 N).
Plain case with a default is the safe choice. Avoid casex, because X and Z in the selector become wildcards.
AND-OR form is for selects that are already one-hot; with no select active it gives 0.
Rule of thumb: give every case a default, or assign the output before it.
A Parameterized Mux
Pack the inputs into one NW vector and pick input i with the part-select [iW +: W].
Guard the select width for N = 1, because $clog2(1) is 0.
Guard with sel < N. For a power-of-two N the tool removes the guard for free.
Rule of thumb: decide what an out-of-range select does, and write it down.
Binary Decoder and One-Hot Enables
A k-bit decoder gives 2^k outputs with one high; with enable low, all are low.
$onehot0(we) is true for zero or one bit set, but it does not reject X, so also check !$isunknown(we).
Wide address decodes are built in stages, because each select bit doubles the outputs.
Rule of thumb: use one decoder, not a chain of address comparisons that must be edited when the map changes.
Priority Encoder
Always output valid with the index, because index 0 is ambiguous without it.
Scanning from the high index down lets the lowest set bit overwrite last and win. Reverse the loop for highest-first.
A literal loop has depth proportional to N; for 32 or 64 inputs use a tree.
Rule of thumb: carry the valid flag with the index.
Mini project: a 32-bit leading-zero counter and normaliser
Practice question: Write a 2:1 multiplexer in SystemVerilog
Code lab: Build a 4-bit 4:1 mux
Practice question: A parameterized-width multiplexer written to be testable
Practice question: A 3-to-8 decoder with enable: one-hot guarantees and X-propagation
Code lab: Build a 2:4 decoder
Interview: run a mock interview on this topic