• HDLBits-Lemmings2


    See also: Lemmings1.

    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.

    题目地址:Lemmings2 - HDLBits (01xz.net)

    由于增加了坠落(fall)状态,所以可以通过增加两个状态 即 L2G 和R2G 分别表示向左走时进入坠落状态,向右走时进入坠落状态 。

    注意,areset为异步的,所以应该为always @(posedge clk or posedge areset)begin

    end

    代码如下:

    1. module top_module(
    2. input clk,
    3. input areset, // Freshly brainwashed Lemmings walk left.
    4. input bump_left,
    5. input bump_right,
    6. input ground,
    7. output walk_left,
    8. output walk_right,
    9. output aaah );
    10. //增加两个状态L2G 和R2G 分别表示左下坠、右下坠。
    11. reg [1:0] state,nextstate;
    12. wire [2:0]dirt = {ground,bump_left,bump_right};
    13. parameter left = 0,
    14. l2g = 1,
    15. right = 2,
    16. r2g = 3;
    17. always @(posedge clk or posedge areset) begin
    18. if(areset)
    19. state <= left;
    20. else
    21. state <= nextstate;
    22. end
    23. // logic translation
    24. always @(*)begin
    25. // nextstate <= left;
    26. case(state)
    27. left:if(~ground)
    28. nextstate <= l2g;
    29. else if(dirt==3'b111 || dirt==3'b110)
    30. nextstate <= right;
    31. else
    32. nextstate <= left;
    33. right:if(~ground)
    34. nextstate <=r2g;
    35. else if(dirt==3'b111 || dirt==3'b101)
    36. nextstate <= left;
    37. else
    38. nextstate <= right;
    39. l2g:if(ground)
    40. nextstate <= left;
    41. else
    42. nextstate <= l2g;
    43. r2g:if(ground)
    44. nextstate <= right;
    45. else
    46. nextstate <= r2g;
    47. default:
    48. nextstate <= left;
    49. endcase
    50. end
    51. assign walk_left = (state==left);
    52. assign walk_right = (state==right);
    53. assign aaah = (state==l2g || state==r2g);
    54. endmodule

  • 相关阅读:
    SQL RDBMS 概念
    根据输入类型来选择函数不同的实现方法functools.singledispatch
    Linux中Tomcat发布war包后无法正常访问非静态资源
    Cocos2d Opengl2.1 升级到 opengl3.3
    1767. 寻找没有被执行的任务对(当时对递归不熟)(NO)
    实战Kaggle比赛:预测房价
    SpringBoot实战(二十四)集成 LoadBalancer
    【图数据库实战】图数据库典型应用场景
    Redis分布式锁
    7.1ASP.NET Core中的依赖注入
  • 原文地址:https://blog.csdn.net/Kai666888/article/details/133702739