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

// Exercise BB7 in the hackathon setup of Basics-Graphics-Music (BGM):
// https://github.com/yuri-panchul/basics-graphics-music
//
// Copy a directory such as hackathon/problems/4_binary_counter, replace its
// hackathon_top.sv with this file and add decade_counter.sv.
//
// The counter runs from slow_clock.  The LEDs show Q7-Q0, the rightmost
// digit the number of the output that is 1 with CARRY OUT on its dot, and
// the screen ten lamps, Q0 on the left, and CARRY OUT in blue on the right:
// like the two LED modules on the breadboard.

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 [9:0] q;
    logic       carry_out;

    decade_counter i_decade_counter
    (
        .clk       ( slow_clock ),
        .rst       ( reset      ),
        .q         ( q          ),
        .carry_out ( carry_out  )
    );

    assign led = q [7:0];

    //------------------------------------------------------------------------
    // The digit whose output is 1, on a 7-segment display: a is the top
    // segment, g the middle one.  No output at 1: blank.

    logic [6:0] abcdefg;

    always_comb
        case (q)
        10'b00_0000_0001: abcdefg = 7'b111_1110;  // 0
        10'b00_0000_0010: abcdefg = 7'b011_0000;  // 1
        10'b00_0000_0100: abcdefg = 7'b110_1101;  // 2
        10'b00_0000_1000: abcdefg = 7'b111_1001;  // 3
        10'b00_0001_0000: abcdefg = 7'b011_0011;  // 4
        10'b00_0010_0000: abcdefg = 7'b101_1011;  // 5
        10'b00_0100_0000: abcdefg = 7'b101_1111;  // 6
        10'b00_1000_0000: abcdefg = 7'b111_0000;  // 7
        10'b01_0000_0000: abcdefg = 7'b111_1111;  // 8
        10'b10_0000_0000: abcdefg = 7'b111_1011;  // 9
        default:          abcdefg = 7'b000_0000;
        endcase

    assign abcdefgh = { abcdefg, carry_out };
    assign digit    = 8'b0000_0001;

    //------------------------------------------------------------------------
    // Ten square lamps on the 480 x 272 screen, 30 pixels wide, 40 apart,
    // Q0 on the left, and CARRY OUT in blue at the right end.  Bright when
    // 1, dim when 0.

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

        for (int i = 0; i < 10; i ++)
            if (x >= 9' (25 + i * 40) & x < 9' (55 + i * 40) & y >= 121 & y < 151)
                green = q [i] ? 63 : 12;

        if (x >= 425 & x < 455 & y >= 121 & y < 151)
            blue = carry_out ? 31 : 6;
    end

endmodule
