Advertisement
rrrr23231

Untitled

Aug 14th, 2023 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. module FSM (input clk, reset, NB, SB, output reg fsm_TR, fsm_TY, fsm_TG, fsm_PR, fsm_PG);
  2. reg[1:0] currentState, nextState;
  3. reg[2:0] count;
  4. reg[3:0] count_1;
  5. reg previous_State;
  6. reg neg_P_req;
  7. wire P_req;
  8. parameter StateA = 3'b000, StateB = 3'b001, StateC = 3'b010, StateD = 3'b011, StateE = 3'b100, StateF = 3'b101;
  9. assign P_req = NB || SB;
  10. always @ (posedge clk) begin
  11. if (reset == 1'b1)begin
  12. currentState <= StateA;
  13. previous_State <= StateA;
  14. end
  15. else
  16. currentState <= nextState;
  17. end
  18. always @ (*) begin
  19. case(currentState)
  20. StateA: begin
  21. if (P_req == 1'b1)
  22. nextState <= StateB;
  23. else if (count_1[3] == 1 && count_1[2] == 1 && count_1[1] == 0 && count_1[0] == 0 && neg_P_req == 1'b1)
  24. nextState <= StateA;
  25. else
  26. nextState <= StateA;
  27. end
  28. StateB:
  29. if (count[2:0] <= 3'b010)
  30. nextState <= StateC;
  31. else begin
  32. nextState <= StateB;
  33. count[2:0] <= 3'b000;
  34. end
  35. StateC:
  36. if (count[2:0] <= 3'b010)
  37. nextState <= StateD;
  38. else begin
  39. nextState <= StateC;
  40. count[2:0] <= 3'b000;
  41. end
  42. StateD:
  43. if (count[2:0] <= 3'b110)
  44. nextState <= StateD;
  45. else begin
  46. nextState <= StateE;
  47. count[2:0] <= 3'b000;
  48. end
  49. StateE:
  50. if (count[2:0] <= 3'b110)
  51. nextState <= StateE;
  52. else begin
  53. nextState <= StateF;
  54. count[2:0] <= 3'b000;
  55. end
  56. StateF:
  57. if (count[2:0] <= 3'b010)
  58. nextState <= StateF;
  59. else begin
  60. nextState <= StateA;
  61. count[2:0] <= 3'b000;
  62. previous_State <= StateF;
  63. end
  64. endcase
  65. end
  66. always @ (*) begin
  67. case(currentState)
  68. StateA: begin
  69. fsm_TR = 1'b0;
  70. fsm_TY = 1'b0;
  71. fsm_TG = 1'b1;
  72. fsm_PR = 1'b1;
  73. fsm_PG = 1'b0;
  74. end
  75. StateB: begin
  76. fsm_TR = 1'b0;
  77. fsm_TY = 1'b1;
  78. fsm_TG = 1'b0;
  79. fsm_PR = 1'b1;
  80. fsm_PG = 1'b0;
  81. end
  82. StateC: begin
  83. fsm_TR = 1'b1;
  84. fsm_TY = 1'b0;
  85. fsm_TG = 1'b0;
  86. fsm_PR = 1'b1;
  87. fsm_PG = 1'b0;
  88. end
  89. StateD: begin
  90. if (count[2:0] <= 3'b110)begin
  91. fsm_TR = 1'b1;
  92. fsm_TY = 1'b0;
  93. fsm_TG = 1'b0;
  94. fsm_PR = 1'b0;
  95. fsm_PG = 1'b1;
  96. end
  97. else begin
  98. fsm_TR = 1'b1;
  99. fsm_TY = 1'b0;
  100. fsm_TG = 1'b0;
  101. fsm_PR = 1'b0;
  102. fsm_PG = 1'b0;
  103. end
  104. end
  105. StateE: begin
  106. fsm_TR = 1'b1;
  107. fsm_TY = 1'b0;
  108. fsm_TG = 1'b0;
  109. fsm_PR = 1'b0;
  110. if (count[2:0] <= 3'b110) begin
  111. if (count[0] == 0)
  112. fsm_PG = 1'b1;
  113. else
  114. fsm_PG = 1'b0;
  115. end
  116. end
  117. StateF: begin
  118. fsm_TR = 1'b1;
  119. fsm_TY = 1'b0;
  120. fsm_TG = 1'b0;
  121. fsm_PR = 1'b1;
  122. fsm_PG = 1'b0;
  123. end
  124. endcase
  125. end
  126. endmodule
  127. module counter (input clk, reset, output reg [2:0] count);
  128. always @ (posedge clk) begin
  129. if (reset == 1'b1)
  130. count <= 3'b000;
  131. else
  132. count <= count + 3'b001;
  133. end
  134. endmodule
  135. module controller (input clk, reset, NB, SB, output reg TR, TY, TG, PR, PG);
  136. wire [2:0]counter_count;
  137. reg neg_P_req;
  138. reg [3:0]count_1;
  139. wire fsm_P_req;
  140. reg previous_State;
  141. wire fsm_TR, fsm_TY, fsm_TG, fsm_PR, fsm_PG;
  142. counter M1 (.clk(clk), .reset(reset), .count(counter_count));
  143. FSM M2 (.clk(clk), .reset(reset), .NB(NB), .SB(SB), .fsm_TR(fsm_TR), .fsm_TY(fsm_TY), .fsm_TG(fsm_TG), .fsm_PR(fsm_PR), .fsm_PG(fsm_PG));
  144. always @ (posedge clk)begin
  145. if (reset == 1'b1) begin
  146. count_1 <= 4'b0000;
  147. neg_P_req <= 1'b0;
  148. end
  149. else begin
  150. if (M2.currentState == M2.StateA && count_1[3] == 1 && count_1[2] == 1 && count_1[1] == 0 && count_1[0] == 0 && M2.P_req == 1'b1 && M2.previous_State == M2.StateF)
  151. neg_P_req <= ~M2.P_req;
  152. if (M2.currentState == M2.StateA && M2.previous_State == M2.StateF)
  153. count_1 <= count_1 + 4'b0001;
  154. end
  155. end
  156. assign fsm_P_req = M2.P_req;
  157. always @(posedge clk) begin
  158. TR <= fsm_TR;
  159. TY <= fsm_TY;
  160. TG <= fsm_TG;
  161. PR <= fsm_PR;
  162. PG <= fsm_PG;
  163. end
  164. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement