/*
 * Exercise B4 on Tiny Tapeout: a D flip-flop, with DIP switch 0 as D and
 * the 7-segment display of the demo board showing Q as the digit 0 or 1.
 *
 * Add d_flip_flop.sv to source_files in info.yaml next to project.v.
 * Rename the module to tt_um_<your GitHub name>_<project name> and use the
 * same name as top_module in info.yaml.
 */

`default_nettype none

module tt_um_example (
    input  wire [7:0] ui_in,    // DIP switches, INPUT Pmod, RP2350 GPIO17-24
    output wire [7:0] uo_out,   // 7-segment display, OUTPUT Pmod, RP2350 GPIO33-40
    input  wire [7:0] uio_in,   // BIDIR Pmod, RP2350 GPIO25-32
    output wire [7:0] uio_out,  //   (same pins)
    output wire [7:0] uio_oe,   //   1 = your design drives the pin
    input  wire       ena,      // set by the on-chip project mux, not a board pin
    input  wire       clk,      // RP2350 GPIO16
    input  wire       rst_n     // RP2350 GPIO14
);

    // Set the project clock to 1 Hz (clock_hz: 1 in info.yaml): the demo
    // board makes it, and clk drives the flip-flop directly, like the 555
    // module on the breadboard.  The reset button of the board is rst_n.

    wire d = ui_in [0];
    wire q, q_n;

    d_flip_flop i_d_flip_flop (
        .clk ( clk      ),
        .rst ( ~ rst_n  ),
        .d   ( d        ),
        .q   ( q        ),
        .q_n ( q_n      )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g,
    // uo_out [7] is the dot.  The digit shows Q: segments b and c make 1,
    // a-f make 0.  The dot shows D, the switch.

    assign uo_out  = { d, q ? 7'b000_0110 : 7'b011_1111 };

    assign uio_out = 8'b0;
    assign uio_oe  = 8'b0;

    // List all unused inputs to prevent warnings

    wire _unused = & { ena, ui_in [7:1], uio_in, q_n, 1'b0 };

endmodule
