HDLBits练习——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.

在这里插入图片描述
HDLBits练习——Lemmings2_第1张图片

前言

五个输入,包括一个时钟clk,一个高电平有效的异步置位信号areset,一个左行遇到障碍的输入信号bump_left,一个右行遇到障碍的输入信号bump_right,一个控制地面高度信号ground;三个输出,包括一个左行输出信号walk_left,一个右行输出信号walk_right,一个降落的声音信号aaah。

代码

module top_module(
    input clk,
    input areset,   
    input bump_left,
    input bump_right,
    input ground,
    output walk_left,
    output walk_right,
    output aaah ); 
    
	parameter D_LEFT=2'b00,LEFT=2'b10,D_RIGHT=2'b01,RIGHT=2'b11;
    reg [1:0]state,next_state;
    
    always@(*)begin
        case(state)
            D_LEFT:next_state=ground?LEFT:D_LEFT;
            LEFT:next_state=(!ground)?D_LEFT:bump_left?RIGHT:LEFT;
            D_RIGHT:next_state=ground?RIGHT:D_RIGHT;
            RIGHT:next_state=(!ground)?D_RIGHT:bump_right?LEFT:RIGHT;
            default:;
        endcase
    end
    
    always@(posedge clk or posedge areset)begin
        if(areset) state<=LEFT;
        else state<=next_state;
    end
    
    assign walk_left=state==LEFT;
    assign walk_right=state==RIGHT;
    assign aaah=state[1]==1'b0;
    
endmodule

总结

注意一下,这里的输出信号 walk_left 和 walk_right 指的是在 ground = 1’b1 条件下的左行和右行。

你可能感兴趣的:(fpga开发)