// Board configuration: tang_nano_9k_lcd_480_272_tm1638_hackathon
// This module uses few parameterization and relaxed typing rules

// Exercise BB10 in the hackathon setup of Basics-Graphics-Music (BGM):
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy a directory such as hackathon/problems/1_gates_and_muxes, replace its
// hackathon_top.sv with this file and add add_sub.sv.
//
// Keys: key [3:0] a, key [7:4] b.  All eight keys are taken, so there is no
// key for SUB: two copies of the design run side by side, one adding and
// one subtracting.  The LEDs: led [3:0] a + b, led [7:4] a - b.  The screen
// shows three rows of lamps, bit 3 on the left: A and B in blue; then F,
// Cn+4 and A=B of the sum in green; then the same for the difference in
// orange.

module hackathon_top
(
    input  logic       clock,
    input  logic       slow_clock,
    input  logic       reset,

    input  logic [7:0] key,
    output logic [7:0] led,

    // A dynamic seven-segment display

    output logic [7:0] abcdefgh,
    output logic [7:0] digit,

    // LCD screen interface

    input  logic [8:0] x,
    input  logic [8:0] y,

    output logic [4:0] red,
    output logic [5:0] green,
    output logic [4:0] blue,

    inout  logic [3:0] gpio
);

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

    wire [3:0] a = key [3:0];
    wire [3:0] b = key [7:4];

    logic [3:0] f_add,        f_sub;
    logic       carry_n_add,  carry_n_sub;
    logic       all_ones_add, all_ones_sub;

    add_sub i_add
    (
        .a        ( a            ),
        .b        ( b            ),
        .sub      ( 1'b0         ),
        .f        ( f_add        ),
        .carry_n  ( carry_n_add  ),
        .all_ones ( all_ones_add )
    );

    add_sub i_sub
    (
        .a        ( a            ),
        .b        ( b            ),
        .sub      ( 1'b1         ),
        .f        ( f_sub        ),
        .carry_n  ( carry_n_sub  ),
        .all_ones ( all_ones_sub )
    );

    assign led      = { f_sub, f_add };

    assign abcdefgh = '0;
    assign digit    = '0;

    //------------------------------------------------------------------------
    // Square lamps on the 480 x 272 screen, 30 pixels wide, 40 apart, bit 3
    // on the left.  Top row: A, a gap, B.  Middle row: F, a gap, Cn+4 and
    // A=B of the sum.  Bottom row: the same for the difference.

    wire [8:0] row_in  = { a, 1'b0, b };
    wire [8:0] row_add = { f_add, 1'b0, carry_n_add, all_ones_add, 2'b0 };
    wire [8:0] row_sub = { f_sub, 1'b0, carry_n_sub, all_ones_sub, 2'b0 };

    always_comb
    begin
        red   = 0;
        green = 0;
        blue  = 0;

        for (int i = 0; i < 9; i ++)
            if (x >= 9' (60 + i * 40) & x < 9' (90 + i * 40))
            begin
                if (y >= 50 & y < 80 & i != 4)
                    blue = row_in [8 - i] ? 31 : 6;

                if (y >= 120 & y < 150 & i < 7 & i != 4)
                    green = row_add [8 - i] ? 63 : 12;

                if (y >= 190 & y < 220 & i < 7 & i != 4)
                begin
                    red   = row_sub [8 - i] ? 31 : 6;
                    green = row_sub [8 - i] ? 32 : 6;
                end
            end
    end

endmodule
