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

// Exercise B9 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 adder_display.sv.
//
// Keys 3-0 are A, keys 7-4 are B: hold them like the buttons on the
// breadboard.  The three rightmost digits show A, B and the sum, the dot of
// the sum is the carry, the LEDs show the carry and the sum, and the screen
// draws the sum as a large digit.

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

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

    logic [3:0] sum;
    logic       carry;
    logic [6:0] abcdefg_a, abcdefg_b, abcdefg_sum;

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

    assign led = { 3'b0, carry, sum };

    //------------------------------------------------------------------------
    // Three digits on the dynamic display: one at a time, about 2 ms each.
    // The counter only chooses the digit; it does not make a clock.

    logic [17:0] refresh;

    always_ff @ (posedge clock)
        if (reset)
            refresh <= '0;
        else
            refresh <= refresh + 1'd1;

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

    //------------------------------------------------------------------------
    // The sum as a large digit on the 480 x 272 screen, the carry as a dot

    wire seg_a = x >= 210 & x < 270 & y >=  36 & y <  56;
    wire seg_g = x >= 210 & x < 270 & y >= 126 & y < 146;
    wire seg_d = x >= 210 & x < 270 & y >= 216 & y < 236;
    wire seg_f = x >= 190 & x < 210 & y >=  56 & y < 126;
    wire seg_b = x >= 270 & x < 290 & y >=  56 & y < 126;
    wire seg_e = x >= 190 & x < 210 & y >= 146 & y < 216;
    wire seg_c = x >= 270 & x < 290 & y >= 146 & y < 216;
    wire dot   = x >= 300 & x < 320 & y >= 216 & y < 236;

    wire [7:0] here = { seg_a, seg_b, seg_c, seg_d, seg_e, seg_f, seg_g, dot };

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

        if (| (here & { abcdefg_sum, carry }))
            green = 63;
        else if (| here)
        begin
            red   = 2;
            green = 8;
            blue  = 2;
        end
    end

endmodule
