Lemmings4

题目:

Although Lemmings can walk, fall, and dig, Lemmings aren't invulnerable. If a Lemming falls for too long then hits the ground, it can splatter. In particular, if a Lemming falls for more than 20 clock cycles then hits the ground, it will splatter and cease walking, falling, or digging (all 4 outputs become 0), forever (Or until the FSM gets reset). There is no upper limit on how far a Lemming can fall before hitting the ground. Lemmings only splatter when hitting the ground; they do not splatter in mid-air.

Extend your finite state machine to model this behaviour.

Falling for 20 cycles is survivable:

代码:

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 ); 
    reg [2:0] state,state_next;
    reg [7:0] count;
    parameter LEFT=0,RIGHT=1,DIG_L=2,DIG_R=3,G_L=4,G_R=5,OVER=6;
    reg [3:0] out;
    always @(posedge clk or posedge areset)
    begin
        if(areset)
            state <= LEFT;
        else
            state <= state_next;
    end
    
    always @(posedge clk or posedge areset)
        begin
            if(areset)
                count <= 0;
            else
                begin
                    if(!ground)
                        count <= count + 1 ;
                    else
                        count <= 0;
                end
        end
    
    always @(*)
        begin
            case(state)
                LEFT:  state_next= ground?(dig?DIG_L:(bump_left?RIGHT:LEFT)):G_L;
                RIGHT: state_next= ground?(dig?DIG_R:(bump_right?LEFT:RIGHT)):G_R;
                DIG_L: state_next= ground?DIG_L:G_L;
                DIG_R: state_next= ground?DIG_R:G_R;
                G_L:   state_next= ground?((count>=5'd21)?OVER:LEFT):G_L;
                G_R:   state_next= ground?((count>=5'd21)?OVER:RIGHT):G_R;
                OVER:  state_next= OVER;
            endcase
        end
    always @(posedge clk or posedge areset) begin
        if(areset == 1'b1) begin
            out <= 4'b1000;
        end
        else begin
            case(state_next)
                LEFT   :out <= 4'b1000;
                RIGHT  :out <= 4'b0100;
                G_R    :out <= 4'b0010;
                G_L    :out <= 4'b0010;
                DIG_R  :out <= 4'b0001;
                DIG_L  :out <= 4'b0001;
                OVER   :out <= 4'b0000;
                default:out <= 4'b1000;
            endcase
        end
    end
    assign {walk_left,walk_right,aaah,digging}=out;

endmodule

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值