verilog HDLBits刷题[Finite State Machines]“Lemmings4”---Lemmings4
2026/7/29 23:49:26 网站建设 项目流程

1、题目

See also: Lemmings1, Lemmings2, and Lemmings3.

Although Lemmings can walk, fall, and dig, Lemmings aren't invulnerable. If a Lemming falls for too long then hits the ground, it can splatter. In particular, if a Lemming falls for more than 20 clock cycles then hits the ground, it will splatter and cease walking, falling, or digging (all 4 outputs become 0), forever (Or until the FSM gets reset). There is no upper limit on how far a Lemming can fall before hitting the ground. Lemmings only splatter when hitting the ground; they do not splatter in mid-air.

Extend your finite state machine to model this behaviour.

Falling for 20 cycles is survivable:

2、代码

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 ); localparam LEFT = 1, RIGHT = 2, FALL_L = 3, FALL_R = 4, DIG_L = 5, DIG_R = 6, SPLAT = 7; reg [2:0] state; reg [2:0] next_state; reg [6:0] cnt; // watch out this! always @(posedge clk , posedge areset)begin if(areset) state <= LEFT; else state <= next_state; end always @(posedge clk or posedge areset)begin if(areset) cnt <= 7'd0; else if(state == FALL_L || state == FALL_R) cnt <= cnt + 1'b1; else cnt <= 7'd0; end always @(*) case(state) LEFT : next_state = ~ground ? FALL_L : (dig ? DIG_L : (bump_left ? RIGHT : LEFT)); RIGHT : next_state = ~ground ? FALL_R : (dig ? DIG_R : (bump_right ? LEFT : RIGHT)); FALL_L : next_state = ground ? (cnt >= 5'd20 ? SPLAT : LEFT) : FALL_L; FALL_R : next_state = ground ? (cnt >=5'd20 ? SPLAT : RIGHT) : FALL_R; DIG_L : next_state = ~ground ? FALL_L : DIG_L; DIG_R : next_state = ~ground ? FALL_R : DIG_R; SPLAT : next_state = SPLAT; endcase 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

3、结果

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询