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

// Exercise BB3 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 comparator.sv.
//
// Keys: key [3:0] a, key [7:4] b. The LEDs: led [0] a_gt_b, led [1] a_eq_b,
// led [2] a_lt_b. The screen shows 11 lamps in a row: A0, A1, A2, A3, B0, B1,
// B2, B3, A>B, A=B, A<B; 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 a_gt_b;
    logic a_eq_b;
    logic a_lt_b;

    comparator i_comparator
    (
        .a      ( a      ),
        .b      ( b      ),
        .a_gt_b ( a_gt_b ),
        .a_eq_b ( a_eq_b ),
        .a_lt_b ( a_lt_b )
    );

    assign led      = { 5'b0, a_lt_b, a_eq_b, a_gt_b };

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

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

    wire [10:0] lamp = { a_lt_b, a_eq_b, a_gt_b, b, a };

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

        for (int i = 0; i < 11; i ++)
            if (x >= 9' (20 + i * 41) & x < 9' (49 + i * 41) & y >= 122 & y < 151)
            begin
                if (i < 8)
                    blue  = lamp [i] ? 31 : 6;
                else
                    green = lamp [i] ? 63 : 12;
            end
    end

endmodule
