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

// Exercise BB2 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 serializer.sv.
//
// Key 0 is LOAD and keys 7-1 are P7-P1; P8 is 0.  The design runs from
// slow_clock.  The LEDs show Q6, Q7 and Q8, and the screen shows the three
// outputs as lamps: Q8 large, in orange, Q6 and Q7 small, 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
);

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

    logic q6, q7, q8;

    serializer i_serializer
    (
        .clk  ( slow_clock          ),
        .load ( key [0]             ),
        .p    ( { 1'b0, key [7:1] } ),
        .q6   ( q6                  ),
        .q7   ( q7                  ),
        .q8   ( q8                  )
    );

    assign led      = { 5'b0, q8, q7, q6 };

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

    //------------------------------------------------------------------------
    // Q6 and Q7 as small green squares, Q8, the serial output, as a large
    // orange one.  Bright when 1, dim when 0.

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

        if (x >= 80 & x < 120 & y >= 116 & y < 156)
            green = q6 ? 63 : 12;
        else if (x >= 160 & x < 200 & y >= 116 & y < 156)
            green = q7 ? 63 : 12;
        else if (x >= 260 & x < 360 & y >= 86 & y < 186)
        begin
            red   = q8 ? 31 : 6;
            green = q8 ? 32 : 6;
        end
    end

endmodule
