// Exercise BB8 on an FPGA board with the Basics-Graphics-Music (BGM) labs:
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy labs/1_basics/1_07_shift_register to a new directory, replace its
// lab_top.sv with this file and add fifo_demo.sv.  For the chip-level design
// add fifo_demo_chips.sv, cd4013.sv and cd40105.sv instead, and change the
// module name below.
//
// The FIFO runs from slow_clk, 1 Hz on every BGM board, and rst is the
// reset of the board.  Switches 2-0 are the data, key 0 is PUSH and key 1
// is POP: hold a key until the next tick of slow_clk.  LEDs 2-0 show the
// head of the queue, LED 3 DOR and LED 7 DIR.  The rightmost digit shows
// the head as a number while the FIFO is not empty; its dot lights while
// the FIFO is full.

`include "config.svh"

module lab_top
# (
    parameter  clk_mhz       = 50,
               w_key         = 4,
               w_sw          = 8,
               w_led         = 8,
               w_digit       = 8,
               w_gpio        = 100,

               screen_width  = 640,
               screen_height = 480,

               w_red         = 4,
               w_green       = 4,
               w_blue        = 4,

               w_x           = $clog2 ( screen_width  ),
               w_y           = $clog2 ( screen_height )
)
(
    input                        clk,
    input                        slow_clk,
    input                        rst,

    // Keys, switches, LEDs

    input        [w_key   - 1:0] key,
    input        [w_sw    - 1:0] sw,
    output logic [w_led   - 1:0] led,

    // A dynamic seven-segment display

    output logic [          7:0] abcdefgh,
    output logic [w_digit - 1:0] digit,

    // Graphics

    input        [w_x     - 1:0] x,
    input        [w_y     - 1:0] y,

    output logic [w_red   - 1:0] red,
    output logic [w_green - 1:0] green,
    output logic [w_blue  - 1:0] blue,

    // Microphone, sound output and UART

    input        [         23:0] mic,
    output       [         15:0] sound,

    input                        uart_rx,
    output                       uart_tx,

    // General-purpose Input/Output

    inout        [w_gpio  - 1:0] gpio
);

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

       assign red        = '0;
       assign green      = '0;
       assign blue       = '0;
       assign sound      = '0;
       assign uart_tx    = '1;

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

    logic [2:0] head;
    logic       dor, dir;

    fifo_demo i_fifo_demo
    (
        .clk  ( slow_clk  ),
        .rst  ( rst       ),
        .push ( key [0]   ),
        .pop  ( key [1]   ),
        .data ( sw  [2:0] ),
        .head ( head      ),
        .dor  ( dor       ),
        .dir  ( dir       )
    );

    assign led = w_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    = w_digit' (1);

endmodule
