• [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.

    Falling for 20 cycles is survivable:

    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. input dig,
    8. output walk_left,
    9. output walk_right,
    10. output aaah,
    11. output digging );
    12. parameter LEFT=0, RIGHT=1,FALLL=2,FALLR=3,DIGL=4,DIGR=5,DIE=6;
    13. parameter DIE_CLOCK=19;
    14. //fall分出左右更有利于编程,dig应该也是这样
    15. reg [2:0]state, next_state;
    16. reg [4:0]die_count;
    17. /*我原计划把这里die_count设置成足够大的位数(12位),让它一直掉也无所谓。后来还是报错。
    18. 所以只好设置一个signal表示该死了,把die_count控制在最高19*/
    19. reg signal;
    20. //这里两位状态寄存器,必须记住!
    21. always @(*) begin
    22. // State transition logic
    23. case(state)
    24. LEFT:next_state<=ground?(dig?DIGL:(bump_left?RIGHT:LEFT)):FALLL;
    25. RIGHT:next_state<=ground?(dig?DIGR:(bump_right?LEFT:RIGHT)):FALLR;
    26. //这里要记得ground判定在前
    27. FALLL:next_state<=ground?(signal?DIE:LEFT):FALLL;
    28. FALLR:next_state<=ground?(signal?DIE:RIGHT):FALLR;
    29. DIGL:next_state<=ground?DIGL:FALLL;
    30. DIGR:next_state<=ground?DIGR:FALLR;
    31. DIE:next_state<=DIE;
    32. endcase
    33. end
    34. //这里把是否死亡的测试摘出来单独成一个always
    35. always@(posedge clk,posedge areset) begin
    36. if(areset) begin
    37. signal<=0;
    38. die_count<=0;
    39. end
    40. else if(state==FALLL ||state==FALLR) begin
    41. if(die_count==19)
    42. signal<=1;
    43. else
    44. die_count<=die_count+1;
    45. end
    46. else begin
    47. die_count<=0;
    48. signal<=0;
    49. end
    50. end
    51. always @(posedge clk, posedge areset) begin
    52. // State flip-flops with asynchronous reset
    53. if(areset) begin
    54. state<=LEFT;
    55. //die_count<=0;
    56. end
    57. else begin
    58. state<=next_state;
    59. /*
    60. if(state==FALLL||state==FALLR)
    61. die_count<=die_count+1;
    62. else
    63. die_count<=0;
    64. */
    65. end
    66. end
    67. // Output logic
    68. assign walk_left = (state == LEFT);
    69. assign walk_right = (state == RIGHT);
    70. assign aaah=(state==FALLL||state==FALLR);
    71. assign digging=(state==DIGL||state==DIGR);
    72. endmodule

  • 相关阅读:
    宠物疾病 与 光线疗法
    使用JMeter测试.Net5.0,.Net6.0框架下无数据处理的并发情况
    dapr入门系列之前言
    Altium Designer学习笔记7
    菜鸟学Kubernetes(K8s)系列——(番外)安装Ingress-Nginx(工作原理)
    SQL基础语句入门
    Android性能优化之应用瘦身(APK瘦身)
    list的模拟实现
    【云原生】Kubernetes介绍
    1.【刷爆LeetCode】替换空格(多方法、多思路解决)
  • 原文地址:https://blog.csdn.net/m0_66480474/article/details/133212823