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

// Exercise M9 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 mask_merge.sv.
//
// Keys: key [0] ka, key [1] kb, key [5:2] a. The LEDs: led [3:0] d, led [7:4]
// e. The screen shows 14 lamps in a row: Ka, Kb, A1, A2, A3, A4, D1, D2, D3,
// D4, E1, E2, E3, E4; inputs in blue, outputs in green.
//
// B4-B1 is fixed at 0011, as the wires on the breadboard.
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       ka = key [0];
    wire       kb = key [1];
    wire [3:0] a = key [5:2];

    logic [3:0] d;
    logic [3:0] e;

    mask_merge i_mask_merge
    (
        .ka ( ka      ),
        .kb ( kb      ),
        .a  ( a       ),
        .b  ( 4'b0011 ),
        .d  ( d       ),
        .e  ( e       )
    );

    assign led      = { e, d };

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

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

    wire [13:0] lamp = { e, d, a, kb, ka };

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

        for (int i = 0; i < 14; i ++)
            if (x >= 9' (22 + i * 32) & x < 9' (42 + i * 32) & y >= 126 & y < 146)
            begin
                if (i < 6)
                    blue  = lamp [i] ? 31 : 6;
                else
                    green = lamp [i] ? 63 : 12;
            end
    end

endmodule
