data/full_repos/permissive/432862802/DigitalPiano.srcs/sources_1/new/Keyboard.v
1: b"%Error: data/full_repos/permissive/432862802/DigitalPiano.srcs/sources_1/new/Keyboard.v:62: Cannot find file containing module: 'PS2'\nPS2 PS2(\n^~~\n        ... Looked in:\n             data/full_repos/permissive/432862802/DigitalPiano.srcs/sources_1/new,data/full_repos/permissive/432862802/PS2\n             data/full_repos/permissive/432862802/DigitalPiano.srcs/sources_1/new,data/full_repos/permissive/432862802/PS2.v\n             data/full_repos/permissive/432862802/DigitalPiano.srcs/sources_1/new,data/full_repos/permissive/432862802/PS2.sv\n             PS2\n             PS2.v\n             PS2.sv\n             obj_dir/PS2\n             obj_dir/PS2.v\n             obj_dir/PS2.sv\n%Error: Exiting due to 1 error(s)\n"
module Keyboard(

    input clk_100,


    input [1:0] state,
    input rst_n,
    

    input clock_USB_in,
    input data_USB_in,


    output [3:0] note_0,
    output [3:0] note_1,
    output [3:0] note_2,
    output [3:0] note_3,
    output [1:0] octave_0,
    output [1:0] octave_1,
    output [1:0] octave_2,
    output [1:0] octave_3,

    output [9:0] od_level,
    output [9:0] od_level_bottom,

    output tone
    );


reg [15:0] DATA_16BITS;
wire data_valid_flag;
wire [7:0] DATA_OUT;
reg ena;
reg space_detect = 1'b0;
reg f1_detect = 1'b0;
reg f2_detect = 1'b0;
reg f3_detect = 1'b0;


PS2 PS2(
    .clk_100(clk_100),
    .kclk(clock_USB_in),
    .kdata(data_USB_in),
    .data_out(DATA_OUT),
    .data_valid_flag(data_valid_flag)    
        );


always @ (posedge data_valid_flag or negedge rst_n)
begin
    if(!rst_n) begin
        DATA_16BITS <= 16'b0;
    end
    else begin
        DATA_16BITS <= {DATA_16BITS[7:0], DATA_OUT};
    end
end



reg [3:0] note;
reg [1:0] octave;

always @ (posedge clk_100 or negedge rst_n)
begin
    if(!rst_n) begin
        ena <= 1'b0;
        note <= 4'd12;
        octave <= 2'd0;
    end
    else if(data_valid_flag == 1'b1)
    begin
        case(DATA_16BITS[7:0])
        8'h1A:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd0;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd0;
                octave <= 2'd0;
            end
        end

        8'h1B:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd1;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd1;
                octave <= 2'd0;
            end
        end

        8'h22:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd2;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd2;
                octave <= 2'd0;
            end
        end

        8'h23:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd3;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd3;
                octave <= 2'd0;
            end
        end

        8'h21:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd4;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd4;
                octave <= 2'd0;
            end
        end

        8'h2A:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd5;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd5;
                octave <= 2'd0;
            end
        end

        8'h34:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd6;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd6;
                octave <= 2'd0;
            end
        end

        8'h32:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd7;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd7;
                octave <= 2'd0;
            end
        end

        8'h33:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd8;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd8;
                octave <= 2'd0;
            end
        end

        8'h31:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd9;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd9;
                octave <= 2'd0;
            end
        end

        8'h3B:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd10;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd10;
                octave <= 2'd0;
            end
        end

        8'h3A:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd11;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd11;
                octave <= 2'd0;
            end
        end

        8'h15:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd0;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd0;
                octave <= 2'd1;
            end
        end

        8'h1E:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd1;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd1;
                octave <= 2'd1;
            end
        end

        8'h1D:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd2;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd2;
                octave <= 2'd1;
            end
        end

        8'h26:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd3;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd3;
                octave <= 2'd1;
            end
        end

        8'h24:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd4;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd4;
                octave <= 2'd1;
            end
        end

        8'h2D:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd5;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd5;
                octave <= 2'd1;
            end
        end

        8'h2E:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd6;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd6;
                octave <= 2'd1;
            end
        end

        8'h2C:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd7;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd7;
                octave <= 2'd1;
            end
        end

        8'h36:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd8;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd8;
                octave <= 2'd1;
            end
        end

        8'h35:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd9;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd9;
                octave <= 2'd1;
            end
        end

        8'h3D:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd10;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd10;
                octave <= 2'd1;
            end
        end

        8'h3C:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd11;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd11;
                octave <= 2'd1;
            end
        end

        8'h29:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                space_detect <= 0;
            end
            else begin
                space_detect <= 1;
            end
        end

        8'h05:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                f1_detect <= 0;
            end
            else begin
                f1_detect <= 1;
            end
        end

        8'h06:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                f2_detect <= 0;
            end
            else begin
                f2_detect <= 1;
            end
        end

        8'h04:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                f3_detect <= 0;
            end
            else begin
                f3_detect <= 1;
            end
        end

        default: begin
            ena <= ena;
            note <= note;
            octave <= octave;
            space_detect <= space_detect;
        end
        endcase
    end
end

reg octave_select = 1'b0;


always @ (posedge space_detect or negedge rst_n)
begin
    if(!rst_n) begin
        octave_select <= 0;
    end
    else begin
        octave_select <= ~octave_select;
    end
end

reg [9:0] overdrive_level = 1023;


always @ (posedge f1_detect or negedge rst_n)
begin
    if(!rst_n) begin
        overdrive_level <= 1023;
    end
    else begin
        overdrive_level <= overdrive_level - 50;
        if(overdrive_level < 600)
        begin
            overdrive_level <= 1023;
        end
    end
end

reg [9:0] overdrive_level_bottom = 0;


always @ (posedge f2_detect or negedge rst_n)
begin
    if(!rst_n) begin
        overdrive_level_bottom <= 0;
    end
    else begin
        overdrive_level_bottom <= overdrive_level_bottom + 50;
        if(overdrive_level_bottom > 423)
        begin
            overdrive_level_bottom <= 0;
        end
    end
end

assign od_level = overdrive_level;
assign od_level_bottom = overdrive_level_bottom;

reg tone_select = 0;


always @ (posedge f3_detect or negedge rst_n)
begin
    if(!rst_n) begin
        tone_select <= 0;
    end
    else begin
        tone_select <= ~tone_select;
    end
end

assign tone = tone_select;

reg [3:0] note_arr[3:0];
reg [1:0] octave_arr[3:0];

initial
begin
    note_arr[0] = 12;
    note_arr[1] = 12;
    note_arr[2] = 12;
    note_arr[3] = 12;
    octave_arr[0] = 0;
    octave_arr[1] = 0;
    octave_arr[2] = 0;
    octave_arr[3] = 0;
end

always @ (posedge clk_100 or negedge rst_n)
begin
    if(!rst_n) begin
        note_arr[0] = 12;
        note_arr[1] = 12;
        note_arr[2] = 12;
        note_arr[3] = 12;
        octave_arr[0] = 0;
        octave_arr[1] = 0;
        octave_arr[2] = 0;
        octave_arr[3] = 0;
    end
    else if(ena == 1'b1)
    begin
        if((note_arr[3] != note || octave_arr[3] != octave) && (note_arr[2] != note || octave_arr[2] != octave) && (note_arr[1] != note || octave_arr[1] != octave) && (note_arr[0] != note || octave_arr[0] != octave))
        begin
            if(note_arr[3] == 12)
            begin
                note_arr[3] <= note;
                octave_arr[3] <= octave;
            end
            else if(note_arr[2] == 12)
            begin
                note_arr[2] <= note;
                octave_arr[2] <= octave;
            end
            else if(note_arr[1] == 12)
            begin
                note_arr[1] <= note;
                octave_arr[1] <= octave;
            end
            else if(note_arr[0] == 12)
            begin
                note_arr[0] <= note;
                octave_arr[0] <= octave;
            end
        end
    end
    else begin
        if(note == note_arr[3] && octave == octave_arr[3])
        begin
            note_arr[3] <= 4'd12;
        end
        else if(note == note_arr[2] && octave == octave_arr[2])
        begin
            note_arr[2] <= 4'd12;
        end
        else if(note == note_arr[1] && octave == octave_arr[1])
        begin
            note_arr[1] <= 4'd12;
        end
        else if(note == note_arr[0] && octave == octave_arr[0])
        begin
            note_arr[0] <= 4'd12;
        end
    end
end

assign note_0 = note_arr[0];
assign note_1 = note_arr[1];
assign note_2 = note_arr[2];
assign note_3 = note_arr[3];
assign octave_0 = octave_arr[0] + (octave_select?2'b10:2'b00);
assign octave_1 = octave_arr[1] + (octave_select?2'b10:2'b00);
assign octave_2 = octave_arr[2] + (octave_select?2'b10:2'b00);
assign octave_3 = octave_arr[3] + (octave_select?2'b10:2'b00);

endmodule
module Keyboard(

    input clk_100,


    input [1:0] state,
    input rst_n,
    

    input clock_USB_in,
    input data_USB_in,


    output [3:0] note_0,
    output [3:0] note_1,
    output [3:0] note_2,
    output [3:0] note_3,
    output [1:0] octave_0,
    output [1:0] octave_1,
    output [1:0] octave_2,
    output [1:0] octave_3,

    output [9:0] od_level,
    output [9:0] od_level_bottom,

    output tone
    );



reg [15:0] DATA_16BITS;
wire data_valid_flag;
wire [7:0] DATA_OUT;
reg ena;
reg space_detect = 1'b0;
reg f1_detect = 1'b0;
reg f2_detect = 1'b0;
reg f3_detect = 1'b0;


PS2 PS2(
    .clk_100(clk_100),
    .kclk(clock_USB_in),
    .kdata(data_USB_in),
    .data_out(DATA_OUT),
    .data_valid_flag(data_valid_flag)    
        );


always @ (posedge data_valid_flag or negedge rst_n)
begin
    if(!rst_n) begin
        DATA_16BITS <= 16'b0;
    end
    else begin
        DATA_16BITS <= {DATA_16BITS[7:0], DATA_OUT};
    end
end



reg [3:0] note;
reg [1:0] octave;

always @ (posedge clk_100 or negedge rst_n)
begin
    if(!rst_n) begin
        ena <= 1'b0;
        note <= 4'd12;
        octave <= 2'd0;
    end
    else if(data_valid_flag == 1'b1)
    begin
        case(DATA_16BITS[7:0])
        8'h1A:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd0;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd0;
                octave <= 2'd0;
            end
        end

        8'h1B:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd1;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd1;
                octave <= 2'd0;
            end
        end

        8'h22:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd2;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd2;
                octave <= 2'd0;
            end
        end

        8'h23:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd3;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd3;
                octave <= 2'd0;
            end
        end

        8'h21:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd4;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd4;
                octave <= 2'd0;
            end
        end

        8'h2A:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd5;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd5;
                octave <= 2'd0;
            end
        end

        8'h34:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd6;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd6;
                octave <= 2'd0;
            end
        end

        8'h32:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd7;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd7;
                octave <= 2'd0;
            end
        end

        8'h33:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd8;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd8;
                octave <= 2'd0;
            end
        end

        8'h31:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd9;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd9;
                octave <= 2'd0;
            end
        end

        8'h3B:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd10;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd10;
                octave <= 2'd0;
            end
        end

        8'h3A:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd11;
                octave <= 2'd0;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd11;
                octave <= 2'd0;
            end
        end

        8'h15:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd0;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd0;
                octave <= 2'd1;
            end
        end

        8'h1E:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd1;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd1;
                octave <= 2'd1;
            end
        end

        8'h1D:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd2;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd2;
                octave <= 2'd1;
            end
        end

        8'h26:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd3;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd3;
                octave <= 2'd1;
            end
        end

        8'h24:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd4;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd4;
                octave <= 2'd1;
            end
        end

        8'h2D:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd5;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd5;
                octave <= 2'd1;
            end
        end

        8'h2E:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd6;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd6;
                octave <= 2'd1;
            end
        end

        8'h2C:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd7;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd7;
                octave <= 2'd1;
            end
        end

        8'h36:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd8;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd8;
                octave <= 2'd1;
            end
        end

        8'h35:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd9;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd9;
                octave <= 2'd1;
            end
        end

        8'h3D:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd10;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd10;
                octave <= 2'd1;
            end
        end

        8'h3C:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                ena <= 1'b0;
                note <= 4'd11;
                octave <= 2'd1;
            end
            else begin
                ena <= 1'b1;
                note <= 4'd11;
                octave <= 2'd1;
            end
        end

        8'h29:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                space_detect <= 0;
            end
            else begin
                space_detect <= 1;
            end
        end

        8'h05:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                f1_detect <= 0;
            end
            else begin
                f1_detect <= 1;
            end
        end

        8'h06:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                f2_detect <= 0;
            end
            else begin
                f2_detect <= 1;
            end
        end

        8'h04:begin
            if(DATA_16BITS[15:8] == 8'hF0)
            begin
                f3_detect <= 0;
            end
            else begin
                f3_detect <= 1;
            end
        end

        default: begin
            ena <= ena;
            note <= note;
            octave <= octave;
            space_detect <= space_detect;
        end
        endcase
    end
end

reg octave_select = 1'b0;


always @ (posedge space_detect or negedge rst_n)
begin
    if(!rst_n) begin
        octave_select <= 0;
    end
    else begin
        octave_select <= ~octave_select;
    end
end

reg [9:0] overdrive_level = 1023;


always @ (posedge f1_detect or negedge rst_n)
begin
    if(!rst_n) begin
        overdrive_level <= 1023;
    end
    else begin
        overdrive_level <= overdrive_level - 50;
        if(overdrive_level < 600)
        begin
            overdrive_level <= 1023;
        end
    end
end

reg [9:0] overdrive_level_bottom = 0;


always @ (posedge f2_detect or negedge rst_n)
begin
    if(!rst_n) begin
        overdrive_level_bottom <= 0;
    end
    else begin
        overdrive_level_bottom <= overdrive_level_bottom + 50;
        if(overdrive_level_bottom > 423)
        begin
            overdrive_level_bottom <= 0;
        end
    end
end

assign od_level = overdrive_level;
assign od_level_bottom = overdrive_level_bottom;

reg tone_select = 0;


always @ (posedge f3_detect or negedge rst_n)
begin
    if(!rst_n) begin
        tone_select <= 0;
    end
    else begin
        tone_select <= ~tone_select;
    end
end

assign tone = tone_select;

reg [3:0] note_arr[3:0];
reg [1:0] octave_arr[3:0];

initial
begin
    note_arr[0] = 12;
    note_arr[1] = 12;
    note_arr[2] = 12;
    note_arr[3] = 12;
    octave_arr[0] = 0;
    octave_arr[1] = 0;
    octave_arr[2] = 0;
    octave_arr[3] = 0;
end

always @ (posedge clk_100 or negedge rst_n)
begin
    if(!rst_n) begin
        note_arr[0] = 12;
        note_arr[1] = 12;
        note_arr[2] = 12;
        note_arr[3] = 12;
        octave_arr[0] = 0;
        octave_arr[1] = 0;
        octave_arr[2] = 0;
        octave_arr[3] = 0;
    end
    else if(ena == 1'b1)
    begin
        if((note_arr[3] != note || octave_arr[3] != octave) && (note_arr[2] != note || octave_arr[2] != octave) && (note_arr[1] != note || octave_arr[1] != octave) && (note_arr[0] != note || octave_arr[0] != octave))
        begin
            if(note_arr[3] == 12)
            begin
                note_arr[3] <= note;
                octave_arr[3] <= octave;
            end
            else if(note_arr[2] == 12)
            begin
                note_arr[2] <= note;
                octave_arr[2] <= octave;
            end
            else if(note_arr[1] == 12)
            begin
                note_arr[1] <= note;
                octave_arr[1] <= octave;
            end
            else if(note_arr[0] == 12)
            begin
                note_arr[0] <= note;
                octave_arr[0] <= octave;
            end
        end
    end
    else begin
        if(note == note_arr[3] && octave == octave_arr[3])
        begin
            note_arr[3] <= 4'd12;
        end
        else if(note == note_arr[2] && octave == octave_arr[2])
        begin
            note_arr[2] <= 4'd12;
        end
        else if(note == note_arr[1] && octave == octave_arr[1])
        begin
            note_arr[1] <= 4'd12;
        end
        else if(note == note_arr[0] && octave == octave_arr[0])
        begin
            note_arr[0] <= 4'd12;
        end
    end
end

assign note_0 = note_arr[0];
assign note_1 = note_arr[1];
assign note_2 = note_arr[2];
assign note_3 = note_arr[3];
assign octave_0 = octave_arr[0] + (octave_select?2'b10:2'b00);
assign octave_1 = octave_arr[1] + (octave_select?2'b10:2'b00);
assign octave_2 = octave_arr[2] + (octave_select?2'b10:2'b00);
assign octave_3 = octave_arr[3] + (octave_select?2'b10:2'b00);

endmodule