/*
 * Exercise P5 on Tiny Tapeout: a Moore FSM that recognizes 0, 1 on its
 * input, the snail of Harris & Harris.  The 7-segment display of the demo
 * board shows the number of the state, and its dot is the output Y.
 *
 * Add snail_fsm.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 input A: set it before a tick of the clock.  The reset button
    // of the board resets the FSM.

    wire [1:0] state;

    snail_fsm i_snail_fsm (
        .clk   ( clk       ),
        .rst   ( ~ rst_n   ),
        .a     ( ui_in [0] ),
        .state ( state     )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g,
    // uo_out [7] is the dot.  0, 1 or 2 for the states S0, S1, S2; the dot
    // is Y = S1.

    reg [6:0] gfedcba;

    always @(*)
        case (state)
        2'b00:   gfedcba = 7'b0111111;  // 0
        2'b01:   gfedcba = 7'b0000110;  // 1
        2'b10:   gfedcba = 7'b1011011;  // 2
        default: gfedcba = 7'b1000000;  // never reached: a dash
        endcase

    assign uo_out  = { state [1], gfedcba };

    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, 1'b0 };

endmodule
