Guest User

Untitled

a guest
Feb 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. module traffic
  2. (
  3. input wire clk,reset,
  4. output reg[2:0] N,S,E,W
  5. );
  6.  
  7. localparam[2:0] S0=3'b000,
  8. S1=3'b001,
  9. S2=3'b010,
  10. S3=3'b011,
  11. S4=3'b100,
  12. S5=3'b101,
  13. S6=3'b110,
  14. S7=3'b111;
  15.  
  16. reg[2:0]state,next_state;
  17. reg[24:0]count1=0;
  18. reg[24:0]count2=0;
  19. reg count20_en=0,count3_en=0;
  20. reg delay20s=0,delay3s=0;
  21. wire clk_en;
  22.  
  23. always @(posedge clk,posedge reset)
  24. if(reset)
  25. state <= S0;
  26. else
  27. state <= next_state;
  28.  
  29. always @*
  30. begin
  31. next_state = state;
  32. N = 0;
  33. E = 0;
  34. S = 0;
  35. W = 0;
  36. case(state)
  37.  
  38. S0:begin
  39. N=3'b001;
  40. E=3'b100;
  41. S=3'b100;
  42. W=3'b100;
  43. count20_en = 1;
  44. count3_en = 0;
  45. if(delay20s)
  46. next_state = S1;
  47. else
  48. next_state = S0;
  49. end
  50.  
  51. S1:begin
  52. N=3'b010;
  53. E=3'b010;
  54. S=3'b100;
  55. W=3'b100;
  56. count20_en = 0;
  57. count3_en = 1;
  58. if(delay3s)
  59. next_state = S2;
  60. else
  61. next_state = S1;
  62. end
  63.  
  64. S2: begin
  65. N = 3'b100;
  66. E = 3'b001;
  67. S = 3'b100;
  68. W = 3'b100;
  69. count20_en = 1;
  70. count3_en = 0;
  71. if(delay20s)
  72. next_state = S3;
  73. else
  74. next_state = S2;
  75. end
  76.  
  77. S3: begin
  78. N= 3'b100;
  79. E = 3'b010;
  80. S = 3'b010;
  81. W = 3'b100;
  82. count20_en = 0;
  83. count3_en = 1;
  84. if(delay3s)
  85. next_state = S4;
  86. else
  87. next_state = S3;
  88. end
  89.  
  90. S4: begin
  91. N = 3'b100;
  92. E = 3'b100;
  93. S = 3'b001;
  94. W = 3'b100;
  95. count20_en = 1;
  96. count3_en = 0;
  97. if(delay20s)
  98. next_state = S5;
  99. else
  100. next_state = S4;
  101. end
  102.  
  103. S5: begin
  104. N = 3'b100;
  105. E = 3'b100;
  106. S = 3'b010;
  107. W = 3'b010;
  108. count20_en = 0;
  109. count3_en = 1;
  110. if(delay3s)
  111. next_state = S6;
  112. else
  113. next_state = S5;
  114. end
  115.  
  116. S6: begin
  117. N = 3'b100;
  118. E = 3'b100;
  119. S = 3'b100;
  120. W = 3'b001;
  121. count20_en = 1;
  122. count3_en = 0;
  123. if(delay20s)
  124. next_state = S7;
  125. else
  126. next_state = S6;
  127. end
  128.  
  129. S7: begin
  130. N = 3'b010;
  131. E = 3'b100;
  132. S = 3'b100;
  133. W = 3'b010;
  134. count20_en = 0;
  135. count3_en = 1;
  136. if(delay3s)
  137. next_state = S0;
  138. else
  139. next_state = S7;
  140. end
  141.  
  142. default: next_state = S0;
  143. endcase
  144. end
  145.  
  146.  
  147. always @(posedge clk)
  148. begin
  149. if(clk_en == 1)
  150. begin
  151. if(count20_en || count3_en)
  152. count2 = count2 + 1;
  153. if(count2 == 19 & count20_en)
  154. begin
  155. delay20s = 1;
  156. delay3s = 0;
  157. count2 = 0;
  158. end
  159. else if(count2 == 2 & count3_en)
  160. begin
  161. delay20s =0;
  162. delay3s = 1;
  163. end
  164. else
  165. begin
  166. delay20s = 0;
  167. delay3s = 0;
  168. end
  169. end
  170. end
  171.  
  172. always @(posedge clk)
  173. begin
  174. count1 <= count1 + 1;
  175. if(count1 == 3)
  176. count1 <= 0;
  177. end
  178. assign clk_en = (count1 == 3) ? 1 : 0;
  179.  
  180.  
  181.  
  182. endmodule
Add Comment
Please, Sign In to add comment