2021年5月2日 星期日

HBLbits_Verilog Basic_Lemmings3

HBLbits_Verilog Basic_Lemmings3 

See also: Lemmings1 and Lemmings2.
In addition to walking and falling, Lemmings can sometimes be told to do useful things, like dig (it starts digging when dig=1). A Lemming can dig if it is currently walking on ground (ground=1 and not falling), and will continue digging until it reaches the other side (ground=0). At that point, since there is no ground, it will fall (aaah!), then continue walking in its original direction once it hits ground again. As with falling, being bumped while digging has no effect, and being told to dig when falling or when there is no ground is ignored.
(In other words, a walking Lemming can fall, dig, or switch directions. If more than one of these conditions are satisfied, fall has higher precedence than dig, which has higher precedence than switching directions.)
Extend your finite state machine to model this behaviour.

該題在上一題的基礎上又增加了dig挖地的技能,因此也有了digging的狀態輸出。這裡設定的是只能向下挖直到挖空為止,挖空後進入掉落狀態。與掉落狀態類似,digging狀態也不區分左右方向,在挖空、掉落、著陸後保持挖地之前的方向,因此只需要在上題的基礎上再增加left_digright_dig狀態即可,同樣這兩個狀態也不能互相轉換。同時需要注意diggingwalk狀態均為0,與fall狀態一致。波形圖和狀態機如下



module top_module(
    input clk,
    input areset,    // Freshly brainwashed Lemmings walk left.
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output walk_left,
    output walk_right,
    output aaah,
    output digging ); 
  
parameter left=0,right=1,left_dig=2,right_dig=3,left_fall=4,right_fall=5;
    
    reg[2:0] state, next_state;
    reg[3:0] out;
    
    //state
    always@(posedge clk or posedge areset) begin
        if(areset)
            state <= left;
        else
            state <= next_state;
    end
    
    //transition
    always@(*) begin
        case(state)
            left      : next_state= ground?(dig?left_dig:(bump_left?right:left)):left_fall;
            right     : next_state= ground?(dig?right_dig:(bump_right?left:right)):right_fall;
            left_dig  : next_state= ground?left_dig:left_fall;
            right_dig : next_state= ground?right_dig:right_fall;
            left_fall : next_state= ground?left:left_fall;
            right_fall: next_state= ground?right:right_fall;
        endcase
    end
    
    //out
    always@(posedge clk or posedge areset) begin
        if(areset)
            out <= 4'b1000;
        else
            case(next_state)
                left:          out <= 4'b1000;
                left_dig:   out <= 4'b0001;
                left_fall:   out <= 4'b0010;
                right:        out <= 4'b0100;
                right_dig: out <= 4'b0001;
                right_fall: out <= 4'b0010;
            endcase
    end
    
    assign {walk_left, walk_right, aaah, digging} = out;
    
endmodule

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...