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
開始了,這個有意思的小遊戲