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

// Exercise B6 in the hackathon setup of Basics-Graphics-Music (BGM):
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy hackathon/problems/5_shift_register, replace its hackathon_top.sv
// with this file and add shift_register.sv.
//
// The register shifts on slow_clock, one beat per second, like the
// breadboard with the 555 module.  Any key is D.  The LEDs and a row of
// eight squares on the screen show the bits; Q1, the newest, is on the left.

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 [7:0] q;

    shift_register i_shift_register
    (
        .clk ( slow_clock ),
        .rst ( reset      ),
        .d   ( | key      ),
        .q   ( q          )
    );

    assign led      = q;

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

    //------------------------------------------------------------------------
    // Eight squares on the 480 x 272 screen, bright orange for 1, dim for 0

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

        for (int i = 0; i < 8; i ++)
            if (x >= 9' (24 + i * 56) & x < 9' (64 + i * 56) & y >= 116 & y < 156)
            begin
                red   = q [i] ? 31 : 6;
                green = q [i] ? 40 : 8;
            end
    end

endmodule
