3-to-8 Decoder

  1. Introduction


    A n to 2n decoder is a combinatorial logic device which has n input lines and 2n output lines. For each possible combination of n input binary lines, one and only one output signal will be logic 1. Thus, the decoder is a min-term generator in which each output corresponds to one min-term. Decoders are important logic blocks that find a wide variety of applications in the design of digital systems.
    Input: (log2n)
    Output: n bits (exactly one output pins is 1, rest of the pins are 0)
    Example: 3-8 decoder
    Input: 3 bits representing a binary input number
    Output: 1 bit corresponding to the value of the binary input number is set to 1
    Figure 1 shows the block diagram of the 3-to-8 decoder.

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

  2. Truth Table


    Figure 2 shows the truth table of a 3-to-8 decoder. Ip0 to Ip2 are the binary input lines and the Op0 to Op7 are the eight output lines.

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

  3. Verilog Module


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

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

  4. Verilog Code for 3-to-8 Decoder (decoder3to8.v)



    1. module decoder3to8( Ip0,
    2. Ip1,
    3. Ip2,
    4. Op0,
    5. Op1,
    6. Op2,
    7. Op3,
    8. Op4,
    9. Op5,
    10. Op6,
    11. Op7,
    12. EN
    13. );
    14. input Ip0,
    15. Ip1,
    16. Ip2; //The three Input lines of the decoder
    17. input EN; //The decoder Enable input. High active.
    18. output reg Op0=0,
    19. Op1=0,
    20. Op2=0,
    21. Op3=0,
    22. Op4=0,
    23. Op5=0,
    24. Op6=0,
    25. Op7=0;
    26. always @ (EN,Ip2,Ip1,Ip0)
    27. begin
    28. Op0=0;
    29. Op1=0;
    30. Op2=0;
    31. Op3=0;
    32. Op4=0;
    33. Op5=0;
    34. Op6=0;
    35. Op7=0;
    36. if (EN==1'b1)
    37. begin
    38. case ({Ip2,Ip1,Ip0})
    39. 3'b000: Op0=1;
    40. 3'b001: Op1=1;
    41. 3'b010: Op2=1;
    42. 3'b011: Op3=1;
    43. 3'b100: Op4=1;
    44. 3'b101: Op5=1;
    45. 3'b110: Op6=1;
    46. 3'b111: Op7=1;
    47. default: begin
    48. Op0=0;
    49. Op1=0;
    50. Op2=0;
    51. Op3=0;
    52. Op4=0;
    53. Op5=0;
    54. Op6=0;
    55. Op7=0;
    56. end
    57. endcase
    58. end
    59. end
    60. endmodule
    Figure 4. Verilog Code for 3-to-8 decoder

  5. Verilog Test Bench for 3-to-8 Decoder (decoder3to8_tb.v)



    1. `timescale 1ns / 1ps
    2. module decoder3to8_tb;
    3. // Inputs
    4. reg Ip0;
    5. reg Ip1;
    6. reg Ip2;
    7. reg EN;
    8. // Outputs
    9. wire Op0;
    10. wire Op1;
    11. wire Op2;
    12. wire Op3;
    13. wire Op4;
    14. wire Op5;
    15. wire Op6;
    16. wire Op7;
    17. //temporary variable
    18. reg [2:0] count = 3'd0;
    19. // Instantiate the Unit Under Test (UUT)
    20. decoder3to8 uut (
    21. .Ip0(Ip0),
    22. .Ip1(Ip1),
    23. .Ip2(Ip2),
    24. .Op0(Op0),
    25. .Op1(Op1),
    26. .Op2(Op2),
    27. .Op3(Op3),
    28. .Op4(Op4),
    29. .Op5(Op5),
    30. .Op6(Op6),
    31. .Op7(Op7),
    32. .EN(EN)
    33. );
    34. initial begin
    35. // Initialize Inputs
    36. Ip0 = 1'b0;
    37. Ip1 = 1'b0;
    38. Ip2 = 1'b0;
    39. EN = 1'b0;
    40. // Wait 100 ns for global reset to finish
    41. #100;
    42. // Add stimulus here
    43. EN = 1'b1;
    44. #20;
    45. for (count = 0; count < 8; count = count + 1'b1)
    46. begin
    47. {Ip0,Ip1,Ip2} = {Ip0,Ip1,Ip2} + 1'b1;
    48. #20;
    49. end
    50. EN = 1'b0;
    51. end
    52. endmodule
    Figure 5. Verilog Test-bench for 3-to-8 decoder

  6. Timing Diagram


    Figure 6. Timing diagram of 3-to-8 decoder