// CD4051B: 8-channel analog multiplexer/demultiplexer.
//
// Ports in pin order, named as in the datasheet.  The power pins, VEE (7),
// VSS (8) and VDD (16), are left out.
//
// The real chip is a set of analog switches: the selected channel is joined
// to COM and a signal can go either way.  This digital model passes the
// selected channel to COM only, the way the exercises use it; INH = 1 opens
// all switches and COM floats.  Synthesizable while INH is 0.

module cd4051
(
    input  logic ch4,  // Pin 1
    input  logic ch6,  // Pin 2
    output logic com,  // Pin 3:  common in/out
    input  logic ch7,  // Pin 4
    input  logic ch5,  // Pin 5
    input  logic inh,  // Pin 6:  1 = all switches open
                       // Pin 7:  VEE
                       // Pin 8:  VSS
    input  logic c,    // Pin 9:  select, weight 4
    input  logic b,    // Pin 10: select, weight 2
    input  logic a,    // Pin 11: select, weight 1
    input  logic ch3,  // Pin 12
    input  logic ch0,  // Pin 13
    input  logic ch1,  // Pin 14
    input  logic ch2   // Pin 15
                       // Pin 16: VDD
);

    wire [7:0] ch  = { ch7, ch6, ch5, ch4, ch3, ch2, ch1, ch0 };
    wire [2:0] sel = { c, b, a };

    assign com = inh ? 1'bz : ch [sel];

endmodule
