Advertisement
Guest User

Untitled

a guest
Jul 14th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.42 KB | None | 0 0
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4.  
  5. Entity Mealy_SM IS Port
  6. (
  7.  clk_input, rst_n, x_motion, x_LT, x_ET, x_GT, ext_out      : IN std_logic;
  8.  clk_en, UD_en, ext_en, error                                           : OUT std_logic
  9.  );
  10. END ENTITY;
  11.  
  12.  
  13.  Architecture SM of Mealy_SM is
  14.  
  15.  
  16.  TYPE STATE_NAMES IS (STILL, MOVING, ERROR_STATE);   -- list all the STATE_NAMES values but use more meaningful names
  17.  
  18.  
  19.  SIGNAL current_state, next_state   :  STATE_NAMES;         -- signals of type STATE_NAMES
  20.  signal input_signals : std_logic_vector(4 downto 0);
  21.  
  22.  
  23.  BEGIN
  24.  
  25.  input_signals(4) <= x_motion;
  26.  input_signals(3) <=    x_LT;
  27.  input_signals(2) <= x_ET;
  28.  input_signals(1) <= x_GT;
  29.  input_signals(0) <= ext_out;
  30.  
  31.  
  32.  --------------------------------------------------------------------------------
  33.  --State Machine:
  34.  --------------------------------------------------------------------------------
  35.  
  36.  -- REGISTER_LOGIC PROCESS:
  37.  
  38. Register_Section: PROCESS (clk_input, rst_n)  -- this process synchronizes the activity to a clock
  39. BEGIN
  40.     IF (rst_n = '0') THEN
  41.         current_state <= STILL;
  42.     ELSIF(rising_edge(clk_input)) THEN
  43.         current_state <= next_State;
  44.     END IF;
  45. END PROCESS;   
  46.  
  47.  
  48.  
  49. -- TRANSITION LOGIC PROCESS
  50.  
  51. Transition_Section: PROCESS (input_signals, current_state)
  52.  
  53. BEGIN
  54.      CASE current_state IS
  55.           WHEN STILL =>    
  56.                 IF(input_signals = "0-0-0") THEN -- pb is pressed ,  not equal, not extended
  57.                     next_state <= MOVING;            
  58.                 ELSIF(input_signals = "0-0-1") THEN -- pb is pressed, target is not equal, extended
  59.                     next_state <= ERROR_STATE;
  60.                 ELSE
  61.                     next_state <= STILL;  -- stay in the same state otherwise
  62.                 END IF;    
  63.                
  64.              WHEN MOVING =>
  65.                 IF(input_signals = "1-0-0") THEN -- pb is not pressed and less than or greater than, not extended
  66.                     next_state <= STILL;
  67.                 ELSIF(input_signals = "-0100") THEN -- don't care if pressed or not, value is equal, not extended
  68.                     next_state <= STILL;
  69.                 ELSE
  70.                     next_state <= MOVING;
  71.                 END IF;
  72.                
  73.              WHEN ERROR_STATE =>
  74.                 IF(input_signals = "----0") THEN -- the only way to exit the error state is when you toggle the extender OFF
  75.                     next_state <= STILL;
  76.                 ELSE
  77.                     next_state <= ERROR_STATE; -- otherwise stay
  78.                 END IF;
  79.                
  80.                 WHEN OTHERS =>
  81.                next_state <= STILL;
  82.         END CASE;
  83.  
  84.  
  85.  END PROCESS;
  86.  
  87. -- DECODER SECTION PROCESS
  88.  
  89. Decoder_Section: PROCESS (input_signals,current_state)
  90.  
  91. BEGIN
  92.      CASE current_state IS
  93.          WHEN STILL =>     
  94.                 IF(input_signals = "1-0-0") THEN -- if pb not pressed, and val is greater than or less than, extender is not out, do nothing
  95.                     clk_en <= '0';
  96.                     UD_en  <= '-';
  97.                     ext_en <= '0';
  98.                     error    <= '0';
  99.                 ELSIF(input_signals = "-0100") THEN -- don't care if pb is pressed or not, if current equals target, extender is not out, enable extender
  100.                     clk_en <= '0';
  101.                     UD_en  <= '-';
  102.                     ext_en <= '1';
  103.                     error    <= '0';
  104.                 ELSIF(input_signals = "10101") THEN-- if the pb is not pressed and current equals target, and extender is out, disable extender
  105.                     clk_en <= '0';
  106.                     UD_en  <= '-';
  107.                     ext_en <= '0';
  108.                     error    <= '0';
  109.                 ELSIF(input_signals = "01000") THEN -- if the push button pressed and extender is retracted, inputs less than
  110.                     clk_en <= '1';                          -- enable clock and count up
  111.                     UD_en  <= '1';
  112.                     ext_en <= '0';
  113.                     error    <= '0';
  114.                 ELSIF(input_signals = "00010") THEN -- if the push button pressed and extender is retracted, inputs are  greater than
  115.                     clk_en <= '1';                          -- enable clock and count down
  116.                     UD_en  <= '0';
  117.                     ext_en <= '0';
  118.                     error    <= '0';
  119.                 END IF;
  120.            
  121.             WHEN MOVING =>
  122.                 IF(input_signals= "01000") THEN  -- if the push button is pressed and the value is less than, not extended, enable clock, count up
  123.                     clk_en <= '1';                         
  124.                     UD_en  <= '1';
  125.                     ext_en <= '0';
  126.                     error    <= '0';
  127.                 ELsIF(input_signals= "000010") THEN  -- if the push button is pressed and the value is greater than,and not extended, then enable clock, count down
  128.                     clk_en <= '1';                         
  129.                     UD_en  <= '0';
  130.                     ext_en <= '0';
  131.                     error    <= '0';
  132.                 END IF;
  133.                
  134.             WHEN ERROR_STATE =>
  135.                 IF(input_signals = "----1") THEN--once in the error state, input with the extender toggle on  gets an error output
  136.                     clk_en <= '0';                         
  137.                     UD_en  <= '-';
  138.                     ext_en <= '0';
  139.                     error    <= '1';   
  140.                 END IF;
  141.                                
  142.       END CASE;
  143.      
  144.  END PROCESS;
  145.  
  146.  END ARCHITECTURE SM;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement