HDLbits: Lemmings2

苦思冥想怎么把FALLING下落的逻辑加进去,下落是状态吗?好像是,因为人物状态就是这三种:LEFT、RIGHT、FALLING,但又好像不是,因为FALLING的时候还要“记住”之前是LEFT还是RIGHT。苦思冥想不知道怎么加进去。于是写出了下面的代码,当ground和bump_left/bump_right同时为1时会报错:

module top_module(
    input clk,
    input areset,    // Freshly brainwashed Lemmings walk left.
    input bump_left,
    input bump_right,
    input ground,
    output walk_left,
    output walk_right,
    output aaah ); 
    
    parameter LEFT=0, RIGHT=1;
    reg state,next_state;

    always@(*)begin
        casez(state)
            LEFT: next_state = ground?(bump_left?RIGHT:LEFT):LEFT;
            RIGHT: next_state = ground?(bump_left?LEFT:RIGHT):RIGHT;                      
        endcase            
    end
    
    always@(posedge clk or posedge areset)begin
        if(areset)
          state <= LEFT;
        else
          state <= next_state;
    end
    
    always@(posedge clk)begin
        if(!ground)
            aaah = 1;
        else
            aaah = 0;
    end                
            
    assign walk_left = ((~aaah) && (state == LEFT));
    assign walk_right = ((~aaah) && (state == RIGHT));
    
endmodule

参考了大神的答案,思路是把人物状态扩充到四种:LEFT、RIGHT、LEFT_FALLING、RIGHT_FALLING。

module top_module(
    input clk,
    input areset,    // Freshly brainwashed Lemmings walk left.
    input bump_left,
    input bump_right,
    input ground,
    output walk_left,
    output walk_right,
    output aaah ); 
    
    parameter LEFT=4'b0001, RIGHT=4'b0010, LEFT_FALLING=4'b0100, RIGHT_FALLING=4'b1000;
    reg [3:0] state,next_state;

    always@(*)begin
        case(state)
            LEFT: next_state = ground?(bump_left?RIGHT:LEFT):LEFT_FALLING;
            RIGHT: next_state = ground?(bump_right?LEFT:RIGHT):RIGHT_FALLING;  
            LEFT_FALLING: next_state = ground?LEFT:LEFT_FALLING;
            RIGHT_FALLING: next_state = ground?RIGHT:RIGHT_FALLING;
            default: next_state = LEFT;
        endcase            
    end
    
    always@(posedge clk or posedge areset)begin
        if(areset)
          state <= LEFT;
        else
          state <= next_state;
    end
    
    /*always@(posedge clk)begin
        if(!ground)
            aaah = 1;
        else
            aaah = 0;
    end  */              
            
    assign walk_left = (state == LEFT);
    assign walk_right = (state == RIGHT);
    assign aaah = (state == LEFT_FALLING || state == RIGHT_FALLING);
    
endmodule

你可能感兴趣的:(verilog学习)