data/full_repos/permissive/118895292/peripheral/keyboard/ps2_keyboard.v
data/verilator_xmls/f34fba53-1fa3-44dd-90d2-3c29dbd497ba.xml
module ps2_keyboard(
	clk,
	ps2_clk,
	ps2_data,
	decoded_key,
	read_key
);

input clk;
inout ps2_clk;
inout ps2_data;
output reg [7:0] decoded_key = 0;
output reg read_key = 0;

reg [3:0] bitctr = 0;
reg num_bits = 0;
reg ps2_clk_sync = 0;
reg ps2_data_sync = 0;
reg [9:0] clk_filter_ctr = 0;



assign ps2_clk = 1'bz;
assign ps2_data = 1'bz;







always @(posedge clk)
begin
	if (ps2_clk_sync != ps2_clk)
	begin
		if (clk_filter_ctr == 750)
			ps2_clk_sync <= ps2_clk;
		else
			clk_filter_ctr <= clk_filter_ctr + 1;
	end
	else
		clk_filter_ctr <= 0;
	ps2_data_sync <= ps2_data;
end

always @(negedge ps2_clk_sync)
begin
	case (bitctr)
	4'd0: 
		begin



			if (!ps2_data_sync)
			begin
				bitctr <= bitctr + 1;
				num_bits <= 0;
				decoded_key <= 0;
			end
			else
				bitctr <= 0;
		end
	4'd1, 4'd2, 4'd3, 4'd4, 4'd5, 4'd6, 4'd7, 4'd8:
		begin

			decoded_key[bitctr - 1] <= ps2_data_sync;
			if (ps2_data_sync)
				num_bits <= num_bits + 1;
			bitctr <= bitctr + 1;
		end
	4'd9:
		begin





			if (~num_bits == ps2_data_sync)
			begin
				bitctr <= bitctr + 1;
				read_key <= 1;
			end
			else
				bitctr <= 0;
		end
	4'd10:
		begin


			bitctr <= 0;
			read_key <= 0;
		end
	default:
		begin

			bitctr <= 0;
		end
	endcase
end

endmodule
module ps2_keyboard(
	clk,
	ps2_clk,
	ps2_data,
	decoded_key,
	read_key
);


input clk;
inout ps2_clk;
inout ps2_data;
output reg [7:0] decoded_key = 0;
output reg read_key = 0;

reg [3:0] bitctr = 0;
reg num_bits = 0;
reg ps2_clk_sync = 0;
reg ps2_data_sync = 0;
reg [9:0] clk_filter_ctr = 0;



assign ps2_clk = 1'bz;
assign ps2_data = 1'bz;







always @(posedge clk)
begin
	if (ps2_clk_sync != ps2_clk)
	begin
		if (clk_filter_ctr == 750)
			ps2_clk_sync <= ps2_clk;
		else
			clk_filter_ctr <= clk_filter_ctr + 1;
	end
	else
		clk_filter_ctr <= 0;
	ps2_data_sync <= ps2_data;
end

always @(negedge ps2_clk_sync)
begin
	case (bitctr)
	4'd0: 
		begin



			if (!ps2_data_sync)
			begin
				bitctr <= bitctr + 1;
				num_bits <= 0;
				decoded_key <= 0;
			end
			else
				bitctr <= 0;
		end
	4'd1, 4'd2, 4'd3, 4'd4, 4'd5, 4'd6, 4'd7, 4'd8:
		begin

			decoded_key[bitctr - 1] <= ps2_data_sync;
			if (ps2_data_sync)
				num_bits <= num_bits + 1;
			bitctr <= bitctr + 1;
		end
	4'd9:
		begin





			if (~num_bits == ps2_data_sync)
			begin
				bitctr <= bitctr + 1;
				read_key <= 1;
			end
			else
				bitctr <= 0;
		end
	4'd10:
		begin


			bitctr <= 0;
			read_key <= 0;
		end
	default:
		begin

			bitctr <= 0;
		end
	endcase
end

endmodule