// Exercise P2 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 serial_adder_gates.sv.  For the
// chip-level design add serial_adder_gates_chips.sv and cd4014.sv,
// cd4070.sv, cd4081.sv, cd4071.sv, cd4013.sv, cd4015.sv, cd4511.sv instead,
// and change the module name below.
//
// The adder runs from slow_clk, 1 Hz on every BGM board.  Switches 3-0 are
// A, switches 7-4 are B, key 0 is LOAD: hold it through a tick of slow_clk.
// LEDs 3-0 show the deserializer, LEDs 6-4 the bit streams of A, B and the
// sum, LED 7 the carry.  The rightmost digit shows the deserializer as the
// 4511 would.

// LOAD is a synchronous load for the serializers and an asynchronous clear
// for the carry and the deserializer, as on the breadboard
// verilator lint_off SYNCASYNCNET

`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       serial_a, serial_b, serial_sum, carry;
    logic [3:0] sum;
    logic [6:0] abcdefg_a, abcdefg_b, abcdefg_sum;

    serial_adder_gates i_serial_adder
    (
        .clk         ( slow_clk    ),
        .load        ( key [0]     ),
        .a           ( sw [3:0]    ),
        .b           ( sw [7:4]    ),
        .serial_a    ( serial_a    ),
        .serial_b    ( serial_b    ),
        .serial_sum  ( serial_sum  ),
        .carry       ( carry       ),
        .sum         ( sum         ),
        .abcdefg_a   ( abcdefg_a   ),
        .abcdefg_b   ( abcdefg_b   ),
        .abcdefg_sum ( abcdefg_sum )
    );

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

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

endmodule
