| Status | Title | Difficulty | Tags |
|---|---|---|---|
An Encoder is a combinational circuit that converts 2ⁿ input lines into n output lines.
At any time, only one input should be active (logic 1).
The encoder outputs the binary code corresponding to the active input line.
In this tutorial, we will design a 4-to-2 encoder in Verilog and verify it using a testbench.
A 4-to-2 encoder has:
I0, I1, I2, I3Y1, Y0Encoding rules:
I0 = 1 → Output = 00I1 = 1 → Output = 01I2 = 1 → Output = 10I3 = 1 → Output = 11Boolean equations:
Y0 = I1 | I3Y1 = I2 | I3Limitation: If more than one input is 1, the output becomes undefined.
(That’s why we use a Priority Encoder in real designs.)
encoder4to2 with:
I0, I1, I2, I3Y0, Y1assign Y0 = I1 | I3;assign Y1 = I2 | I3;testbench module:
| I3 | I2 | I1 | I0 | Y1 | Y0 |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 | 0 |
| 0 | 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 0 | 0 | 1 | 1 |
1.Y1 is MSB, Y0 is LSB).Submit your Verilog code using Cmd+Enter to see the simulation results and waveform visualization.