Prev Problem
Next Problem

66. Demultiplexer

A. Dataflow one-liner

module demux1to4 (
    input       d,
    input [1:0] s,
    output [3:0] y
);
    // One-hot mask (1 << s), then gate with d replicated across 4 bits.
    assign y = ({4{d}}) & (4'b0001 << s);
endmodule

B. Equivalent case version (Behavioral)

module demux1to4 (
    input       d,
    input [1:0] s,
    output reg [3:0] y
);
    always @* begin
        y = 4'b0000;
        case (s)
            2'b00: y[0] = d;
            2'b01: y[1] = d;
            2'b10: y[2] = d;
            2'b11: y[3] = d;
        endcase
    end
endmodule

💡Remember

  • ({4{d}} & (1 << s)) builds a one-hot at index s and drives it with d.
  • With always @*, always give defaults to avoid unintended latches.
  • If you want X/Z-detecting select handling, use casez and decide what to drive on unknowns (often y = 4'bxxxx;); beginners can stick with plain case and clean inputs.