image.png

    1. `timescale 1ns/1ns
    2. module sequence_detect_2(
    3. input clk,
    4. input rst_n,
    5. input a,
    6. output reg match
    7. );
    8. parameter idle = 4'b0000;
    9. parameter s0 = 4'b0001;
    10. parameter s1 = 4'b0010;
    11. parameter s2 = 4'b0011;
    12. parameter s3 = 4'b0100;
    13. parameter s4 = 4'b0101;
    14. parameter s5 = 4'b0110;
    15. parameter s6 = 4'b0111;
    16. parameter s7 = 4'b1000;
    17. parameter s8 = 4'b1001;
    18. reg [8:0] cs;
    19. reg [8:0] ns;
    20. always @(posedge clk or negedge rst_n) begin
    21. if(!rst_n) begin
    22. cs <= 9'b000000000;
    23. end
    24. else begin
    25. cs <= ns;
    26. end
    27. end
    28. always @(*) begin
    29. case(cs)
    30. idle : ns = (a == 1)? idle : s0;
    31. s0 : ns = (a == 1)? s1 : s0;
    32. s1 : ns = (a == 1)? s2 : s0;
    33. s2 : ns = s3;
    34. s3 : ns = s4;
    35. s4 : ns = s5;
    36. s5 : ns = (a == 1)? s6 : s0;
    37. s6 : ns = (a == 1)? s7 : s0;
    38. s7 : ns = (a == 1)? idle : s8;
    39. s8 : ns = (a == 1)? s1 : s0;
    40. default : ns = idle;
    41. endcase
    42. end
    43. wire match_tmp;
    44. assign match_tmp = (cs == s8);
    45. always @(posedge clk or negedge rst_n) begin
    46. if(!rst_n) begin
    47. match <= 1'b0;
    48. end
    49. else begin
    50. match <= match_tmp;
    51. end
    52. end
    53. endmodule

    由于其中包含了一些无关的序列,所以不需要进行输入状态的判断。