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

📅 2026/7/30 3:58:40 👁️ 阅读次数 📝 编程学习
verilog HDLBits刷题[Finite State Machines]“Lemmings2”---Lemmings2

1、题目

See also: Lemmings1.

In addition to walking left and right, Lemmings will fall (and presumably go "aaah!") if the ground disappears underneath them.

In addition to walking left and right and changing direction when bumped, when ground=0, the Lemming will fall and say "aaah!". When the ground reappears (ground=1), the Lemming will resume walking in the same direction as before the fall. Being bumped while falling does not affect the walking direction, and being bumped in the same cycle as ground disappears (but not yet falling), or when the ground reappears while still falling, also does not affect the walking direction.

Build a finite state machine that models this behaviour.

See also: Lemmings3 and Lemmings4.

2、分析

旅鼠游戏2:旅鼠可以左右行走,如果地面消失于脚下,它们会摔跤并发出“啊啊啊”的声音,;如果地面重新出现,它们就以之前的方向继续前进。注意掉落前的状态为左还是左,如此看来,可知有四种情况:向左,向右,掉落前向左,掉落前向右。

3、代码

module top_module( input clk, input areset, // Freshly brainwashed Lemmings walk left. input bump_left, input bump_right, input ground, output walk_left, output walk_right, output reg aaah ); parameter LEFT=2'b00,RIGHT=2'b01,fall_left=2'b10,fall_right=2'b11; reg [1:0]state,next_state; always@(*)begin next_state = state; case(state) LEFT:begin if(!ground) next_state=fall_left; else begin if(bump_left) next_state=RIGHT; else next_state=LEFT; end end RIGHT:begin if(!ground) next_state=fall_right; else begin if(bump_right) next_state=LEFT; else next_state=RIGHT; end end fall_left:begin if(ground) next_state=LEFT; else next_state=fall_left; end fall_right:begin if(ground) next_state=RIGHT; else next_state=fall_right; end default:next_state=LEFT; endcase end always@(posedge clk,posedge areset)begin if(areset) state<=LEFT; else state<=next_state; end always@(posedge clk,posedge areset)begin if(areset) aaah<=1'b0; else if(ground==1'b0) aaah<=1'b1; else aaah<=1'b0; end assign walk_left=(state==LEFT); assign walk_right=(state==RIGHT); endmodule

4、结果