Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. //explicacion
  2. //mas explicacion
  3.  
  4.  
  5. module stop_light(north, south, east, west, clk, reset);
  6.  
  7. input clk, reset;
  8. output reg [3:0] north, east, south, west;
  9. reg [3:0] clk_count = 4'b0000;
  10.  
  11. // N/S
  12. parameter NS_LAR = 3'b001; // state 001 => north/south light left arrow/red AND east/west light red
  13. parameter NS_G = 3'b010; // state 010 => north/south light green AND east/west light red
  14. parameter NS_Y = 3'b011; // state 011 => north/south light yellow AND east/west light red
  15.  
  16. // E/W
  17. parameter EW_LAR = 3'b100; // state 100 => north/south light red AND east/west light left arrow/red
  18. parameter EW_G = 3'b101; // state 101 => north/south light red AND east/west light green
  19. parameter EW_Y = 3'b110; // state 110 => north/south light red AND east/west light yellow
  20.  
  21. // Reset / All lights to red
  22. parameter NSEW_red = 3'b111; // state 111 => north/south light red AND east/west light red (RESET STATE)
  23.  
  24. // Our current state value (intitally set to turn north/south left arrow/red light on and east/west light red)
  25. reg [3:0] state = NS_LAR;
  26.  
  27. always @(posedge clk)
  28. begin
  29.  
  30. // check to see if the reset switch has been set
  31. if (reset == 1)
  32. begin
  33. state = NSEW_red;
  34. clk_count = 4b'0000;
  35. end //end if
  36.  
  37. else
  38. begin
  39. if (state == NS_LAR)
  40. begin
  41. if (clk_count = 4b'1010) //once the clock has reached 10 seconds
  42. begin
  43. state = NS_G;
  44. clk_count = 4'b0000;
  45. end //end if
  46. else
  47. begin
  48. state = NS_LAR;
  49. clk_count = clk_count + 1;
  50. end //end else
  51. end //end NS_LAR case
  52.  
  53. if (state == NS_G)
  54. begin
  55. if (clk_count = 4b'1111) //once the clock has reached 15 seconds
  56. begin
  57. state = NS_Y;
  58. clk_count = 4'b0000;
  59. end //end if
  60. else
  61. begin
  62. state = NS_G;
  63. clk_count = clk_count + 1;
  64. end //end else
  65. end //end NS_G case
  66.  
  67. if (state == NS_Y)
  68. begin
  69. if (clk_count = 4b'0101) //once the clock has reached 5 seconds
  70. begin
  71. state = EW_LAR;
  72. clk_count = 4'b0000;
  73. end //end if
  74. else
  75. begin
  76. state = NS_Y;
  77. clk_count = clk_count + 1;
  78. end //end else
  79. end //end NS_Y case
  80.  
  81. if (state == EW_LAR)
  82. begin
  83. if (clk_count = 4b'1010) //once the clock has reached 10 seconds
  84. begin
  85. state = EW_G;
  86. clk_count = 4'b0000;
  87. end //end if
  88. else
  89. begin
  90. state = EW_LAR;
  91. clk_count = clk_count + 1;
  92. end //end else
  93. end //end EW_LAR case
  94.  
  95. if (state == EW_G)
  96. begin
  97. if (clk_count = 4b'1111) //once the clock has reached 15 seconds
  98. begin
  99. state = EW_Y;
  100. clk_count = 4'b0000;
  101. end //end if
  102. else
  103. begin
  104. state = EW_G;
  105. clk_count = clk_count + 1;
  106. end //end else
  107. end //end EW_G case
  108.  
  109. if (state == EW_Y)
  110. begin
  111. if (clk_count = 4b'0101) //once the clock has reached 5 seconds
  112. begin
  113. state = NS_LAR;
  114. clk_count = 4'b0000;
  115. end //end if
  116. else
  117. begin
  118. state = EW_Y;
  119. clk_count = clk_count + 1;
  120. end //end else
  121. end //end EW_Y case
  122.  
  123. end //end else
  124.  
  125. end //end always posedge clk
  126.  
  127. always @(state)
  128. begin
  129.  
  130. if (state == NS_LAR)
  131. north = 4'b1001;
  132. south = 4'b1001;
  133. east = 4b'0001;
  134. west = 4b'0001;
  135.  
  136. if (state == NS_G)
  137. north = 4b'0100;
  138. south = 4b'0100;
  139. east = 4b'0001;
  140. west = 4b'0001;
  141.  
  142. if (state == NS_Y)
  143. north = 4b'0010;
  144. south = 4b'0010;;
  145. east = 4b'0001;
  146. west = 4b'0001;
  147.  
  148. if (state == EW_LAR)
  149. north = 4b'0001;
  150. south = 4b'0001;
  151. east = 4'b1001;
  152. west = 4'b1001;
  153.  
  154. if (state == EW_G)
  155. north = 4b'0001;
  156. south = 4b'0001;
  157. east = 4b'0100;
  158. west = 4b'0100;
  159.  
  160. if (state == EW_Y)
  161. north = 4b'0001;
  162. south = 4b'0001;
  163. east = 4b'0010;;
  164. west = 4b'0010;;
  165.  
  166. if (state == NSEW_red)
  167. north = 4b'0001;
  168. south = 4b'0001;
  169. east = 4b'0001;
  170. west = 4b'0001;
  171.  
  172. end //end always state
  173.  
  174.  
  175. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement