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
完美,終於可以完全自己寫出來了,理解了邏輯就好辦了