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

// Exercise M3 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 two_bit_adder.sv.
//
// Keys: key [0] a1, key [1] a0, key [2] b1, key [3] b0. The LEDs: led [0] s2,
// led [1] s1, led [2] s0. The screen shows 7 lamps in a row: A1, A0, B1, B0,
// S2, S1, S0; 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 a1 = key [0];
    wire a0 = key [1];
    wire b1 = key [2];
    wire b0 = key [3];

    logic s2;
    logic s1;
    logic s0;

    two_bit_adder i_two_bit_adder
    (
        .a1 ( a1 ),
        .a0 ( a0 ),
        .b1 ( b1 ),
        .b0 ( b0 ),
        .s2 ( s2 ),
        .s1 ( s1 ),
        .s0 ( s0 )
    );

    assign led      = { 5'b0, s0, s1, s2 };

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

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

    wire [6:0] lamp = { s0, s1, s2, b0, b1, a0, a1 };

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

        for (int i = 0; i < 7; i ++)
            if (x >= 9' (28 + i * 64) & x < 9' (68 + i * 64) & y >= 116 & y < 156)
            begin
                if (i < 4)
                    blue  = lamp [i] ? 31 : 6;
                else
                    green = lamp [i] ? 63 : 12;
            end
    end

endmodule
