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.
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 g1right = 3, g1left = 2, g0right = 1, g0left = 0, digright = 4, digleft = 5, splatter = 6;
//规定7个状态,相比于lemmings3,多加一个splatter状态,意味着下落超过20个周期
reg [2:0] state, next;
reg [10:0] sum; //对下落周期计数,由于下落周期数无上限,所以sum需要取值足够大
always @(*)begin
case (state)
g0left: next = (ground == 1) ? ((sum > 20) ? splatter : g1left) : g0left;
//如果下落时间超过20个周期,进入splatter状态,其他情况与lemmings3没有差别
g0right: next = (ground == 1) ? ((sum > 20) ? splatter : g1right) : g0right;
g1left: begin
if (ground == 0) next = g0left;
else if (dig == 1) next = digleft;
else begin next = (bump_left == 1) ? g1right : g1left; end
end
g1right: begin
if (ground == 0) next = g0right;
else if (dig == 1) next = digright;
else begin next = (bump_right == 1) ? g1left : g1right; end
end
digleft: begin
if (ground == 0) next = g0left;
else next = digleft;
end
digright: begin
if (ground == 0) next = g0right;
else next = digright;
end
endcase
end
always @(posedge clk or posedge areset) begin
if (areset == 1) begin state = g1left; sum = 0; end
else if (next == g0left || next == g0right) begin sum++; state = next; end
//在判断areset后,计算下落周期数
else begin state = next; sum = 0; end //非下落状态将周期计数清零
end
assign walk_left = (state == splatter) ? 0 : (state == g1left);
//如果进入splatter状态,小鼠死亡,所有输出清零
assign walk_right = (state == splatter) ? 0 : (state == g1right);
assign digging = (state == splatter) ? 0 : (state == digleft | state == digright);
assign aaah = (state == splatter) ? 0 : (state == g0left | state == g0right);
endmodule