Advertisement
Guest User

Untitled

a guest
May 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.91 KB | None | 0 0
  1. -- Author: Maksim Kazak
  2. -- 12 variant
  3. -- q11 = "001", q2 = "010", q3 = "100"
  4. -- y1 = "001", y2 = "010", y3 = "100"
  5.  
  6. library ieee;
  7. use ieee.std_logic_1164.all;
  8.  
  9. entity moore_machine is
  10.   port (
  11.     clock, reset: in std_logic;
  12.     data_out: out std_logic_vector (2 downto 0);
  13.     data_in: in std_logic_vector (2 downto 0)
  14.   );
  15. end moore_machine;
  16.  
  17. architecture behavioral1 of moore_machine is
  18.   type state_values is (t1, t22, t3);
  19.   signal pres_state, next_state: state_values;
  20. begin
  21.  
  22. statereg: process (clock, reset)
  23. begin
  24.   if (reset = '0') then
  25.     pres_state <= t1;
  26.   elsif (clock'event and clock = '1') then
  27.     pres_state <= next_state;
  28.   end if;
  29. end process statereg;
  30.  
  31. fsm: process (pres_state, data_in)
  32. begin
  33.   case pres_state is
  34.     when t1 =>
  35.       case data_in is
  36.         when "001" => next_state <= t3;
  37.         when "010" => next_state <= t22;
  38.         when "100" => next_state <= t1;
  39.         when others => next_state <= pres_state;
  40.       end case;
  41.     when t22 =>
  42.       case data_in is
  43.     when "001" => next_state <= t22;
  44.         when "010" => next_state <= t1;
  45.         when "100" => next_state <= t22;
  46.         when others => next_state <= pres_state;
  47.       end case;
  48.     when t3 =>
  49.       case data_in is
  50.         when "001" => next_state <= t1;
  51.         when "010" => next_state <= t22;
  52.         when "100" => next_state <= t3;
  53.         when others => next_state <= pres_state;
  54.       end case;
  55.     when others => next_state <= t1;
  56.   end case;
  57. end process fsm;
  58.  
  59. outputs: process (pres_state, data_in)
  60. begin
  61.   case pres_state is
  62.     when t1 => data_out <= "100";
  63.     when t22 => data_out <= "010";
  64.     when t3 => data_out <= "001";
  65.     when others => data_out <= (others => 'X');
  66.   end case;
  67. end process outputs;
  68.  
  69. end behavioral1;
  70.  
  71. architecture behavioral2 of moore_machine is
  72.   type state_values is (t1, t22, t3);
  73.   signal pres_state, next_state: state_values;
  74. begin
  75.  
  76. pres_state <= t1 when reset = '0' else
  77.               next_state when clock'event and clock ='1';
  78.  
  79. next_state <= t3 when data_in = "001" and pres_state = t1 else
  80.               t22 when data_in = "010" and pres_state = t1 else
  81.               t1 when data_in = "100" and pres_state = t1 else
  82.               t22 when data_in = "001" and pres_state = t22 else
  83.               t1 when data_in = "010" and pres_state = t22 else
  84.               t22 when data_in = "100" and pres_state = t22 else
  85.               t1 when data_in = "001" and pres_state = t3 else
  86.               t22 when data_in = "010" and pres_state = t3 else
  87.               t3 when data_in = "100" and pres_state = t3 else
  88.               pres_state;
  89.  
  90. data_out <= "100" when pres_state = t1 else
  91.             "010" when pres_state = t22 else
  92.             "001" when pres_state = t3 else
  93.             (others => 'X');
  94.  
  95. end behavioral2;
  96.  
  97. ARCHITECTURE behavioral3 OF moore_machine IS
  98.  
  99.    SUBTYPE STATE_TYPE IS
  100.       std_logic_vector(2 DOWNTO 0);
  101.  
  102.    -- Automatic Output Encoding
  103.    CONSTANT t1 : STATE_TYPE := "100";
  104.    CONSTANT t22 : STATE_TYPE := "010";
  105.    CONSTANT t3 : STATE_TYPE := "001";
  106.  
  107.    -- Declare current and next state signals
  108.    SIGNAL current_state : STATE_TYPE;
  109.    SIGNAL next_state : STATE_TYPE;
  110.  
  111. BEGIN
  112.  
  113.    -----------------------------------------------------------------
  114.    clocked_proc : PROCESS (
  115.       clock,
  116.       reset
  117.    )
  118.    -----------------------------------------------------------------
  119.    BEGIN
  120.       IF (reset = '0') THEN
  121.          current_state <= t1;
  122.       ELSIF (clock'EVENT AND clock = '1') THEN
  123.          current_state <= next_state;
  124.       END IF;
  125.    END PROCESS clocked_proc;
  126.  
  127.    -----------------------------------------------------------------
  128.    nextstate_proc : PROCESS (
  129.       current_state,
  130.       data_in
  131.    )
  132.    -----------------------------------------------------------------
  133.    BEGIN
  134.       CASE current_state IS
  135.          WHEN t1 =>
  136.             IF (data_in = "010") THEN
  137.                next_state <= t22;
  138.             ELSIF (data_in = "001") THEN
  139.                next_state <= t3;
  140.             ELSIF (data_in = "100") THEN
  141.                next_state <= t1;
  142.             ELSE
  143.                next_state <= t1;
  144.             END IF;
  145.          WHEN t22 =>
  146.             IF (data_in = "010") THEN
  147.                next_state <= t1;
  148.             ELSIF (data_in = "001" or data_in = "100") THEN
  149.                next_state <= t22;
  150.             ELSE
  151.                next_state <= t22;
  152.             END IF;
  153.          WHEN t3 =>
  154.             IF (data_in = "001") THEN
  155.                next_state <= t1;
  156.             ELSIF (data_in = "010") THEN
  157.                next_state <= t22;
  158.             ELSIF (data_in = "100") THEN
  159.                next_state <= t3;
  160.             ELSE
  161.                next_state <= t3;
  162.             END IF;
  163.          WHEN OTHERS =>
  164.             next_state <= t1;
  165.       END CASE;
  166.    END PROCESS nextstate_proc;
  167.  
  168.    -- State as Output
  169.    data_out <= current_state(2 DOWNTO 0);
  170.  
  171. END behavioral3;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement