HDLBits之Lemmings2

See also: Lemmings1.

In addition to walking left and right, Lemmings will fall (and presumably go "aaah!") if the ground disappears underneath them.

In addition to walking left and right and changing direction when bumped, when ground=0, the Lemming will fall and say "aaah!". When the ground reappears (ground=1), the Lemming will resume walking in the same direction as before the fall. Being bumped while falling does not affect the walking direction, and being bumped in the same cycle as ground disappears (but not yet falling), or when the ground reappears while still falling, also does not affect the walking direction.

Build a finite state machine that models this behaviour.

HDLBits之Lemmings2_第1张图片

左撞右撞还能掉下来发出aaah的状态机

注意:在掉下来后判断往左还是往右使用sel标志位最后结果不对。右转掉下之后walk_left变成1了考虑是逻辑问题?

代码:

module top_module(
    input clk,
    input areset,    // Freshly brainwashed Lemmings walk left.
    input bump_left,
    input bump_right,
    input ground,
    output walk_left,
    output walk_right,
    output aaah ); 
    parameter L=2'b00,R=2'b01,DL=2'b10,DR=2'b11;
    reg [1:0]state,next_state;
    reg sel;
    always@(posedge clk or posedge areset)begin
        if(areset)
            state<=L;
        else
            state<=next_state;
    end
    always@(*)begin
        case(state)
            L:begin
                if(ground==1'b0)begin
                    next_state=DL;
                    //sel=1'b0;
                end
                else begin
                    next_state=bump_left?R:L;
                    //sel=sel;
                end
            end
            R:begin
                if(ground==1'b0)begin
                    next_state=DR;
                    //sel=1'b1;
                end
                else begin
                    next_state=bump_right?L:R;
                    //sel=sel;
                end
            end
            DL:begin
                if(ground==1'b0)
                    next_state=DL;
                else 
                    next_state=L;
                   // next_state=sel?R:L;
            end
            DR:begin
                if(ground==1'b0)
                    next_state=DR;
                else 
                    next_state=R;
                   // next_state=sel?R:L;
            end
        endcase
    end
    always@(*)begin
        if(state==DL|state==DR)begin
            aaah=1'b1;
           walk_left=1'b0;
           walk_right=1'b0;
       end
        else begin
            aaah=1'b0;
           walk_left=state==L?1'b1:1'b0;
           walk_right=state==R?1'b1:1'b0;
        end
    end
            

endmodule

 

你可能感兴趣的:(寒假爆肝fpga,fpga开发)