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.

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 wl=2'd0,wr=2'd1,fl=2'd2,fr=2'd3;
    reg [1:0] s,n_s;
    always@(*)begin
        case(s)
            wl:n_s=ground?(bump_left?wr:wl):fl;
            wr:n_s=ground?(bump_right?wl:wr):fr;
            fl:n_s=ground?wl:fl;
            fr:n_s=ground?wr:fr;
        endcase
    end
    always@(posedge clk or posedge areset)begin
        if(areset)
            s<=wl;
        else
            s<=n_s;
    end
    assign walk_left=~aaah&&(s==wl);
    assign walk_right=~aaah&&(s==wr);
    assign aaah=(s==fl|s==fr);
endmodule

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