module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output walk_left,
    output walk_right,
    output aaah,
    output digging
);

    // State encoding
    typedef enum logic [2:0] {
        STATE_WALK_LEFT  = 3'b000,
        STATE_WALK_RIGHT = 3'b001,
        STATE_FALL_LEFT  = 3'b010,
        STATE_FALL_RIGHT = 3'b011,
        STATE_DIG_LEFT   = 3'b100,
        STATE_DIG_RIGHT  = 3'b101,
        STATE_SPLATTERED = 3'b111
    } state_t;

    state_t state, next_state;
    logic [4:0] fall_count;

    // State transition
    always_ff @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= STATE_WALK_LEFT;
            fall_count <= 5'b0;
        end else begin
            state <= next_state;
            if (next_state == STATE_FALL_LEFT || next_state == STATE_FALL_RIGHT) begin
                fall_count <= fall_count + 1;
            end else begin
                fall_count <= 5'b0;
            end
        end
    end

    // Next state logic
    always_comb begin
        case (state)
            STATE_WALK_LEFT: begin
                if (!ground) begin
                    next_state = STATE_FALL_LEFT;
                end else if (dig) begin
                    next_state = STATE_DIG_LEFT;
                end else if (bump_left) begin
                    next_state = STATE_WALK_RIGHT;
                end else begin
                    next_state = STATE_WALK_LEFT;
                end
            end
            STATE_WALK_RIGHT: begin
                if (!ground) begin
                    next_state = STATE_FALL_RIGHT;
                end else if (dig) begin
                    next_state = STATE_DIG_RIGHT;
                end else if (bump_right) begin
                    next_state = STATE_WALK_LEFT;
                end else begin
                    next_state = STATE_WALK_RIGHT;
                end
            end
            STATE_FALL_LEFT: begin
                if (fall_count > 20) begin
                    next_state = STATE_SPLATTERED;
                end else if (ground) begin
                    next_state = STATE_WALK_LEFT;
                end else begin
                    next_state = STATE_FALL_LEFT;
                end
            end
            STATE_FALL_RIGHT: begin
                if (fall_count > 20) begin
                    next_state = STATE_SPLATTERED;
                end else if (ground) begin
                    next_state = STATE_WALK_RIGHT;
                end else begin
                    next_state = STATE_FALL_RIGHT;
                end
            end
            STATE_DIG_LEFT: begin
                if (!ground) begin
                    next_state = STATE_FALL_LEFT;
                end else begin
                    next_state = STATE_DIG_LEFT;
                end
            end
            STATE_DIG_RIGHT: begin
                if (!ground) begin
                    next_state = STATE_FALL_RIGHT;
                end else begin
                    next_state = STATE_DIG_RIGHT;
                end
            end
            STATE_SPLATTERED: begin
                next_state = STATE_SPLATTERED;
            end
            default: begin
                next_state = STATE_WALK_LEFT;
            end
        endcase
    end

    // Output logic
    assign walk_left = (state == STATE_WALK_LEFT);
    assign walk_right = (state == STATE_WALK_RIGHT);
    assign aaah = (state == STATE_FALL_LEFT || state == STATE_FALL_RIGHT);
    assign digging = (state == STATE_DIG_LEFT || state == STATE_DIG_RIGHT);

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    // Define state encoding
    typedef enum reg [2:0] {
        STATE_WALK_LEFT = 3'b000,
        STATE_WALK_RIGHT = 3'b001,
        STATE_FALL_LEFT = 3'b010,
        STATE_FALL_RIGHT = 3'b011,
        STATE_DIG_LEFT = 3'b100,
        STATE_DIG_RIGHT = 3'b101,
        STATE_SPLATTER = 3'b110
    } state_t;

    state_t state, next_state;
    reg [4:0] fall_count;

    // State transition logic
    always @ (*) begin
        case (state)
            STATE_WALK_LEFT: begin
                if (!ground)
                    next_state = STATE_FALL_LEFT;
                else if (dig)
                    next_state = STATE_DIG_LEFT;
                else if (bump_left || bump_right)
                    next_state = STATE_WALK_RIGHT;
                else
                    next_state = STATE_WALK_LEFT;
            end
            STATE_WALK_RIGHT: begin
                if (!ground)
                    next_state = STATE_FALL_RIGHT;
                else if (dig)
                    next_state = STATE_DIG_RIGHT;
                else if (bump_left || bump_right)
                    next_state = STATE_WALK_LEFT;
                else
                    next_state = STATE_WALK_RIGHT;
            end
            STATE_FALL_LEFT: begin
                if (!ground)
                    next_state = STATE_FALL_LEFT;
                else if (fall_count > 5'd20)
                    next_state = STATE_SPLATTER;
                else
                    next_state = STATE_WALK_LEFT;
            end
            STATE_FALL_RIGHT: begin
                if (!ground)
                    next_state = STATE_FALL_RIGHT;
                else if (fall_count > 5'd20)
                    next_state = STATE_SPLATTER;
                else
                    next_state = STATE_WALK_RIGHT;
            end
            STATE_DIG_LEFT: begin
                if (!ground)
                    next_state = STATE_FALL_LEFT;
                else
                    next_state = STATE_DIG_LEFT;
            end
            STATE_DIG_RIGHT: begin
                if (!ground)
                    next_state = STATE_FALL_RIGHT;
                else
                    next_state = STATE_DIG_RIGHT;
            end
            STATE_SPLATTER: begin
                next_state = STATE_SPLATTER;
            end
            default: next_state = STATE_WALK_LEFT;
        endcase
    end

    // State and output logic
    always @ (posedge clk or posedge areset) begin
        if (areset) begin
            state <= STATE_WALK_LEFT;
            fall_count <= 5'd0;
        end else begin
            state <= next_state;
            if (state == STATE_FALL_LEFT || state == STATE_FALL_RIGHT) begin
                if (!ground)
                    fall_count <= fall_count + 5'd1;
                else
                    fall_count <= 5'd0;
            end else begin
                fall_count <= 5'd0;
            end
        end
    end

    // Output logic
    always @ (*) begin
        walk_left = 1'b0;
        walk_right = 1'b0;
        aaah = 1'b0;
        digging = 1'b0;
        case (state)
            STATE_WALK_LEFT: walk_left = 1'b1;
            STATE_WALK_RIGHT: walk_right = 1'b1;
            STATE_FALL_LEFT, STATE_FALL_RIGHT: aaah = 1'b1;
            STATE_DIG_LEFT: begin
                walk_left = 1'b1;
                digging = 1'b1;
            end
            STATE_DIG_RIGHT: begin
                walk_right = 1'b1;
                digging = 1'b1;
            end
            STATE_SPLATTER: begin
                walk_left = 1'b0;
                walk_right = 1'b0;
                aaah = 1'b0;
                digging = 1'b0;
            end
        endcase
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    // Define state encoding
    typedef enum reg [2:0] {
        WALK_LEFT = 3'b000,
        WALK_RIGHT = 3'b001,
        FALL_LEFT = 3'b010,
        FALL_RIGHT = 3'b011,
        DIG_LEFT = 3'b100,
        DIG_RIGHT = 3'b101,
        SPLAT = 3'b110
    } state_t;

    state_t state, next_state;
    reg [4:0] fall_count;

    // State transition logic
    always @(*) begin
        case (state)
            WALK_LEFT: begin
                if (!ground)
                    next_state = FALL_LEFT;
                else if (dig)
                    next_state = DIG_LEFT;
                else if (bump_left)
                    next_state = WALK_RIGHT;
                else
                    next_state = WALK_LEFT;
            end
            WALK_RIGHT: begin
                if (!ground)
                    next_state = FALL_RIGHT;
                else if (dig)
                    next_state = DIG_RIGHT;
                else if (bump_right)
                    next_state = WALK_LEFT;
                else
                    next_state = WALK_RIGHT;
            end
            FALL_LEFT: begin
                if (ground)
                    next_state = (fall_count > 20) ? SPLAT : WALK_LEFT;
                else
                    next_state = FALL_LEFT;
            end
            FALL_RIGHT: begin
                if (ground)
                    next_state = (fall_count > 20) ? SPLAT : WALK_RIGHT;
                else
                    next_state = FALL_RIGHT;
            end
            DIG_LEFT: begin
                if (!ground)
                    next_state = FALL_LEFT;
                else
                    next_state = DIG_LEFT;
            end
            DIG_RIGHT: begin
                if (!ground)
                    next_state = FALL_RIGHT;
                else
                    next_state = DIG_RIGHT;
            end
            SPLAT: begin
                next_state = SPLAT;
            end
            default: begin
                next_state = WALK_LEFT;
            end
        endcase
    end

    // State flip-flops with asynchronous reset
    always @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= WALK_LEFT;
            fall_count <= 0;
        end else begin
            state <= next_state;
            if (state == FALL_LEFT || state == FALL_RIGHT)
                fall_count <= fall_count + 1;
            else
                fall_count <= 0;
        end
    end

    // Output logic
    always @(*) begin
        walk_left = (state == WALK_LEFT);
        walk_right = (state == WALK_RIGHT);
        aaah = (state == FALL_LEFT || state == FALL_RIGHT);
        digging = (state == DIG_LEFT || state == DIG_RIGHT);
    end

endmodule

module top_module (
	input clk,
	input areset,
	input bump_left,
	input bump_right,
	input ground,
	input dig,
	output reg walk_left,
	output reg walk_right,
	output reg aaah,
	output reg digging
);

	// State Encoding
	typedef enum {
		STATE_WALK_LEFT,
		STATE_WALK_RIGHT,
		STATE_FALL_LEFT,
		STATE_FALL_RIGHT,
		STATE_DIG_LEFT,
		STATE_DIG_RIGHT,
		STATE_SPLATTERED
	} state_t;

	state_t state, next_state;
	integer fall_time;

	// State Transition
	always @(*) begin
		case (state)
			STATE_WALK_LEFT: begin
				if (!ground) next_state = STATE_FALL_LEFT;
				else if (dig) next_state = STATE_DIG_LEFT;
				else if (bump_left) next_state = STATE_WALK_RIGHT;
				else next_state = STATE_WALK_LEFT;
			end

			STATE_WALK_RIGHT: begin
				if (!ground) next_state = STATE_FALL_RIGHT;
				else if (dig) next_state = STATE_DIG_RIGHT;
				else if (bump_right) next_state = STATE_WALK_LEFT;
				else next_state = STATE_WALK_RIGHT;
			end

			STATE_FALL_LEFT: begin
				if (ground) begin
					if (fall_time > 20) next_state = STATE_SPLATTERED;
					else next_state = STATE_WALK_LEFT;
				end
				else next_state = STATE_FALL_LEFT;
			end

			STATE_FALL_RIGHT: begin
				if (ground) begin
					if (fall_time > 20) next_state = STATE_SPLATTERED;
					else next_state = STATE_WALK_RIGHT;
				end
				else next_state = STATE_FALL_RIGHT;
			end

			STATE_DIG_LEFT: begin
				if (!ground) next_state = STATE_FALL_LEFT;
				else next_state = STATE_DIG_LEFT;
			end

			STATE_DIG_RIGHT: begin
				if (!ground) next_state = STATE_FALL_RIGHT;
				else next_state = STATE_DIG_RIGHT;
			end

			STATE_SPLATTERED: begin
				next_state = STATE_SPLATTERED;
			end

			default: next_state = STATE_WALK_LEFT;
		endcase
	end

	// State Register and Fall Time Counter
	always @(posedge clk or posedge areset) begin
		if (areset) begin
			state <= STATE_WALK_LEFT;
			fall_time <= 0;
		end
		else begin
			state <= next_state;
			if (state == STATE_FALL_LEFT || state == STATE_FALL_RIGHT)
				fall_time <= fall_time + 1;
			else
				fall_time <= 0;
		end
	end

	// Output Logic
	always @(*) begin
		walk_left = 0;
		walk_right = 0;
		aaah = 0;
		digging = 0;

		case (state)
			STATE_WALK_LEFT: walk_left = 1;
			STATE_WALK_RIGHT: walk_right = 1;
			STATE_FALL_LEFT, STATE_FALL_RIGHT: aaah = 1;
			STATE_DIG_LEFT: begin
				walk_left = 1;
				digging = 1;
			end
			STATE_DIG_RIGHT: begin
				walk_right = 1;
				digging = 1;
			end
		endcase
	end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    typedef enum logic [2:0] {
        LEFT_WALK = 3'b000,
        RIGHT_WALK = 3'b001,
        FALL = 3'b010,
        LEFT_DIG = 3'b011,
        RIGHT_DIG = 3'b100,
        SPLATTER = 3'b101
    } state_t;

    state_t state, next_state;
    reg [4:0] fall_count;

    always @(*) begin
        case (state)
            LEFT_WALK: begin
                if (ground == 0) next_state = FALL;
                else if (dig == 1) next_state = LEFT_DIG;
                else if (bump_left) next_state = RIGHT_WALK;
                else next_state = LEFT_WALK;
            end
            RIGHT_WALK: begin
                if (ground == 0) next_state = FALL;
                else if (dig == 1) next_state = RIGHT_DIG;
                else if (bump_right) next_state = LEFT_WALK;
                else next_state = RIGHT_WALK;
            end
            FALL: begin
                if (ground == 1) begin
                    if (fall_count > 20) next_state = SPLATTER;
                    else if (state == LEFT_WALK || state == LEFT_DIG) next_state = LEFT_WALK;
                    else if (state == RIGHT_WALK || state == RIGHT_DIG) next_state = RIGHT_WALK;
                end else next_state = FALL;
            end
            LEFT_DIG: begin
                if (ground == 0) next_state = FALL;
                else next_state = LEFT_DIG;
            end
            RIGHT_DIG: begin
                if (ground == 0) next_state = FALL;
                else next_state = RIGHT_DIG;
            end
            SPLATTER: next_state = SPLATTER; // Remains in SPLATTER state
            default: next_state = LEFT_WALK; // Default to avoid latches
        endcase
    end

    always @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= LEFT_WALK;
            fall_count <= 0;
        end else begin
            state <= next_state;
            if (state == FALL && ground == 0) fall_count <= fall_count + 1;
            else fall_count <= 0;
        end
    end

    always @(*) begin
        walk_left = (state == LEFT_WALK);
        walk_right = (state == RIGHT_WALK);
        aaah = (state == FALL);
        digging = (state == LEFT_DIG || state == RIGHT_DIG);
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    typedef enum reg [2:0] {
        STATE_WALK_LEFT,
        STATE_WALK_RIGHT,
        STATE_FALLING_LEFT,
        STATE_FALLING_RIGHT,
        STATE_DIG_LEFT,
        STATE_DIG_RIGHT,
        STATE_SPLATTER
    } state_t;

    reg [4:0] fall_counter; // 5 bits to count up to 20
    state_t state, next_state;

    always @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= STATE_WALK_LEFT;
            fall_counter <= 0;
        end else begin
            state <= next_state;
            if (state == STATE_FALLING_LEFT || state == STATE_FALLING_RIGHT) begin
                fall_counter <= fall_counter + 1;
            end else begin
                fall_counter <= 0;
            end
        end
    end

    always @(*) begin
        // Default outputs
        walk_left = 0;
        walk_right = 0;
        aaah = 0;
        digging = 0;

        case (state)
            STATE_WALK_LEFT: begin
                walk_left = 1;
                if (!ground) begin
                    next_state = STATE_FALLING_LEFT;
                end else if (dig) begin
                    next_state = STATE_DIG_LEFT;
                end else if (bump_left || bump_right) begin
                    next_state = STATE_WALK_RIGHT;
                end else begin
                    next_state = STATE_WALK_LEFT;
                end
            end

            STATE_WALK_RIGHT: begin
                walk_right = 1;
                if (!ground) begin
                    next_state = STATE_FALLING_RIGHT;
                end else if (dig) begin
                    next_state = STATE_DIG_RIGHT;
                end else if (bump_left || bump_right) begin
                    next_state = STATE_WALK_LEFT;
                end else begin
                    next_state = STATE_WALK_RIGHT;
                end
            end

            STATE_FALLING_LEFT: begin
                aaah = 1;
                if (ground && fall_counter <= 20) begin
                    next_state = STATE_WALK_LEFT;
                end else if (ground && fall_counter > 20) begin
                    next_state = STATE_SPLATTER;
                end else begin
                    next_state = STATE_FALLING_LEFT;
                end
            end

            STATE_FALLING_RIGHT: begin
                aaah = 1;
                if (ground && fall_counter <= 20) begin
                    next_state = STATE_WALK_RIGHT;
                end else if (ground && fall_counter > 20) begin
                    next_state = STATE_SPLATTER;
                end else begin
                    next_state = STATE_FALLING_RIGHT;
                end
            end

            STATE_DIG_LEFT: begin
                digging = 1;
                if (!ground) begin
                    next_state = STATE_FALLING_LEFT;
                end else begin
                    next_state = STATE_DIG_LEFT;
                end
            end

            STATE_DIG_RIGHT: begin
                digging = 1;
                if (!ground) begin
                    next_state = STATE_FALLING_RIGHT;
                end else begin
                    next_state = STATE_DIG_RIGHT;
                end
            end

            STATE_SPLATTER: begin
                // All outputs are 0
                next_state = STATE_SPLATTER;
            end

            default: begin
                next_state = STATE_SPLATTER;
            end
        endcase
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    // Define states
    typedef enum logic [2:0] {
        WALKING_LEFT = 3'b000,
        WALKING_RIGHT = 3'b001,
        FALLING_LEFT = 3'b010,
        FALLING_RIGHT = 3'b011,
        DIGGING_LEFT = 3'b100,
        DIGGING_RIGHT = 3'b101,
        SPLATTERED = 3'b110
    } state_t;

    state_t state, next_state;
    logic [4:0] fall_counter; // 5 bits to count up to 20

    // State transition logic
    always @(*) begin
        case (state)
            WALKING_LEFT: begin
                if (!ground) next_state = FALLING_LEFT;
                else if (dig) next_state = DIGGING_LEFT;
                else if (bump_left || bump_right) next_state = WALKING_RIGHT;
                else next_state = WALKING_LEFT;
            end
            WALKING_RIGHT: begin
                if (!ground) next_state = FALLING_RIGHT;
                else if (dig) next_state = DIGGING_RIGHT;
                else if (bump_left || bump_right) next_state = WALKING_LEFT;
                else next_state = WALKING_RIGHT;
            end
            FALLING_LEFT: begin
                if (ground) next_state = (fall_counter > 20) ? SPLATTERED : WALKING_LEFT;
                else next_state = FALLING_LEFT;
            end
            FALLING_RIGHT: begin
                if (ground) next_state = (fall_counter > 20) ? SPLATTERED : WALKING_RIGHT;
                else next_state = FALLING_RIGHT;
            end
            DIGGING_LEFT: begin
                if (!ground) next_state = FALLING_LEFT;
                else next_state = DIGGING_LEFT;
            end
            DIGGING_RIGHT: begin
                if (!ground) next_state = FALLING_RIGHT;
                else next_state = DIGGING_RIGHT;
            end
            SPLATTERED: begin
                next_state = SPLATTERED;
            end
            default: next_state = WALKING_LEFT;
        endcase
    end

    // State flip-flops and fall counter
    always @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= WALKING_LEFT;
            fall_counter <= 0;
        end else begin
            state <= next_state;
            if (!ground && (state == FALLING_LEFT || state == FALLING_RIGHT))
                fall_counter <= fall_counter + 1;
            else if (ground)
                fall_counter <= 0;
        end
    end

    // Output logic
    always @(*) begin
        walk_left = 0;
        walk_right = 0;
        aaah = 0;
        digging = 0;

        case (state)
            WALKING_LEFT: walk_left = 1;
            WALKING_RIGHT: walk_right = 1;
            FALLING_LEFT, FALLING_RIGHT: aaah = 1;
            DIGGING_LEFT: digging = 1;
            DIGGING_RIGHT: digging = 1;
            SPLATTERED: begin
                walk_left = 0;
                walk_right = 0;
                aaah = 0;
                digging = 0;
            end
        endcase
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    typedef enum logic [2:0] {
        LEFT = 3'b000,
        RIGHT = 3'b001,
        FALL_LEFT = 3'b010,
        FALL_RIGHT = 3'b011,
        DIG_LEFT = 3'b100,
        DIG_RIGHT = 3'b101,
        SPLATTER = 3'b110
    } state_t;

    state_t state, next_state;
    logic [4:0] fall_count; // 5 bits to count up to 21

    always @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= LEFT;
            fall_count <= 5'd0;
        end else begin
            state <= next_state;
            if (aaah)
                fall_count <= fall_count + 1;
            else
                fall_count <= 5'd0;
        end
    end

    always @(*) begin
        // Default outputs
        walk_left = 1'b0;
        walk_right = 1'b0;
        aaah = 1'b0;
        digging = 1'b0;
        next_state = state;

        case (state)
            LEFT: begin
                walk_left = 1'b1;
                if (!ground)
                    next_state = FALL_LEFT;
                else if (dig)
                    next_state = DIG_LEFT;
                else if (bump_left || bump_right)
                    next_state = RIGHT;
            end

            RIGHT: begin
                walk_right = 1'b1;
                if (!ground)
                    next_state = FALL_RIGHT;
                else if (dig)
                    next_state = DIG_RIGHT;
                else if (bump_left || bump_right)
                    next_state = LEFT;
            end

            FALL_LEFT: begin
                aaah = 1'b1;
                if (fall_count >= 5'd20 && ground)
                    next_state = SPLATTER;
                else if (ground)
                    next_state = LEFT;
            end

            FALL_RIGHT: begin
                aaah = 1'b1;
                if (fall_count >= 5'd20 && ground)
                    next_state = SPLATTER;
                else if (ground)
                    next_state = RIGHT;
            end

            DIG_LEFT: begin
                digging = 1'b1;
                if (!ground)
                    next_state = FALL_LEFT;
            end

            DIG_RIGHT: begin
                digging = 1'b1;
                if (!ground)
                    next_state = FALL_RIGHT;
            end

            SPLATTER: begin
                // Stay in SPLATTER, all outputs should be 0
            end
        endcase
    end
endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    // State encoding
    typedef enum reg [2:0] {
        WALK_LEFT = 3'd0,
        WALK_RIGHT = 3'd1,
        FALL = 3'd2,
        DIG_LEFT = 3'd3,
        DIG_RIGHT = 3'd4,
        SPLATTER = 3'd5
    } state_t;

    state_t state, next_state;
    reg [4:0] fall_count; // Counter to track fall duration

    always @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= WALK_LEFT;
            fall_count <= 5'd0;
        end else begin
            state <= next_state;
            if (state == FALL) begin
                fall_count <= fall_count + 1;
            end else begin
                fall_count <= 5'd0;
            end
        end
    end

    always @(*) begin
        // Default output states
        walk_left = 1'b0;
        walk_right = 1'b0;
        aaah = 1'b0;
        digging = 1'b0;

        case (state)
            WALK_LEFT: begin
                walk_left = 1'b1;
                if (!ground) begin
                    next_state = FALL;
                end else if (dig) begin
                    next_state = DIG_LEFT;
                end else if (bump_left) begin
                    next_state = WALK_RIGHT;
                end else begin
                    next_state = WALK_LEFT;
                end
            end
            WALK_RIGHT: begin
                walk_right = 1'b1;
                if (!ground) begin
                    next_state = FALL;
                end else if (dig) begin
                    next_state = DIG_RIGHT;
                end else if (bump_right) begin
                    next_state = WALK_LEFT;
                end else begin
                    next_state = WALK_RIGHT;
                end
            end
            FALL: begin
                aaah = 1'b1;
                if (fall_count > 20) begin
                    next_state = SPLATTER;
                end else if (ground) begin
                    if (state == WALK_LEFT || state == DIG_LEFT) begin
                        next_state = WALK_LEFT;
                    end else begin
                        next_state = WALK_RIGHT;
                    end
                end else begin
                    next_state = FALL;
                end
            end
            DIG_LEFT: begin
                digging = 1'b1;
                if (!ground) begin
                    next_state = FALL;
                end else begin
                    next_state = DIG_LEFT;
                end
            end
            DIG_RIGHT: begin
                digging = 1'b1;
                if (!ground) begin
                    next_state = FALL;
                end else begin
                    next_state = DIG_RIGHT;
                end
            end
            SPLATTER: begin
                walk_left = 1'b0;
                walk_right = 1'b0;
                aaah = 1'b0;
                digging = 1'b0;
                next_state = SPLATTER;
            end
        endcase
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    // State encoding
    typedef enum reg [2:0] {
        STATE_WALK_LEFT  = 3'b000,
        STATE_WALK_RIGHT = 3'b001,
        STATE_FALL_LEFT  = 3'b010,
        STATE_FALL_RIGHT = 3'b011,
        STATE_DIG_LEFT   = 3'b100,
        STATE_DIG_RIGHT  = 3'b101,
        STATE_SPLATTER   = 3'b110
    } state_t;

    state_t state, next_state;
    reg [4:0] fall_count; // Counter to track falling duration

    always @(posedge clk or posedge areset) begin
        if (areset) begin
            // Asynchronous reset to walk left
            state <= STATE_WALK_LEFT;
            fall_count <= 0;
        end else begin
            state <= next_state;
            if (aaah) begin
                fall_count <= fall_count + 1;
            end else begin
                fall_count <= 0;
            end
        end
    end

    always @(*) begin
        case(state)
            STATE_WALK_LEFT: begin
                if (!ground) begin
                    next_state = STATE_FALL_LEFT;     // Start falling if no ground
                end else if (dig && ground) begin
                    next_state = STATE_DIG_LEFT;      // Start digging if dig command given on ground
                end else if (bump_left || (bump_left && bump_right)) begin
                    next_state = STATE_WALK_RIGHT;    // Switch direction on bump
                end else begin
                    next_state = STATE_WALK_LEFT;     // Continue walking left
                end
            end
            STATE_WALK_RIGHT: begin
                if (!ground) begin
                    next_state = STATE_FALL_RIGHT;    // Start falling if no ground
                end else if (dig && ground) begin
                    next_state = STATE_DIG_RIGHT;     // Start digging if dig command given on ground
                end else if (bump_right || (bump_left && bump_right)) begin
                    next_state = STATE_WALK_LEFT;     // Switch direction on bump
                end else begin
                    next_state = STATE_WALK_RIGHT;    // Continue walking right
                end
            end
            STATE_FALL_LEFT: begin
                if (ground) begin
                    if (fall_count > 20) begin
                        next_state = STATE_SPLATTER; // Splatter when falling exceeds 20 cycles
                    end else begin
                        next_state = STATE_WALK_LEFT; // Land and walk left
                    end
                end else begin
                    next_state = STATE_FALL_LEFT;    // Continue falling
                end
            end
            STATE_FALL_RIGHT: begin
                if (ground) begin
                    if (fall_count > 20) begin
                        next_state = STATE_SPLATTER; // Splatter when falling exceeds 20 cycles
                    end else begin
                        next_state = STATE_WALK_RIGHT; // Land and walk right
                    end
                end else begin
                    next_state = STATE_FALL_RIGHT;   // Continue falling
                end
            end
            STATE_DIG_LEFT: begin
                if (!ground) begin
                    next_state = STATE_FALL_LEFT;    // Start falling when not on ground
                end else begin
                    next_state = STATE_DIG_LEFT;     // Continue digging
                end
            end
            STATE_DIG_RIGHT: begin
                if (!ground) begin
                    next_state = STATE_FALL_RIGHT;   // Start falling when not on ground
                end else begin
                    next_state = STATE_DIG_RIGHT;    // Continue digging
                end
            end
            STATE_SPLATTER: begin
                next_state = STATE_SPLATTER;         // Remain in splatter state
            end
        endcase
    end

    always @(*) begin
        walk_left = (state == STATE_WALK_LEFT);
        walk_right = (state == STATE_WALK_RIGHT);
        aaah = (state == STATE_FALL_LEFT || state == STATE_FALL_RIGHT);
        digging = (state == STATE_DIG_LEFT || state == STATE_DIG_RIGHT);
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    typedef enum logic [2:0] {
        STATE_WALK_LEFT   = 3'b000,
        STATE_WALK_RIGHT  = 3'b001,
        STATE_FALL_LEFT   = 3'b010,
        STATE_FALL_RIGHT  = 3'b011,
        STATE_DIG_LEFT    = 3'b100,
        STATE_DIG_RIGHT   = 3'b101,
        STATE_SPLATTER    = 3'b110
    } state_t;

    state_t state, next_state;
    integer fall_count;

    always_ff @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= STATE_WALK_LEFT;
            fall_count <= 0;
        end else begin
            state <= next_state;
            if (aaah) begin
                fall_count <= fall_count + 1;
            end else if (ground) begin
                fall_count <= 0;
            end
        end
    end

    always_comb begin
        next_state = state; // Default to no state change.
        walk_left = 0;
        walk_right = 0;
        aaah = 0;
        digging = 0;

        case (state)
            STATE_WALK_LEFT: begin
                walk_left = 1;
                if (!ground) begin
                    next_state = STATE_FALL_LEFT;
                end else if (dig) begin
                    next_state = STATE_DIG_LEFT;
                end else if (bump_left || bump_right) begin
                    next_state = STATE_WALK_RIGHT;
                end
            end

            STATE_WALK_RIGHT: begin
                walk_right = 1;
                if (!ground) begin
                    next_state = STATE_FALL_RIGHT;
                end else if (dig) begin
                    next_state = STATE_DIG_RIGHT;
                end else if (bump_left || bump_right) begin
                    next_state = STATE_WALK_LEFT;
                end
            end

            STATE_FALL_LEFT: begin
                aaah = 1;
                if (fall_count > 20 && ground) begin
                    next_state = STATE_SPLATTER;
                end else if (ground) begin
                    next_state = STATE_WALK_LEFT;
                end
            end

            STATE_FALL_RIGHT: begin
                aaah = 1;
                if (fall_count > 20 && ground) begin
                    next_state = STATE_SPLATTER;
                end else if (ground) begin
                    next_state = STATE_WALK_RIGHT;
                end
            end

            STATE_DIG_LEFT: begin
                digging = 1;
                if (!ground) begin
                    next_state = STATE_FALL_LEFT;
                end
            end

            STATE_DIG_RIGHT: begin
                digging = 1;
                if (!ground) begin
                    next_state = STATE_FALL_RIGHT;
                end
            end

            STATE_SPLATTER: begin
                // Do nothing; all outputs are 0.
            end
        endcase
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    // State encoding
    typedef enum logic [2:0] {
        WL = 3'b000,  // walk_left
        WR = 3'b001,  // walk_right
        F = 3'b010,   // falling
        DL = 3'b011,  // digging left
        DR = 3'b100,  // digging right
        SPLAT = 3'b101 // splattered
    } state_t;

    state_t state, next_state;
    integer fall_count;

    // State transition
    always_ff @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= WL;
            fall_count <= 0;
        end else begin
            state <= next_state;
            if (next_state == F)
                fall_count <= fall_count + 1;
            else
                fall_count <= 0;
        end
    end

    // Next state logic
    always_comb begin
        case (state)
            WL: begin
                if (!ground)
                    next_state = F;
                else if (dig)
                    next_state = DL;
                else if (bump_left || bump_right)
                    next_state = WR;
                else
                    next_state = WL;
            end
            WR: begin
                if (!ground)
                    next_state = F;
                else if (dig)
                    next_state = DR;
                else if (bump_left || bump_right)
                    next_state = WL;
                else
                    next_state = WR;
            end
            F: begin
                if (ground) begin
                    if (fall_count > 20)
                        next_state = SPLAT;
                    else if (state == WL)
                        next_state = WL;
                    else
                        next_state = WR;
                end else
                    next_state = F;
            end
            DL: begin
                if (!ground)
                    next_state = F;
                else
                    next_state = DL;
            end
            DR: begin
                if (!ground)
                    next_state = F;
                else
                    next_state = DR;
            end
            SPLAT: begin
                next_state = SPLAT;
            end
            default: next_state = WL;
        endcase
    end

    // Output logic
    always_ff @(posedge clk or posedge areset) begin
        if (areset) begin
            walk_left <= 1;
            walk_right <= 0;
            aaah <= 0;
            digging <= 0;
        end else begin
            walk_left <= (state == WL);
            walk_right <= (state == WR);
            aaah <= (state == F);
            digging <= ((state == DL) || (state == DR));
        end
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    typedef enum reg [2:0] {
        WALK_LEFT,
        WALK_RIGHT,
        DIG_LEFT,
        DIG_RIGHT,
        FALL_LEFT,
        FALL_RIGHT,
        SPLATTER
    } state_t;

    state_t state, next_state;
    reg [4:0] fall_count;  // Counter for falling clock cycles

    always @(*) begin
        // Default outputs
        walk_left = 0;
        walk_right = 0;
        aaah = 0;
        digging = 0;

        case (state)
            WALK_LEFT: begin
                walk_left = 1;
                if (!ground)
                    next_state = FALL_LEFT;
                else if (dig && ground)
                    next_state = DIG_LEFT;
                else if (bump_left)
                    next_state = WALK_RIGHT;
                else
                    next_state = WALK_LEFT;
            end
            WALK_RIGHT: begin
                walk_right = 1;
                if (!ground)
                    next_state = FALL_RIGHT;
                else if (dig && ground)
                    next_state = DIG_RIGHT;
                else if (bump_right)
                    next_state = WALK_LEFT;
                else
                    next_state = WALK_RIGHT;
            end
            DIG_LEFT: begin
                digging = 1;
                if (!ground)
                    next_state = FALL_LEFT;
                else
                    next_state = DIG_LEFT;
            end
            DIG_RIGHT: begin
                digging = 1;
                if (!ground)
                    next_state = FALL_RIGHT;
                else
                    next_state = DIG_RIGHT;
            end
            FALL_LEFT: begin
                aaah = 1;
                if (ground) begin
                    if (fall_count > 20)
                        next_state = SPLATTER;
                    else
                        next_state = WALK_LEFT;
                end else
                    next_state = FALL_LEFT;
            end
            FALL_RIGHT: begin
                aaah = 1;
                if (ground) begin
                    if (fall_count > 20)
                        next_state = SPLATTER;
                    else
                        next_state = WALK_RIGHT;
                end else
                    next_state = FALL_RIGHT;
            end
            SPLATTER: begin
                next_state = SPLATTER;
            end
        endcase
    end

    always @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= WALK_LEFT;
            fall_count <= 0;
        end else begin
            state <= next_state;
            if (state == FALL_LEFT || state == FALL_RIGHT)
                fall_count <= fall_count + 1;
            else
                fall_count <= 0;
        end
    end
endmodule

module top_module (
	input clk,
	input areset,
	input bump_left,
	input bump_right,
	input ground,
	input dig,
	output reg walk_left,
	output reg walk_right,
	output reg aaah,
	output reg digging
);

    typedef enum reg [2:0] {
        LEFT = 3'b000,
        RIGHT = 3'b001,
        FALLING_LEFT = 3'b010,
        FALLING_RIGHT = 3'b011,
        DIG_LEFT = 3'b100,
        DIG_RIGHT = 3'b101,
        SPLATTER = 3'b110
    } state_t;

    reg [2:0] state, next_state;
    reg [4:0] fall_counter; // Counter for falling

    always @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= LEFT;
            fall_counter <= 0;
        end else begin
            state <= next_state;
            if (aaah)
                fall_counter <= fall_counter + 1;
            else
                fall_counter <= 0;
        end
    end

    always @(*) begin
        walk_left = 0;
        walk_right = 0;
        aaah = 0;
        digging = 0;
        next_state = state;

        case (state)
            LEFT: begin
                walk_left = 1;
                if (!ground)
                    next_state = FALLING_LEFT;
                else if (dig)
                    next_state = DIG_LEFT;
                else if (bump_left)
                    next_state = RIGHT;
            end

            RIGHT: begin
                walk_right = 1;
                if (!ground)
                    next_state = FALLING_RIGHT;
                else if (dig)
                    next_state = DIG_RIGHT;
                else if (bump_right)
                    next_state = LEFT;
            end

            FALLING_LEFT: begin
                aaah = 1;
                if (ground)
                    next_state = (fall_counter >= 20) ? SPLATTER : LEFT;
            end

            FALLING_RIGHT: begin
                aaah = 1;
                if (ground)
                    next_state = (fall_counter >= 20) ? SPLATTER : RIGHT;
            end

            DIG_LEFT: begin
                digging = 1;
                if (!ground)
                    next_state = FALLING_LEFT;
            end

            DIG_RIGHT: begin
                digging = 1;
                if (!ground)
                    next_state = FALLING_RIGHT;
            end

            SPLATTER: begin
                walk_left = 0;
                walk_right = 0;
                aaah = 0;
                digging = 0;
            end

            default: begin
                next_state = LEFT;
            end
        endcase
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    // Define state encoding
    typedef enum logic [2:0] {
        WALK_LEFT = 3'b000,
        WALK_RIGHT = 3'b001,
        FALL_LEFT = 3'b010,
        FALL_RIGHT = 3'b011,
        DIG_LEFT = 3'b100,
        DIG_RIGHT = 3'b101,
        SPLATTERED = 3'b111
    } state_t;

    state_t state, next_state;
    integer fall_count;

    // State transition logic
    always @(*) begin
        case (state)
            WALK_LEFT: begin
                if (!ground)
                    next_state = FALL_LEFT;
                else if (dig)
                    next_state = DIG_LEFT;
                else if (bump_left || (bump_left && bump_right))
                    next_state = WALK_RIGHT;
                else
                    next_state = WALK_LEFT;
            end
            WALK_RIGHT: begin
                if (!ground)
                    next_state = FALL_RIGHT;
                else if (dig)
                    next_state = DIG_RIGHT;
                else if (bump_right || (bump_left && bump_right))
                    next_state = WALK_LEFT;
                else
                    next_state = WALK_RIGHT;
            end
            FALL_LEFT: begin
                if (ground) begin
                    if (fall_count > 20)
                        next_state = SPLATTERED;
                    else
                        next_state = WALK_LEFT;
                end
                else
                    next_state = FALL_LEFT;
            end
            FALL_RIGHT: begin
                if (ground) begin
                    if (fall_count > 20)
                        next_state = SPLATTERED;
                    else
                        next_state = WALK_RIGHT;
                end
                else
                    next_state = FALL_RIGHT;
            end
            DIG_LEFT: begin
                if (!ground)
                    next_state = FALL_LEFT;
                else
                    next_state = DIG_LEFT;
            end
            DIG_RIGHT: begin
                if (!ground)
                    next_state = FALL_RIGHT;
                else
                    next_state = DIG_RIGHT;
            end
            SPLATTERED: next_state = SPLATTERED;
            default: next_state = WALK_LEFT;
        endcase
    end

    // State flip-flops
    always @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= WALK_LEFT;
            fall_count <= 0;
        end
        else begin
            state <= next_state;
            if (state == FALL_LEFT || state == FALL_RIGHT) begin
                if (!ground)
                    fall_count <= fall_count + 1;
                else
                    fall_count <= 0;
            end
            else
                fall_count <= 0;
        end
    end

    // Output logic
    always @(*) begin
        walk_left = (state == WALK_LEFT);
        walk_right = (state == WALK_RIGHT);
        aaah = (state == FALL_LEFT || state == FALL_RIGHT);
        digging = (state == DIG_LEFT || state == DIG_RIGHT);
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    // State Encoding
    typedef enum reg [2:0] {
        S_WALK_LEFT      = 3'd0,
        S_WALK_RIGHT     = 3'd1,
        S_FALL_LEFT      = 3'd2,
        S_FALL_RIGHT     = 3'd3,
        S_DIG_LEFT       = 3'd4,
        S_DIG_RIGHT      = 3'd5,
        S_SPLATTER       = 3'd6
    } state_t;

    reg [2:0] state, next_state;
    reg [4:0] fall_counter; // 5 bits to count up to 20

    // Next state logic
    always @(*) begin
        next_state = state; // Default: stay in the same state
        case(state)
            S_WALK_LEFT: begin
                if (~ground)
                    next_state = S_FALL_LEFT;
                else if (dig)
                    next_state = S_DIG_LEFT;
                else if (bump_left)
                    next_state = S_WALK_RIGHT;
            end
            S_WALK_RIGHT: begin
                if (~ground)
                    next_state = S_FALL_RIGHT;
                else if (dig)
                    next_state = S_DIG_RIGHT;
                else if (bump_right)
                    next_state = S_WALK_LEFT;
            end
            S_FALL_LEFT, S_FALL_RIGHT: begin
                if (ground) begin
                    if (fall_counter > 20)
                        next_state = S_SPLATTER;
                    else
                        next_state = (state == S_FALL_LEFT) ? S_WALK_LEFT : S_WALK_RIGHT;
                end
            end
            S_DIG_LEFT: begin
                if (~ground)
                    next_state = S_FALL_LEFT;
            end
            S_DIG_RIGHT: begin
                if (~ground)
                    next_state = S_FALL_RIGHT;
            end
            S_SPLATTER: begin
                // Remain in S_SPLATTER
            end
        endcase
    end

    // State register update
    always @(posedge clk or posedge areset) begin
        if (areset)
            state <= S_WALK_LEFT;
        else
            state <= next_state;
    end

    // Fall counter logic
    always @(posedge clk or posedge areset) begin
        if (areset)
            fall_counter <= 0;
        else if (state == S_FALL_LEFT || state == S_FALL_RIGHT)
            fall_counter <= fall_counter + 1;
        else
            fall_counter <= 0;
    end

    // Output logic
    always @(*) begin
        walk_left = (state == S_WALK_LEFT);
        walk_right = (state == S_WALK_RIGHT);
        aaah = (state == S_FALL_LEFT || state == S_FALL_RIGHT);
        digging = (state == S_DIG_LEFT || state == S_DIG_RIGHT);
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    typedef enum reg [2:0] {
        WALK_LEFT,
        WALK_RIGHT,
        FALL_LEFT,
        FALL_RIGHT,
        DIG_LEFT,
        DIG_RIGHT,
        SPLATTERED
    } state_t;

    state_t state, next_state;
    reg [4:0] fall_count;

    always_ff @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= WALK_LEFT;
            fall_count <= 0;
        end else begin
            state <= next_state;
            if (aaah) begin
                fall_count <= fall_count + 1;
            end else begin
                fall_count <= 0;
            end
        end
    end

    always_comb begin
        next_state = state;
        walk_left = 0;
        walk_right = 0;
        aaah = 0;
        digging = 0;

        case (state)
            WALK_LEFT: begin
                if (!ground) begin
                    next_state = FALL_LEFT;
                end else if (dig) begin
                    next_state = DIG_LEFT;
                end else if (bump_left) begin
                    next_state = WALK_RIGHT;
                end
            end

            WALK_RIGHT: begin
                if (!ground) begin
                    next_state = FALL_RIGHT;
                end else if (dig) begin
                    next_state = DIG_RIGHT;
                end else if (bump_right) begin
                    next_state = WALK_LEFT;
                end
            end

            FALL_LEFT: begin
                aaah = 1;
                if (fall_count > 20) begin
                    next_state = SPLATTERED;
                end else if (ground) begin
                    next_state = WALK_LEFT;
                end
            end

            FALL_RIGHT: begin
                aaah = 1;
                if (fall_count > 20) begin
                    next_state = SPLATTERED;
                end else if (ground) begin
                    next_state = WALK_RIGHT;
                end
            end

            DIG_LEFT: begin
                digging = 1;
                if (!ground) begin
                    next_state = FALL_LEFT;
                end
            end

            DIG_RIGHT: begin
                digging = 1;
                if (!ground) begin
                    next_state = FALL_RIGHT;
                end
            end

            SPLATTERED: begin
                // Do nothing, Lemming is splattered
            end
        endcase
    end

    always_comb begin
        case (state)
            WALK_LEFT: walk_left = 1;
            WALK_RIGHT: walk_right = 1;
            FALL_LEFT, FALL_RIGHT: aaah = 1;
            DIG_LEFT: begin walk_left = 1; digging = 1; end
            DIG_RIGHT: begin walk_right = 1; digging = 1; end
            SPLATTERED: begin
                walk_left = 0;
                walk_right = 0;
                aaah = 0;
                digging = 0;
            end
        endcase
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    typedef enum logic [2:0] {
        STATE_WALK_LEFT = 3'b000,
        STATE_WALK_RIGHT = 3'b001,
        STATE_FALL_LEFT = 3'b010,
        STATE_FALL_RIGHT = 3'b011,
        STATE_DIG_LEFT = 3'b100,
        STATE_DIG_RIGHT = 3'b101,
        STATE_SPLATTER = 3'b110
    } state_t;

    state_t state, next_state;
    logic [4:0] fall_count;

    always_ff @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= STATE_WALK_LEFT;
            fall_count <= 5'd0;
        end else begin
            state <= next_state;
            if (aaah)
                fall_count <= fall_count + 5'd1;
            else
                fall_count <= 5'd0;
        end
    end

    always_comb begin
        // Default outputs
        walk_left = 1'b0;
        walk_right = 1'b0;
        aaah = 1'b0;
        digging = 1'b0;
        next_state = state;

        case (state)
            STATE_WALK_LEFT: begin
                if (!ground) begin
                    aaah = 1'b1;
                    next_state = STATE_FALL_LEFT;
                end else if (dig) begin
                    digging = 1'b1;
                    next_state = STATE_DIG_LEFT;
                end else if (bump_left | bump_right) begin
                    next_state = STATE_WALK_RIGHT;
                end else begin
                    walk_left = 1'b1;
                end
            end
            STATE_WALK_RIGHT: begin
                if (!ground) begin
                    aaah = 1'b1;
                    next_state = STATE_FALL_RIGHT;
                end else if (dig) begin
                    digging = 1'b1;
                    next_state = STATE_DIG_RIGHT;
                end else if (bump_left | bump_right) begin
                    next_state = STATE_WALK_LEFT;
                end else begin
                    walk_right = 1'b1;
                end
            end
            STATE_FALL_LEFT: begin
                aaah = 1'b1;
                if (ground) begin
                    if (fall_count > 5'd20) begin
                        next_state = STATE_SPLATTER;
                    end else begin
                        next_state = STATE_WALK_LEFT;
                    end
                end
            end
            STATE_FALL_RIGHT: begin
                aaah = 1'b1;
                if (ground) begin
                    if (fall_count > 5'd20) begin
                        next_state = STATE_SPLATTER;
                    end else begin
                        next_state = STATE_WALK_RIGHT;
                    end
                end
            end
            STATE_DIG_LEFT: begin
                digging = 1'b1;
                if (!ground) begin
                    aaah = 1'b1;
                    next_state = STATE_FALL_LEFT;
                end
            end
            STATE_DIG_RIGHT: begin
                digging = 1'b1;
                if (!ground) begin
                    aaah = 1'b1;
                    next_state = STATE_FALL_RIGHT;
                end
            end
            STATE_SPLATTER: begin
                // All outputs are 0
            end
        endcase
    end

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output walk_left,
    output walk_right,
    output aaah,
    output digging
);

    // State encoding
    typedef enum reg [2:0] {
        WALK_LEFT   = 3'b000,
        WALK_RIGHT  = 3'b001,
        FALL_LEFT   = 3'b010,
        FALL_RIGHT  = 3'b011,
        DIG_LEFT    = 3'b100,
        DIG_RIGHT   = 3'b101,
        SPLATTER    = 3'b110
    } state_t;

    state_t current_state, next_state;
    reg [4:0] fall_counter; // Counts the number of cycles while falling

    // State transitions
    always @(*) begin
        case (current_state)
            WALK_LEFT: begin
                if (!ground)
                    next_state = FALL_LEFT;
                else if (dig)
                    next_state = DIG_LEFT;
                else if (bump_left)
                    next_state = WALK_RIGHT;
                else
                    next_state = WALK_LEFT;
            end

            WALK_RIGHT: begin
                if (!ground)
                    next_state = FALL_RIGHT;
                else if (dig)
                    next_state = DIG_RIGHT;
                else if (bump_right)
                    next_state = WALK_LEFT;
                else
                    next_state = WALK_RIGHT;
            end

            FALL_LEFT: begin
                if (ground) begin
                    if (fall_counter > 20)
                        next_state = SPLATTER;
                    else
                        next_state = WALK_LEFT;
                end
                else
                    next_state = FALL_LEFT;
            end

            FALL_RIGHT: begin
                if (ground) begin
                    if (fall_counter > 20)
                        next_state = SPLATTER;
                    else
                        next_state = WALK_RIGHT;
                end
                else
                    next_state = FALL_RIGHT;
            end

            DIG_LEFT: begin
                if (!ground)
                    next_state = FALL_LEFT;
                else
                    next_state = DIG_LEFT;
            end

            DIG_RIGHT: begin
                if (!ground)
                    next_state = FALL_RIGHT;
                else
                    next_state = DIG_RIGHT;
            end

            SPLATTER: begin
                next_state = SPLATTER;
            end

            default: next_state = WALK_LEFT;
        endcase
    end

    // Sequential logic
    always @(posedge clk or posedge areset) begin
        if (areset) begin
            current_state <= WALK_LEFT;
            fall_counter <= 0;
        end
        else begin
            current_state <= next_state;
            if (current_state == FALL_LEFT || current_state == FALL_RIGHT)
                fall_counter <= fall_counter + 1;
            else
                fall_counter <= 0;
        end
    end

    // Outputs
    assign walk_left  = (current_state == WALK_LEFT);
    assign walk_right = (current_state == WALK_RIGHT);
    assign aaah       = (current_state == FALL_LEFT || current_state == FALL_RIGHT);
    assign digging    = (current_state == DIG_LEFT || current_state == DIG_RIGHT);

endmodule

module top_module (
    input clk,
    input areset,
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output reg walk_left,
    output reg walk_right,
    output reg aaah,
    output reg digging
);

    typedef enum reg [2:0] {
        LEFT = 3'b000,
        RIGHT = 3'b001,
        FALL_LEFT = 3'b010,
        FALL_RIGHT = 3'b011,
        DIG_LEFT = 3'b100,
        DIG_RIGHT = 3'b101,
        SPLATTERED = 3'b110
    } state_t;

    state_t state, next_state;
    reg [4:0] fall_counter; // 5 bits to count from 0 to 31

    // State transition
    always @(*) begin
        case (state)
            LEFT: begin
                if (!ground)
                    next_state = FALL_LEFT;
                else if (dig)
                    next_state = DIG_LEFT;
                else if (bump_left || bump_right)
                    next_state = RIGHT;
                else
                    next_state = LEFT;
            end
            RIGHT: begin
                if (!ground)
                    next_state = FALL_RIGHT;
                else if (dig)
                    next_state = DIG_RIGHT;
                else if (bump_left || bump_right)
                    next_state = LEFT;
                else
                    next_state = RIGHT;
            end
            FALL_LEFT: begin
                if (ground && fall_counter > 20)
                    next_state = SPLATTERED;
                else if (ground)
                    next_state = LEFT;
                else
                    next_state = FALL_LEFT;
            end
            FALL_RIGHT: begin
                if (ground && fall_counter > 20)
                    next_state = SPLATTERED;
                else if (ground)
                    next_state = RIGHT;
                else
                    next_state = FALL_RIGHT;
            end
            DIG_LEFT: begin
                if (!ground)
                    next_state = FALL_LEFT;
                else
                    next_state = DIG_LEFT;
            end
            DIG_RIGHT: begin
                if (!ground)
                    next_state = FALL_RIGHT;
                else
                    next_state = DIG_RIGHT;
            end
            SPLATTERED: begin
                next_state = SPLATTERED;
            end
            default: begin
                next_state = LEFT;
            end
        endcase
    end

    // State flip-flops
    always @(posedge clk or posedge areset) begin
        if (areset) begin
            state <= LEFT;
            fall_counter <= 0;
        end else begin
            state <= next_state;
            if (state == FALL_LEFT || state == FALL_RIGHT)
                fall_counter <= fall_counter + 1;
            else
                fall_counter <= 0;
        end
    end

    // Output logic
    always @(*) begin
        walk_left = 0;
        walk_right = 0;
        aaah = 0;
        digging = 0;
        case (state)
            LEFT: walk_left = 1;
            RIGHT: walk_right = 1;
            FALL_LEFT, FALL_RIGHT: aaah = 1;
            DIG_LEFT: begin
                digging = 1;
                walk_left = 1;
            end
            DIG_RIGHT: begin
                digging = 1;
                walk_right = 1;
            end
            SPLATTERED: begin
                walk_left = 0;
                walk_right = 0;
                aaah = 0;
                digging = 0;
            end
        endcase
    end

endmodule