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 );
reg [2:0]state,next_state;
parameter left=3'd0,right=3'd1,leftfall=3'd2,rightfall=3'd3,leftdig=3'd4,rightdig=3'd5,kaca=3'd6;
reg [4:0] j;
always@(posedge clk or posedge areset)
begin
if(areset)
state<=left;
else
state<=next_state;
end
always@(posedge clk or posedge areset) begin
if(areset) begin
j <= 5'd0;
end
else if(j==5'd20)
j<=j;
else if((state == leftfall) || (state == rightfall)) begin
j <= j + 1'b1;
end
else
j<=5'd0;
end
always@(*)
case(state)
left:begin
if(~ground)
next_state=leftfall;
else if(dig)
next_state=leftdig;
else if(bump_left)
next_state=right;
else
next_state=state;
end
right:begin
if(~ground)
next_state=rightfall;
else if(dig)
next_state=rightdig;
else if(bump_right)
next_state=left;
else
next_state=state;
end
leftfall:begin
if(ground)
begin
if(j==5'd20)
next_state=kaca;
else
next_state=left;
end
else
next_state=state;
end
rightfall:begin
if(ground)
begin
if(j==5'd20)
next_state=kaca;
else
next_state=right;
end
else
next_state=state;
end
leftdig:begin
if(~ground)
next_state=leftfall;
else
next_state=state;
end
rightdig:begin
if(~ground)
next_state=rightfall;
else
next_state=state;
end
kaca:next_state=state;
endcase
assign walk_left=(state==left);
assign walk_right=(state==right);
assign aaah=((state==leftfall)||(state==rightfall));
assign digging=((state==leftdig)||(state==rightdig));
endmodule
不太理解的一点:
always@(posedge clk or posedge areset) begin
if(areset) begin
j <= 5'd0;
end
else if((state == leftfall) || (state == rightfall)) begin
j <= j + 1'b1;
end
else
j<=5'd0;
end
如果去掉j==20的时候 j<=j ,在下面的rightfall和leftfall中判断条件改为j>=20,为什么是错误的。求解