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

// Exercise BB9 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 register_file.sv.
//
// The register file runs from slow_clock.  Keys 3-0 are the data and keys
// 5-4 the address: the word to write and the word that read port A shows.
// Hold key 7, WRITE, until the next tick of slow_clock.  Read port B shows
// the four words in turn, one per tick.  LEDs 7-4 show port A, LEDs 3-0
// port B.  The screen shows port A in blue on the top row, port B in orange
// below it, and the address of port B 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 [1:0] raddr_b;
    logic [3:0] rdata_a, rdata_b;

    // Read port B scans the four words

    always_ff @ (posedge slow_clock or posedge reset)
        if (reset)
            raddr_b <= '0;
        else
            raddr_b <= raddr_b + 1'd1;

    register_file i_register_file
    (
        .clk     ( slow_clock ),
        .we      ( key [7]    ),
        .waddr   ( key [5:4]  ),
        .wdata   ( key [3:0]  ),
        .raddr_a ( key [5:4]  ),
        .rdata_a ( rdata_a    ),
        .raddr_b ( raddr_b    ),
        .rdata_b ( rdata_b    )
    );

    assign led      = { rdata_a, rdata_b };

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

    //------------------------------------------------------------------------
    // Square lamps on the 480 x 272 screen, bit 3 on the left: port A in
    // blue on the top row, port B in orange on the bottom row, and the two
    // address bits of port B in grey before it.  Bright for 1, dim for 0.

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

        for (int i = 0; i < 4; i ++)
        begin
            if (x >= 9' (200 + i * 50) & x < 9' (240 + i * 50) & y >= 80 & y < 120)
            begin
                green = rdata_a [3 - i] ? 40 : 8;
                blue  = rdata_a [3 - i] ? 31 : 6;
            end

            if (x >= 9' (200 + i * 50) & x < 9' (240 + i * 50) & y >= 152 & y < 192)
            begin
                red   = rdata_b [3 - i] ? 31 : 6;
                green = rdata_b [3 - i] ? 32 : 6;
            end
        end

        for (int i = 0; i < 2; i ++)
            if (x >= 9' (60 + i * 50) & x < 9' (100 + i * 50) & y >= 152 & y < 192)
            begin
                red   = raddr_b [1 - i] ? 20 : 4;
                green = raddr_b [1 - i] ? 40 : 8;
                blue  = raddr_b [1 - i] ? 20 : 4;
            end
    end

endmodule
