Verilog HDLbits:Lemmings3(Moore型有限元状态机)

题目

In addition to walking left and right, Lemmings will fall (and presumably go “aaah!”) if the ground disappears underneath them.

In addition to walking left and right and changing direction when bumped, when ground=0, the Lemming will fall and say “aaah!”. When the ground reappears (ground=1), the Lemming will resume walking in the same direction as before the fall. Being bumped while falling does not affect the walking direction, and being bumped in the same cycle as ground disappears (but not yet falling), or when the ground reappears while still falling, also does not affect the walking direction.

Build a finite state machine that models this behaviour.

状态转移图

Verilog HDLbits:Lemmings3(Moore型有限元状态机)_第1张图片

代码如下

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;
    parameter right = 1;
    parameter left_dig = 2;
    parameter right_dig = 3;
    parameter left_fall = 4;
    parameter 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)
           //方法一 if——else if语句
            left:begin
                if(!ground)
                    next_state=left_fall;
                else if(dig)
                    next_state=left_dig;
                else if(bump_left)
                    next_state=right;
                else
                    next_state=left;
                    end
             right:begin
                if(!ground)
                    next_state=right_fall;
                else if(dig)
                    next_state=right_dig;
                 else if(bump_right)
                    next_state=left;
                else
                    next_state=right;
                    end
            left_dig:begin
                if(!ground)
                    next_state=left_fall;
                else
                    next_state=left_dig;
            end
           right_dig:begin
                if(!ground)
                    next_state=right_fall;
                else
                    next_state=right_dig;
            end
              right_fall:begin
                 if(!ground)
                    next_state=right_fall;
                  else
                      next_state=right;
              end
                   left_fall:begin
                 if(!ground)
                    next_state=left_fall;
                  else
                      next_state=left;
                   end
//方法2:嵌套三目运算符写法,精简但容易出错
            /*
            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


你可能感兴趣的:(HDLbits,fpga,verilog)