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

// Exercise BB8 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_demo.sv.
//
// The FIFO runs from slow_clock.  Keys 7-5 are the data, key 1 is PUSH and
// key 0 is POP: hold the data keys, then hold PUSH until the next tick of
// slow_clock.  The LEDs and the rightmost digit are as in lab_top.  The
// screen shows five lamps: DOR, the three bits of the head and DIR.

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;
    logic       dor, dir;

    fifo_demo i_fifo_demo
    (
        .clk  ( slow_clock ),
        .rst  ( reset      ),
        .push ( key [1]    ),
        .pop  ( key [0]    ),
        .data ( key [7:5]  ),
        .head ( head       ),
        .dor  ( dor        ),
        .dir  ( dir        )
    );

    assign led = { dir, 3'b0, dor, head };

    // The head as a digit, dark while the FIFO is empty

    logic [6:0] abcdefg;

    always_comb
        if (~ dor)
            abcdefg = 7'b0000000;
        else
            case (head)
            3'd0: abcdefg = 7'b1111110;
            3'd1: abcdefg = 7'b0110000;
            3'd2: abcdefg = 7'b1101101;
            3'd3: abcdefg = 7'b1111001;
            3'd4: abcdefg = 7'b0110011;
            3'd5: abcdefg = 7'b1011011;
            3'd6: abcdefg = 7'b1011111;
            3'd7: abcdefg = 7'b1110000;
            endcase

    assign abcdefgh = { abcdefg, ~ dir };
    assign digit    = 8'b0000_0001;

    //------------------------------------------------------------------------
    // Five square lamps on the 480 x 272 screen, left to right: DOR in
    // magenta, Q2, Q1, Q0 in cyan, DIR in orange.  Bright for 1, dim for 0;
    // the head is dark while the FIFO is empty.

    wire [4:0] lamp = { dir, head & { 3 { dor } }, dor };

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

        for (int i = 0; i < 5; i ++)
            if (x >= 9' (80 + i * 70) & x < 9' (130 + i * 70) & y >= 111 & y < 161)
            begin
                if (i == 0)
                begin
                    red   = lamp [0] ? 31 : 6;
                    blue  = lamp [0] ? 16 : 3;
                end
                else if (i < 4)
                begin
                    green = lamp [4 - i] ? 50 : 10;
                    blue  = lamp [4 - i] ? 31 : 6;
                end
                else
                begin
                    red   = lamp [4] ? 31 : 6;
                    green = lamp [4] ? 36 : 7;
                end
            end
    end

endmodule
