Lemmings 3

See also: Lemmings1 and Lemmings2.

In addition to walking and falling, Lemmings can sometimes be told to do useful things, like dig (it starts digging when dig=1). A Lemming can dig if it is currently walking on ground (ground=1 and not falling), and will continue digging until it reaches the other side (ground=0). At that point, since there is no ground, it will fall (aaah!), then continue walking in its original direction once it hits ground again. As with falling, being bumped while digging has no effect, and being told to dig when falling or when there is no ground is ignored.

(In other words, a walking Lemming can fall, dig, or switch directions. If more than one of these conditions are satisfied, fall has higher precedence than dig, which has higher precedence than switching directions.)

参见:Lemmings1 和 Lemmings2。

除了走路和跌倒之外,旅鼠有时还可以被告知做一些有用的事情,比如挖掘(当 dig=1 时它开始挖掘)。如果旅鼠目前在地面上行走(地面=1且没有坠落),则可以挖掘,并将继续挖掘,直到到达另一侧(地面=0)。在这一点上,由于没有地面,它会掉下来(啊!),然后在它再次落地后继续向原来的方向行走。与跌倒一样,在挖掘时被撞到是没有效果的,在跌倒或没有地面时被告知挖掘是被忽略的。

(换句话说,行走的旅鼠可以跌倒、挖掘或改变方向。如果满足这些条件中的多个条件,则 fall 的优先级高于 dig,而 dig 的优先级高于切换方向的优先级。

扩展有限状态机以对此行为进行建模。
题目网站
状态转换如下:
啊

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=6'b000_001,
    		  RIGHT=6'b000_010,
    		  FALL_L=6'b000_100,
    		  FALL_R=6'b001_000,
    		  DIG_L=6'b010_000,
    		  DIG_R=6'b100_000;
    reg [5:0]state,nstate;
    reg [3:0]judge;
    assign judge={ground,dig,bump_left,bump_right};
    
    always@(*)begin
        case(state)
            LEFT:begin
                casex(judge)
                    4'b0xxx:nstate=FALL_L;
                    4'b11xx:nstate=DIG_L;
                    4'b101x:nstate=RIGHT;
                    default:nstate=LEFT;
                endcase
            end
            RIGHT:begin
                casex(judge)
                    4'b0xxx:nstate=FALL_R;
                    4'b11xx:nstate=DIG_R;
                    4'b1001:nstate=LEFT;
                    default:nstate=RIGHT;
                endcase
            end
            FALL_L:begin
                casex(judge)
                    4'b1xxx:nstate=LEFT;
                    default:nstate=FALL_L;
                endcase
            end
            FALL_R:begin
                casex(judge)
                    4'b1xxx:nstate=RIGHT;
                    default:nstate=FALL_R;
                endcase
            end
            DIG_L:begin
                casex(judge)
                    4'b0xxx:nstate=FALL_L;
                    default:nstate=DIG_L;
                endcase
            end
            DIG_R:begin
                casex(judge)
                    4'b0xxx:nstate=FALL_R;
                    default:nstate=DIG_R;
                endcase
            end
        endcase
    end
    
    always@(posedge clk or posedge areset)begin
        if(areset)begin
            state<=LEFT;
        end
        else begin
            state<=nstate;
        end
    end
    
    assign walk_left=(state==LEFT);
    assign walk_right=(state==RIGHT);
    assign digging=(state==DIG_L||state==DIG_R);
    assign aaah=(state==FALL_L||state==FALL_R);
            
endmodule

完美,终于可以完全自己写出来了,理解了逻辑就好办了

posted @ 2024-04-15 00:42  江左子固  阅读(37)  评论(0编辑  收藏  举报