Lemmings2

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

Lemmings2_第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 = 2'b00, RIGHT = 2'b01, FALL_LE = 2'b10, FALL_RI = 2'b11;
    reg [1:0] state, next_state;
    
    always@ (*)
        case(state)
        	LEFT:
                if(ground == 0)
                     next_state <= FALL_LE;
                else if(bump_left)
                    next_state <= RIGHT;
            	else 
                    next_state <= LEFT;
			RIGHT: 
                if(ground == 0)
                     next_state <= FALL_RI;
                else if(bump_right)
                    next_state <= LEFT;
            	else 
                    next_state <= RIGHT;
            FALL_LE: 
                if(ground == 0)
                     next_state <= FALL_LE;
            else  
                 next_state <= LEFT;
            FALL_RI: 
                if(ground == 0)
                     next_state <= FALL_RI;
            else  
                 next_state <= RIGHT;
            default:
                next_state <= RIGHT;
        endcase
    
    always@(posedge clk or posedge areset)
        if(areset)
            state <= LEFT;
    	else 
    		state <= next_state;
    
    always@(posedge clk or posedge areset)
        if(areset)
            aaah <= 1'b0;
    else if(ground == 0)
           aaah <= 1'b1; 
    else
        aaah <= 0;
    
    assign walk_left = (state == LEFT);
    assign walk_right = (state == RIGHT);
 
     
    
endmodule

你可能感兴趣的:(HDLBits题目,verilog)