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.
en | sel (binary) | hit[2:0] | Meaning |
|---|---|---|---|
| 1 | 00 | 001 | Channel 0 |
| 1 | 01 | 010 | Channel 1 |
| 1 | 10 | 100 | Channel 2 |
| 1 | 11 | 000 | No channel |
| 0 | Any value | 000 | Selection 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.
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 time | Event | en | sel (decimal) | Demux din | Demux y1 | hit[2:0] |
|---|---|---|---|---|---|---|
| 1 ns | T0 | 1 | 0 | 52 | 00 | 001 |
| 11 ns | T1 | 1 | 1 | 52 | 52 | 010 |
| 21 ns | T2 | 1 | 1 | 00 | 00 | 010 |
| 31 ns | T3 | 1 | 2 | 00 | 00 | 100 |
| 41 ns | T4 | 1 | 3 | 00 | 00 | 000 |
| 51 ns | T5 | 0 | 1 | 52 | 00 | 000 |
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.
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?
| Channel | Select line |
|---|---|
| 0 | hit[0] = 1 |
| 1 | hit[1] = 0 |
| 2 | hit[2] = 0 |
hit[2:0] = 001
Teaching film
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)