• 牛客刷题<25>输入序列连续的序列检测


    题目:输入序列连续的序列检测_牛客题霸_牛客网

    解法一:最容易想到的状态机

    1. `timescale 1ns/1ns
    2. module sequence_detect(
    3. input clk,
    4. input rst_n,
    5. input a,
    6. output reg match
    7. );
    8. parameter IDLE = 3'd0;
    9. parameter s0 = 3'd1;
    10. parameter s1 = 3'd2;
    11. parameter s2 = 3'd3;
    12. parameter s3 = 3'd4;
    13. parameter s4 = 3'd5;
    14. parameter s5 = 3'd6;
    15. parameter s6 = 3'd7;
    16. reg [2:0] state,next_state;
    17. reg flag;
    18. always@(*)begin
    19. if(!rst_n)begin
    20. state <= IDLE;
    21. next_state <= IDLE;
    22. end
    23. else begin
    24. case(state)
    25. IDLE:begin
    26. if(a==0)
    27. next_state <= s0;
    28. else
    29. next_state <= IDLE;
    30. end
    31. s0: begin
    32. if(a==1)
    33. next_state <= s1;
    34. else
    35. next_state <= s0;
    36. end
    37. s1: begin
    38. if(a==1)
    39. next_state <= s2;
    40. else
    41. next_state <= s0;
    42. end
    43. s2: begin
    44. if(a==1)
    45. next_state <= s3;
    46. else
    47. next_state <= s0;
    48. end
    49. s3: begin
    50. if(a==0)
    51. next_state <= s4;
    52. else
    53. next_state <= IDLE;
    54. end
    55. s4: begin
    56. if(a==0)
    57. next_state <= s5;
    58. else
    59. next_state <= s1;
    60. end
    61. s5: begin
    62. if(a==0)
    63. next_state <= s6;
    64. else
    65. next_state <= s1;
    66. end
    67. s6: begin
    68. if(a==1)
    69. next_state <= s1;
    70. else
    71. next_state <= s0;
    72. end
    73. default : next_state <= IDLE;
    74. endcase
    75. end
    76. end
    77. always@(posedge clk or negedge rst_n)begin
    78. if(!rst_n)
    79. state <= IDLE;
    80. else
    81. state <= next_state;
    82. end
    83. always@(posedge clk or negedge rst_n)begin
    84. if(!rst_n)
    85. flag <= 1'b0;
    86. else if(state==s6 && a)
    87. flag <= 1'b1;
    88. else
    89. flag <= 0;
    90. end
    91. always@(posedge clk or negedge rst_n)begin
    92. if(!rst_n)
    93. match <= 1'b0;
    94. else
    95. match <= flag;
    96. end
    97. endmodule

    解法二:同样状态机,三目运算符写法   match <= state==EIGHT;也是三目运算符写法

    1. `timescale 1ns/1ns
    2. module sequence_detect(
    3. input clk,
    4. input rst_n,
    5. input a,
    6. output reg match
    7. );
    8. parameter ZERO=0,ONE=1,TWO=2,THREE=3,FOUR=4,FIVE=5,SIX=6,SEVEN=7,EIGHT=8;
    9. reg [3:0] state,nstate;
    10. always@(posedge clk or negedge rst_n)begin
    11. if(~rst_n)
    12. state <= ZERO;
    13. else
    14. state <= nstate;
    15. end
    16. always@(*)begin
    17. case(state)
    18. ZERO :nstate = a? ZERO : ONE;
    19. ONE :nstate = a? TWO : ONE;
    20. TWO :nstate = a? THREE: ONE;
    21. THREE :nstate = a? FOUR : ONE;
    22. FOUR :nstate = a? ZERO : FIVE;
    23. FIVE :nstate = a? TWO : SIX;
    24. SIX :nstate = a? TWO : SEVEN;
    25. SEVEN :nstate = a? EIGHT : ONE;
    26. EIGHT :nstate = a? THREE : ONE;
    27. default: nstate = ZERO;
    28. endcase
    29. end
    30. always@(posedge clk or negedge rst_n)begin
    31. if(!rst_n)
    32. match <= 0;
    33. else
    34. match <= state==EIGHT;
    35. end
    36. endmodule

    解法三:使用位拼接运算

    1. `timescale 1ns/1ns
    2. module sequence_detect(
    3. input clk,
    4. input rst_n,
    5. input a,
    6. output reg match
    7. );
    8. reg [7:0] a_r;
    9. always@(posedge clk or negedge rst_n)begin
    10. if(!rst_n)begin
    11. a_r <= 1'b0;
    12. end
    13. else begin
    14. a_r <= {a_r[6:0],a};
    15. end
    16. end
    17. always@(posedge clk or negedge rst_n)begin
    18. if(!rst_n)begin
    19. match <= 1'b0;
    20. end
    21. else begin
    22. match <= a_r==8'b01110001;
    23. end
    24. end
    25. endmodule

    解法四:解法三的简化

    1. `timescale 1ns/1ns
    2. module sequence_detect(
    3. input clk,
    4. input rst_n,
    5. input a,
    6. output reg match
    7. );
    8. reg [7:0] a_reg;
    9. always@(posedge clk or negedge rst_n)begin
    10. if(!rst_n)begin
    11. a_reg <= 8'd0;
    12. match <= 1'b0;
    13. end
    14. else begin
    15. a_reg <= {a_reg[6:0],a};
    16. if(a_reg == 8'b0111_0001)
    17. match <= 1'b1;
    18. else
    19. match <= 1'b0;
    20. end
    21. end
    22. endmodule

  • 相关阅读:
    horizontal image flip(Neon优化)
    if选择结构分析
    Python爬虫实战:根据关键字爬取某度图片批量下载到本地
    成功解决ES高亮内容引起的字段显示不一致问题
    【Zotero】翻译插件导入百度API
    NGINX缓存详解之服务端缓存
    15 检验的优良性
    JS / 事件基础知识
    浅谈事件冒泡前世今生
    泛微低代码平台应用合集,开箱即用,助力组织快速数字化
  • 原文地址:https://blog.csdn.net/mxh3600/article/details/126789626