Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ECSE 222
- -- Lab 3
- -- Group 26
- -- Kelly Ma (260444591)
- -- Sofia Dieguez (260836263)
- -- This project creates a finite state machine (FSM)
- -- The FSM acts as a bi-directional counter
- library IEEE;
- use IEEE.STD_LOGIC_1164.ALL;
- use IEEE.NUMERIC_STD.ALL;
- -- Declare the FSM entity
- -- The entity has 4 boolean inputs: enable, direction, reset, clk
- -- The entity has one 4-bit vector output: count
- entity g26_FSM is
- Port (enable : in std_logic;
- direction : in std_logic;
- reset : in std_logic;
- clk : in std_logic;
- count : out std_logic_vector(3 downto 0));
- end g26_FSM;
- -- Declare FSM architecture
- architecture behaviour of g26_FSM is
- -- Create a type signal to to store the counter's states
- -- Each state corresponds to a letter
- -- Each letter then corresponds to an integer
- -- For example, A -> 1, B -> 2, etc.
- type state is (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
- -- Create signal to hold the state of count
- signal count_tmp : state;
- begin
- -- Process block for the sequential circuit that keeps track of reset and clk
- -- Where other variables are synchronized with clk
- process(clk, reset) begin
- -- Check the value of reset first
- -- Reset is an active-low, asynchronous signal
- if (reset = '0') then
- -- Enter the state machine in state A
- count_tmp <= A;
- elsif (rising_edge(clk)) then
- -- Check if enable is at logical high
- -- Enable is active-high
- if (enable = '1') then
- -- Use a case-statement for the different states
- -- Step 1: Check for the counting direction (1 is up, 0 is down)
- -- Step 2: Assign the next state based on counting direction
- case count_tmp is
- when A => -- The current state is A
- if (direction = '1') then
- count_tmp <= B;
- else
- count_tmp <= O;
- end if;
- when B =>
- if (direction = '1') then
- count_tmp <= C;
- else
- count_tmp <= A;
- end if;
- when C =>
- if (direction = '1') then
- count_tmp <= D;
- else
- count_tmp <= B;
- end if;
- when D =>
- if (direction = '1') then
- count_tmp <= E;
- else
- count_tmp <= C;
- end if;
- when E =>
- if (direction = '1') then
- count_tmp <= F;
- else
- count_tmp <= D;
- end if;
- when F =>
- if (direction = '1') then
- count_tmp <= G;
- else
- count_tmp <= E;
- end if;
- when G =>
- if (direction = '1') then
- count_tmp <= H;
- else
- count_tmp <= F;
- end if;
- when H =>
- if (direction = '1') then
- count_tmp <= I;
- else
- count_tmp <= G;
- end if;
- when I =>
- if (direction = '1') then
- count_tmp <= J;
- else
- count_tmp <= H;
- end if;
- when J =>
- if (direction = '1') then
- count_tmp <= K;
- else
- count_tmp <= I;
- end if;
- when K =>
- if (direction = '1') then
- count_tmp <= L;
- else
- count_tmp <= J;
- end if;
- when L =>
- if (direction = '1') then
- count_tmp <= M;
- else
- count_tmp <= K;
- end if;
- when M =>
- if (direction = '1') then
- count_tmp <= N;
- else
- count_tmp <= L;
- end if;
- when N =>
- if (direction = '1') then
- count_tmp <= O;
- else
- count_tmp <= M;
- end if;
- when O =>
- if (direction = '1') then
- count_tmp <= A;
- else
- count_tmp <= N;
- end if;
- end case;
- -- Enable is at logical-low
- else
- count_tmp <= count_tmp; -- Nothing happens
- end if;
- end if;
- end process;
- -- Determine the count output for a given count_tmp state
- -- Use a selected signal assignment to determine the output count
- WITH count_tmp SELECT
- count <="0001" when A, -- 1
- "0010" when B, -- 2
- "0100" when C, -- 4
- "1000" when D, -- 8
- "0011" when E, -- 3
- "0110" when F, -- 6
- "1100" when G, -- 12
- "1011" when H, -- 11
- "0101" when I, -- 5
- "1010" when J, -- 10
- "0111" when K, -- 7
- "1110" when L, -- 14
- "1111" when M, -- 15
- "1101" when N, -- 13
- "1001" when O, -- 9
- "0000" when OTHERS;
- end behaviour;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement