// Exercise BB10, chip-level version: the chips and wires of schematic 2 and
// of the breadboard.  Same ports as add_sub.sv.
//
// Generated by tools/bb_lib.py from the checked breadboard wire list;
// change the layout in tools/bb_bb10.py, not this file.  Synthesizable.

module add_sub_chips
(
    input  logic [3:0] a,
    input  logic [3:0] b,
    input  logic       sub,
    output logic [3:0] f,
    output logic       carry_n,
    output logic       all_ones
);

    // Connections between the chips that are not ports

    wire nsub;

    // The power rails of the breadboard, for tied-off inputs

    wire gnd = 1'b0;
    wire vdd = 1'b1;

    // Outputs that go nowhere

    wire unused_u2_h;
    wire unused_u2_i;
    wire unused_u2_j;
    wire unused_u2_k;
    wire unused_u2_l;
    wire unused_u1_p;
    wire unused_u1_g;

    //------------------------------------------------------------------------

    // U2: CD4069 hex inverter (six NOT gates), pin 1 in column 10

    cd4069 i_u2
    (
        .a ( sub         ),  // Pin 1 <- button SUB
        .g ( nsub        ),  // Pin 2 -> ~SUB
        .b ( vdd         ),  // Pin 3 <- +5V, unused input
        .h ( unused_u2_h ),  // Pin 4, unused output
        .c ( vdd         ),  // Pin 5 <- +5V, unused input
        .i ( unused_u2_i ),  // Pin 6, unused output
        .j ( unused_u2_j ),  // Pin 8, unused output
        .d ( gnd         ),  // Pin 9 <- GND, unused input
        .k ( unused_u2_k ),  // Pin 10, unused output
        .e ( gnd         ),  // Pin 11 <- GND, unused input
        .l ( unused_u2_l ),  // Pin 12, unused output
        .f ( gnd         )   // Pin 13 <- GND, unused input
    );

    //------------------------------------------------------------------------

    // U1: CD40181 4-bit arithmetic logic unit (ALU): 16 arithmetic and 16 logic functions, pin 1 in column 21

    cd40181 i_u1
    (
        .b0     ( b [0]       ),  // Pin 1 <- button B0
        .a0     ( a [0]       ),  // Pin 2 <- button A0
        .s3     ( nsub        ),  // Pin 3 <- U2 pin 2 (G)
        .s2     ( sub         ),  // Pin 4 <- button SUB
        .s1     ( sub         ),  // Pin 5 <- button SUB
        .s0     ( nsub        ),  // Pin 6 <- U2 pin 2 (G)
        .cn     ( nsub        ),  // Pin 7 <- U2 pin 2 (G)
        .m      ( gnd         ),  // Pin 8 <- GND
        .f0     ( f [0]       ),  // Pin 9 -> F0
        .f1     ( f [1]       ),  // Pin 10 -> F1
        .f2     ( f [2]       ),  // Pin 11 -> F2
        .f3     ( f [3]       ),  // Pin 13 -> F3
        .a_eq_b ( all_ones    ),  // Pin 14 -> A=B
        .p      ( unused_u1_p ),  // Pin 15, unused output
        .cn4    ( carry_n     ),  // Pin 16 -> C4
        .g      ( unused_u1_g ),  // Pin 17, unused output
        .b3     ( b [3]       ),  // Pin 18 <- button B3
        .a3     ( a [3]       ),  // Pin 19 <- button A3
        .b2     ( b [2]       ),  // Pin 20 <- button B2
        .a2     ( a [2]       ),  // Pin 21 <- button A2
        .b1     ( b [1]       ),  // Pin 22 <- button B1
        .a1     ( a [1]       )   // Pin 23 <- button A1
    );

endmodule
