/*
 * Exercise BB7 on Tiny Tapeout: a decade counter like the 4017.  The
 * 7-segment display of the demo board shows the number of the output that
 * is 1, and its dot CARRY OUT.
 *
 * Add decade_counter.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.  The reset
    // button of the board resets the counter.

    wire [9:0] q;
    wire       carry_out;

    decade_counter i_decade_counter (
        .clk       ( clk       ),
        .rst       ( ~ rst_n   ),
        .q         ( q         ),
        .carry_out ( carry_out )
    );

    // The digit whose output is 1, on the 7-segment display: a is the top
    // segment, g the middle one.  No output at 1: blank.

    function [6:0] digit_of (input [9:0] one_hot);
        case (one_hot)
        10'b00_0000_0001: digit_of = 7'b111_1110;  // 0
        10'b00_0000_0010: digit_of = 7'b011_0000;  // 1
        10'b00_0000_0100: digit_of = 7'b110_1101;  // 2
        10'b00_0000_1000: digit_of = 7'b111_1001;  // 3
        10'b00_0001_0000: digit_of = 7'b011_0011;  // 4
        10'b00_0010_0000: digit_of = 7'b101_1011;  // 5
        10'b00_0100_0000: digit_of = 7'b101_1111;  // 6
        10'b00_1000_0000: digit_of = 7'b111_0000;  // 7
        10'b01_0000_0000: digit_of = 7'b111_1111;  // 8
        10'b10_0000_0000: digit_of = 7'b111_1011;  // 9
        default:          digit_of = 7'b000_0000;
        endcase
    endfunction

    wire [6:0] abcdefg = digit_of (q);

    // uo_out [0] is segment a, ..., uo_out [6] is g, uo_out [7] is the dot

    assign uo_out  = { carry_out, abcdefg [0], abcdefg [1], abcdefg [2], abcdefg [3],
                             abcdefg [4], abcdefg [5], abcdefg [6] };

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

    // List all unused inputs to prevent warnings

    wire _unused = & { ena, ui_in, uio_in, 1'b0 };

endmodule
