data/full_repos/permissive/484316383/rtl/common/KB_8042.v
1: b"%Error: data/full_repos/permissive/484316383/rtl/common/KB_8042.v:115: Too many digits for 1 bit number: 1'b111111\n reg [5:0]joy_map_aux = 1'b111111;\n                        ^~~~~~~~~\n%Error: Exiting due to 1 error(s)\n"
module KB_Mouse_8042(
    input CS,
	 input WR,
    input cmd,
	 input [7:0]din,
	 output [7:0]dout, 
	 input clk,
	 output I_KB,
	 output I_MOUSE,
	 output reg CPU_RST = 0,
	 inout PS2_CLK1,
	 inout PS2_CLK2,
	 inout PS2_DATA1,
	 inout PS2_DATA2,
	 output reg [1:0] monochrome_switcher,
	 output reg [1:0] cpu_speed_switcher,
	 output reg kbd_mreset,
	 output reg kbd_creset,
	 output reg nmi_button,
	 input cpu_speed_io,
	 input [1:0] cpu_speed,
	 input  wire joy_up,
	 input  wire joy_down,
	 input  wire joy_left,
	 input  wire joy_right,
	 input  wire joy_fire1,
	 input  wire joy_fire2,
	 input coreset
	 
    );
	 
	 initial begin
        monochrome_switcher = 2'b00;
		  nmi_button = 1'b0;
		  cpu_speed_switcher = 2'd1;
		  kbd_mreset = 1'b1;
		  kbd_creset = 1'b1;
    end
	 

	reg [3:0]cmdbyte = 4'b1100;
	reg wcfg = 0;
	reg next_mouse = 0;
	reg ctl_outb = 0;
	reg [9:0]wr_data;
	reg [7:0]clkdiv128 = 0;
	reg [7:0]cnt100us = 0;
	reg wr_mouse = 0;
	reg wr_kb = 0;
	reg rd_kb = 0;
	reg rd_mouse = 0;
	reg OBF = 0;
	reg MOBF = 0;
	reg [7:0]s_data;
	reg ctrl_pressed = 0;
	reg alt_pressed = 0;
	wire [5:0]joy_map;
	reg [5:0]joy_map_aux = 1'b111111;
	reg [5:0]joy_map_changes = 0;	
	reg kbd_mreset_req = 1'b0;
	reg kbd_creset_req = 1'b0;
	reg [20:0] monochrome_switcher_req = 0;	
	reg nmi_button_req = 1'b0;

	wire [7:0]kb_data;	
	wire [7:0]mouse_data;
	wire kb_data_out_ready;
	wire kb_data_in_ready;
	wire mouse_data_out_ready;
	wire mouse_data_in_ready;
	wire IBF = ((wr_kb | ~kb_data_in_ready) & ~cmdbyte[2]) | ((wr_mouse  | ~mouse_data_in_ready) & ~cmdbyte[3]);
	wire kb_shift;
	wire mouse_shift;
	
	assign dout = cmd ? {2'b00, MOBF, 1'b1, wcfg, 1'b1, IBF, OBF | MOBF | ctl_outb} : ctl_outb ? {2'b00, cmdbyte[3:2], 2'b00, cmdbyte[1:0]} : s_data;
	assign I_KB = cmdbyte[0] & OBF;
	assign I_MOUSE = cmdbyte[1] & MOBF;
	assign joy_map = {joy_up, joy_down, joy_left, joy_right, joy_fire1, joy_fire2};
	
	PS2Interface Keyboard
	(
		.PS2_CLK(PS2_CLK1),
		.PS2_DATA(PS2_DATA1),
		.clk(clk),
		.rd(rd_kb),
		.wr(wr_kb),
		.data_in(wr_data[0]),
		.data_out(kb_data),
		.data_out_ready(kb_data_out_ready),
		.data_in_ready(kb_data_in_ready),
		.delay100us(cnt100us[7]),
		.data_shift(kb_shift),
		.clk_sample(clkdiv128[7])
	);

	PS2Interface Mouse
	(
		.PS2_CLK(PS2_CLK2),
		.PS2_DATA(PS2_DATA2),
		.clk(clk),
		.rd(rd_mouse),
		.wr(wr_mouse),
		.data_in(wr_data[0]),
		.data_out(mouse_data),
		.data_out_ready(mouse_data_out_ready),
		.data_in_ready(mouse_data_in_ready),
		.delay100us(cnt100us[7]),
		.data_shift(mouse_shift),
		.clk_sample(clkdiv128[7])
	);
	
	always @(posedge clk) begin
		if (kbd_mreset_req) kbd_mreset <= 1'b0;
		if (kbd_creset_req) begin
			kbd_creset <= ~coreset;
			kbd_creset_req <= 1'b0;
		end
		if (nmi_button_req) nmi_button <= 1'b1;
		if (nmi_button) nmi_button <= 1'b0;
		
		if (monochrome_switcher_req != 0) begin			
			if (monochrome_switcher_req == 1) monochrome_switcher <= monochrome_switcher + 1;			
			monochrome_switcher_req <= monochrome_switcher_req + 1;			
		end
		
		if (nmi_button_req) begin
			nmi_button <= 1'b1;
			nmi_button_req <= 1'b0;
		end
		
		
		CPU_RST <= 0;
		if(~kb_data_in_ready) wr_kb <= 1'b0;
		if(~kb_data_out_ready) rd_kb <= 1'b0;
		if(~mouse_data_in_ready) wr_mouse <= 0;
		if(~mouse_data_out_ready) rd_mouse <= 1'b0;
		if(cpu_speed_io) cpu_speed_switcher <= cpu_speed;

		clkdiv128 <= clkdiv128[6:0] + 1'b1;
		if(CS & WR & ~cmd & ~wcfg) cnt100us <= 0;
		else if(!cnt100us[7] & clkdiv128[7]) cnt100us <= cnt100us + 1'b1;
								
		if(~OBF & ~MOBF)
			
			if (~joy_map_changes[0] & joy_map[0] != joy_map_aux[0])
				joy_map_changes[0] <= 1;
			if (~joy_map_changes[1] & joy_map[1] != joy_map_aux[1])
				joy_map_changes[1] <= 1;
			if (~joy_map_changes[2] & joy_map[2] != joy_map_aux[2])
				joy_map_changes[2] <= 1;
			if (~joy_map_changes[3] & joy_map[3] != joy_map_aux[3])
				joy_map_changes[3] <= 1;
			if (~joy_map_changes[4] & joy_map[4] != joy_map_aux[4])
				joy_map_changes[4] <= 1;
			if (~joy_map_changes[5] & joy_map[5] != joy_map_aux[5])
				joy_map_changes[5] <= 1;
		
			if(kb_data_out_ready & ~rd_kb & ~cmdbyte[2]) begin
				OBF <= 1'b1;
												
				if (s_data != 8'he0) begin					
					if (kb_data == 8'h1d) ctrl_pressed = 1'b1;
					if (kb_data == 8'h9d) ctrl_pressed = 1'b0;
					if (kb_data == 8'h38) alt_pressed = 1'b1;
					if (kb_data == 8'hb8) alt_pressed = 1'b0;				
					if (ctrl_pressed == 1'b1 && alt_pressed == 1'b1) begin
						case (kb_data)								

							8'hd8: nmi_button_req <= 1'b1;

							8'h0e: kbd_mreset_req <= 1'b1;

							8'h46: monochrome_switcher_req <= monochrome_switcher_req + 1;

							8'hca: cpu_speed_switcher <= cpu_speed_switcher < 2'd2 ? cpu_speed_switcher + 2'd1 : cpu_speed_switcher;

							8'hce: cpu_speed_switcher <= cpu_speed_switcher > 2'd1 ? cpu_speed_switcher - 2'd1 : cpu_speed_switcher;						
						endcase
					end
				end
				else begin
					if (ctrl_pressed == 1'b1 && alt_pressed == 1'b1) begin
						case (kb_data)															

							8'h53: kbd_creset_req <= 1'b1;							
						endcase
					end					
				end
				
				s_data <= kb_data;
				
			end
			else if(mouse_data_out_ready & ~rd_mouse & ~cmdbyte[3]) begin
				MOBF <= 1'b1;
				s_data <= mouse_data;
			end
			else if (joy_map_changes[5]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[5] ? 8'h48 : 8'hc8;
				joy_map_aux[5] <= joy_map[5];
				joy_map_changes[5] <= 0;
			end
			else if (joy_map_changes[4]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[4] ? 8'h50 : 8'hd0;
				joy_map_aux[4] <= joy_map[4];
				joy_map_changes[4] <= 0;
			end
			else if (joy_map_changes[3]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[3] ? 8'h4b : 8'hcb;
				joy_map_aux[3] <= joy_map[3];
				joy_map_changes[3] <= 0;
			end
			else if (joy_map_changes[2]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[2] ? 8'h4d : 8'hcd;
				joy_map_aux[2] <= joy_map[2];
				joy_map_changes[2] <= 0;
			end	
			else if (joy_map_changes[1]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[1] ? 8'h1c : 8'h9c;
				joy_map_aux[1] <= joy_map[1];
				joy_map_changes[1] <= 0;
			end				
			else if (joy_map_changes[0]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[0] ? 8'h39 : 8'hb9;
				joy_map_aux[0] <= joy_map[0];
				joy_map_changes[0] <= 0;
			end				
		
		if(kb_shift | mouse_shift) wr_data <= {1'b1, wr_data[9:1]};
		
		if(CS) 
			if(WR)
				if(cmd)
					case(din)
						8'h20: ctl_outb <= 1'b1;
						8'h60: wcfg <= 1;
						8'ha7: cmdbyte[3] <= 1;
						8'ha8: cmdbyte[3] <= 0;
						8'had: cmdbyte[2] <= 1;
						8'hae: cmdbyte[2] <= 0;
						8'hd4: next_mouse <= 1;
8'hfe: CPU_RST <= 1;
					endcase 
				else begin
					if(wcfg) cmdbyte <= {din[5:4], din[1:0]};
					else begin
						next_mouse <= 0;
						wr_mouse <= next_mouse;
						wr_kb <= ~next_mouse;
						wr_data <= {~^din, din, 1'b0};
					end
					wcfg <= 0;
				end
			else
				if(~cmd) begin	
					ctl_outb <= 1'b0;
					if(!ctl_outb) begin
						OBF <= 1'b0;
						MOBF <= 1'b0;
						rd_kb <= OBF;
						rd_mouse <= MOBF;
					end
				end
	end
endmodule
module KB_Mouse_8042(
    input CS,
	 input WR,
    input cmd,
	 input [7:0]din,
	 output [7:0]dout, 
	 input clk,
	 output I_KB,
	 output I_MOUSE,
	 output reg CPU_RST = 0,
	 inout PS2_CLK1,
	 inout PS2_CLK2,
	 inout PS2_DATA1,
	 inout PS2_DATA2,
	 output reg [1:0] monochrome_switcher,
	 output reg [1:0] cpu_speed_switcher,
	 output reg kbd_mreset,
	 output reg kbd_creset,
	 output reg nmi_button,
	 input cpu_speed_io,
	 input [1:0] cpu_speed,
	 input  wire joy_up,
	 input  wire joy_down,
	 input  wire joy_left,
	 input  wire joy_right,
	 input  wire joy_fire1,
	 input  wire joy_fire2,
	 input coreset
	 
    );

	 
	 initial begin
        monochrome_switcher = 2'b00;
		  nmi_button = 1'b0;
		  cpu_speed_switcher = 2'd1;
		  kbd_mreset = 1'b1;
		  kbd_creset = 1'b1;
    end
	 

	reg [3:0]cmdbyte = 4'b1100;
	reg wcfg = 0;
	reg next_mouse = 0;
	reg ctl_outb = 0;
	reg [9:0]wr_data;
	reg [7:0]clkdiv128 = 0;
	reg [7:0]cnt100us = 0;
	reg wr_mouse = 0;
	reg wr_kb = 0;
	reg rd_kb = 0;
	reg rd_mouse = 0;
	reg OBF = 0;
	reg MOBF = 0;
	reg [7:0]s_data;
	reg ctrl_pressed = 0;
	reg alt_pressed = 0;
	wire [5:0]joy_map;
	reg [5:0]joy_map_aux = 1'b111111;
	reg [5:0]joy_map_changes = 0;	
	reg kbd_mreset_req = 1'b0;
	reg kbd_creset_req = 1'b0;
	reg [20:0] monochrome_switcher_req = 0;	
	reg nmi_button_req = 1'b0;

	wire [7:0]kb_data;	
	wire [7:0]mouse_data;
	wire kb_data_out_ready;
	wire kb_data_in_ready;
	wire mouse_data_out_ready;
	wire mouse_data_in_ready;
	wire IBF = ((wr_kb | ~kb_data_in_ready) & ~cmdbyte[2]) | ((wr_mouse  | ~mouse_data_in_ready) & ~cmdbyte[3]);
	wire kb_shift;
	wire mouse_shift;
	
	assign dout = cmd ? {2'b00, MOBF, 1'b1, wcfg, 1'b1, IBF, OBF | MOBF | ctl_outb} : ctl_outb ? {2'b00, cmdbyte[3:2], 2'b00, cmdbyte[1:0]} : s_data;
	assign I_KB = cmdbyte[0] & OBF;
	assign I_MOUSE = cmdbyte[1] & MOBF;
	assign joy_map = {joy_up, joy_down, joy_left, joy_right, joy_fire1, joy_fire2};
	
	PS2Interface Keyboard
	(
		.PS2_CLK(PS2_CLK1),
		.PS2_DATA(PS2_DATA1),
		.clk(clk),
		.rd(rd_kb),
		.wr(wr_kb),
		.data_in(wr_data[0]),
		.data_out(kb_data),
		.data_out_ready(kb_data_out_ready),
		.data_in_ready(kb_data_in_ready),
		.delay100us(cnt100us[7]),
		.data_shift(kb_shift),
		.clk_sample(clkdiv128[7])
	);

	PS2Interface Mouse
	(
		.PS2_CLK(PS2_CLK2),
		.PS2_DATA(PS2_DATA2),
		.clk(clk),
		.rd(rd_mouse),
		.wr(wr_mouse),
		.data_in(wr_data[0]),
		.data_out(mouse_data),
		.data_out_ready(mouse_data_out_ready),
		.data_in_ready(mouse_data_in_ready),
		.delay100us(cnt100us[7]),
		.data_shift(mouse_shift),
		.clk_sample(clkdiv128[7])
	);
	
	always @(posedge clk) begin
		if (kbd_mreset_req) kbd_mreset <= 1'b0;
		if (kbd_creset_req) begin
			kbd_creset <= ~coreset;
			kbd_creset_req <= 1'b0;
		end
		if (nmi_button_req) nmi_button <= 1'b1;
		if (nmi_button) nmi_button <= 1'b0;
		
		if (monochrome_switcher_req != 0) begin			
			if (monochrome_switcher_req == 1) monochrome_switcher <= monochrome_switcher + 1;			
			monochrome_switcher_req <= monochrome_switcher_req + 1;			
		end
		
		if (nmi_button_req) begin
			nmi_button <= 1'b1;
			nmi_button_req <= 1'b0;
		end
		
		
		CPU_RST <= 0;
		if(~kb_data_in_ready) wr_kb <= 1'b0;
		if(~kb_data_out_ready) rd_kb <= 1'b0;
		if(~mouse_data_in_ready) wr_mouse <= 0;
		if(~mouse_data_out_ready) rd_mouse <= 1'b0;
		if(cpu_speed_io) cpu_speed_switcher <= cpu_speed;

		clkdiv128 <= clkdiv128[6:0] + 1'b1;
		if(CS & WR & ~cmd & ~wcfg) cnt100us <= 0;
		else if(!cnt100us[7] & clkdiv128[7]) cnt100us <= cnt100us + 1'b1;
								
		if(~OBF & ~MOBF)
			
			if (~joy_map_changes[0] & joy_map[0] != joy_map_aux[0])
				joy_map_changes[0] <= 1;
			if (~joy_map_changes[1] & joy_map[1] != joy_map_aux[1])
				joy_map_changes[1] <= 1;
			if (~joy_map_changes[2] & joy_map[2] != joy_map_aux[2])
				joy_map_changes[2] <= 1;
			if (~joy_map_changes[3] & joy_map[3] != joy_map_aux[3])
				joy_map_changes[3] <= 1;
			if (~joy_map_changes[4] & joy_map[4] != joy_map_aux[4])
				joy_map_changes[4] <= 1;
			if (~joy_map_changes[5] & joy_map[5] != joy_map_aux[5])
				joy_map_changes[5] <= 1;
		
			if(kb_data_out_ready & ~rd_kb & ~cmdbyte[2]) begin
				OBF <= 1'b1;
												
				if (s_data != 8'he0) begin					
					if (kb_data == 8'h1d) ctrl_pressed = 1'b1;
					if (kb_data == 8'h9d) ctrl_pressed = 1'b0;
					if (kb_data == 8'h38) alt_pressed = 1'b1;
					if (kb_data == 8'hb8) alt_pressed = 1'b0;				
					if (ctrl_pressed == 1'b1 && alt_pressed == 1'b1) begin
						case (kb_data)								

							8'hd8: nmi_button_req <= 1'b1;

							8'h0e: kbd_mreset_req <= 1'b1;

							8'h46: monochrome_switcher_req <= monochrome_switcher_req + 1;

							8'hca: cpu_speed_switcher <= cpu_speed_switcher < 2'd2 ? cpu_speed_switcher + 2'd1 : cpu_speed_switcher;

							8'hce: cpu_speed_switcher <= cpu_speed_switcher > 2'd1 ? cpu_speed_switcher - 2'd1 : cpu_speed_switcher;						
						endcase
					end
				end
				else begin
					if (ctrl_pressed == 1'b1 && alt_pressed == 1'b1) begin
						case (kb_data)															

							8'h53: kbd_creset_req <= 1'b1;							
						endcase
					end					
				end
				
				s_data <= kb_data;
				
			end
			else if(mouse_data_out_ready & ~rd_mouse & ~cmdbyte[3]) begin
				MOBF <= 1'b1;
				s_data <= mouse_data;
			end
			else if (joy_map_changes[5]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[5] ? 8'h48 : 8'hc8;
				joy_map_aux[5] <= joy_map[5];
				joy_map_changes[5] <= 0;
			end
			else if (joy_map_changes[4]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[4] ? 8'h50 : 8'hd0;
				joy_map_aux[4] <= joy_map[4];
				joy_map_changes[4] <= 0;
			end
			else if (joy_map_changes[3]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[3] ? 8'h4b : 8'hcb;
				joy_map_aux[3] <= joy_map[3];
				joy_map_changes[3] <= 0;
			end
			else if (joy_map_changes[2]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[2] ? 8'h4d : 8'hcd;
				joy_map_aux[2] <= joy_map[2];
				joy_map_changes[2] <= 0;
			end	
			else if (joy_map_changes[1]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[1] ? 8'h1c : 8'h9c;
				joy_map_aux[1] <= joy_map[1];
				joy_map_changes[1] <= 0;
			end				
			else if (joy_map_changes[0]) begin
				OBF <= 1'b1;
				s_data <= joy_map_aux[0] ? 8'h39 : 8'hb9;
				joy_map_aux[0] <= joy_map[0];
				joy_map_changes[0] <= 0;
			end				
		
		if(kb_shift | mouse_shift) wr_data <= {1'b1, wr_data[9:1]};
		
		if(CS) 
			if(WR)
				if(cmd)
					case(din)
						8'h20: ctl_outb <= 1'b1;
						8'h60: wcfg <= 1;
						8'ha7: cmdbyte[3] <= 1;
						8'ha8: cmdbyte[3] <= 0;
						8'had: cmdbyte[2] <= 1;
						8'hae: cmdbyte[2] <= 0;
						8'hd4: next_mouse <= 1;
8'hfe: CPU_RST <= 1;
					endcase 
				else begin
					if(wcfg) cmdbyte <= {din[5:4], din[1:0]};
					else begin
						next_mouse <= 0;
						wr_mouse <= next_mouse;
						wr_kb <= ~next_mouse;
						wr_data <= {~^din, din, 1'b0};
					end
					wcfg <= 0;
				end
			else
				if(~cmd) begin	
					ctl_outb <= 1'b0;
					if(!ctl_outb) begin
						OBF <= 1'b0;
						MOBF <= 1'b0;
						rd_kb <= OBF;
						rd_mouse <= MOBF;
					end
				end
	end
endmodule