HDLBits—Lemmings4

虽然旅鼠可以走路、摔倒和挖掘,但旅鼠并非无懈可击。如果旅鼠坠落太久然后撞到地面,它可能会飞溅。特别是,如果一只旅鼠坠落超过20个时钟周期,然后撞到地面,它将飞溅并停止行走,坠落或挖掘(所有4个输出变为0),永远(或直到FSM被重置)。旅鼠在落地前可以跌落多远没有上限。旅鼠只有在撞击地面时才会飞溅,它们不会在半空中飞溅。

HDLBits—Lemmings4_第1张图片

基本上和Lemmings3都一样,但是多了个计下落时钟数,关键点在于计数用的reg位数设置,由于不知道要计数到多少,最好直接用integer(32位)就行了

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 ); 
    reg[2:0] state,next_state,before_state;
    integer count;
    
    always@(*)begin
        case(state)
            0:begin
                walk_left = 1;
                walk_right = 0;
                aaah = 0;
                digging = 0;
                if(!ground)begin
                   next_state = 3; 
                   before_state = state;
                end
                else begin
                    if(dig)begin
                       next_state = 2;
                       before_state = state;
                    end
                    else begin
                       before_state = 0;
                        if(bump_left)begin
                           next_state = 1; 
                        end
                        else begin
                            next_state = 0; 
                        end
                    end
                end
            end
            1:begin
                walk_left = 0;
                walk_right = 1;
                aaah = 0;
                digging = 0;
                if(!ground)begin
                   next_state = 3; 
                   before_state = state;
                end
                else begin
                    if(dig)begin
                       next_state = 2;
                       before_state = state;
                    end
                    else begin
                        before_state = 0;
                        if(bump_right)begin
                           next_state = 0; 
                        end
                        else begin
                           next_state = 1; 
                        end
                    end
                end
            end
            2:begin
                walk_left = 0;
                walk_right = 0;
                aaah = 0;
                digging = 1;
                if(!ground)begin
                    next_state = 3; 
                end
                else begin
                    next_state = 2; 
                end
            end
            3:begin
                walk_left = 0;
                walk_right = 0;
                aaah = 1;
                digging = 0;
                if(ground)begin
                    if(count>=20)begin
                        next_state = 4;
                    end
                    else begin
                        next_state = before_state; 
                    end
                end
                else begin
                    next_state = 3;
                end
            end
            4:begin
                walk_left = 0;
                walk_right = 0;
                aaah = 0;
                digging = 0;
                next_state = 4;
            end
        endcase
    end
    always@(posedge clk or posedge areset)begin
        if(areset)begin
           state<=0; 
           count<=0;
        end
        else begin
            if(state==3)begin
                count<=count+1;
            end
            else begin
                count<=0;
            end
            state<=next_state;
        end
    end
endmodule

你可能感兴趣的:(其他)