Decoder

Three channels each have a select line. See how two index bits select one line and decide what happens to the fourth possible code.

The panel uses a two-bit sel to choose a sensor channel. The operator wants to know which channel is selected even when its reading is 00. A decoder turns the selection code into three channel indicators, hit[2:0], one per channel.

How does a two-bit code select one of three channels?

With en=1, selections 0, 1 and 2 raise hit[0], hit[1] and hit[2] respectively. In the display hit[2:0], channel 0 is on the right. The panel has three channels, so the fourth code, 2'b11, leaves all three lines low. Disabling selection with en=0 also leaves them all low.

ensel (binary)hit[2:0]Meaning
100001Channel 0
101010Channel 1
110100Channel 2
111000No channel
0Any value000Selection disabled

A valid selection raises one bit. A disabled or unused selection leaves all three low. With stable binary inputs, the output therefore contains at most one high bit, a property called one-hot-or-zero.

With en high and sel equal to 01, three comparisons check sel against 0, 1 and 2. Only hit[1] is high.
Selection code 01 chooses channel 1, so hit[2:0] is 010.

Give each hit line its own condition

Each assign combines an enabled selection with a matching code. The RTL is synthesizable and combinational. The current values of en and sel determine each output. The module exposes en, sel and hit as its three ports. Changes in a routed data word leave the selected channel unchanged.

module decoder3 (
    input  logic       en,
    input  logic [1:0] sel,
    output logic [2:0] hit
);
    assign hit[0] = en && (sel == 2'd0);
    assign hit[1] = en && (sel == 2'd1);
    assign hit[2] = en && (sel == 2'd2);
endmodule

Try en=1, sel=2'b11. All three comparisons are false, so the result is 000. The continuous assignments give every output a value for each input combination. See F04 for more combinational RTL.

Why does channel 1 stay selected when the reading becomes 00?

A demultiplexer, or demux, sends an input word to the selected output. The hit lines show the panel which channel was chosen. The comparison below uses the same en and sel for both. The testbench also sends an eight-bit word din to a separate demux probe and observes its channel-1 data output, y1. The decoder sees only the control signals.

Sample timeEventensel (decimal)Demux dinDemux y1hit[2:0]
1 nsT0105200001
11 nsT1115252010
21 nsT2110000010
31 nsT3120000100
41 nsT4130000000
51 nsT5015200000

Payloads are hexadecimal. From T1 to T2 only din changes, from 52 to 00, and y1 follows it. The selection remains 1, so hit stays 010. At T5, en=0 clears all three hit lines even though sel is 1 again. Both samples show y1=00. Hit distinguishes a selected channel carrying zero from an inactive channel. Follow the payload in the demux entry.

At T1 and T2 of the Icarus waveform, en is high and sel is 01. The demux input and y1 change from 52 to 00 while decoder hit stays 010.
The recorded simulation gives T1 and T2 identical control values. Only the payload on the demux probe changes.

What happens while the control lines are changing?

Switching sel from 01 to 10 changes two bits. Physical wires and logic paths can have different delays. If 00 appears briefly during the transition, channel 0 can pulse for an instant. This is a glitch. Use the truth table once the inputs and outputs have settled. The stable values in this RTL simulation do not measure the width of a physical glitch.

Synchronous downstream logic can sample the stable result at a rising edge that meets setup and hold requirements. If hit drives a control that acts without waiting for a clock edge, the design must account for these delays and glitches.

Replay the truth table and trace

Extract the executable lab package on Linux or WSL and run bash run.sh. The testbench checks the six samples above. It then tests both 00 and 52 for every enable/select combination, for 16 additional cases. It waits 1 ns after applying each stimulus to observe the result. That interval belongs to the testbench. The package includes the recorded Icarus Verilog 12.0 log and VCD.

Interactive lab

Choose 0, 1 and 2 in turn and watch which hit line goes high. Try 3, then return to 1 and switch enable off. When are all three lines low?

Select lines by channel number
ChannelSelect line
0hit[0] = 1
1hit[1] = 0
2hit[2] = 0

hit[2:0] = 001

Teaching film

Read the transcript

Run the RTL yourself

Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.

Download Decoder RTL (.zip)

Check your understanding

1. Read hit[2:0] from left to right. What do you get with en = 1 and sel = 1?
2. With sel held at 2, what happens when en goes low?
3. Two select bits encode four values. Which line goes high for sel = 3?
4. The neighboring demux input changes from 52 to 00 while en and sel hold steady. What happens to hit?
5. Once inputs settle, which hit pattern violates the selection rule?