128 lemmings1
The game Lemmings involves critters with fairly simple brains. So simple that we are going to model it using a finite state machine.
In the Lemmings' 2D world, Lemmings can be in one of two states: walking left or walking right. It will switch directions if it hits an obstacle. In particular, if a Lemming is bumped on the left, it will walk right. If it's bumped on the right, it will walk left. If it's bumped on both sides at the same time, it will still switch directions.
Implement a Moore state machine with two states, two inputs, and one output that models this behaviour.
See also: Lemmings2, Lemmings3, and Lemmings4.
Hint...
module top_module(
input clk,
input areset, // Freshly brainwashed Lemmings walk left.
input bump_left,
input bump_right,
output walk_left,
output walk_right); //
// parameter LEFT=0, RIGHT=1, ...
parameter LEFT=1'd0, RIGHT=1'd1;
reg state, next_state;
always @(*) begin
// State transition logic
next_state = state;
case(state)
LEFT :begin
if(bump_left)begin
next_state=RIGHT;
end
end
RIGHT :begin
if(bump_right)begin
next_state=LEFT;
end
end
endcase
end
always @(posedge clk, posedge areset) begin
// State flip-flops with asynchronous reset
if(areset)begin
state <= LEFT;
end
else begin
state <= next_state;
end
end
// Output logic
// assign walk_left = (state == ...);
// assign walk_right = (state == ...);
assign walk_left = (state == LEFT);
assign walk_right = (state == RIGHT);
endmodule
129 lemmings2
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.
Hint...
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 aaah );
// parameter LEFT=0, RIGHT=1, ...
parameter LEFT=2'd0, RIGHT=2'd1,FALL_L=2'd2,FALL_R=2'd3;
reg [1:0]state, next_state;
always @(*) begin
// State transition logic
next_state = state;
case(state)
LEFT :begin
if(~ground)begin
next_state=FALL_L;
end
else if(bump_left)begin
next_state=RIGHT;
end
end
RIGHT :begin
if(~ground)begin
next_state=FALL_R;
end
else if(bump_right)begin
next_state=LEFT;
end
end
FALL_L:begin
if(ground)begin
next_state=LEFT;
end
end
FALL_R:begin
if(ground)begin
next_state=RIGHT;
end
end
endcase
end
always @(posedge clk, posedge areset) begin
// State flip-flops with asynchronous reset
if(areset)begin
state <= LEFT;
end
else begin
state <= next_state;
end
end
// Output logic
// assign walk_left = (state == ...);
// assign walk_right = (state == ...);
assign walk_left = (state == LEFT);
assign walk_right = (state == RIGHT);
assign aaah = (state == FALL_L)|((state == FALL_R));
endmodule
130 lemmings3
See also: Lemmings1 and Lemmings2.
In addition to walking and falling, Lemmings can sometimes be told to do useful things, like dig (it starts digging when dig=1). A Lemming can dig if it is currently walking on ground (ground=1 and not falling), and will continue digging until it reaches the other side (ground=0). At that point, since there is no ground, it will fall (aaah!), then continue walking in its original direction once it hits ground again. As with falling, being bumped while digging has no effect, and being told to dig when falling or when there is no ground is ignored.
(In other words, a walking Lemming can fall, dig, or switch directions. If more than one of these conditions are satisfied, fall has higher precedence than dig, which has higher precedence than switching directions.)
Extend your finite state machine to model this behaviour.
See also: Lemmings4.
Hint...
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 LEFT=0, RIGHT=1, ...
parameter LEFT=3'd0, RIGHT=3'd1,FALL_L=3'd2,FALL_R=3'd3,DIG_L=3'd4,DIG_R=3'd5;
reg [2:0]state, next_state;
always @(*) begin
// State transition logic
next_state = state;
case(state)
LEFT :begin
if(~ground)begin
next_state=FALL_L;
end
else if(dig)begin
next_state=DIG_L;
end
else if(bump_left)begin
next_state=RIGHT;
end
end
RIGHT :begin
if(~ground)begin
next_state=FALL_R;
end
else if(dig)begin
next_state=DIG_R;
end
else if(bump_right)begin
next_state=LEFT;
end
end
FALL_L:begin
if(ground)begin
next_state=LEFT;
end
end
FALL_R:begin
if(ground)begin
next_state=RIGHT;
end
end
DIG_L:begin
if(!ground)begin
next_state=FALL_L;
end
end
DIG_R:begin
if(!ground)begin
next_state=FALL_R;
end
end
endcase
end
always @(posedge clk, posedge areset) begin
// State flip-flops with asynchronous reset
if(areset)begin
state <= LEFT;
end
else begin
state <= next_state;
end
end
// Output logic
// assign walk_left = (state == ...);
// assign walk_right = (state == ...);
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
131 lemmings4
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.
Hint...
Use the FSM to control a counter that tracks how long the Lemming has been falling.
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 LEFT=0, RIGHT=1, ...
parameter LEFT=3'd0, RIGHT=3'd1,FALL_L=3'd2,FALL_R=3'd3,DIG_L=3'd4,DIG_R=3'd5,SPLAT=3'd6;
reg [2:0]state, next_state;
reg [7:0] fall_cnt,fall_cnt_next;
always @(*) begin
// State transition logic
next_state = state;
fall_cnt_next = fall_cnt;
case(state)
LEFT :begin
fall_cnt_next = 8'd0;
if(~ground)begin
next_state=FALL_L;
end
else if(dig)begin
next_state=DIG_L;
end
else if(bump_left)begin
next_state=RIGHT;
end
end
RIGHT :begin
fall_cnt_next = 8'd0;
if(~ground)begin
next_state=FALL_R;
end
else if(dig)begin
next_state=DIG_R;
end
else if(bump_right)begin
next_state=LEFT;
end
end
FALL_L:begin
fall_cnt_next = fall_cnt + 1'd1;
if(ground)begin
if(fall_cnt>=8'd20)begin
next_state=SPLAT;
end
else begin
next_state=LEFT;
end
end
end
FALL_R:begin
fall_cnt_next = fall_cnt + 1'd1;
if(ground)begin
if(fall_cnt>=8'd20)begin
next_state=SPLAT;
end
else begin
next_state=RIGHT;
end
end
end
DIG_L:begin
if(!ground)begin
next_state=FALL_L;
end
end
DIG_R:begin
if(!ground)begin
next_state=FALL_R;
end
end
SPLAT:begin
next_state=SPLAT;
end
endcase
end
always @(posedge clk, posedge areset) begin
// State flip-flops with asynchronous reset
if(areset)begin
state <= LEFT;
fall_cnt <= 8'd0;
end
else begin
state <= next_state;
fall_cnt <= fall_cnt_next;
end
end
// Output logic
// assign walk_left = (state == ...);
// assign walk_right = (state == ...);
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