// Exercise P3 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_adder.sv.  For the chip-level
// design add fifo_adder_chips.sv and cd4013.sv, cd40105.sv, cd4008.sv,
// cd4082.sv instead, and change the module name below.
//
// The adder 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 A, key 1
// PUSH B and key 2 POP C: hold a key until the next tick of slow_clk.
// LEDs 3-0 show the head of C, LED 4 DOR C, LED 5 DIR C, LED 6 DOR A and
// LED 7 DOR B.  The rightmost digit shows the head of C in hexadecimal.

`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_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_clk ),
        .rst      ( rst      ),
        .push_a   ( key [0]  ),
        .push_b   ( key [1]  ),
        .pop_c    ( key [2]  ),
        .data     ( sw [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 = w_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    = w_digit' (1);

endmodule
