HDLbits---Lemmings3

HDLbits---Lemmings3

作者说如果旅鼠在地面上(ground = 1)并且挖掘信号为1,则它可以开始挖洞,一直挖,直到把地都挖穿了(ground = 0),此时,旅鼠就掉下去了,并且发出大叫。一开始以为dig会一直保持,结果只有一个周期的高电平,而只有当ground = 0时,才会从挖掘状态跳变成别的状态。

module top_module(
    input clk,
    input areset,    // Freshly brainwashed Lemmings walk left.
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output walk_left,
    output walk_right,
    output aaah,
    output digging ); 
	parameter LEFT = 3'd0, RIGHT = 3'd1, GROUND_LEFT = 3'd2, GROUND_RIGHT = 3'd3,DIG_LEFT = 3'd4,DIG_RIGHT = 3'd5;
    reg	[2:0]	current_state;
    reg [2:0]	next_state;
    always@(posedge clk or posedge areset )begin
        if(areset)begin
            current_state = LEFT;
        end
        else begin
            current_state = next_state;
        end
    end

always@(*)begin
    case(current_state)
        LEFT:begin
            if(!ground)begin
                next_state = GROUND_LEFT;
            end
            else begin
                case({dig,bump_left})
                    2'b00:next_state = current_state;
                    2'b01:next_state = RIGHT;
                    2'b11:next_state = DIG_LEFT;
                    2'b10:next_state = DIG_LEFT;
                    default:next_state = current_state;
                endcase
            end
        end
        RIGHT:begin
            if(!ground)begin
                next_state = GROUND_RIGHT;
            end
            else begin
                case({dig,bump_right})
                    2'b00:next_state = current_state;
                    2'b01:next_state = LEFT;
                    2'b11:next_state = DIG_RIGHT;
                    2'b10:next_state = DIG_RIGHT;
                    default:next_state = current_state;
                endcase
            end
        end
        GROUND_LEFT:begin
            if(!ground)begin
                next_state = GROUND_LEFT;
            end
            else begin    
                next_state = LEFT;
            end
        end
        GROUND_RIGHT:begin
            if(!ground)begin
                next_state = GROUND_RIGHT;
            end
            else begin    
                next_state = RIGHT;
            end
        end
        DIG_LEFT:begin
            if(!ground)begin
                next_state = GROUND_LEFT;
            end
            else begin
                next_state = current_state;
            end
        end
        DIG_RIGHT:begin
            if(!ground)begin
                next_state = GROUND_RIGHT;
            end
            else begin
                next_state = current_state;
            end
        end
        default:next_state = current_state;
    endcase
end
assign walk_left = (current_state == LEFT);
assign walk_right = (current_state == RIGHT);
assign aaah = (current_state == GROUND_LEFT || current_state == GROUND_RIGHT);
assign digging = (current_state == DIG_LEFT || current_state == DIG_RIGHT);
endmodule

HDLbits---Lemmings3_第1张图片

你可能感兴趣的:(verilog)