verilog HDLBits刷题[Finite State Machines]“Lemmings3”---Lemmings3

📅 2026/7/30 5:23:19
verilog HDLBits刷题[Finite State Machines]“Lemmings3”---Lemmings3
1、题目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 dig1). A Lemming can dig if it is currently walking on ground (ground1 and not falling), and will continue digging until it reaches the other side (ground0). 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.)Extend your finite state machine to model this behaviour.2、分析旅鼠3旅鼠可以左右前进碰到障碍物反向前进可以挖掘直到地面下降才前进按照之前的方向地面下降会发出“啊啊啊”的声音地面上升后又按照之前的方向前进。跌倒优先级高于挖掘而挖掘优先级高于切换方向3、代码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 left3b000,right3b001,dig_treasure_left3b010,dig_treasure_right3b011,fall_left3b100,fall_right3b101; reg [2:0]state,next_state; always(posedge clk,posedge areset)begin if(areset) stateleft; else statenext_state; end always(*)begin next_statestate; case(state) left:begin if(!ground) next_statefall_left; else if(dig) next_statedig_treasure_left; else if(bump_left) next_stateright; end right:begin if(!ground) next_statefall_right; else if(dig) next_statedig_treasure_right; else if(bump_right) next_stateleft; end dig_treasure_left:begin if(!ground) next_statefall_left; end dig_treasure_right:begin if(!ground) next_statefall_right; end fall_left:begin if(ground) next_stateleft; end fall_right:begin if(ground) next_stateright; end default:next_stateleft; endcase end always(*)begin case(state) left:{walk_left,walk_right,aaah,digging}4b1000; right:{walk_left,walk_right,aaah,digging}4b0100; dig_treasure_left:{walk_left,walk_right,aaah,digging}4b0001; dig_treasure_right:{walk_left,walk_right,aaah,digging}4b0001; fall_left:{walk_left,walk_right,aaah,digging}4b0010; fall_right:{walk_left,walk_right,aaah,digging}4b0010; default:{walk_left,walk_right,aaah,digging}4b0000; endcase end endmodule4、结果