// Exercise M6: an 8:1 multiplexer from a decoder, two AND-OR-INVERT gates
// and an inverter.
//
// Generic version that matches schematic 1, not tied to any chip.  Each
// AND-OR-INVERT gate, like the 4086, ORs four AND terms and inverts the
// result.  The output of the first one goes to ENABLE of the second: when
// one of the first four terms is 1, j1 is 0 and forces j2 to 0, just as a
// fifth term would.  The inverter turns j2 back into the selected data
// bit.  Synthesizable.

module mux_8_aoi
(
    input  logic [7:0] d,       // d [0] is D0
    input  logic [2:0] sel,     // C B A: sel [2] is C, weight 4

    output logic       mux_out
);

    wire [7:0] q = 8'b1 << sel;  // The decoder, like the 4028 with D = 0

    wire j1 = ~ (q [0] & d [0] | q [1] & d [1] | q [2] & d [2] | q [3] & d [3]);
    wire j2 = ~ (q [4] & d [4] | q [5] & d [5] | q [6] & d [6] | q [7] & d [7] | ~ j1);

    assign mux_out = ~ j2;

endmodule
