data/full_repos/permissive/433444400/maze.v
data/verilator_xmls/19666b58-c46b-4f41-aeb5-be448139e13c.xml
module maze(
input 		clk,
input [maze_width - 1:0] 	starting_col, starting_row,
input 		maze_in,
output reg [maze_width - 1:0] row, col,
output reg	maze_oe,
output reg	maze_we,
output reg	done);

	parameter maze_width  = 6;
	parameter state_width = 3;
	
	parameter UP    =  0;
	parameter LEFT  =  1;
	parameter DOWN  =  2;
	parameter RIGHT =  3;

	reg [maze_width  - 1:0] last_row;
	reg [maze_width  - 1:0] last_col;
	reg [state_width - 1:0] state; 
	reg [state_width - 1:0] next_state;
	reg [1:0] direction = DOWN;

	`define init	                  0
	`define orientation	            1	
	`define move	                  2	
	`define mission_accomplished	   3


	always @(posedge clk) begin

		if (done == 0) begin
			state <= next_state;
		end
	end


	always @(*) begin

	next_state = `init;
	maze_oe = 0;
	maze_we = 0;
	done = 0;

	case(state)
	
		`init : begin	

			row = starting_row;
			col = starting_col;

			maze_we = 1;
			next_state = `orientation;	
		end
		
		`orientation : begin

			last_row = row;
			last_col = col;
			maze_oe = 1;
			next_state = `move;
			


			case (direction)				
						
				UP : begin         
					direction = LEFT;
					row = row - 1;
				end
				
				LEFT : begin        
					direction = DOWN;
					col = col - 1;
				end

				DOWN : begin       
					direction = RIGHT;
					row = row + 1;
				end
					
				RIGHT : begin       
					direction = UP;
					col = col + 1;
				end
				
			endcase
			
		end
		
		`move : begin

			next_state = `orientation;
			

			if (maze_in == 1) begin	
				
				row = last_row;
				col = last_col;
				
		   end else begin		

				maze_we = 1;
				

			   if (last_col - 1 == col && last_row == row) begin		
					direction = UP;
				end

				if (last_row + 1 == row && last_col == col) begin		
					direction = LEFT;
				end

			   if (last_col + 1 == col && last_row == row) begin		
				   direction = DOWN;
				end

			   if (last_row - 1 == row && last_col == col) begin		
				   direction = RIGHT;
				end
			   if (col <= 0 || col >= 63 || row <= 0 || row >= 63) begin
				   next_state = `mission_accomplished;
				end
			end
		end
			
		`mission_accomplished : begin
			done = 1;
		end


	endcase
	end

endmodule
module maze(
input 		clk,
input [maze_width - 1:0] 	starting_col, starting_row,
input 		maze_in,
output reg [maze_width - 1:0] row, col,
output reg	maze_oe,
output reg	maze_we,
output reg	done);


	parameter maze_width  = 6;
	parameter state_width = 3;
	
	parameter UP    =  0;
	parameter LEFT  =  1;
	parameter DOWN  =  2;
	parameter RIGHT =  3;

	reg [maze_width  - 1:0] last_row;
	reg [maze_width  - 1:0] last_col;
	reg [state_width - 1:0] state; 
	reg [state_width - 1:0] next_state;
	reg [1:0] direction = DOWN;

	`define init	                  0
	`define orientation	            1	
	`define move	                  2	
	`define mission_accomplished	   3


	always @(posedge clk) begin

		if (done == 0) begin
			state <= next_state;
		end
	end


	always @(*) begin

	next_state = `init;
	maze_oe = 0;
	maze_we = 0;
	done = 0;

	case(state)
	
		`init : begin	

			row = starting_row;
			col = starting_col;

			maze_we = 1;
			next_state = `orientation;	
		end
		
		`orientation : begin

			last_row = row;
			last_col = col;
			maze_oe = 1;
			next_state = `move;
			


			case (direction)				
						
				UP : begin         
					direction = LEFT;
					row = row - 1;
				end
				
				LEFT : begin        
					direction = DOWN;
					col = col - 1;
				end

				DOWN : begin       
					direction = RIGHT;
					row = row + 1;
				end
					
				RIGHT : begin       
					direction = UP;
					col = col + 1;
				end
				
			endcase
			
		end
		
		`move : begin

			next_state = `orientation;
			

			if (maze_in == 1) begin	
				
				row = last_row;
				col = last_col;
				
		   end else begin		

				maze_we = 1;
				

			   if (last_col - 1 == col && last_row == row) begin		
					direction = UP;
				end

				if (last_row + 1 == row && last_col == col) begin		
					direction = LEFT;
				end

			   if (last_col + 1 == col && last_row == row) begin		
				   direction = DOWN;
				end

			   if (last_row - 1 == row && last_col == col) begin		
				   direction = RIGHT;
				end
			   if (col <= 0 || col >= 63 || row <= 0 || row >= 63) begin
				   next_state = `mission_accomplished;
				end
			end
		end
			
		`mission_accomplished : begin
			done = 1;
		end


	endcase
	end

endmodule