// Exercise B5 on an FPGA board with the Basics-Graphics-Music (BGM) labs:
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy labs/1_basics/1_06_binary_counter to a new directory, replace its
// lab_top.sv with this file and add up_down_counter.sv.  For the chip-level
// design add up_down_counter_chips.sv and cd4029.sv instead, and change the
// module name below.
//
// The counter runs from slow_clk, the 1 Hz clock that every BGM board
// wrapper makes, like the breadboard with the 555 module.  The reset button
// of the board resets the count.  It counts up, or down while key 0 is
// pressed; it counts 0-9, or 0-15 while key 1 is pressed.  LEDs 0-3 show
// the count, LED 4 shows ~CARRY OUT, and the display shows the count in
// hexadecimal with seven_segment_display from labs/common.

`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 [3:0] q;
    logic       carry_out_n;

    up_down_counter i_up_down_counter
    (
        .clk         ( slow_clk    ),
        .rst         ( rst         ),
        .up          ( ~ key [0]   ),
        .binary      ( key [1]     ),
        .q           ( q           ),
        .carry_out_n ( carry_out_n )
    );

    assign led = w_led' ({ carry_out_n, q });

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

    seven_segment_display # (w_digit) i_7segment
    (
        .clk      ( clk                   ),
        .rst      ( rst                   ),
        .number   ( (w_digit * 4)' (q)    ),
        .dots     ( w_digit' (0)          ),
        .abcdefgh ( abcdefgh              ),
        .digit    ( digit                 )
    );

endmodule
