HDLbits--lemmings4

在前一题基础上修改,只有当fall状态才能转化为dead状态 dead状态下输出全为0,直到复位。 

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 left=0,right=1,fall_l=2,fall_r=3,dig_l=4,dig_r=5,dead=6;
    reg[2:0] state,next_state;
    int count;
    
    //state change logic
    always @(*)
        begin
            case(state)
                left:
                    next_state<=(ground)? (dig?dig_l:(bump_left?right:left)):fall_l;
                right:
                    next_state<=ground? (dig?dig_r: (bump_right?left:right)):fall_r;
                fall_r:
                    next_state<=ground? count>20?dead:right:fall_r;
                fall_l:
                    next_state<=ground?count>20?dead:left:fall_l;
                dig_l:
                    next_state<=ground? dig_l:fall_l;
                dig_r:
                    next_state<=ground? dig_r:fall_r;
                dead:
                    next_state<=dead;
            endcase
        end
    
    //
    always@(posedge clk,posedge areset)
        begin
            if(areset)
                state<= left;
            else
                state<=next_state;
        end
    
    always@(posedge clk)
        begin
            if(state==fall_r||state==fall_l)
                count=count+1;
            else
                count=0;
        end
            
    
    //output logic
    assign walk_left=(state==left);
    assign walk_right=(state==right);
    assign digging= (state==dig_r|| state==dig_l);
    assign aaah=(state==fall_r ||state==fall_l);
                    

endmodule

结果在580秒时错误,walkleft/right仍有输出

修改:在计数时序逻辑块内吧state改成next-state

结果正确: 

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 left=0,right=1,fall_l=2,fall_r=3,dig_l=4,dig_r=5,dead=6;
    reg[2:0] state,next_state;
    int count;
    
    //state change logic
    always @(*)
        begin
            case(state)
                left:
                    next_state<=(ground)? (dig?dig_l:(bump_left?right:left)):fall_l;
                right:
                    next_state<=ground? (dig?dig_r: (bump_right?left:right)):fall_r;
                fall_r:
                    next_state<=ground? count>20?dead:right:fall_r;
                fall_l:
                    next_state<=ground?count>20?dead:left:fall_l;
                dig_l:
                    next_state<=ground? dig_l:fall_l;
                dig_r:
                    next_state<=ground? dig_r:fall_r;
                dead:
                    next_state<=dead;
            endcase
        end
    
    //
    always@(posedge clk,posedge areset)
        begin
            if(areset)
                state<= left;
            else
                state<=next_state;
        end
    
    always@(posedge clk)
        begin
            if(next_state==fall_r||next_state==fall_l)
                count=count+1;
            else
                count=0;
        end
            
    
    //output logic
    assign walk_left=(state==left);
    assign walk_right=(state==right);
    assign digging= (state==dig_r|| state==dig_l);
    assign aaah=(state==fall_r ||state==fall_l);
                    

endmodule

反思:仍然不清楚组合逻辑和时序逻辑,最时序没有搞清楚

有人指出:时序逻辑内参考状态为 next-state 组合逻辑内参考状态为state。

参考链接: (11条消息) HDLBits刷题记录——FSM Lemmings4_Candy_579的博客-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值