data/full_repos/permissive/476876341/maze.v
data/verilator_xmls/b58d2c2e-2593-45c0-9b0a-8ec5330aa383.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;
	

	reg [1:0] state;
	reg [1:0] next_state;
	reg [maze_width - 1:0] prev_row, prev_col;
	reg [1:0] direction;

	

	always @(posedge clk) begin
		if(!done)
			state <= next_state;
	end


	always @(*) begin
	
		next_state = `start;
		maze_we = 0;
		maze_oe = 0;
		done = 0;
		
		case(state)
			`start: begin
				maze_we = 1;
				row = starting_row;
				col = starting_col;
				direction = 0;
				next_state = `move;
			end
			  
			`move: begin
				prev_row = row;
				prev_col = col; 
				case(direction)
					0: row = row - 1;
					1: col = col + 1;
					2: row = row + 1;
					3: col = col - 1;
				endcase
				maze_oe = 1;
				next_state = `look;
			end
			
			`look: begin

				if(maze_in == 0) begin
					if(row == 0 || row == 63 || col == 0 || col == 63) begin
						maze_we = 1;
						done = 1;
					end else begin
						case(direction)
							0: direction = 1;
							1: direction = 2;
							2: direction = 3;
							3: direction = 0;
						endcase
						maze_we = 1;
						next_state = `move;
					end
				end

				if(maze_in == 1) begin
					row = prev_row;
					col = prev_col;
					case(direction)
						0: direction = 3;
						1: direction = 0;
						2: direction = 1;
						3: direction = 2;
					endcase
					next_state = `move;
				end
			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;
	

	reg [1:0] state;
	reg [1:0] next_state;
	reg [maze_width - 1:0] prev_row, prev_col;
	reg [1:0] direction;

	

	always @(posedge clk) begin
		if(!done)
			state <= next_state;
	end


	always @(*) begin
	
		next_state = `start;
		maze_we = 0;
		maze_oe = 0;
		done = 0;
		
		case(state)
			`start: begin
				maze_we = 1;
				row = starting_row;
				col = starting_col;
				direction = 0;
				next_state = `move;
			end
			  
			`move: begin
				prev_row = row;
				prev_col = col; 
				case(direction)
					0: row = row - 1;
					1: col = col + 1;
					2: row = row + 1;
					3: col = col - 1;
				endcase
				maze_oe = 1;
				next_state = `look;
			end
			
			`look: begin

				if(maze_in == 0) begin
					if(row == 0 || row == 63 || col == 0 || col == 63) begin
						maze_we = 1;
						done = 1;
					end else begin
						case(direction)
							0: direction = 1;
							1: direction = 2;
							2: direction = 3;
							3: direction = 0;
						endcase
						maze_we = 1;
						next_state = `move;
					end
				end

				if(maze_in == 1) begin
					row = prev_row;
					col = prev_col;
					case(direction)
						0: direction = 3;
						1: direction = 0;
						2: direction = 1;
						3: direction = 2;
					endcase
					next_state = `move;
				end
			end	
		 endcase
	end
	
endmodule