Prev Problem
Next Problem

67. Decoder

A. Dataflow

module decoder3to8 (
    input  [2:0] s,
    output [7:0] y
);
    assign y = (8'b0000_0001 << s);
endmodule

B. Behavioral case 

module decoder3to8 (
    input  [2:0] s,
    output reg [7:0] y
);
    always @* begin
        y = 8'b0000_0000;
        case (s)
            3'b000: y[0] = 1'b1;
            3'b001: y[1] = 1'b1;
            3'b010: y[2] = 1'b1;
            3'b011: y[3] = 1'b1;
            3'b100: y[4] = 1'b1;
            3'b101: y[5] = 1'b1;
            3'b110: y[6] = 1'b1;
            3'b111: y[7] = 1'b1;
        endcase
    end
endmodule

C. Structural (gate-level) — optional

module decoder3to8 (
    input  [2:0] s,
    output [7:0] y
);
    wire n2, n1, n0;
    not (n2, s[2]); not (n1, s[1]); not (n0, s[0]);
    and (y[0], n2, n1, n0);
    and (y[1], n2, n1, s[0]);
    and (y[2], n2, s[1], n0);
    and (y[3], n2, s[1], s[0]);
    and (y[4], s[2], n1, n0);
    and (y[5], s[2], n1, s[0]);
    and (y[6], s[2], s[1], n0);
    and (y[7], s[2], s[1], s[0]);
endmodule

💡Remember

  • A decoder produces a one-hot output: exactly one bit high for valid s.
  • With always @*, always give a default assignment to avoid latches.
  • For unknown selects (s contains X/Z), the dataflow shift may produce X; if you want explicit handling, use casez and decide what to drive (e.g., y = 8'bxxxx_xxxx;).