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

// Exercise B7 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 seven_segment.sv.
//
// Keys 3-0 are the buttons D C B A.  The rightmost digit shows the code the
// way the 4511 does, the LEDs show the code, and the screen draws the same
// digit large: each segment is a bar, red when lit and dim when not.

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] bcd = key [3:0];
    logic [6:0] abcdefg;

    seven_segment i_seven_segment
    (
        .bcd     ( bcd     ),
        .abcdefg ( abcdefg )
    );

    assign abcdefgh = { abcdefg, 1'b0 };
    assign digit    = 8'b0000_0001;
    assign led      = { 4'b0, bcd };

    //------------------------------------------------------------------------
    // A large digit on the 480 x 272 screen.  Segment bars are 20 pixels
    // thick: a, g and d across, f, b, e and c up and down.

    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 [6:0] here = { seg_a, seg_b, seg_c, seg_d, seg_e, seg_f, seg_g };

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

        if (| (here & abcdefg))
            red = 31;
        else if (| here)
        begin
            red   = 6;
            green = 4;
            blue  = 2;
        end
    end

endmodule
