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

// Exercise P3 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 fifo_adder.sv.
//
// The adder runs from slow_clock.  Keys 2-0 are the data, key 5 is PUSH A,
// key 6 PUSH B and key 7 POP C: hold the data keys, then hold a PUSH key
// until the next tick of slow_clock.  The LEDs and the rightmost digit are
// as in lab_top.  The screen shows three columns of lamps, A, B and C: the
// head of each FIFO, bit 3 on top, and below it whether it has data; under
// C also whether it has room, and the transfer lamp T between B and C.

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 [2:0] head_a, head_b;
    logic [3:0] head_c;
    logic       dor_a, dor_b, dor_c, dir_c, transfer;

    fifo_adder i_fifo_adder
    (
        .clk      ( slow_clock ),
        .rst      ( reset      ),
        .push_a   ( key [5]    ),
        .push_b   ( key [6]    ),
        .pop_c    ( key [7]    ),
        .data     ( key [2:0]  ),
        .head_a   ( head_a     ),
        .dor_a    ( dor_a      ),
        .head_b   ( head_b     ),
        .dor_b    ( dor_b      ),
        .head_c   ( head_c     ),
        .dor_c    ( dor_c      ),
        .dir_c    ( dir_c      ),
        .transfer ( transfer   )
    );

    assign led = { dor_b, dor_a, dir_c, dor_c, head_c };

    // The head of C as a hexadecimal digit, dark while C is empty

    logic [6:0] abcdefg;

    always_comb
        if (~ dor_c)
            abcdefg = 7'b000_0000;
        else
            case (head_c)
            4'h0: abcdefg = 7'b111_1110;
            4'h1: abcdefg = 7'b011_0000;
            4'h2: abcdefg = 7'b110_1101;
            4'h3: abcdefg = 7'b111_1001;
            4'h4: abcdefg = 7'b011_0011;
            4'h5: abcdefg = 7'b101_1011;
            4'h6: abcdefg = 7'b101_1111;
            4'h7: abcdefg = 7'b111_0000;
            4'h8: abcdefg = 7'b111_1111;
            4'h9: abcdefg = 7'b111_1011;
            4'ha: abcdefg = 7'b111_0111;
            4'hb: abcdefg = 7'b001_1111;
            4'hc: abcdefg = 7'b100_1110;
            4'hd: abcdefg = 7'b011_1101;
            4'he: abcdefg = 7'b100_1111;
            4'hf: abcdefg = 7'b100_0111;
            endcase

    assign abcdefgh = { abcdefg, 1'b0 };
    assign digit    = 8'b0000_0001;

    //------------------------------------------------------------------------
    // Columns at x = 80 (A), 200 (B) and 340 (C), 40 pixels wide.  Rows of
    // 30 pixels: the head bits 3-0 from y = 30, then DOR, then DIR for C.
    // Data in blue, flags in green, T in orange at x = 270.

    wire [3:0] col_a = { 1'b0, head_a & { 3 { dor_a } } };
    wire [3:0] col_b = { 1'b0, head_b & { 3 { dor_b } } };
    wire [3:0] col_c = head_c & { 4 { dor_c } };

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

        for (int i = 0; i < 4; i ++)
            if (y >= 9' (30 + i * 36) & y < 9' (60 + i * 36))
            begin
                if (x >= 80 & x < 120)
                    blue = col_a [3 - i] ? 31 : 5;

                if (x >= 200 & x < 240)
                    blue = col_b [3 - i] ? 31 : 5;

                if (x >= 340 & x < 380)
                    blue = col_c [3 - i] ? 31 : 5;
            end

        if (y >= 180 & y < 210)
        begin
            if (x >= 80 & x < 120)
                green = dor_a ? 63 : 10;

            if (x >= 200 & x < 240)
                green = dor_b ? 63 : 10;

            if (x >= 340 & x < 380)
                green = dor_c ? 63 : 10;
        end

        if (y >= 216 & y < 246 & x >= 340 & x < 380)
            green = dir_c ? 63 : 10;

        if (y >= 102 & y < 132 & x >= 270 & x < 310)
        begin
            red   = transfer ? 31 : 6;
            green = transfer ? 30 : 6;
        end
    end

endmodule
