Advertisement
rustyelectron

edge_detector_fsm

Feb 24th, 2024
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.67 KB | None | 0 0
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4.  
  5. entity edge_detector is
  6.   port (
  7.     rst_i    : in  std_logic;
  8.     clk_i    : in  std_logic;
  9.     strobe_i : in  std_logic;
  10.     p1_o     : out std_logic;
  11.     p2_o     : out std_logic;
  12.     p3_o     : out std_logic);
  13. end entity edge_detector;
  14.  
  15. architecture rtl of edge_detector is
  16.     type FSM_L is (ZERO, EDGE, ONE);
  17.     type FSM_M is (ZERO, ONE);
  18.     type FSM_R is (ZERO, DELAY, ONE);
  19.    
  20.     signal cur_state_l_s: FSM_L := ZERO;
  21.     signal nxt_state_l_s: FSM_L := ZERO;
  22.    
  23.     signal cur_state_m_s: FSM_M := ZERO;
  24.     signal nxt_state_m_s: FSM_M := ZERO;
  25.    
  26.     signal cur_state_r_s: FSM_R := ZERO;
  27.     signal nxt_state_r_s: FSM_R := ZERO;
  28.  
  29. begin  -- architecture rtl
  30.     transitions: process(cur_state_l_s, strobe_i) is
  31.         begin
  32.             case cur_state_l_s is
  33.                 when ZERO =>
  34.                     nxt_state_l_s <= ZERO;
  35.                     if strobe_i = '1' then
  36.                         nxt_state_l_s <= EDGE;
  37.                     end if;  
  38.                 when EDGE =>
  39.                     nxt_state_l_s <= ZERO;
  40.                     if strobe_i = '1' then
  41.                         nxt_state_l_s <= ONE;
  42.                     end if;
  43.                 when ONE =>
  44.                     nxt_state_l_s <= ZERO;
  45.                     if strobe_i = '1' then
  46.                         nxt_state_l_s <= ONE;
  47.                     end if;
  48.             end case;
  49.         end process transitions;
  50.        
  51.     next_state_process: process(clk_i) is
  52.     begin
  53.         if rising_edge(clk_i) and clk_i = '1' then
  54.             if rst_i = '1' then
  55.                 cur_state_l_s <= ZERO;
  56.             else
  57.                 cur_state_l_s <= nxt_state_l_s;
  58.             end if;
  59.         end if;
  60.     end process next_state_process;
  61.                
  62.     output_process: process(cur_state_l_s) is
  63.     begin
  64.         p1_o <= '0';
  65. --        case cur_state_l_s is
  66. --            when ZERO =>
  67. --                p1_o <= '0';
  68. --            when EDGE =>
  69. --                p1_o <= '1';
  70. --            when ONE =>
  71. --                p1_o <= '0';
  72. --        end case;
  73.         if cur_state_l_s = EDGE then
  74.             p1_o <= '1';
  75.         end if;
  76.     end process output_process;
  77.    
  78. --    p2_o <= '0';
  79. --    p3_o <= '0';
  80.    
  81.     transitions_M: process(cur_state_m_s, strobe_i) is
  82.     begin
  83.         case cur_state_m_s is
  84.             when ZERO =>
  85.                 nxt_state_m_s <= ZERO;
  86.                 if strobe_i = '1' then
  87.                     nxt_state_m_s <= ONE;
  88.                 end if;
  89.             when ONE =>
  90.                 nxt_state_m_s <= ZERO;
  91.                 if strobe_i = '1' then
  92.                     nxt_state_m_s <= ONE;
  93.                 end if;
  94.         end case;
  95.     end process transitions_M;
  96.    
  97.     next_state_process_M: process(clk_i) is
  98.     begin
  99.         if rising_edge(clk_i) then
  100.             if rst_i = '1' then
  101.                 cur_state_m_s <= ZERO;
  102.             else
  103.                 cur_state_m_s <= nxt_state_m_s;
  104.             end if;
  105.         end if;
  106.     end process next_state_process_M;
  107.    
  108.     output_logic_M: process(cur_state_m_s) is
  109.     begin
  110.         p2_o <= '0';
  111.         if cur_state_m_s = ZERO and strobe_i = '1' then
  112.             p2_o <= '1';
  113.         end if;
  114.     end process output_logic_M;
  115.    
  116.     transitions_R: process(cur_state_r_s, strobe_i) is
  117.     begin
  118.         case cur_state_r_s is
  119.             when ZERO =>
  120.                 nxt_state_r_s <= ZERO;
  121.                 if strobe_i = '1' then
  122.                     nxt_state_r_s <= DELAY;
  123.                 end if;
  124.             when DELAY =>
  125.                 nxt_state_r_s <= ZERO;
  126.                 if strobe_i = '1' then
  127.                     nxt_state_r_s <= ONE;
  128.                 end if;
  129.             when ONE =>
  130.                 nxt_state_r_s <= ZERO;
  131.                 if strobe_i = '1' then
  132.                     nxt_state_r_s <= ONE;
  133.                 end if;
  134.         end case;
  135.     end process transitions_R;
  136.    
  137.     next_state_process_R: process(clk_i) is
  138.     begin
  139.         if rising_edge(clk_i) then
  140.             if rst_i = '1' then
  141.                 cur_state_r_s <= ZERO;
  142.             else
  143.                 cur_state_r_s <= nxt_state_r_s;
  144.             end if;
  145.         end if;
  146.     end process next_state_process_R;
  147.    
  148.     output_logic_R: process(cur_state_r_s) is
  149.     begin
  150.         p3_o <= '0';
  151.         case cur_state_r_s is
  152.             when ZERO =>
  153.                 if strobe_i = '1' then
  154.                     p3_o <= '1';
  155.                 end if;
  156.             when DELAY =>
  157.                 p3_o <= '1';
  158.             when ONE => null;
  159.         end case;
  160.     end process output_logic_R;
  161.    
  162. end architecture rtl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement