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

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

    // Connections between the chips that are not ports

    wire na;
    wire d1;

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

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

    // Outputs that go nowhere

    wire unused_u1_q1_n;
    wire unused_u1_q2_n;
    wire unused_u2_k;
    wire unused_u2_l;
    wire unused_u2_m;
    wire unused_u3_g;
    wire unused_u3_h;
    wire unused_u3_j;
    wire unused_u3_k;
    wire unused_u3_l;

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

    // U1: CD4013 dual D flip-flop with set and reset, pin 1 in column 12

    cd4013 i_u1
    (
        .q1     ( state [0]      ),  // Pin 1 -> S0
        .q1_n   ( unused_u1_q1_n ),  // Pin 2, unused output
        .clock1 ( clk            ),  // Pin 3 <- 555 clock module
        .reset1 ( rst            ),  // Pin 4 <- button RESET
        .d1     ( na             ),  // Pin 5 <- U3 pin 6 (I)
        .set1   ( gnd            ),  // Pin 6 <- GND
        .set2   ( gnd            ),  // Pin 8 <- GND
        .d2     ( d1             ),  // Pin 9 <- U2 pin 3 (J)
        .reset2 ( rst            ),  // Pin 10 <- button RESET
        .clock2 ( clk            ),  // Pin 11 <- 555 clock module
        .q2_n   ( unused_u1_q2_n ),  // Pin 12, unused output
        .q2     ( state [1]      )   // Pin 13 -> S1
    );

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

    // U2: CD4081 quad 2-input AND, pin 1 in column 20

    cd4081 i_u2
    (
        .a ( state [0]   ),  // Pin 1 <- U1 pin 1 (Q1)
        .b ( a           ),  // Pin 2 <- button A
        .j ( d1          ),  // Pin 3 -> S0·A
        .k ( unused_u2_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_u2_l ),  // Pin 10, unused output
        .m ( unused_u2_m ),  // Pin 11, unused output
        .g ( vdd         ),  // Pin 12 <- +5V, unused input
        .h ( vdd         )   // Pin 13 <- +5V, unused input
    );

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

    // U3: CD4069 hex inverter (six NOT gates), pin 1 in column 28

    cd4069 i_u3
    (
        .a ( vdd         ),  // Pin 1 <- +5V, unused input
        .g ( unused_u3_g ),  // Pin 2, unused output
        .b ( vdd         ),  // Pin 3 <- +5V, unused input
        .h ( unused_u3_h ),  // Pin 4, unused output
        .c ( a           ),  // Pin 5 <- button A
        .i ( na          ),  // Pin 6 -> ~A
        .j ( unused_u3_j ),  // Pin 8, unused output
        .d ( gnd         ),  // Pin 9 <- GND, unused input
        .k ( unused_u3_k ),  // Pin 10, unused output
        .e ( gnd         ),  // Pin 11 <- GND, unused input
        .l ( unused_u3_l ),  // Pin 12, unused output
        .f ( gnd         )   // Pin 13 <- GND, unused input
    );

endmodule
