// Exercise BB5 on an FPGA board with the Basics-Graphics-Music (BGM) labs:
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy labs/1_basics/1_03_decoder to a new directory, replace its
// lab_top.sv with this file and add decoder.sv.  For the chip-level design
// add decoder_chips.sv and cd4028.sv instead, and change the module name
// below.
//
// Keys 3-0 are the code D C B A.  LEDs 7-0 show Q7-Q0, and the rightmost
// digit of the display shows the number of the output that is 1, so Q8 and
// Q9 are seen there too.  Codes 10-15 leave the digit blank.

`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 [9:0] q;

    decoder i_decoder
    (
        .code ( 4' (key) ),
        .q    ( q        )
    );

    assign led = w_led' (q);

    //------------------------------------------------------------------------
    // The digit whose output is 1, on a 7-segment display: a is the top
    // segment, g the middle one.  No output at 1 (codes 10-15): blank.

    logic [6:0] abcdefg;

    always_comb
        case (q)
        10'b00_0000_0001: abcdefg = 7'b111_1110;  // 0
        10'b00_0000_0010: abcdefg = 7'b011_0000;  // 1
        10'b00_0000_0100: abcdefg = 7'b110_1101;  // 2
        10'b00_0000_1000: abcdefg = 7'b111_1001;  // 3
        10'b00_0001_0000: abcdefg = 7'b011_0011;  // 4
        10'b00_0010_0000: abcdefg = 7'b101_1011;  // 5
        10'b00_0100_0000: abcdefg = 7'b101_1111;  // 6
        10'b00_1000_0000: abcdefg = 7'b111_0000;  // 7
        10'b01_0000_0000: abcdefg = 7'b111_1111;  // 8
        10'b10_0000_0000: abcdefg = 7'b111_1011;  // 9
        default:          abcdefg = 7'b000_0000;
        endcase

    assign abcdefgh = { abcdefg, 1'b0 };
    assign digit    = w_digit' (1);

endmodule
