data/full_repos/permissive/408212214/n64_receive_controller_data.v
data/verilator_xmls/a2e44542-a828-4d27-8b37-cec7fbb401c1.xml
module n64_receive_controller_data(
	input sys_clk,
	input trigger,
    output reg [31:0] controller_data,
	input n64d,
	output reg receiving
);





localparam zero_one_border = 25;
localparam one_two_border = 75;
localparam two_three_border = 125;
localparam three_four_border = 175;

localparam idle=0, waiting_for_low=1, waiting_for_high=2, end_of_command=3;
reg [1:0] state;
reg [7:0] timer;
reg [5:0] position;

initial begin
    controller_data = 0;
    timer = 0;
    position = 0;
    state = 0;
    receiving = 0;
end

always @(posedge sys_clk) begin
    if(trigger) begin
        receiving <= 1'b1;
    end

    case(state)
        idle: begin
            if(receiving) begin
                position <= 0;
                controller_data <= 0;
                timer <= 0;
        		state <= waiting_for_low;
    		end
        end
        waiting_for_low: begin

            if(!n64d) begin
                timer <= timer + 1'b1;
                state <= waiting_for_high;
            end
        end
        waiting_for_high: begin
            if(timer > 250) begin
                state <= idle;
            end else if(n64d) begin

                if(timer > zero_one_border && timer <= one_two_border) begin
                    if(position == 32) begin
                        state <= end_of_command;
                    end else begin
                        controller_data[31-position] <= 1'b1;
                        timer <= 0;
                        state <= waiting_for_low;
                    end
                end else if(timer > one_two_border && timer <= two_three_border) begin
                    state <= end_of_command;
                end else if(timer > two_three_border && timer <= three_four_border) begin
                    if(position == 32) begin
                        state <= end_of_command;
                    end else begin
                        controller_data[31-position] <= 1'b0;
                        timer <= 0;
                        state <= waiting_for_low;
                    end
                end else begin
                    state <= idle;
                end

                position <= position + 1'b1;
            end else begin
                timer <= timer + 1'b1;
            end
        end
        end_of_command: begin
            receiving <= 1'b0;
    		state <= idle;
        end
    endcase
end

endmodule
module n64_receive_controller_data(
	input sys_clk,
	input trigger,
    output reg [31:0] controller_data,
	input n64d,
	output reg receiving
);






localparam zero_one_border = 25;
localparam one_two_border = 75;
localparam two_three_border = 125;
localparam three_four_border = 175;

localparam idle=0, waiting_for_low=1, waiting_for_high=2, end_of_command=3;
reg [1:0] state;
reg [7:0] timer;
reg [5:0] position;

initial begin
    controller_data = 0;
    timer = 0;
    position = 0;
    state = 0;
    receiving = 0;
end

always @(posedge sys_clk) begin
    if(trigger) begin
        receiving <= 1'b1;
    end

    case(state)
        idle: begin
            if(receiving) begin
                position <= 0;
                controller_data <= 0;
                timer <= 0;
        		state <= waiting_for_low;
    		end
        end
        waiting_for_low: begin

            if(!n64d) begin
                timer <= timer + 1'b1;
                state <= waiting_for_high;
            end
        end
        waiting_for_high: begin
            if(timer > 250) begin
                state <= idle;
            end else if(n64d) begin

                if(timer > zero_one_border && timer <= one_two_border) begin
                    if(position == 32) begin
                        state <= end_of_command;
                    end else begin
                        controller_data[31-position] <= 1'b1;
                        timer <= 0;
                        state <= waiting_for_low;
                    end
                end else if(timer > one_two_border && timer <= two_three_border) begin
                    state <= end_of_command;
                end else if(timer > two_three_border && timer <= three_four_border) begin
                    if(position == 32) begin
                        state <= end_of_command;
                    end else begin
                        controller_data[31-position] <= 1'b0;
                        timer <= 0;
                        state <= waiting_for_low;
                    end
                end else begin
                    state <= idle;
                end

                position <= position + 1'b1;
            end else begin
                timer <= timer + 1'b1;
            end
        end
        end_of_command: begin
            receiving <= 1'b0;
    		state <= idle;
        end
    endcase
end

endmodule