// Exercise B9 on an FPGA board with the Basics-Graphics-Music (BGM) labs:
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy a lab, for example labs/1_basics/1_05_7seven_segment_letter, to a
// new directory, replace its lab_top.sv with this file and add
// adder_display.sv.  For the chip-level design add adder_display_chips.sv,
// cd4008.sv and cd4511.sv instead, and change the module name below.
//
// Switches 3-0 are A, switches 7-4 are B (on a board without switches use
// keys).  The display shows A, B and the sum on its three rightmost digits,
// the carry on the dot of the sum, and the LEDs show the carry and the sum.
// The digits take turns: a counter on clk picks one at a time, quickly
// enough that all three seem lit.  The counter does not make a clock; it
// only chooses the digit.

`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;

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

    wire  [7:0] switches = 8' (sw);
    logic [3:0] sum;
    logic       carry;
    logic [6:0] abcdefg_a, abcdefg_b, abcdefg_sum;

    adder_display i_adder_display
    (
        .a           ( switches [3:0] ),
        .b           ( switches [7:4] ),
        .sum         ( sum            ),
        .carry       ( carry          ),
        .abcdefg_a   ( abcdefg_a      ),
        .abcdefg_b   ( abcdefg_b      ),
        .abcdefg_sum ( abcdefg_sum    )
    );

    assign led = w_led' ({ carry, sum });

    //------------------------------------------------------------------------
    // Three digits on a dynamic display: one at a time, about 1 ms each

    logic [17:0] refresh;

    always_ff @ (posedge clk or posedge rst)
        if (rst)
            refresh <= '0;
        else
            refresh <= refresh + 1'd1;

    always_comb
        case (refresh [17:16])
        2'd0:    begin abcdefgh = { abcdefg_sum, carry }; digit = w_digit' (1); end
        2'd1:    begin abcdefgh = { abcdefg_b,   1'b0  }; digit = w_digit' (2); end
        default: begin abcdefgh = { abcdefg_a,   1'b0  }; digit = w_digit' (4); end
        endcase

endmodule
