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

module code_lock_chips
(
    input  logic       clk,
    input  logic       rst,
    input  logic       btn,
    output logic       a,
    output logic [1:0] state,
    output logic       open
);

    // Connections between the chips that are not ports

    wire nrst;
    wire na;
    wire d0;
    wire d1;
    wire t2;
    wire t3;

    // 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_i;
    wire unused_u2_j;
    wire unused_u2_k;
    wire unused_u2_l;
    wire unused_u1_q4;
    wire unused_u1_q5;
    wire unused_u1_q6;
    wire unused_u3_j;
    wire unused_u4_k;
    wire unused_u4_l;

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

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

    cd4069 i_u2
    (
        .a ( rst         ),  // Pin 1 <- reset button
        .g ( nrst        ),  // Pin 2 -> ~RST
        .b ( a           ),  // Pin 3 <- U1 pin 2 (Q1)
        .h ( na          ),  // Pin 4 -> ~A
        .c ( gnd         ),  // Pin 5 <- GND, unused input
        .i ( unused_u2_i ),  // Pin 6, unused output
        .j ( unused_u2_j ),  // Pin 8, unused output
        .d ( vdd         ),  // Pin 9 <- +5V, unused input
        .k ( unused_u2_k ),  // Pin 10, unused output
        .e ( vdd         ),  // Pin 11 <- +5V, unused input
        .l ( unused_u2_l ),  // Pin 12, unused output
        .f ( vdd         )   // Pin 13 <- +5V, unused input
    );

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

    // U1: CD40174 hex D flip-flop with common clock and clear, pin 1 in column 21

    cd40174 i_u1
    (
        .clear_n ( nrst         ),  // Pin 1 <- U2 pin 2 (G)
        .q1      ( a            ),  // Pin 2 -> A
        .d1      ( btn          ),  // Pin 3 <- code button
        .d2      ( d0           ),  // Pin 4 <- U4 pin 3 (J)
        .q2      ( state [0]    ),  // Pin 5 -> S0
        .d3      ( d1           ),  // Pin 6 <- U4 pin 11 (M)
        .q3      ( state [1]    ),  // Pin 7 -> S1
        .clock   ( clk          ),  // Pin 9 <- 555 clock module
        .q4      ( unused_u1_q4 ),  // Pin 10, unused output
        .d4      ( vdd          ),  // Pin 11 <- +5V, unused input
        .q5      ( unused_u1_q5 ),  // Pin 12, unused output
        .d5      ( vdd          ),  // Pin 13 <- +5V, unused input
        .d6      ( vdd          ),  // Pin 14 <- +5V, unused input
        .q6      ( unused_u1_q6 )   // Pin 15, unused output
    );

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

    // U3: CD4081 quad 2-input AND, pin 1 in column 31

    cd4081 i_u3
    (
        .a ( vdd         ),  // Pin 1 <- +5V, unused input
        .b ( vdd         ),  // Pin 2 <- +5V, unused input
        .j ( unused_u3_j ),  // Pin 3, unused output
        .k ( t2          ),  // Pin 4 -> S0·~A
        .c ( state [0]   ),  // Pin 5 <- U1 pin 5 (Q2)
        .d ( na          ),  // Pin 6 <- U2 pin 4 (H)
        .e ( state [1]   ),  // Pin 8 <- U1 pin 7 (Q3)
        .f ( state [0]   ),  // Pin 9 <- U1 pin 5 (Q2)
        .l ( open        ),  // Pin 10 -> OPEN
        .m ( t3          ),  // Pin 11 -> S1·A
        .g ( state [1]   ),  // Pin 12 <- U1 pin 7 (Q3)
        .h ( a           )   // Pin 13 <- U1 pin 2 (Q1)
    );

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

    // U4: CD4071 quad 2-input OR, pin 1 in column 39

    cd4071 i_u4
    (
        .a ( open        ),  // Pin 1 <- U3 pin 10 (L)
        .b ( a           ),  // Pin 2 <- U1 pin 2 (Q1)
        .j ( d0          ),  // Pin 3 -> S0'
        .k ( unused_u4_k ),  // Pin 4, unused output
        .c ( gnd         ),  // Pin 5 <- GND, unused input
        .d ( gnd         ),  // Pin 6 <- GND, unused input
        .e ( vdd         ),  // Pin 8 <- +5V, unused input
        .f ( vdd         ),  // Pin 9 <- +5V, unused input
        .l ( unused_u4_l ),  // Pin 10, unused output
        .m ( d1          ),  // Pin 11 -> S1'
        .g ( t3          ),  // Pin 12 <- U3 pin 11 (M)
        .h ( t2          )   // Pin 13 <- U3 pin 4 (K)
    );

endmodule
