HDLBits刷题_Lemmings4

See also: Lemmings1Lemmings2, and Lemmings3.

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.

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 ); 
    
    parameter g1right = 3, g1left = 2, g0right = 1, g0left = 0, digright = 4, digleft = 5, splatter = 6; 
//规定7个状态,相比于lemmings3,多加一个splatter状态,意味着下落超过20个周期
    reg [2:0] state, next;
    reg [10:0] sum; //对下落周期计数,由于下落周期数无上限,所以sum需要取值足够大
    
    always @(*)begin
    case (state)
        
        g0left: next = (ground == 1) ? ((sum > 20) ? splatter : g1left) : g0left;
//如果下落时间超过20个周期,进入splatter状态,其他情况与lemmings3没有差别
        g0right: next = (ground == 1) ? ((sum > 20) ? splatter : g1right) : g0right;
        
        g1left: begin
            if (ground == 0) next = g0left;
            else if (dig == 1) next = digleft;
            else begin next = (bump_left == 1) ? g1right : g1left; end
        end
        
        g1right: begin
            if (ground == 0) next = g0right;
            else if (dig == 1) next = digright;
            else begin next = (bump_right == 1) ? g1left : g1right; end
        end
        
        digleft: begin
            if (ground == 0) next = g0left;
            else next = digleft;
        end
        
        digright: begin
            if (ground == 0) next = g0right;
            else next = digright;
        end
        
    endcase
    end
    
    always @(posedge clk or posedge areset) begin
        if (areset == 1) begin state = g1left; sum = 0; end
        else if (next == g0left || next == g0right) begin sum++; state = next; end
//在判断areset后,计算下落周期数
        else begin state = next; sum = 0; end //非下落状态将周期计数清零     
    end
    
    assign walk_left = (state == splatter) ? 0 : (state == g1left);
//如果进入splatter状态,小鼠死亡,所有输出清零
    assign walk_right = (state == splatter) ? 0 : (state == g1right);
    assign digging = (state == splatter) ? 0 : (state == digleft | state == digright);
    assign aaah = (state == splatter) ? 0 : (state == g0left | state == g0right); 

endmodule

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值