Lemmings is a puzzle strategy game developed in 1991. The main objective of the game is to guide the lemmings to an exit. The game mechanics of Lemmings is pretty easy and straightforward, so we will develop the entire Lemmings game in Verilog.
Firstly let us design the movement mechanics. The game is 2D so the motion can only be along a particular axis in a plane. Let us focus on the horizontal motion of the lemmings for the time being. Now a lemming can walk either left or right. While walking right if it faces an obstacle it will turn left, and in the same way while walking left if it faces an obstacle it will turn right. Now we will consider that lemmings walk left when they are first dropped in.
We will now design a moore machine to implement this above logic.
module top_module(
input clk,
input areset,
input bump_left,
input bump_right,
output walk_left,
output walk_right);
parameter LEFT=0, RIGHT=1;
reg state, next_state;
always @(*) begin
case(state)
LEFT:next_state=(bump_left)?RIGHT:LEFT;
RIGHT:next_state=(bump_right)?LEFT:RIGHT;
endcase
end
always @(posedge clk, posedge areset) begin
if(areset)
state<=LEFT;
else
state<=next_state;
end
assign walk_left=(state==LEFT);
assign walk_right=(state==RIGHT);
endmodule
The verilog simulation output looks like this
Now we all have probably read quotes like
"Many of those engaged in a lemming-like march to the sea are proud of their individualism." ~ Neal A. Maxwell
"I've probably said a million times in my life something about, "All those people are just lemmings. They'd follow each other off a cliff." Well, no such thing." ~ Cassandra Peterson
Also we must have seen art works and comic strips comparing human herd mentality to the lemming's.
Lemmings are know to commit mass suicide by jumping off sea cliffs. However this is basically a myth. Every few years, Lemming's population increases drastically and to survive they jump off the cliff by the sea to swim to a new place where there is not a huge competition for resources.
Now in the lemmings game too, there is a mechanics of falling, just like in the real world. If the ground surface is removed from the feet of the lemmings it will fall and scream out "aaah!!!!". When it finds the ground again after falling for a certain time it will again start walking again. Lemmings do not change directions if they are bump into left right obstacles midflight. We will represent no ground as logic 0 and logic 1 for when ground reappears again. The left and right motion will be same as before. We will also represent the "aaah!!!!" scream of the falling lemming as an output state.
module top_module(
input clk,
input areset,
input bump_left,
input bump_right,
input ground,
output walk_left,
output walk_right,
output aaah );
parameter LEFT=0, RIGHT=1,FALLING_LEFT=2,FALLING_RIGHT=3;
reg [1:0] state, next_state;
always @(*) begin
case(state)
LEFT: next_state=(~ground) ? FALLING_LEFT : ((bump_left)?RIGHT:LEFT);
RIGHT: next_state=(~ground) ? FALLING_RIGHT : ((bump_right )?LEFT:RIGHT);
FALLING_LEFT: next_state=(ground) ? LEFT : FALLING_LEFT;
FALLING_RIGHT: next_state=(ground) ? RIGHT : FALLING_RIGHT;
endcase
end
always @(posedge clk, posedge areset) begin
if(areset)
state<=LEFT;
else
state<=next_state;
end
assign walk_left=(state==LEFT);
assign walk_right=(state==RIGHT);
assign aaah=(state==FALLING_LEFT || state==FALLING_RIGHT);
endmodule
The verilog simulation output looks like this
Hurrah!!!!!!!! we have completed nearly the entire 2D movement mechanics of the lemmings game
Now apart from movement mechanics, Lemmings also can do certain special tasks. One of them is digging. Now its pretty evident that a Lemming can dig only when it is on ground and not in a falling state. Various interesting scenarios may arise when we introduce this new mechanics. The lemmings can fall after digging a hole, then after sometime may find flat land and again start digging or walking. A lemming that was walking left, can start digging, falls and again starts walking left. There can be many scenarios possible for combination of all the states.
So now we will incorporate this digging mechanics into our Lemmings code
module top_module(
input clk,
input areset,
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,FALLING_LEFT=2,FALLING_RIGHT=3,DIGGING_LEFT=4,DIGGING_RIGHT=5;
reg [2:0] state, next_state;
always @(*) begin
case(state)
LEFT: next_state=(~ground) ? FALLING_LEFT : ((dig)? DIGGING_LEFT:((bump_left)?RIGHT:LEFT));
RIGHT: next_state=(~ground) ? FALLING_RIGHT : ((dig)? DIGGING_RIGHT:((bump_right )?LEFT:RIGHT));
FALLING_LEFT: next_state=(ground) ? LEFT : FALLING_LEFT;
FALLING_RIGHT: next_state=(ground) ? RIGHT : FALLING_RIGHT;
DIGGING_LEFT: next_state=(ground) ? DIGGING_LEFT : FALLING_LEFT;
DIGGING_RIGHT: next_state=(ground) ? DIGGING_RIGHT : FALLING_RIGHT;
endcase
end
always @(posedge clk, posedge areset) begin
if(areset)
state<=LEFT;
else
state<=next_state;
end
assign walk_left=(state==LEFT);
assign walk_right=(state==RIGHT);
assign aaah=(state==FALLING_LEFT || state==FALLING_RIGHT);
assign digging=(state==DIGGING_LEFT || state==DIGGING_RIGHT);
endmodule
The verilog simulation output looks like this
Now, Lemmings cannot survive jump for large heights. Here we will consider that the lemming will be alive if they reach the ground by the 20 clock cycle or else they will splatter
Quoting from the Britannica
Lemmings are small creatures with wild reputations. In the 17th century, naturalists perplexed by the habit of Norway lemmings to suddenly appear in large numbers, seemingly out of nowhere, came to the conclusion that the animals were being spontaneously generated in the sky and then falling to earth like rain. (The prosaic truth is that they migrate in herds.) Some people also thought that lemmings explode if they become sufficiently angry. This is also a myth, of course—lemmings are indeed one of the more irascible rodents, but they mostly channel their rage into fights with other lemmings. People probably came up with the notion of exploding lemmings after seeing the picked-over lemming carcasses that were left behind following a migration.
Thus we are introducing a new state called "splat" which defines this phenomenon of explosion of the lemming when it falls from a large height.
module counter (
input clk,
input reset,
input areset,
output signal);
reg [5:0] count;
always@(posedge clk)begin
if(areset || (reset && count<6'd19) )
begin
count=6'd0;
signal=0;
end
else if(~reset && count<6'd19)
begin
count++;
signal=0;
end
else if(reset && count>=6'd19)
begin
count=6'd0;
signal=1;
end
else
signal=1;
end
endmodule
module top_module(
input clk,
input areset,
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,FALLING_LEFT=2,FALLING_RIGHT=3,DIGGING_LEFT=4,DIGGING_RIGHT=5,SPLAT=6;
reg [2:0] state, next_state;
wire splat_left,splat_right;
counter A(clk,~(state==FALLING_LEFT),areset,splat_left);
counter B(clk,~(state==FALLING_RIGHT),areset,splat_right);
always @(*) begin
case(state)
LEFT: next_state=(~ground) ? FALLING_LEFT : ((dig)? DIGGING_LEFT:((bump_left)?RIGHT:LEFT));
RIGHT: next_state=(~ground) ? FALLING_RIGHT : ((dig)? DIGGING_RIGHT:((bump_right )?LEFT:RIGHT));
FALLING_LEFT: next_state=(ground) ? ((splat_left==1)? SPLAT:LEFT) : FALLING_LEFT;
FALLING_RIGHT: next_state=(ground) ? ((splat_right==1)? SPLAT:RIGHT): FALLING_RIGHT;
DIGGING_LEFT: next_state=(ground) ? DIGGING_LEFT : FALLING_LEFT;
DIGGING_RIGHT: next_state=(ground) ? DIGGING_RIGHT : FALLING_RIGHT;
SPLAT : next_state=SPLAT;
endcase
end
always @(posedge clk, posedge areset) begin
if(areset==1)
state<=LEFT;
else
state<=next_state;
end
assign walk_left=(state==LEFT)&&~(state==SPLAT);
assign walk_right=(state==RIGHT)&&~(state==SPLAT);
assign aaah=(state==FALLING_LEFT || state==FALLING_RIGHT)&&~(state==SPLAT);
assign digging=(state==DIGGING_LEFT || state==DIGGING_RIGHT)&&~(state==SPLAT);
endmodule
The verilog simulation output looks like this
Thus we have successfully written the Verilog code for Lemmings game
Comments
Please log in or sign up to comment.