Advertisement
lukicdarkoo

Simple implementation of finite-state machine

Dec 12th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 3.55 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date:    23:36:39 12/12/2014
  6. -- Design Name:
  7. -- Module Name:    MainModule - Behavioral
  8. -- Project Name:
  9. -- Target Devices:
  10. -- Tool versions:
  11. -- Description:
  12. --
  13. -- Dependencies:
  14. --
  15. -- Revision:
  16. -- Revision 0.01 - File Created
  17. -- Additional Comments:
  18. --
  19. ----------------------------------------------------------------------------------
  20. library IEEE;
  21. use IEEE.STD_LOGIC_1164.ALL;
  22. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  23.  
  24. -- Uncomment the following library declaration if using
  25. -- arithmetic functions with Signed or Unsigned values
  26. --use IEEE.NUMERIC_STD.ALL;
  27.  
  28. -- Uncomment the following library declaration if instantiating
  29. -- any Xilinx primitives in this code.
  30. --library UNISIM;
  31. --use UNISIM.VComponents.all;
  32.  
  33. entity MainModule is
  34.     Port ( iCLK : in  STD_LOGIC;
  35.            inRST : in  STD_LOGIC;
  36.            iSEL : in  STD_LOGIC_VECTOR (1 downto 0);
  37.            iDURATION : in  STD_LOGIC_VECTOR (7 downto 0);
  38.            iSTART : in  STD_LOGIC;
  39.            o150W : out  STD_LOGIC;
  40.            o300W : out  STD_LOGIC;
  41.            o650W : out  STD_LOGIC;
  42.            o800W : out  STD_LOGIC;
  43.            oWARN : out  STD_LOGIC);
  44. end MainModule;
  45.  
  46. architecture Behavioral of MainModule is
  47.  
  48. -- FSM
  49. type tSTATES is (IDLE, HEAT, WARNING);
  50. signal sSTATE: tSTATES;
  51. signal sWarn_EN: STD_LOGIC;
  52. signal sHeat_EN: STD_LOGIC;
  53.  
  54. -- WarnCounter
  55. signal sWarnCNT: STD_LOGIC_VECTOR(2 downto 0);
  56. signal sWarnCNT_TC: STD_LOGIC;
  57. signal sWarnCNT_EN: STD_LOGIC;
  58.  
  59. -- HeatCounter
  60. signal sHeatCNT: STD_LOGIC_VECTOR(7 downto 0);
  61. signal sHeatCNT_TC: STD_LOGIC;
  62. signal sHeatCNT_EN: STD_LOGIC;
  63.  
  64. begin
  65.     WarnCounter: process (iCLK, inRST) begin
  66.         if (inRST = '0') then
  67.             sWarnCNT <= (others => '0');
  68.             sWarnCNT_TC <= '0';
  69.         elsif (rising_edge(iCLK)) then
  70.             if (sWarnCNT_EN = '1') then
  71.                 if (sWarnCNT = 7) then
  72.                     sWarnCNT_TC <= not sWarnCNT_TC;
  73.                     sWarnCNT <= (others => '0');
  74.                 else
  75.                     sWarnCNT <= sWarnCNT + 1;
  76.                 end if;
  77.             end if;
  78.         end if;
  79.     end process;
  80.  
  81.     HeatCounter: process (iCLK, inRST) begin
  82.         if (inRST = '0') then
  83.             sHeatCNT <= (others => '0');
  84.             sHeatCNT_TC <= '0';
  85.         elsif (rising_edge(iCLK)) then
  86.             if (sHeatCNT_EN = '1') then
  87.                 if (sHeatCNT = iDURATION - 1) then
  88.                     sHeatCNT_TC <= not sHeatCNT_TC;
  89.                     sHeatCNT <= (others => '0');
  90.                 else
  91.                     sHeatCNT <= sHeatCNT + 1;
  92.                 end if;
  93.             end if;
  94.         end if;
  95.     end process;
  96.    
  97.     FSM: process (iCLK, inRST) begin
  98.         if (inRST = '0') then
  99.             sSTATE <= IDLE;
  100.             sHeatCNT_EN <= '1';
  101.             sWarnCNT_EN <= '1';
  102.         elsif (rising_edge(iCLK)) then
  103.             case (sSTATE) is
  104.                 when IDLE => sHeatCNT_EN <= '0';
  105.                             sWarnCNT_EN <= '0';
  106.                             sWarn_EN <= '0';
  107.                             sHeat_EN <= '0';
  108.                            
  109.                             if (iSTART = '1') then
  110.                                 sSTATE <= HEAT;
  111.                             end if;
  112.                            
  113.                 when HEAT => sHeatCNT_EN <= '1';
  114.                             sWarnCNT_EN <= '0';
  115.                             sWarn_EN <= '1';
  116.                             sHeat_EN <= '0';
  117.                            
  118.                             if (sHeatCNT_TC = '1') then
  119.                                 sSTATE <= WARNING;
  120.                             end if;
  121.                            
  122.                 when WARNING => sHeatCNT_EN <= '0';
  123.                             sWarnCNT_EN <= '1';
  124.                             sWarn_EN <= '0';
  125.                             sHeat_EN <= '1';
  126.                            
  127.                             if (sWarnCNT_TC = '1') then
  128.                                 sSTATE <= IDLE;
  129.                             end if;
  130.             end case;      
  131.         end if;
  132.     end process;
  133.    
  134.     oWARN <= sWARN_EN;
  135.    
  136.     -- Demux
  137.     o150W <= sHEAT_EN when iSEL = "00" else '0';
  138.     o300W <= sHEAT_EN when iSEL = "01" else '0';
  139.     o650W <= sHEAT_EN when iSEL = "10" else '0';
  140.     o800W <= sHEAT_EN when iSEL = "11" else '0';
  141.    
  142. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement