2021年5月1日 星期六

HBLbits_Verilog Basic_Lemmings1

HBLbits_Verilog Basic_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.

See also: Lemmings2Lemmings3, and Lemmings4.

Lemmings is a puzzle video game originally developed by DMA Design in Dundee, Scotland and published by Psygnosis for the Amiga in 1991 and later ported for numerous other platforms. The game was programmed by Russell Kay, Mike Dailly and David Jones, and was inspired by a simple animation that Dailly created while experimenting with Deluxe Paint.

大概意思:有兩個方向可以走:往左走和往右走;如果碰到障礙物就需要切換方向。同時碰到障礙物也要切換方向。

根據輸出,輸出walk_left,walk_right分別對應輸入bump_left,bump_right,當輸入對應位置出現1,即出現障礙物,輸出需要翻轉一次。

因此,定義兩個狀態 (往左走LEFT,往右走RIGHT);在LEFT狀態下,如果遇到左邊有障礙物或者兩邊都有障礙物,則狀態切換到RIGHT。另一個狀態下同理。

 module top_module(
    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 W_LEFT=0, W_RIGHT=1;
reg state,next;
    // Combinational always block for state transition logic. 
    // Combinational always block: Use blocking assignments. 
    // State transition logic
    always@(*) begin
case (state)
W_LEFT : next = bump_left  ? W_RIGHT : W_LEFT;
W_RIGHT: next = bump_right ? W_LEFT  : W_RIGHT;
endcase
    end
    
  // State flip-flops with asynchronous reset   
    always @(posedge clk, posedge areset) begin
if (areset) state <= W_LEFT;
        else state <= next;
end
    // Output logic
    // assign walk_left = (state == ...);
    // assign walk_right = (state == ...);
assign walk_left  = (state==W_LEFT);
assign walk_right = (state==W_RIGHT);
endmodule

    input clk,


沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...