当前位置:   article > 正文

Circuits--Build large --FSM

Circuits--Build large --FSM

1. FSM: Enable shift register

  1. module top_module (
  2. input clk,
  3. input reset, // Synchronous reset
  4. output reg shift_ena);
  5. reg[2:0] count;
  6. always@(posedge clk)
  7. begin
  8. if(reset)
  9. begin
  10. count = 3'd0;
  11. shift_ena = 1'b1;
  12. end
  13. else
  14. begin
  15. if(count==3'd3)
  16. shift_ena = 1'b0;
  17. else
  18. begin
  19. count = count +3'd1 ;
  20. shift_ena = 1'b1;
  21. end
  22. end
  23. end
  24. endmodule

2.FSM : the complete FSM

  1. module top_module (
  2. input clk,
  3. input reset, // Synchronous reset
  4. input data,
  5. output shift_ena,
  6. output counting,
  7. input done_counting,
  8. output done,
  9. input ack );
  10. parameter S = 4'd0;
  11. parameter S1 = 4'd1;
  12. parameter S11 = 4'd2;
  13. parameter S110 = 4'd3;
  14. parameter B0 = 4'd4;
  15. parameter B1 = 4'd5;
  16. parameter B2 = 4'd6;
  17. parameter B3 = 4'd7;
  18. parameter Count = 4'd8;
  19. parameter Wait = 4'd9;
  20. reg[3:0] state;
  21. reg[3:0] next_state;
  22. always@(*)
  23. begin
  24. case(state)
  25. S: next_state = data ? S1 : S;
  26. S1: next_state = data ? S11 : S;
  27. S11: next_state = data ? S11 : S110;
  28. S110: next_state = data ? B0 : S;
  29. B0 : next_state = B1;
  30. B1 : next_state = B2;
  31. B2 : next_state = B3;
  32. B3 : next_state = Count;
  33. Count : next_state = done_counting ? Wait : Count;
  34. Wait : next_state = ack ? S : Wait;
  35. endcase
  36. end
  37. always@(posedge clk)
  38. begin
  39. if(reset)
  40. state = S;
  41. else
  42. state = next_state;
  43. end
  44. assign shift_ena = (state == B0)|(state == B1)|(state == B2)|(state == B3);
  45. assign counting = (state == Count);
  46. assign done = (state == Wait);
  47. endmodule

3.the complete timer

  1. module top_module (
  2. input clk,
  3. input reset, // Synchronous reset
  4. input data,
  5. output [3:0] count,
  6. output counting,
  7. output done,
  8. input ack );
  9. parameter S0 = 4'd0;
  10. parameter S1 = 4'd1;
  11. parameter S2 = 4'd2;
  12. parameter S3 = 4'd3;
  13. parameter C0 = 4'd4;
  14. parameter C1 = 4'd5;
  15. parameter C2 = 4'd6;
  16. parameter C3 = 4'd7;
  17. parameter Count_1000 = 4'd8;
  18. parameter Done = 4'd9;
  19. reg[3:0] state;
  20. reg[3:0] next_state;
  21. reg[15:0] num;
  22. reg[3:0] delay;
  23. reg[3:0] acount;
  24. wire count_state;
  25. assign count_state = (num == (delay + 1'b1)*1000) ? 1'b1 : 1'b0;
  26. always@(*)
  27. begin
  28. if(num <= 16'd1000)
  29. acount = 4'd0;
  30. else if(num > 16'd1000&&num <= 16'd2000)
  31. acount = 4'd1;
  32. else if(num > 16'd2000&&num <= 16'd3000)
  33. acount = 4'd2;
  34. else if(num > 16'd3000&&num <= 16'd4000)
  35. acount = 4'd3;
  36. else if(num > 16'd4000&&num <= 16'd5000)
  37. acount = 4'd4;
  38. else if(num > 16'd5000&&num <= 16'd6000)
  39. acount = 4'd5;
  40. else if(num > 16'd6000&&num <= 16'd7000)
  41. acount = 4'd6;
  42. else if(num > 16'd7000&&num <= 16'd8000)
  43. acount = 4'd7;
  44. else if(num > 16'd8000&&num <= 16'd9000)
  45. acount = 4'd8;
  46. else if(num > 16'd9000&&num <= 16'd10000)
  47. acount = 4'd9;
  48. else if(num > 16'd10000&&num <= 16'd11000)
  49. acount = 4'd10;
  50. else if(num > 16'd11000&&num <= 16'd12000)
  51. acount = 4'd11;
  52. else if(num > 16'd12000&&num <= 16'd13000)
  53. acount = 4'd12;
  54. else if(num > 16'd13000&&num <= 16'd14000)
  55. acount = 4'd13;
  56. else if(num > 16'd14000&&num <= 16'd15000)
  57. acount = 4'd14;
  58. else
  59. acount = 4'd15;
  60. end
  61. always@(posedge clk)
  62. begin
  63. if(reset)
  64. num <= 16'd0;
  65. else if(next_state == Done)
  66. num <= 16'd0;
  67. else if(next_state == Count_1000)
  68. num <= num + 16'd1;
  69. end
  70. always@(*)
  71. begin
  72. case(state)
  73. S0: next_state = data ? S1 : S0;
  74. S1: next_state = data ? S2 : S0;
  75. S2: next_state = data ? S2 : S3;
  76. S3: next_state = data ? C0 : S0;
  77. C0:
  78. begin
  79. next_state = C1;
  80. delay[3] = data;
  81. end
  82. C1:
  83. begin
  84. next_state = C2;
  85. delay[2] = data;
  86. end
  87. C2:
  88. begin
  89. next_state = C3;
  90. delay[1] = data;
  91. end
  92. C3:
  93. begin
  94. next_state = Count_1000;
  95. delay[0] = data;
  96. end
  97. Count_1000:
  98. next_state = count_state ? Done : Count_1000;
  99. Done:
  100. next_state = ack ? S0 : Done;
  101. default:
  102. next_state = S0;
  103. endcase
  104. end
  105. always@(posedge clk)
  106. begin
  107. if(reset)
  108. state <= S0;
  109. else
  110. state <= next_state;
  111. end
  112. assign count = (state == Count_1000) ? (delay - acount) : 4'd0;
  113. assign counting = (state == Count_1000);
  114. assign done = (state == Done);
  115. endmodule

4. FSM:one hot

  1. module top_module(
  2. input d,
  3. input done_counting,
  4. input ack,
  5. input [9:0] state, // 10-bit one-hot current state
  6. output B3_next,
  7. output S_next,
  8. output S1_next,
  9. output Count_next,
  10. output Wait_next,
  11. output done,
  12. output counting,
  13. output shift_ena
  14. ); //
  15. // You may use these parameters to access state bits using e.g., state[B2] instead of state[6].
  16. parameter S=0, S1=1, S11=2, S110=3, B0=4, B1=5, B2=6, B3=7, Count=8, Wait=9;
  17. assign B3_next = state[B2];
  18. assign S_next = ~d & state[S] | ~d & state[S1] | ~d & state[S110] | ack & state[Wait];
  19. assign S1_next = d & state[S];
  20. assign Count_next = state[B3] | ~done_counting & state[Count];
  21. assign Wait_next = done_counting & state[Count] | ~ack & state[Wait];
  22. assign done = state[Wait];
  23. assign counting = state[Count];
  24. assign shift_ena = state[B0] | state[B1] | state[B2] | state[B3];
  25. endmodule

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/660020
推荐阅读
相关标签
  

闽ICP备14008679号