1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4.  
  5. library work;
  6.  
  7. entity fsm is
  8. port(
  9. clk : in std_logic;
  10. rstn : in std_logic
  11. );
  12. end entity;
  13.  
  14. architecture rtl of fsm is
  15.  
  16. type state_type is (S0, S1);
  17.  
  18. type reg_type is record
  19. state : state_type;
  20. end record;
  21.  
  22. signal r, rin : reg_type;
  23.  
  24. begin
  25.  
  26. comb: process(r)
  27. variable v : reg_type;
  28. begin
  29. v := r;
  30.  
  31. case r.state is
  32. when S0 =>
  33. v.state := S1;
  34.  
  35. when S1 =>
  36. v.state := S0;
  37. end case;
  38.  
  39. rin <= v;
  40. end process;
  41.  
  42. reg: process(clk)
  43. begin
  44. if rising_edge(clk) then
  45. if rstn='0' then
  46. r <= (state => S0);
  47. else
  48. r <= rin;
  49. end if;
  50. end if;
  51. end process;
  52.  
  53. end rtl;