data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v
None: at end of input
1: b'%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:24: Cannot find include file: parameter.h\n`include "parameter.h"\n         ^~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/117439411/Pacman_ACL_v1,data/full_repos/permissive/117439411/parameter.h\n             data/full_repos/permissive/117439411/Pacman_ACL_v1,data/full_repos/permissive/117439411/parameter.h.v\n             data/full_repos/permissive/117439411/Pacman_ACL_v1,data/full_repos/permissive/117439411/parameter.h.sv\n             parameter.h\n             parameter.h.v\n             parameter.h.sv\n             obj_dir/parameter.h\n             obj_dir/parameter.h.v\n             obj_dir/parameter.h.sv\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:77: Define or directive not defined: \'`H_PACMAN_SIZE\'\n wire [10:0] h_limit_off = (h_pacman_counter + `H_PACMAN_SIZE);  \n                                               ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:77: syntax error, unexpected \')\', expecting TYPE-IDENTIFIER\n wire [10:0] h_limit_off = (h_pacman_counter + `H_PACMAN_SIZE);  \n                                                             ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:79: Define or directive not defined: \'`V_PACMAN_SIZE\'\n wire [9:0] v_limit_off = (v_pacman_counter + `V_PACMAN_SIZE);  \n                                              ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:79: syntax error, unexpected \')\', expecting TYPE-IDENTIFIER\n wire [9:0] v_limit_off = (v_pacman_counter + `V_PACMAN_SIZE);  \n                                                            ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:187: Define or directive not defined: \'`H_PACMAN_ON\'\n     h_pacman_counter <= `H_PACMAN_ON;\n                         ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:187: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     h_pacman_counter <= `H_PACMAN_ON;\n                                     ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:188: Define or directive not defined: \'`V_PACMAN_ON\'\n     v_pacman_counter <= `V_PACMAN_ON;\n                         ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:188: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     v_pacman_counter <= `V_PACMAN_ON;\n                                     ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:196: Define or directive not defined: \'`H_PACMAN_ON\'\n     h_pacman_counter <= `H_PACMAN_ON;\n                         ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:196: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     h_pacman_counter <= `H_PACMAN_ON;\n                                     ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:197: Define or directive not defined: \'`V_PACMAN_ON\'\n     v_pacman_counter <= `V_PACMAN_ON;\n                         ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:197: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n     v_pacman_counter <= `V_PACMAN_ON;\n                                     ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:232: Define or directive not defined: \'`H_PACMAN_ON\'\n       h_pacman_counter <= `H_PACMAN_ON;\n                           ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:232: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n       h_pacman_counter <= `H_PACMAN_ON;\n                                       ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:233: Define or directive not defined: \'`V_PACMAN_ON\'\n       v_pacman_counter <= `V_PACMAN_ON;\n                           ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:233: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n       v_pacman_counter <= `V_PACMAN_ON;\n                                       ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:253: Define or directive not defined: \'`S0\'\n    `S0:\n    ^~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:253: syntax error, unexpected \':\', expecting endcase\n    `S0:\n       ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:260: Define or directive not defined: \'`H_BOARD_OFF\'\n          if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))\n                                  ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:260: Define or directive not defined: \'`H_PACMAN_SIZE\'\n          if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))\n                                               ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:260: syntax error, unexpected \')\', expecting TYPE-IDENTIFIER\n          if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))\n                                                             ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:262: Define or directive not defined: \'`H_BOARD_ON\'\n            h_pacman_cnt = `H_BOARD_ON + 1;\n                           ^~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:263: Define or directive not defined: \'`V_PACMAN_ON\'\n            v_pacman_cnt = `V_PACMAN_ON - 180;\n                           ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:264: Define or directive not defined: \'`S0\'\n            pacman_next_state = `S0;\n                                ^~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:264: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n            pacman_next_state = `S0;\n                                   ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:271: Define or directive not defined: \'`S0\'\n            pacman_next_state = `S0;\n                                ^~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:271: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n            pacman_next_state = `S0;\n                                   ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:278: Define or directive not defined: \'`S0\'\n            pacman_next_state = `S0;\n                                ^~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:278: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n            pacman_next_state = `S0;\n                                   ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:285: Define or directive not defined: \'`S3\'\n          pacman_next_state = `S3;\n                              ^~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:285: syntax error, unexpected \';\', expecting TYPE-IDENTIFIER\n          pacman_next_state = `S3;\n                                 ^\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:289: syntax error, unexpected else\n      else if (down)\n      ^~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:293: Define or directive not defined: \'`H_BOARD_OFF\'\n          if(h_pacman_counter  == (`H_BOARD_OFF - `H_PACMAN_SIZE))\n                                   ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:293: Define or directive not defined: \'`H_PACMAN_SIZE\'\n          if(h_pacman_counter  == (`H_BOARD_OFF - `H_PACMAN_SIZE))\n                                                  ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:295: Define or directive not defined: \'`H_BOARD_ON\'\n            h_pacman_cnt = `H_BOARD_ON + 1;\n                           ^~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:296: Define or directive not defined: \'`V_PACMAN_ON\'\n            v_pacman_cnt = `V_PACMAN_ON - 180;\n                           ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:297: Define or directive not defined: \'`S0\'\n            pacman_next_state = `S0;\n                                ^~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:304: Define or directive not defined: \'`S0\'\n            pacman_next_state = `S0;\n                                ^~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:311: Define or directive not defined: \'`S0\'\n            pacman_next_state = `S0;\n                                ^~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:318: Define or directive not defined: \'`S2\'\n          pacman_next_state = `S2;\n                              ^~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:323: Define or directive not defined: \'`H_BOARD_ON\'\n        if(h_pacman_counter == `H_BOARD_ON)\n                               ^~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:325: Define or directive not defined: \'`H_BOARD_OFF\'\n          h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;\n                         ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:325: Define or directive not defined: \'`H_PACMAN_SIZE\'\n          h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;\n                                            ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:326: Define or directive not defined: \'`V_PACMAN_ON\'\n          v_pacman_cnt = `V_PACMAN_ON - 180;\n                         ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:327: Define or directive not defined: \'`S1\'\n          pacman_next_state = `S1;\n                              ^~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:331: Define or directive not defined: \'`H_BOARD_OFF\'\n          if(h_pacman_counter  == (`H_BOARD_OFF - `H_PACMAN_SIZE))\n                                   ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:331: Define or directive not defined: \'`H_PACMAN_SIZE\'\n          if(h_pacman_counter  == (`H_BOARD_OFF - `H_PACMAN_SIZE))\n                                                  ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:333: Define or directive not defined: \'`H_BOARD_ON\'\n            h_pacman_cnt = `H_BOARD_ON + 1;\n                           ^~~~~~~~~~~\n%Error: data/full_repos/permissive/117439411/Pacman_ACL_v1/pacman_controller.v:334: Define or directive not defined: \'`V_PACMAN_ON\'\n            v_pacman_cnt = `V_PACMAN_ON - 180;\n                           ^~~~~~~~~~~~\n%Error: Exiting due to too many errors encountered; --error-limit=50\n'
module pacman_controller(
	 input clk,
	 input reset,
    input [15:0]   xAxis,
    input [15:0]   yAxis,
	 input pause,
	 input stall,
	 input hz_enable,
	 input pacman_dead,
	 input end_of_game,
	 input up_pacman_limit,
	 input down_pacman_limit,
	 input left_pacman_limit,
	 input right_pacman_limit,
	 input [9:0] v_counter,
	 input [10:0] h_counter,
	 output pacman_en,
	 output [9:0] v_pacman_tile_counter,
	 output [10:0] h_pacman_tile_counter,
	 output reg waka,
	 output reg up_pacman_state,
	 output reg down_pacman_state,
	 output reg left_pacman_state,
	 output reg right_pacman_state,
	 output reg [7:0] pacman_pixel
	 );



	reg up;
	reg down;
	reg left;
	reg right;
	reg image_sel;
	reg image_sel_1;
	reg [1:0] pacman_state;
	reg [1:0] pacman_image;
	reg [1:0] pacman_next_state;
	reg [7:0] movement_en;
	reg [13:0] h_addr;
	reg [10:0] h_pacman_counter;
	reg [9:0] v_pacman_counter;
	reg [10:0] h_pacman_cnt;
	reg [9:0] v_pacman_cnt;
	
	wire pixel;
	wire h_pacman = (h_counter > (h_limit_on)) &
						 (h_counter < (h_limit_off));
	wire v_pacman = (v_counter > (v_limit_on)) &
						 (v_counter < (v_limit_off));
	wire pacman = h_pacman & v_pacman;
	wire [10:0] h_limit_on = h_pacman_counter;
	wire [10:0] h_limit_off = (h_pacman_counter + `H_PACMAN_SIZE);
	wire [9:0] v_limit_on = v_pacman_counter;
	wire [9:0] v_limit_off = (v_pacman_counter + `V_PACMAN_SIZE);
	
	wire pacman_tile = (h_counter == h_pacman_tile_counter) &
							 (v_counter == v_pacman_tile_counter);
	always @ (posedge clk or posedge reset)
		begin
			if(reset)
				begin
					up <= 1'b0;
					down <= 1'b0;
					left <= 1'b0;
					right <= 1'b0;
				end
			else
				begin
					if(xAxis[15:12] == 4'h0)
						begin
							if(xAxis[11:8] == 4'h1)
								begin
									up <= 1'b1;
									down <= 1'b0;
								end
							else if(xAxis[7:4] > 4'h4)
								begin
									up <= 1'b1;
									down <= 1'b0;
								end
							else
								begin
									up <= 1'b0;
									down <= 1'b0;
								end
						end
					else
						begin
							if(xAxis[11:8] == 4'h1)
								begin
									down <= 1'b1;
									up <= 1'b0;
								end
							else if(xAxis[7:4] > 4'h4)
								begin
									down <= 1'b1;
									up <= 1'b0;
								end
							else
								begin
									down <= 1'b0;
									up <= 1'b0;
								end
						end
					if(yAxis[15:12] == 4'hf)
						begin
							if(yAxis[11:8] == 4'h1)
								begin
									right <= 1'b1;
									left <= 1'b0;
								end
							else if(yAxis[7:4] > 4'h4)
								begin
									right <= 1'b1;
									left <= 1'b0;
								end
							else
								begin
									right <= 1'b0;
									left <= 1'b0;
								end
						end
					else
						begin
							if(yAxis[11:8] == 4'h1)
								begin
									left <= 1'b1;
									right <= 1'b0;
								end
							else if(yAxis[7:4] > 4'h4)
								begin
									left <= 1'b1;
									right <= 1'b0;
								end
							else
								begin
									left <= 1'b0;
									right <= 1'b0;
								end
						end
				end
		end
	
	assign h_pacman_tile_counter = (h_pacman_counter + 5'b10001);
	assign v_pacman_tile_counter = (v_pacman_counter + 5'b10001);
	assign pause_en = (pause | stall);







	always @ (posedge clk or posedge reset)
		begin
			if(reset)
				begin
					image_sel <= 1;
					movement_en <= 0;
					pacman_image <= 0;
					pacman_state <= 0;
					h_pacman_counter <= `H_PACMAN_ON;
					v_pacman_counter <= `V_PACMAN_ON;
				end
			else if(pacman_dead || end_of_game)
				begin
					image_sel <= 1;
					movement_en <= 0;
					pacman_image <= 0;
					pacman_state <= 0;
					h_pacman_counter <= `H_PACMAN_ON;
					v_pacman_counter <= `V_PACMAN_ON;
				end
			else if (hz_enable & (!pause_en))
				begin
					if(!pacman_dead)
						begin
							if (movement_en == 8'b1010)
								begin
									image_sel <= image_sel_1;
									movement_en <= 0;
								end
							else
								begin
									image_sel <= image_sel;
									movement_en <= movement_en + 1;
								end
							h_pacman_counter <= (h_pacman_cnt);
							v_pacman_counter <= (v_pacman_cnt);
							pacman_state <= pacman_next_state;
							pacman_image <= pacman_next_state;
						end
					else
						begin



							h_pacman_counter <= `H_PACMAN_ON;
							v_pacman_counter <= `V_PACMAN_ON;
							
							pacman_state <= pacman_state;
							pacman_image <= pacman_state;
						end
				end
			else
				begin
					image_sel <= image_sel;
					movement_en <= movement_en;
					h_pacman_counter <= (h_pacman_counter);
					v_pacman_counter <= (v_pacman_counter);
					pacman_state <= pacman_state;
				end
		end

	always @ ( * )
		begin
			waka = 1'b1;
			case(pacman_state)
				`S0:
					begin
						image_sel_1 = ~image_sel;
						if(up)
							begin
								if(!up_pacman_limit)
									begin
										if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))
											begin
												h_pacman_cnt = `H_BOARD_ON + 1;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S0;
												end
										else if(!right_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S0;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter + 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S0;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter - 1;
										pacman_next_state = `S3;
									end
										
							end
						else if (down)
							begin
								if(!down_pacman_limit)
									begin
										if(h_pacman_counter  == (`H_BOARD_OFF - `H_PACMAN_SIZE))
											begin
												h_pacman_cnt = `H_BOARD_ON + 1;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S0;
											end
										else if(!right_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S0;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter + 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S0;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter + 1;
										pacman_next_state = `S2;
									end
							end
						else if (left)
							begin
								if(h_pacman_counter == `H_BOARD_ON)
									begin
										h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S1;
									end
								else if(!left_pacman_limit)
									begin
										if(h_pacman_counter  == (`H_BOARD_OFF - `H_PACMAN_SIZE))
											begin
												h_pacman_cnt = `H_BOARD_ON + 1;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S0;
											end
										else if(!right_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S0;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter + 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S0;
											end										
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter - 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S1;
									end			
							end
						else
							begin									
								if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))
									begin
										h_pacman_cnt = `H_BOARD_ON + 1;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S0;
									end
								else if(!right_pacman_limit)
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter;
										waka = 1'b0;
										pacman_next_state = `S0;
										image_sel_1 = 1;
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter + 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S0;
									end
							end
					end
				`S1:
					begin
						image_sel_1 = ~image_sel;
						if(up)
							begin
								if(!up_pacman_limit)
									begin
										if(h_pacman_counter == `H_BOARD_ON)
											begin
												h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S1;
											end
										else if(!left_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S1;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter - 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S1;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter - 1;
										pacman_next_state = `S3;
									end
							end
						else if (down)
							begin
								if(!down_pacman_limit)
									begin
										if(h_pacman_counter == `H_BOARD_ON)
											begin
												h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S1;
											end
										else if(!left_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S1;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter - 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S1;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter + 1;
										pacman_next_state = `S2;
									end
							end
						else if (right)
							begin									
								if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))
									begin
										h_pacman_cnt = `H_BOARD_ON + 1;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S0;
									end
								else if(!right_pacman_limit)
									begin
										if(h_pacman_counter == `H_BOARD_ON)
											begin
												h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S1;
											end
										else if(!left_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S1;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter - 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S1;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter + 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S0;
									end
							end
						else
							begin
								if(h_pacman_counter == `H_BOARD_ON)
									begin
										h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S1;
									end
								else if(!left_pacman_limit)
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter;
										waka = 1'b0;
										pacman_next_state = `S1;
										image_sel_1 = 1;
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter - 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S1;
									end
							end
					end
				`S2:
					begin
						image_sel_1 = ~image_sel;
						if(up)
							begin
								if(!up_pacman_limit)
									begin
										if(!down_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S2;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter + 1;
												pacman_next_state = `S2;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter - 1;
										pacman_next_state = `S3;
									end
							end
						else if (right)
							begin									
								if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))
									begin
										h_pacman_cnt = `H_BOARD_ON + 1;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S0;
									end
								else if(!right_pacman_limit)
									begin
										if(!down_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S2;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter + 1;
												pacman_next_state = `S2;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter + 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S0;
									end
							end
						else if (left)
							begin
								if(h_pacman_counter == `H_BOARD_ON)
									begin
										h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S1;
									end
								else if(!left_pacman_limit)
									begin
										if(!down_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S2;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter + 1;
												pacman_next_state = `S2;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter - 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S1;
									end
							end
						else
							begin
								if(!down_pacman_limit)
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter;
										waka = 1'b0;
										pacman_next_state = `S2;
										image_sel_1 = 1;
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter + 1;
										pacman_next_state = `S2;
									end
							end
					end
				`S3:
					begin
						image_sel_1 = ~image_sel;
						if (right)
							begin									
								if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))
									begin
										h_pacman_cnt = `H_BOARD_ON + 1;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S0;
									end
								else if(!right_pacman_limit)
									begin
										if(!up_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S3;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter - 1;
												pacman_next_state = `S3;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter + 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S0;
									end
							end
						else if (left)
							begin
								if(h_pacman_counter == `H_BOARD_ON)
									begin
										h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S1;
									end
								else if(!left_pacman_limit)
									begin
										if(!up_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S3;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter - 1;
												pacman_next_state = `S3;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter - 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S1;
									end
							end
						else if (down)
							begin
								if(!down_pacman_limit)
									begin
										if(!up_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S3;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter - 1;
												pacman_next_state = `S3;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter + 1;
										pacman_next_state = `S2;
									end
							end
						else
							begin
								if(!up_pacman_limit)
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter;
										waka = 1'b0;
										pacman_next_state = `S3;
										image_sel_1 = 1;
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter - 1;
										pacman_next_state = `S3;
									end
							end
					end
			endcase
		end

	always @ ( * )
		begin
			if(pacman_state == `S0)
				begin
					up_pacman_state = 0;
					down_pacman_state = 0;
					left_pacman_state = 0;
					right_pacman_state = 1;
				end
			else if(pacman_state == `S1)
				begin
					up_pacman_state = 0;
					down_pacman_state = 0;
					left_pacman_state = 1;
					right_pacman_state = 0;
				end
			else if(pacman_state == `S2)
				begin
					up_pacman_state = 0;
					down_pacman_state = 1;
					left_pacman_state = 0;
					right_pacman_state = 0;
				end
			else if(pacman_state == `S3)
				begin
					up_pacman_state = 1;
					down_pacman_state = 0;
					left_pacman_state = 0;
					right_pacman_state = 0;
				end
			else
				begin
					up_pacman_state = 0;
					down_pacman_state = 0;
					left_pacman_state = 0;
					right_pacman_state = 0;
				end
		end

	always @ (posedge clk or posedge reset)	
		begin
			if (reset)
				begin
					h_addr <= 2049;
				end
			else if (pacman)
				begin
					if(stall)
						h_addr <= h_addr + 1;
					else if(!pacman_dead)
						begin
							case(pacman_image)
								`S0: h_addr <= h_addr + 1;
								`S1: h_addr <= h_addr - 1;
								`S2: h_addr <= h_addr + 32;
								`S3: h_addr <= h_addr + 32;
							endcase
						end 
					else
						h_addr <= h_addr + 1;
				end
			else if ((h_counter == (h_limit_off))&(v_pacman))
				begin
					if(stall)
						h_addr <= h_addr;
					else if(!pacman_dead)
						begin
							case(pacman_image)
								`S0: h_addr <= h_addr;
								`S1: h_addr <= h_addr;
								`S2: h_addr <= h_addr - 1023;
								`S3: h_addr <= h_addr - 1025;
							endcase
						end
					else
						h_addr <= h_addr;
				end
			else if (!v_pacman)
				begin
					if(!pacman_dead)
						begin
							case(pacman_image)
								`S0: h_addr <= image_sel?1025:1;
								`S1: h_addr <= image_sel?2047:1023;
								`S2: h_addr <= image_sel?1024:0;
								`S3: h_addr <= image_sel?1055:31;
							endcase
						end
					else if(stall & (!pacman_dead))
						h_addr <= 2049;

						else if (pacman_dead)
							begin
								h_addr <= 2049;
							end
					else
						h_addr <= h_addr;
				end
			else
				begin
					h_addr <= h_addr;
				end
		end

	always @ ( * )
		begin
if(pacman & (!end_of_game))
				begin
					if(pixel == 0)
						pacman_pixel = 8'b0;
					else
						pacman_pixel = 8'hfc;
				end
			else
				pacman_pixel = 8'b0;
		end
	assign pacman_en = pacman;
	pacmanRom Rom(clk,h_addr,pixel);
endmodule
module pacman_controller(
	 input clk,
	 input reset,
    input [15:0]   xAxis,
    input [15:0]   yAxis,
	 input pause,
	 input stall,
	 input hz_enable,
	 input pacman_dead,
	 input end_of_game,
	 input up_pacman_limit,
	 input down_pacman_limit,
	 input left_pacman_limit,
	 input right_pacman_limit,
	 input [9:0] v_counter,
	 input [10:0] h_counter,
	 output pacman_en,
	 output [9:0] v_pacman_tile_counter,
	 output [10:0] h_pacman_tile_counter,
	 output reg waka,
	 output reg up_pacman_state,
	 output reg down_pacman_state,
	 output reg left_pacman_state,
	 output reg right_pacman_state,
	 output reg [7:0] pacman_pixel
	 );




	reg up;
	reg down;
	reg left;
	reg right;
	reg image_sel;
	reg image_sel_1;
	reg [1:0] pacman_state;
	reg [1:0] pacman_image;
	reg [1:0] pacman_next_state;
	reg [7:0] movement_en;
	reg [13:0] h_addr;
	reg [10:0] h_pacman_counter;
	reg [9:0] v_pacman_counter;
	reg [10:0] h_pacman_cnt;
	reg [9:0] v_pacman_cnt;
	
	wire pixel;
	wire h_pacman = (h_counter > (h_limit_on)) &
						 (h_counter < (h_limit_off));
	wire v_pacman = (v_counter > (v_limit_on)) &
						 (v_counter < (v_limit_off));
	wire pacman = h_pacman & v_pacman;
	wire [10:0] h_limit_on = h_pacman_counter;
	wire [10:0] h_limit_off = (h_pacman_counter + `H_PACMAN_SIZE);
	wire [9:0] v_limit_on = v_pacman_counter;
	wire [9:0] v_limit_off = (v_pacman_counter + `V_PACMAN_SIZE);
	
	wire pacman_tile = (h_counter == h_pacman_tile_counter) &
							 (v_counter == v_pacman_tile_counter);
	always @ (posedge clk or posedge reset)
		begin
			if(reset)
				begin
					up <= 1'b0;
					down <= 1'b0;
					left <= 1'b0;
					right <= 1'b0;
				end
			else
				begin
					if(xAxis[15:12] == 4'h0)
						begin
							if(xAxis[11:8] == 4'h1)
								begin
									up <= 1'b1;
									down <= 1'b0;
								end
							else if(xAxis[7:4] > 4'h4)
								begin
									up <= 1'b1;
									down <= 1'b0;
								end
							else
								begin
									up <= 1'b0;
									down <= 1'b0;
								end
						end
					else
						begin
							if(xAxis[11:8] == 4'h1)
								begin
									down <= 1'b1;
									up <= 1'b0;
								end
							else if(xAxis[7:4] > 4'h4)
								begin
									down <= 1'b1;
									up <= 1'b0;
								end
							else
								begin
									down <= 1'b0;
									up <= 1'b0;
								end
						end
					if(yAxis[15:12] == 4'hf)
						begin
							if(yAxis[11:8] == 4'h1)
								begin
									right <= 1'b1;
									left <= 1'b0;
								end
							else if(yAxis[7:4] > 4'h4)
								begin
									right <= 1'b1;
									left <= 1'b0;
								end
							else
								begin
									right <= 1'b0;
									left <= 1'b0;
								end
						end
					else
						begin
							if(yAxis[11:8] == 4'h1)
								begin
									left <= 1'b1;
									right <= 1'b0;
								end
							else if(yAxis[7:4] > 4'h4)
								begin
									left <= 1'b1;
									right <= 1'b0;
								end
							else
								begin
									left <= 1'b0;
									right <= 1'b0;
								end
						end
				end
		end
	
	assign h_pacman_tile_counter = (h_pacman_counter + 5'b10001);
	assign v_pacman_tile_counter = (v_pacman_counter + 5'b10001);
	assign pause_en = (pause | stall);







	always @ (posedge clk or posedge reset)
		begin
			if(reset)
				begin
					image_sel <= 1;
					movement_en <= 0;
					pacman_image <= 0;
					pacman_state <= 0;
					h_pacman_counter <= `H_PACMAN_ON;
					v_pacman_counter <= `V_PACMAN_ON;
				end
			else if(pacman_dead || end_of_game)
				begin
					image_sel <= 1;
					movement_en <= 0;
					pacman_image <= 0;
					pacman_state <= 0;
					h_pacman_counter <= `H_PACMAN_ON;
					v_pacman_counter <= `V_PACMAN_ON;
				end
			else if (hz_enable & (!pause_en))
				begin
					if(!pacman_dead)
						begin
							if (movement_en == 8'b1010)
								begin
									image_sel <= image_sel_1;
									movement_en <= 0;
								end
							else
								begin
									image_sel <= image_sel;
									movement_en <= movement_en + 1;
								end
							h_pacman_counter <= (h_pacman_cnt);
							v_pacman_counter <= (v_pacman_cnt);
							pacman_state <= pacman_next_state;
							pacman_image <= pacman_next_state;
						end
					else
						begin



							h_pacman_counter <= `H_PACMAN_ON;
							v_pacman_counter <= `V_PACMAN_ON;
							
							pacman_state <= pacman_state;
							pacman_image <= pacman_state;
						end
				end
			else
				begin
					image_sel <= image_sel;
					movement_en <= movement_en;
					h_pacman_counter <= (h_pacman_counter);
					v_pacman_counter <= (v_pacman_counter);
					pacman_state <= pacman_state;
				end
		end

	always @ ( * )
		begin
			waka = 1'b1;
			case(pacman_state)
				`S0:
					begin
						image_sel_1 = ~image_sel;
						if(up)
							begin
								if(!up_pacman_limit)
									begin
										if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))
											begin
												h_pacman_cnt = `H_BOARD_ON + 1;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S0;
												end
										else if(!right_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S0;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter + 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S0;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter - 1;
										pacman_next_state = `S3;
									end
										
							end
						else if (down)
							begin
								if(!down_pacman_limit)
									begin
										if(h_pacman_counter  == (`H_BOARD_OFF - `H_PACMAN_SIZE))
											begin
												h_pacman_cnt = `H_BOARD_ON + 1;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S0;
											end
										else if(!right_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S0;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter + 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S0;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter + 1;
										pacman_next_state = `S2;
									end
							end
						else if (left)
							begin
								if(h_pacman_counter == `H_BOARD_ON)
									begin
										h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S1;
									end
								else if(!left_pacman_limit)
									begin
										if(h_pacman_counter  == (`H_BOARD_OFF - `H_PACMAN_SIZE))
											begin
												h_pacman_cnt = `H_BOARD_ON + 1;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S0;
											end
										else if(!right_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S0;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter + 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S0;
											end										
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter - 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S1;
									end			
							end
						else
							begin									
								if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))
									begin
										h_pacman_cnt = `H_BOARD_ON + 1;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S0;
									end
								else if(!right_pacman_limit)
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter;
										waka = 1'b0;
										pacman_next_state = `S0;
										image_sel_1 = 1;
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter + 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S0;
									end
							end
					end
				`S1:
					begin
						image_sel_1 = ~image_sel;
						if(up)
							begin
								if(!up_pacman_limit)
									begin
										if(h_pacman_counter == `H_BOARD_ON)
											begin
												h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S1;
											end
										else if(!left_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S1;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter - 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S1;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter - 1;
										pacman_next_state = `S3;
									end
							end
						else if (down)
							begin
								if(!down_pacman_limit)
									begin
										if(h_pacman_counter == `H_BOARD_ON)
											begin
												h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S1;
											end
										else if(!left_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S1;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter - 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S1;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter + 1;
										pacman_next_state = `S2;
									end
							end
						else if (right)
							begin									
								if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))
									begin
										h_pacman_cnt = `H_BOARD_ON + 1;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S0;
									end
								else if(!right_pacman_limit)
									begin
										if(h_pacman_counter == `H_BOARD_ON)
											begin
												h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
												v_pacman_cnt = `V_PACMAN_ON - 180;
												pacman_next_state = `S1;
											end
										else if(!left_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S1;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter - 1;
												v_pacman_cnt = v_pacman_counter;
												pacman_next_state = `S1;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter + 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S0;
									end
							end
						else
							begin
								if(h_pacman_counter == `H_BOARD_ON)
									begin
										h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S1;
									end
								else if(!left_pacman_limit)
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter;
										waka = 1'b0;
										pacman_next_state = `S1;
										image_sel_1 = 1;
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter - 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S1;
									end
							end
					end
				`S2:
					begin
						image_sel_1 = ~image_sel;
						if(up)
							begin
								if(!up_pacman_limit)
									begin
										if(!down_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S2;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter + 1;
												pacman_next_state = `S2;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter - 1;
										pacman_next_state = `S3;
									end
							end
						else if (right)
							begin									
								if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))
									begin
										h_pacman_cnt = `H_BOARD_ON + 1;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S0;
									end
								else if(!right_pacman_limit)
									begin
										if(!down_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S2;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter + 1;
												pacman_next_state = `S2;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter + 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S0;
									end
							end
						else if (left)
							begin
								if(h_pacman_counter == `H_BOARD_ON)
									begin
										h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S1;
									end
								else if(!left_pacman_limit)
									begin
										if(!down_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S2;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter + 1;
												pacman_next_state = `S2;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter - 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S1;
									end
							end
						else
							begin
								if(!down_pacman_limit)
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter;
										waka = 1'b0;
										pacman_next_state = `S2;
										image_sel_1 = 1;
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter + 1;
										pacman_next_state = `S2;
									end
							end
					end
				`S3:
					begin
						image_sel_1 = ~image_sel;
						if (right)
							begin									
								if(h_pacman_counter == (`H_BOARD_OFF-`H_PACMAN_SIZE))
									begin
										h_pacman_cnt = `H_BOARD_ON + 1;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S0;
									end
								else if(!right_pacman_limit)
									begin
										if(!up_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S3;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter - 1;
												pacman_next_state = `S3;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter + 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S0;
									end
							end
						else if (left)
							begin
								if(h_pacman_counter == `H_BOARD_ON)
									begin
										h_pacman_cnt = `H_BOARD_OFF - 1 - `H_PACMAN_SIZE;
										v_pacman_cnt = `V_PACMAN_ON - 180;
										pacman_next_state = `S1;
									end
								else if(!left_pacman_limit)
									begin
										if(!up_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S3;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter - 1;
												pacman_next_state = `S3;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter - 1;
										v_pacman_cnt = v_pacman_counter;
										pacman_next_state = `S1;
									end
							end
						else if (down)
							begin
								if(!down_pacman_limit)
									begin
										if(!up_pacman_limit)
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter;
												waka = 1'b0;
												pacman_next_state = `S3;
												image_sel_1 = 1;
											end
										else 
											begin
												h_pacman_cnt = h_pacman_counter;
												v_pacman_cnt = v_pacman_counter - 1;
												pacman_next_state = `S3;
											end
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter + 1;
										pacman_next_state = `S2;
									end
							end
						else
							begin
								if(!up_pacman_limit)
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter;
										waka = 1'b0;
										pacman_next_state = `S3;
										image_sel_1 = 1;
									end
								else 
									begin
										h_pacman_cnt = h_pacman_counter;
										v_pacman_cnt = v_pacman_counter - 1;
										pacman_next_state = `S3;
									end
							end
					end
			endcase
		end

	always @ ( * )
		begin
			if(pacman_state == `S0)
				begin
					up_pacman_state = 0;
					down_pacman_state = 0;
					left_pacman_state = 0;
					right_pacman_state = 1;
				end
			else if(pacman_state == `S1)
				begin
					up_pacman_state = 0;
					down_pacman_state = 0;
					left_pacman_state = 1;
					right_pacman_state = 0;
				end
			else if(pacman_state == `S2)
				begin
					up_pacman_state = 0;
					down_pacman_state = 1;
					left_pacman_state = 0;
					right_pacman_state = 0;
				end
			else if(pacman_state == `S3)
				begin
					up_pacman_state = 1;
					down_pacman_state = 0;
					left_pacman_state = 0;
					right_pacman_state = 0;
				end
			else
				begin
					up_pacman_state = 0;
					down_pacman_state = 0;
					left_pacman_state = 0;
					right_pacman_state = 0;
				end
		end

	always @ (posedge clk or posedge reset)	
		begin
			if (reset)
				begin
					h_addr <= 2049;
				end
			else if (pacman)
				begin
					if(stall)
						h_addr <= h_addr + 1;
					else if(!pacman_dead)
						begin
							case(pacman_image)
								`S0: h_addr <= h_addr + 1;
								`S1: h_addr <= h_addr - 1;
								`S2: h_addr <= h_addr + 32;
								`S3: h_addr <= h_addr + 32;
							endcase
						end 
					else
						h_addr <= h_addr + 1;
				end
			else if ((h_counter == (h_limit_off))&(v_pacman))
				begin
					if(stall)
						h_addr <= h_addr;
					else if(!pacman_dead)
						begin
							case(pacman_image)
								`S0: h_addr <= h_addr;
								`S1: h_addr <= h_addr;
								`S2: h_addr <= h_addr - 1023;
								`S3: h_addr <= h_addr - 1025;
							endcase
						end
					else
						h_addr <= h_addr;
				end
			else if (!v_pacman)
				begin
					if(!pacman_dead)
						begin
							case(pacman_image)
								`S0: h_addr <= image_sel?1025:1;
								`S1: h_addr <= image_sel?2047:1023;
								`S2: h_addr <= image_sel?1024:0;
								`S3: h_addr <= image_sel?1055:31;
							endcase
						end
					else if(stall & (!pacman_dead))
						h_addr <= 2049;

						else if (pacman_dead)
							begin
								h_addr <= 2049;
							end
					else
						h_addr <= h_addr;
				end
			else
				begin
					h_addr <= h_addr;
				end
		end

	always @ ( * )
		begin
if(pacman & (!end_of_game))
				begin
					if(pixel == 0)
						pacman_pixel = 8'b0;
					else
						pacman_pixel = 8'hfc;
				end
			else
				pacman_pixel = 8'b0;
		end
	assign pacman_en = pacman;
	pacmanRom Rom(clk,h_addr,pixel);
endmodule