// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon
// This module uses few parameterization and relaxed typing rules

// Exercise P1 in the hackathon setup of Basics-Graphics-Music (BGM):
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy a directory such as hackathon/problems/5_shift_register, replace its
// hackathon_top.sv with this file and add serial_adder.sv.
//
// The adder runs from slow_clock.  Eight keys are not enough for two 4-bit
// numbers and LOAD, so A and B are 3 bits here: keys 2-0 are A, keys 5-3
// are B and key 7 is LOAD.  The LEDs and the rightmost digit are as in
// lab_top.  The screen shows the bit streams of A, B and the sum as three
// lamps on the left and the deserializer as four lamps on the right.

// 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 hackathon_top
(
    input  logic       clock,
    input  logic       slow_clock,
    input  logic       reset,

    input  logic [7:0] key,
    output logic [7:0] led,

    // A dynamic seven-segment display

    output logic [7:0] abcdefgh,
    output logic [7:0] digit,

    // LCD screen interface

    input  logic [8:0] x,
    input  logic [8:0] y,

    output logic [4:0] red,
    output logic [5:0] green,
    output logic [4:0] blue,

    inout  logic [3:0] gpio
);

    //------------------------------------------------------------------------

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

    serial_adder i_serial_adder
    (
        .clk         ( slow_clock         ),
        .load        ( key [7]            ),
        .a           ( { 1'b0, key [2:0] } ),
        .b           ( { 1'b0, key [5:3] } ),
        .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        )
    );

    assign led      = { 1'b0, serial_sum, serial_b, serial_a, sum };

    assign abcdefgh = { abcdefg_sum, 1'b0 };
    assign digit    = 8'b0000_0001;

    //------------------------------------------------------------------------
    // The streams A, B, S on the left, top to bottom; the deserializer on
    // the right, bit 3 first.  Bright for 1, dim for 0.

    wire [2:0] stream = { serial_a, serial_b, serial_sum };

    always_comb
    begin
        red   = 0;
        green = 0;
        blue  = 0;

        for (int i = 0; i < 3; i ++)
            if (x >= 60 & x < 100 & y >= 9' (56 + i * 60) & y < 9' (96 + i * 60))
            begin
                green = stream [2 - i] ? 40 : 8;
                blue  = stream [2 - i] ? 31 : 6;
            end

        for (int i = 0; i < 4; i ++)
            if (x >= 9' (200 + i * 60) & x < 9' (240 + i * 60) & y >= 116 & y < 156)
                green = sum [3 - i] ? 63 : 12;
    end

endmodule
