Close Menu
  • Analog Design
    • Latest Analog Layout Interview Questions (2025)
  • Digital Design
    • Digital Electronics Interview Question(2025)
    • Top VLSI Interview Questions
  • Physical Design
    • Physical Design Interview Questions for VLSI Engineers
  • Verilog
    • Verilog Interview Questions(2024)
  • Forum
Facebook Instagram YouTube LinkedIn WhatsApp
SiliconvlsiSiliconvlsi
Forum Questions Register in Forum Login in Forum
Facebook Instagram YouTube LinkedIn WhatsApp
  • Analog Design
    • Latest Analog Layout Interview Questions (2025)
  • Digital Design
    • Digital Electronics Interview Question(2025)
    • Top VLSI Interview Questions
  • Physical Design
    • Physical Design Interview Questions for VLSI Engineers
  • Verilog
    • Verilog Interview Questions(2024)
  • Forum
SiliconvlsiSiliconvlsi
Home»Verilog»3-to-8 Decoder Verilog Code
Verilog

3-to-8 Decoder Verilog Code

siliconvlsiBy siliconvlsiAugust 4, 2023Updated:July 16, 2024No Comments4 Mins Read
Facebook Pinterest LinkedIn Email WhatsApp
Share
Facebook Twitter LinkedIn Pinterest Email

3-to-8 Decoder Verilog Code

A 3-to-8 decoder is a combinational logic device that takes three input lines and produces eight output lines. For each possible combination of the three input binary lines, one and only one output signal will be logic 1. The decoder acts as a min-term generator, where each output corresponds to one min-term. Decoders find various applications in the design of digital systems.

Figure 1. Block diagram of a 3-to-8 decoder
Figure 1. Block diagram of a 3-to-8 decoder

Truth Table for 3-to-8 Decoder

The truth table for the 3-to-8 decoder is shown in Figure 2. The inputs Ip0 to Ip2 are the binary input lines, and the outputs Op0 to Op7 represent the eight output lines.

Figure 2. Truth table of 3-to-8 decoder.
Figure 2. The truth table of 3-to-8 decoder.

Verilog Module: 3-to-8 Decoder

Figure 3 displays the Verilog module of the 3-to-8 decoder. The module takes three 1-bit binary values from the input ports Ip0 to Ip2. The eight 1-bit binary outputs are presented in eight output ports Op0 to Op7. The decoder function is controlled by the enable signal, EN.

Figure 3. Verilog module of 3-to-8 decoder
Figure 3. Verilog module of 3-to-8 decoder

 

Verilog Code for 3-to-8 Decoder

module decoder3to8(
input Ip0,
input Ip1,
input Ip2,
input EN,
output reg Op0=0,
output reg Op1=0,
output reg Op2=0,
output reg Op3=0,
output reg Op4=0,
output reg Op5=0,
output reg Op6=0,
output reg Op7=0
);

always @ (EN, Ip2, Ip1, Ip0)
begin
Op0 = 0;
Op1 = 0;
Op2 = 0;
Op3 = 0;
Op4 = 0;
Op5 = 0;
Op6 = 0;
Op7 = 0;

if (EN == 1’b1)
begin
case ({Ip2, Ip1, Ip0})
3’b000: Op0 = 1;
3’b001: Op1 = 1;
3’b010: Op2 = 1;
3’b011: Op3 = 1;
3’b100: Op4 = 1;
3’b101: Op5 = 1;
3’b110: Op6 = 1;
3’b111: Op7 = 1;
default: begin
Op0 = 0;
Op1 = 0;
Op2 = 0;
Op3 = 0;
Op4 = 0;
Op5 = 0;
Op6 = 0;
Op7 = 0;
end
encase
end
end

endmodule

Verilog Test Bench: 3-to-8 Decoder

The Verilog test bench for the 3-to-8 decoder is demonstrated in Figure 5. It tests the functionality of the decoder by providing stimulus to the input ports (Ip0 to Ip2) and observing the output ports (Op0 to Op7) based on different values of the enable signal (EN).

module decoder3to8_tb;
// Inputs
reg Ip0;
reg Ip1;
reg Ip2;
reg EN;

// Outputs
wire Op0;
wire Op1;
wire Op2;
wire Op3;
wire Op4;
wire Op5;
wire Op6;
wire Op7;

//temporary variable
reg [2:0] count = 3’d0;

// Instantiate the Unit Under Test (UUT)
decoder3to8 uut (
.Ip0(Ip0),
.Ip1(Ip1),
.Ip2(Ip2),
.Op0(Op0),
.Op1(Op1),
.Op2(Op2),
.Op3(Op3),
.Op4(Op4),
.Op5(Op5),
.Op6(Op6),
.Op7(Op7),
.EN(EN)
);

initial begin
// Initialize Inputs
Ip0 = 1’b0;
Ip1 = 1’b0;
Ip2 = 1’b0;
EN = 1’b0;

// Wait 100 ns for global reset to finish
#100;

// Add stimulus here
EN = 1’b1;
#20;

for (count = 0; count < 8; count = count + 1’b1) begin
{Ip0, Ip1, Ip2} = {Ip0, Ip1, Ip2} + 1’b1;
#20;
end

EN = 1’b0;
end
endmodule

Timing Diagram for 3-to-8 Decoder

The following figure presents the timing diagram of the 3-to-8 decoder, illustrating the waveforms of the output ports (Op0 to Op7) as the input lines are selected based on different values of the enable signal (EN).

Figure 4. Timing diagram of 3-to-8 decoder
Figure 4. The timing diagram of 3-to-8 decoder

The 3-to-8 decoder is a fundamental building block in digital systems that enables selecting one of the eight outputs based on the binary input lines. The provided Verilog module and test bench ensure the correct functionality of the decoder for different input selections.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

Related Posts

What is a Bell’s Theorem?

January 13, 2024

First-In-First-Out Buffer Verilog Code

August 9, 2023

Last-In-First-Out Buffer Verilog Code

August 9, 2023
Leave A Reply Cancel Reply

Facebook X (Twitter) Instagram Pinterest Vimeo YouTube
  • About Us
  • Contact Us
  • Privacy Policy
© 2025 Siliconvlsi.

Type above and press Enter to search. Press Esc to cancel.