/*
 * Exercise P6 on Tiny Tapeout: a code lock, a state machine that opens
 * after the input gives 1, 0, 1 at three clock edges in a row.  The
 * 7-segment display of the demo board shows the state, and its dot is OPEN.
 *
 * Add code_lock.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
);

    // The demo board makes the project clock; set it to 1 Hz.  DIP switch 0
    // is the code button: set it before a tick of the clock.  The reset
    // button of the board closes the lock.

    wire       a, open;
    wire [1:0] state;

    code_lock i_code_lock (
        .clk   ( clk       ),
        .rst   ( ~ rst_n   ),
        .btn   ( ui_in [0] ),
        .a     ( a         ),
        .state ( state     ),
        .open  ( open      )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g,
    // uo_out [7] is the dot.  The state, 0 to 3; the dot is OPEN.

    reg [6:0] gfedcba;

    always @(*)
        case (state)
        2'd0: gfedcba = 7'b0111111;  // 0
        2'd1: gfedcba = 7'b0000110;  // 1
        2'd2: gfedcba = 7'b1011011;  // 2
        2'd3: gfedcba = 7'b1001111;  // 3
        endcase

    assign uo_out  = { open, gfedcba };

    // The sampled input and OPEN on BIDIR pins 0-1

    assign uio_out = { 6'b0, open, a };
    assign uio_oe  = 8'b0000_0011;

    // List all unused inputs to prevent warnings

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

endmodule
