data/full_repos/permissive/128426084/src/sources_1/new/Klawiatura_ps2.v
'utf-8' codec can't decode byte 0x85 in position 4876: invalid start byte
data/verilator_xmls/537c17dd-52d8-4eb5-838b-9538360596c0.xml
module Keyboard(
	input CLK,
   inout PS2_CLK,
   inout PS2_DATA,





   output reg [7:0] LED
   );

	wire [7:0] ARROW_UP = 8'h75;
	wire [7:0] ARROW_DOWN = 8'h72;





	reg read;
	reg [11:0] count_reading;
	reg PREVIOUS_STATE;
	reg scan_err;
	reg [10:0] scan_code;
	reg [7:0] CODEWORD;
	reg TRIG_ARR;
	reg [3:0]COUNT;
	reg TRIGGER = 0;
	reg [11:0]DOWNCOUNTER = 0;


	initial begin
		PREVIOUS_STATE = 1;		
		scan_err = 0;		
		scan_code = 0;
		COUNT = 0;			
		CODEWORD = 0;
		LED = 0;
		read = 0;
		count_reading = 0;
	end

	always @(posedge CLK) begin
		if (DOWNCOUNTER < 2000) begin
			DOWNCOUNTER <= DOWNCOUNTER + 1;
			TRIGGER <= 0;
		end
		else begin
			DOWNCOUNTER <= 0;
			TRIGGER <= 1;
		end
	end
	
	always @(posedge CLK) begin	
		if (TRIGGER) begin
			if (read)
				count_reading <= count_reading + 1;
			else
				count_reading <= 0;
		end
	end


	always @(posedge CLK) begin		
	if (TRIGGER) begin
		if (PS2_CLK != PREVIOUS_STATE) begin
			if (!PS2_CLK) begin
				read <= 1;
				scan_err <= 0;
				scan_code[10:0] <= {PS2_DATA, scan_code[10:1]};
				COUNT <= COUNT + 1;
			end
		end
		else if (COUNT == 11) begin
			COUNT <= 0;
			read <= 0;
			TRIG_ARR <= 1;

			if (!scan_code[10] || scan_code[0] || !(scan_code[1]^scan_code[2]^scan_code[3]^scan_code[4]
				^scan_code[5]^scan_code[6]^scan_code[7]^scan_code[8]
				^scan_code[9]))
				scan_err <= 1;
			else 
				scan_err <= 0;
		end	
		else  begin
			TRIG_ARR <= 0;
			if (COUNT < 11 && count_reading >= 4000) begin
				COUNT <= 0;
				read <= 0;
			end
		end
	PREVIOUS_STATE <= PS2_CLK;
	end
	end


	always @(posedge CLK) begin
		if (TRIGGER) begin
			if (TRIG_ARR) begin
				if (scan_err) begin
					CODEWORD <= 8'd0;
				end
				else begin
					CODEWORD <= scan_code[8:1];
				end
			end
			else CODEWORD <= 8'd0;
		end
		else CODEWORD <= 8'd0;
	end
	
	always @(posedge CLK) begin
	if (TRIGGER) begin
		if (TRIG_ARR) begin
		LED<=scan_code[8:1];







		end
	end
	end

endmodule
module Keyboard(
	input CLK,
   inout PS2_CLK,
   inout PS2_DATA,





   output reg [7:0] LED
   );


	wire [7:0] ARROW_UP = 8'h75;
	wire [7:0] ARROW_DOWN = 8'h72;





	reg read;
	reg [11:0] count_reading;
	reg PREVIOUS_STATE;
	reg scan_err;
	reg [10:0] scan_code;
	reg [7:0] CODEWORD;
	reg TRIG_ARR;
	reg [3:0]COUNT;
	reg TRIGGER = 0;
	reg [11:0]DOWNCOUNTER = 0;


	initial begin
		PREVIOUS_STATE = 1;		
		scan_err = 0;		
		scan_code = 0;
		COUNT = 0;			
		CODEWORD = 0;
		LED = 0;
		read = 0;
		count_reading = 0;
	end

	always @(posedge CLK) begin
		if (DOWNCOUNTER < 2000) begin
			DOWNCOUNTER <= DOWNCOUNTER + 1;
			TRIGGER <= 0;
		end
		else begin
			DOWNCOUNTER <= 0;
			TRIGGER <= 1;
		end
	end
	
	always @(posedge CLK) begin	
		if (TRIGGER) begin
			if (read)
				count_reading <= count_reading + 1;
			else
				count_reading <= 0;
		end
	end


	always @(posedge CLK) begin		
	if (TRIGGER) begin
		if (PS2_CLK != PREVIOUS_STATE) begin
			if (!PS2_CLK) begin
				read <= 1;
				scan_err <= 0;
				scan_code[10:0] <= {PS2_DATA, scan_code[10:1]};
				COUNT <= COUNT + 1;
			end
		end
		else if (COUNT == 11) begin
			COUNT <= 0;
			read <= 0;
			TRIG_ARR <= 1;

			if (!scan_code[10] || scan_code[0] || !(scan_code[1]^scan_code[2]^scan_code[3]^scan_code[4]
				^scan_code[5]^scan_code[6]^scan_code[7]^scan_code[8]
				^scan_code[9]))
				scan_err <= 1;
			else 
				scan_err <= 0;
		end	
		else  begin
			TRIG_ARR <= 0;
			if (COUNT < 11 && count_reading >= 4000) begin
				COUNT <= 0;
				read <= 0;
			end
		end
	PREVIOUS_STATE <= PS2_CLK;
	end
	end


	always @(posedge CLK) begin
		if (TRIGGER) begin
			if (TRIG_ARR) begin
				if (scan_err) begin
					CODEWORD <= 8'd0;
				end
				else begin
					CODEWORD <= scan_code[8:1];
				end
			end
			else CODEWORD <= 8'd0;
		end
		else CODEWORD <= 8'd0;
	end
	
	always @(posedge CLK) begin
	if (TRIGGER) begin
		if (TRIG_ARR) begin
		LED<=scan_code[8:1];







		end
	end
	end

endmodule