HDLBits练习——Lemmings3

In addition to walking and falling, Lemmings can sometimes be told to do useful things, like dig (it starts digging when dig=1). A Lemming can dig if it is currently walking on ground (ground=1 and not falling), and will continue digging until it reaches the other side (ground=0). At that point, since there is no ground, it will fall (aaah!), then continue walking in its original direction once it hits ground again. As with falling, being bumped while digging has no effect, and being told to dig when falling or when there is no ground is ignored.

(In other words, a walking Lemming can fall, dig, or switch directions. If more than one of these conditions are satisfied, fall has higher precedence than dig, which has higher precedence than switching directions.)

Extend your finite state machine to model this behaviour.
在这里插入图片描述

在这里插入图片描述


前言

六个输入,包括一个时钟clk,一个高电平有效的异步置位信号areset,一个左遇障碍的输入信号bump_left,一个右遇障碍的输入信号bump_right,一个控制地面高度的输入信号ground,一个挖掘行为的控制信号 dig;四个输出,包括一个左行输出信号walk_left,一个右行输出信号walk_right,一个降落喊声信号aaah,一个挖掘状态输出信号 digging。

代码

module top_module(
    input clk,
    input areset,    
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output walk_left,
    output walk_right,
    output aaah,
    output digging ); 
	
    parameter LEFT=3'b100,DIG_L=3'b110,FALL_L=3'b000,RIGHT=3'b101,DIG_R=3'b111,FALL_R=3'b001;
    reg[2:0] state,next_state;
    
    always@(*)begin
        case(state)
            LEFT:next_state=(!ground)?FALL_L:dig?DIG_L:bump_left?RIGHT:LEFT;
            RIGHT:next_state=(!ground)?FALL_R:dig?DIG_R:bump_right?LEFT:RIGHT;
            DIG_L:next_state=ground?DIG_L:FALL_L;
            DIG_R:next_state=ground?DIG_R:FALL_R;
            FALL_L:next_state=ground?LEFT:FALL_L;
            FALL_R:next_state=ground?RIGHT:FALL_R;
            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[2];
    assign digging=state[1];
    
endmodule

总结

因为除了左右行以外,还有地面高度,挖掘行为的判断,因此选择状态的位宽为3,一共有6个状态,根据状态变化图写三段式状态机。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值