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

// Exercise B1 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 basic_gates.sv.
//
// Keys 0 and 1 are A and B.  The LEDs show the results: led [0] A AND B,
// led [1] A OR B, led [2] A XOR B, led [3] NOT A, led [4] NOT B.
// The screen shows seven lamps in a row: A, B and the five results.

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 a = key [0];
    wire b = key [1];

    logic a_and_b, a_or_b, a_xor_b, not_a, not_b;

    basic_gates i_basic_gates
    (
        .a       ( a       ),
        .b       ( b       ),
        .a_and_b ( a_and_b ),
        .a_or_b  ( a_or_b  ),
        .a_xor_b ( a_xor_b ),
        .not_a   ( not_a   ),
        .not_b   ( not_b   )
    );

    assign led      = { 3'b0, not_b, not_a, a_xor_b, a_or_b, a_and_b };

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

    //------------------------------------------------------------------------
    // Seven square lamps on the 480 x 272 screen, 40 pixels wide, 24 apart:
    // A and B in blue, the results in green.  Bright when 1, dim when 0.

    wire [6:0] lamp = { not_b, not_a, a_xor_b, a_or_b, a_and_b, b, a };

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

        for (int i = 0; i < 7; i ++)
            if (x >= 9' (30 + i * 64) & x < 9' (70 + i * 64) & y >= 116 & y < 156)
            begin
                if (i < 2)
                    blue  = lamp [i] ? 31 : 6;
                else
                    green = lamp [i] ? 63 : 12;
            end
    end

endmodule
