/*
 * Exercise P1 on Tiny Tapeout: a bit-serial adder.  The 7-segment display
 * of the demo board shows the deserializer as a 4511 would.
 *
 * Add serial_adder.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

// LOAD is a synchronous load for the serializers and the carry and an
// asynchronous clear for the deserializer, as on the breadboard
// verilator lint_off SYNCASYNCNET

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 switches
    // 0-3 are A and switches 4-7 are B.  All eight are taken, so LOAD comes
    // from BIDIR pin 0: connect it to VCC through one tick of the clock.

    wire       serial_a, serial_b, serial_sum;
    wire [3:0] sum;
    wire [6:0] abcdefg_a, abcdefg_b, abcdefg_sum;

    serial_adder i_serial_adder (
        .clk         ( clk          ),
        .load        ( uio_in [0]   ),
        .a           ( ui_in  [3:0] ),
        .b           ( ui_in  [7:4] ),
        .serial_a    ( serial_a     ),
        .serial_b    ( serial_b     ),
        .serial_sum  ( serial_sum   ),
        .sum         ( sum          ),
        .abcdefg_a   ( abcdefg_a    ),
        .abcdefg_b   ( abcdefg_b    ),
        .abcdefg_sum ( abcdefg_sum  )
    );

    // 7-segment display: uo_out [0] is segment a, ..., uo_out [6] is g; the
    // dot is the sum bit on its way

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

    // The deserializer and the streams of A and B on BIDIR pins 2-7

    assign uio_out = { sum, serial_b, serial_a, 2'b0 };
    assign uio_oe  = 8'b1111_1100;

    // List all unused inputs to prevent warnings

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

endmodule
