3.2.5.12 Lemmings 3
问题描述
除了走路和摔倒之外,旅鼠有时会被告知做一些有用的事情,比如挖掘(当dig=1时它开始挖掘)。如果旅鼠当前在地面上行走(ground=1并且没有下落),它可以挖掘,并且会继续挖掘直到它到达另一边(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 [2:0] left=0,right=1,fall_l=2,fall_r=3,dig_l=4,dig_r=5;
reg [2:0] state,next_state;
always @(*) begin
case(state)
left:begin
if(!ground) next_state=fall_l;
else if(dig) next_state=dig_l;
else if(bump_left) next_state=right;
else next_state=left;
end
right:begin
if(!ground) next_state=fall_r;
else if(dig) next_state=dig_r;
else if(bump_right) next_state=left;
else next_state=right;
end
fall_l:begin
if(ground) next_state=left;
else next_state=fall_l;
end
fall_r:begin
if(ground) next_state=right;
else next_state=fall_r;
end
dig_l:begin
if(!ground) next_state=fall_l;
else next_state=dig_l;
end
dig_r:begin
if(!ground) next_state=fall_r;
else next_state=dig_r;
end
endcase
end
always @(posedge clk or posedge areset) begin
if(areset) state<=left;
else state<=next_state;
end
assign walk_left=(state==left);
assign walk_right=(state==right);
assign aaah=(state==fall_l || state==fall_r);
assign digging=(state==dig_l || state==dig_r);
endmodule