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

// Exercise BB4b 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 mux_4_dual.sv.
//
// Keys: key [1:0] sel. The LEDs: led [0] x_com, led [1] y_com. The screen
// shows 4 lamps in a row: A, B, X COM, Y COM; inputs in blue, outputs in
// green.
//
// The channels are fixed at X3-X0 = 1101 and Y3-Y0 = 0110, as the wires on
// the breadboard. INH is held at 0: an FPGA has no high-impedance wires
// inside.
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 [1:0] sel = key [1:0];

    logic x_com;
    logic y_com;

    mux_4_dual i_mux_4_dual
    (
        .sel   ( sel     ),
        .x_ch  ( 4'b1101 ),
        .y_ch  ( 4'b0110 ),
        .inh   ( 1'b0    ),
        .x_com ( x_com   ),
        .y_com ( y_com   )
    );

    assign led      = { 6'b0, y_com, x_com };

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

    //------------------------------------------------------------------------
    // 4 square lamps on the 480 x 272 screen, 40 pixels wide, 64 apart:
    // the inputs in blue, the outputs in green.  Bright when 1, dim when 0.

    wire [3:0] lamp = { y_com, x_com, sel };

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

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

endmodule
