题目:
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:
代码:
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,state_next;
reg [7:0] count;
parameter LEFT=0,RIGHT=1,DIG_L=2,DIG_R=3,G_L=4,G_R=5,OVER=6;
reg [3:0] out;
always @(posedge clk or posedge areset)
begin
if(areset)
state <= LEFT;
else
state <= state_next;
end
always @(posedge clk or posedge areset)
begin
if(areset)
count <= 0;
else
begin
if(!ground)
count <= count + 1 ;
else
count <= 0;
end
end
always @(*)
begin
case(state)
LEFT: state_next= ground?(dig?DIG_L:(bump_left?RIGHT:LEFT)):G_L;
RIGHT: state_next= ground?(dig?DIG_R:(bump_right?LEFT:RIGHT)):G_R;
DIG_L: state_next= ground?DIG_L:G_L;
DIG_R: state_next= ground?DIG_R:G_R;
G_L: state_next= ground?((count>=5'd21)?OVER:LEFT):G_L;
G_R: state_next= ground?((count>=5'd21)?OVER:RIGHT):G_R;
OVER: state_next= OVER;
endcase
end
always @(posedge clk or posedge areset) begin
if(areset == 1'b1) begin
out <= 4'b1000;
end
else begin
case(state_next)
LEFT :out <= 4'b1000;
RIGHT :out <= 4'b0100;
G_R :out <= 4'b0010;
G_L :out <= 4'b0010;
DIG_R :out <= 4'b0001;
DIG_L :out <= 4'b0001;
OVER :out <= 4'b0000;
default:out <= 4'b1000;
endcase
end
end
assign {walk_left,walk_right,aaah,digging}=out;
endmodule