当前位置:   article > 正文

Circuits--Sequential--FSM--q5b~q3a

Circuits--Sequential--FSM--q5b~q3a

1. q5b

  1. module top_module (
  2. input clk,
  3. input areset,
  4. input x,
  5. output z
  6. );
  7. parameter A = 1'b0;
  8. parameter B = 1'b1;
  9. reg[1:0] state;
  10. reg[1:0] next_state;
  11. always@(*)
  12. begin
  13. case(state)
  14. A:
  15. if(x) next_state = B;
  16. else next_state = A;
  17. B:
  18. next_state = B;
  19. endcase
  20. end
  21. always@(posedge clk or posedge areset)
  22. begin
  23. if(areset)
  24. state = A;
  25. else
  26. state = next_state;
  27. end
  28. always@(*)
  29. begin
  30. if(state == A)
  31. begin
  32. if(x) z = 1'b1;
  33. else z = 1'b0;
  34. end
  35. else
  36. begin
  37. if(x) z = 1'b0;
  38. else z = 1'b1;
  39. end
  40. end
  41. endmodule

2. q3a

  1. module top_module(
  2. input clk,
  3. input reset,
  4. input s,
  5. input w,
  6. output z
  7. );
  8. parameter A = 1'd0;
  9. parameter B = 1'd1;
  10. reg[1:0] state;
  11. reg[1:0] next_state;
  12. reg[1:0] count;
  13. reg[1:0] num;
  14. always @(*) begin
  15. case(state)
  16. A:
  17. begin
  18. if(s) next_state = B;
  19. else next_state = A;
  20. end
  21. B:
  22. begin
  23. next_state = B;
  24. end
  25. endcase
  26. end
  27. always @(posedge clk) begin
  28. if(reset)
  29. state <= A;
  30. else
  31. state <= next_state;
  32. end
  33. always @(posedge clk) begin
  34. if(reset)
  35. count <= 2'd0;
  36. else if(count == 2'd2)
  37. count <= 2'd0;
  38. else if(state == B)
  39. count <= count + 1'b1;
  40. end
  41. always @(posedge clk) begin
  42. if(reset)
  43. num <= 1'b0;
  44. else
  45. begin
  46. if(count == 2'd0)
  47. begin
  48. if(w) num <= 1'b1;
  49. else num <= 1'b0;
  50. end
  51. else if(state == B)
  52. begin
  53. if(w) num <= num + 1'b1;
  54. else num <= num;
  55. end
  56. end
  57. end
  58. assign z = (state == B && num == 2'd2 && count == 2'd0);
  59. endmodule

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

闽ICP备14008679号