2021年6月26日 星期六

HDLBits FSM/Lemmings1

 HDLBits FSM/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.

Lemmings系列問題是循序漸進的,所以這里第一道題當然是最簡單的。題目表述很明確,Lemmings是2D世界中的遊戲角色,這里只能往左走或往右走,如果在某個方向上遇到障礙就會自動掉頭往另一個方向走,如果沒有遇到障礙就在該方向一直走下去。如果左右都遇到了障礙就在原地不斷掉頭。當然這里的動作都是在時鐘上升沿實現的,要求用Moore狀態機實現。

這里的輸入有clk,areset,bump_left,bump_right,其中bump_left表示左邊遇到障礙,bump_right表示右邊遇到障礙。輸出有walk_left,walk_right,分別表示當前狀態是往左走還是往右走。波形圖如下所示

狀態就兩個,要麽向左走,要麽向右走。


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 @(posedge clk, posedge areset) begin
        // State flip-flops with asynchronous reset
        if(areset)begin
           state <= LEFT; 
        end
        else begin
           state <= next_state; 
        end
    end
    
    always@(*) begin
case (state)
LEFT : next_state = bump_left  ? RIGHT : LEFT;
RIGHT: next_state = bump_right ? LEFT : RIGHT;
endcase
    end
    // Output logic
    assign walk_left =  (state==LEFT)?1:0;
    assign walk_right = (state==RIGHT)?1:0;
endmodule

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...