Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. -- ECSE 222
  2. -- Lab 3
  3. -- Group 26
  4. -- Kelly Ma (260444591)
  5. -- Sofia Dieguez (260836263)
  6.  
  7. -- This project creates a finite state machine (FSM)
  8. -- The FSM acts as a bi-directional counter
  9. library IEEE;
  10. use IEEE.STD_LOGIC_1164.ALL;
  11. use IEEE.NUMERIC_STD.ALL;
  12.  
  13. -- Declare the FSM entity
  14. -- The entity has 4 boolean inputs: enable, direction, reset, clk
  15. -- The entity has one 4-bit vector output: count
  16.  
  17. entity g26_FSM is
  18. Port (enable : in std_logic;
  19. direction : in std_logic;
  20. reset : in std_logic;
  21. clk : in std_logic;
  22. count : out std_logic_vector(3 downto 0));
  23. end g26_FSM;
  24.  
  25. -- Declare FSM architecture
  26. architecture behaviour of g26_FSM is
  27.  
  28. -- Create a type signal to to store the counter's states
  29. -- Each state corresponds to a letter
  30. -- Each letter then corresponds to an integer
  31. -- For example, A -> 1, B -> 2, etc.
  32. type state is (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
  33.  
  34. -- Create signal to hold the state of count
  35. signal count_tmp : state;
  36.  
  37. begin
  38.  
  39. -- Process block for the sequential circuit that keeps track of reset and clk
  40. -- Where other variables are synchronized with clk
  41. process(clk, reset) begin
  42. -- Check the value of reset first
  43. -- Reset is an active-low, asynchronous signal
  44. if (reset = '0') then
  45. -- Enter the state machine in state A
  46. count_tmp <= A;
  47. elsif (rising_edge(clk)) then
  48. -- Check if enable is at logical high
  49. -- Enable is active-high
  50. if (enable = '1') then
  51. -- Use a case-statement for the different states
  52. -- Step 1: Check for the counting direction (1 is up, 0 is down)
  53. -- Step 2: Assign the next state based on counting direction
  54. case count_tmp is
  55. when A => -- The current state is A
  56. if (direction = '1') then
  57. count_tmp <= B;
  58. else
  59. count_tmp <= O;
  60. end if;
  61. when B =>
  62. if (direction = '1') then
  63. count_tmp <= C;
  64. else
  65. count_tmp <= A;
  66. end if;
  67. when C =>
  68. if (direction = '1') then
  69. count_tmp <= D;
  70. else
  71. count_tmp <= B;
  72. end if;
  73. when D =>
  74. if (direction = '1') then
  75. count_tmp <= E;
  76. else
  77. count_tmp <= C;
  78. end if;
  79. when E =>
  80. if (direction = '1') then
  81. count_tmp <= F;
  82. else
  83. count_tmp <= D;
  84. end if;
  85. when F =>
  86. if (direction = '1') then
  87. count_tmp <= G;
  88. else
  89. count_tmp <= E;
  90. end if;
  91. when G =>
  92. if (direction = '1') then
  93. count_tmp <= H;
  94. else
  95. count_tmp <= F;
  96. end if;
  97. when H =>
  98. if (direction = '1') then
  99. count_tmp <= I;
  100. else
  101. count_tmp <= G;
  102. end if;
  103. when I =>
  104. if (direction = '1') then
  105. count_tmp <= J;
  106. else
  107. count_tmp <= H;
  108. end if;
  109. when J =>
  110. if (direction = '1') then
  111. count_tmp <= K;
  112. else
  113. count_tmp <= I;
  114. end if;
  115. when K =>
  116. if (direction = '1') then
  117. count_tmp <= L;
  118. else
  119. count_tmp <= J;
  120. end if;
  121. when L =>
  122. if (direction = '1') then
  123. count_tmp <= M;
  124. else
  125. count_tmp <= K;
  126. end if;
  127. when M =>
  128. if (direction = '1') then
  129. count_tmp <= N;
  130. else
  131. count_tmp <= L;
  132. end if;
  133. when N =>
  134. if (direction = '1') then
  135. count_tmp <= O;
  136. else
  137. count_tmp <= M;
  138. end if;
  139. when O =>
  140. if (direction = '1') then
  141. count_tmp <= A;
  142. else
  143. count_tmp <= N;
  144. end if;
  145. end case;
  146. -- Enable is at logical-low
  147. else
  148. count_tmp <= count_tmp; -- Nothing happens
  149. end if;
  150. end if;
  151. end process;
  152.  
  153. -- Determine the count output for a given count_tmp state
  154. -- Use a selected signal assignment to determine the output count
  155. WITH count_tmp SELECT
  156. count <="0001" when A, -- 1
  157. "0010" when B, -- 2
  158. "0100" when C, -- 4
  159. "1000" when D, -- 8
  160. "0011" when E, -- 3
  161. "0110" when F, -- 6
  162. "1100" when G, -- 12
  163. "1011" when H, -- 11
  164. "0101" when I, -- 5
  165. "1010" when J, -- 10
  166. "0111" when K, -- 7
  167. "1110" when L, -- 14
  168. "1111" when M, -- 15
  169. "1101" when N, -- 13
  170. "1001" when O, -- 9
  171. "0000" when OTHERS;
  172.  
  173. end behaviour;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement