HDLBits在线练习题之Lemmings2

FSM of Lemmings2

题目

地址: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 Declaration
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 );

分析

  1. 首先确认状态数量,本例中使用三个状态:LEFT-向左走,RIGHT-向右走,FALL-下落。并使用state,next_state和state_before_fall分别记录当前状态、下一状态和下落前的状态,下落前的状态用于ground为1后继续下落前的方向行走。
  2. state_before_fall使用时序逻辑描述,重置为LEFT状态,当时钟上升沿到来时,如果ground为0,且上升沿前为非FALL状态,将state赋值给state_before_fall;如果为FALL状态或者ground为1,state_before_fall保持不变,与当前状态不符合也没关系。
  3. 状态转移条件:ground为1时,正常判断,为0时下一状态为FALL。
  4. 摩尔型状态机输出只与当前状态相关。

代码

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 LEFT=2'b00,RIGHT=2'b01,FALL=2'b11;
    reg [1:0] state, next_state, state_before_fall;
    
    always @(posedge clk,posedge areset) begin
        if (areset)
            state_before_fall <= LEFT;
        else if (~ground && (state!=FALL))
            state_before_fall <= state;
        else
            state_before_fall <= state_before_fall;
    end
    
    always @(*) begin
        case (state)
            LEFT: begin
                if (ground) next_state = bump_left?RIGHT:LEFT;
                else next_state = FALL;
            end
            RIGHT: begin
                if (ground) next_state = bump_right?LEFT:RIGHT;
                else next_state = FALL;
            end
            FALL: begin
                if (ground) next_state = state_before_fall;
                else next_state = FALL;
            end
        endcase
    end
        
    always @(posedge clk, posedge areset) begin
        if (areset) 
            state <= LEFT;
    	else 
            state <= next_state;
    end
    
    assign walk_left = (state==LEFT)?1'b1:1'b0;
    assign walk_right = (state==RIGHT)?1'b1:1'b0;
    assign aaah = (state==FALL)?1'b1:1'b0;
        
endmodule

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值