Lemmings 1
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.
旅鼠游戏涉及大脑相当简单的小动物。如此简单,我们将使用有限状态机对其进行建模。
在旅鼠的 2D 世界中,旅鼠可以处于两种状态之一:向左走或向右走。如果它撞到障碍物,它会改变方向。特别是,如果旅鼠在左边被撞到,它会向右走。如果它在右边被撞到,它会向左走。如果它同时在两侧颠簸,它仍然会切换方向。
实现具有两个状态、两个输入和一个输出的摩尔状态机,以模拟这种行为。
题目网站
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=2'b00,right=2'b01;
reg state, next_state;
always @(*) begin
// State transition logic
case(state)
left:begin
next_state=(bump_left)?right:left;
end
right:begin
next_state=(bump_right)?left:right;
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)?1:0;
assign walk_right=(state==right)?1:0;
endmodule
开始了,这个有意思的小游戏