hdlbits
Lemmings1
旅鼠游戏有四个系列
系列1:
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;
reg state, next_state;
always @(*) begin
case(state)
LEFT: if((bump_left==1)&&(bump_right==0))
next_state=RIGHT;
else if((bump_right==1)&&(bump_left==0))
next_state=LEFT;
else if((bump_left==1)&&(bump_right==1))
next_state=RIGHT;
else
next_state=LEFT;
RIGHT: if((bump_left==1)&&(bump_right==0))
next_state=RIGHT;
else if((bump_right==1)&&(bump_left==0))
next_state=LEFT;
else if((bump_left==1)&&(bump_right==1))
next_state=LEFT;
else
next_state=RIGHT;
endcase
// State transition logic
end
always @(posedge clk, posedge areset) begin
if(areset)
state<=LEFT;
else
state<=next_state;
// State flip-flops with asynchronous reset
end
assign walk_left=(state==LEFT)?1:0;
assign walk_right=(state==RIGHT)?1:0;
// Output logic
// assign walk_left = (state == ...);
// assign walk_right = (state == ...);
endmodule
系列2
在系列1的基础上增加了掉落的功能,所以在系列1的基础上增加两个状态,由于掉落之后,一旦出现地,要回到原来的方向,因此需要增加从左侧掉落和从右侧掉落,共四个状态
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 );
localparam left = 0;
localparam right = 1;
localparam fall_l = 2;
localparam fall_r = 3;
reg[1:0] state, next_state;
reg[2:0] out;
always @(posedge clk or posedge areset)
begin
if(areset)
state<=left;
else
state<=next_state;
end
always @(*)
begin
case(state)
left:
next_state=ground?(bump_left?right:left):fall_l;
right:
next_state=ground?(bump_right?left:right):fall_r;
fall_l:
next_state=ground?left:fall_l;
fall_r:
next_state=ground?right:fall_r;
endcase
end
always @(posedge clk or posedge areset)
begin
if(areset)
out<=3'b100;
else
case(next_state)
left: out<=3'b100;
right: out<=3'b010;
fall_l: out<=3'b001;
fall_r: out<=3'b001;
endcase
end
assign {walk_left,walk_right,aaah}=out;
endmodule