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

// Exercise B8 in the hackathon setup of Basics-Graphics-Music (BGM):
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy a directory such as hackathon/problems/1_gates_and_muxes, replace its
// hackathon_top.sv with this file and add adder_leds.sv.
//
// Keys: key [3:0] a, key [7:4] b. The LEDs: led [3:0] sum, led [4] carry. The
// screen shows 13 lamps in a row: A1, A2, A3, A4, B1, B2, B3, B4, S1, S2, S3,
// S4, CO; inputs in blue, outputs in green.
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
);

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

    wire [3:0] a = key [3:0];
    wire [3:0] b = key [7:4];

    logic [3:0] sum;
    logic       carry;

    adder_leds i_adder_leds
    (
        .a     ( a     ),
        .b     ( b     ),
        .sum   ( sum   ),
        .carry ( carry )
    );

    assign led      = { 3'b0, carry, sum };

    assign abcdefgh = '0;
    assign digit    = '0;

    //------------------------------------------------------------------------
    // 13 square lamps on the 480 x 272 screen, 23 pixels wide, 35 apart:
    // the inputs in blue, the outputs in green.  Bright when 1, dim when 0.

    wire [12:0] lamp = { carry, sum, b, a };

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

        for (int i = 0; i < 13; i ++)
            if (x >= 9' (18 + i * 35) & x < 9' (41 + i * 35) & y >= 125 & y < 148)
            begin
                if (i < 8)
                    blue  = lamp [i] ? 31 : 6;
                else
                    green = lamp [i] ? 63 : 12;
            end
    end

endmodule
