Advertisement
Aftaza

main-fsmCounter

Oct 29th, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 2.26 KB | Source Code | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date:    11:36:07 10/29/2023
  6. -- Design Name:
  7. -- Module Name:    fsmCounter - 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. use ieee.numeric_std.all;
  24.  
  25. -- Uncomment the following library declaration if using
  26. -- arithmetic functions with Signed or Unsigned values
  27. --use IEEE.NUMERIC_STD.ALL;
  28.  
  29. -- Uncomment the following library declaration if instantiating
  30. -- any Xilinx primitives in this code.
  31. --library UNISIM;
  32. --use UNISIM.VComponents.all;
  33.  
  34. entity fsmCounter is
  35.     Port ( clk : in  STD_LOGIC;
  36.            pb1 : in  STD_LOGIC;
  37.            pb2 : in  STD_LOGIC;
  38.            led : out  STD_LOGIC_VECTOR (3 downto 0));
  39. end fsmCounter;
  40.  
  41. architecture Behavioral of fsmCounter is
  42.     type state_type is (idle, counting, max);
  43.     signal state, next_state : state_type;
  44.     signal r_reg : UNSIGNED(3 downto 0);
  45.     signal r_next : UNSIGNED(3 downto 0);
  46.  
  47. begin
  48.  
  49.     -- register and clock
  50.     process(clk, pb2)
  51.     begin
  52.    
  53.         if(pb2 = '1') then
  54.             state <= idle;
  55.         elsif(clk'event and clk = '1') then
  56.             state <= next_state;
  57.             r_reg <= r_next;
  58.         end if;
  59.    
  60.     end process;
  61.    
  62.     -- State logic
  63.     process(state, pb1, pb2)
  64.     begin
  65.    
  66.         case state is
  67.             when idle =>
  68.                 if pb1 = '1' then
  69.                     r_next <= r_reg + 1;
  70.                     next_state <= counting;
  71.                 else
  72.                     r_next <= (others => '0');
  73.                     next_state <= idle;
  74.                 end if;
  75.             when counting =>
  76.                 if pb1 = '1' then
  77.                     r_next <= r_reg + 1;
  78.                     next_state <= counting;
  79.                 else
  80.                     r_next <= r_reg;
  81.                 end if;
  82.                 if r_reg = "1111" then
  83.                     next_state <= max;
  84.                 else
  85.                     next_state <= counting;
  86.                 end if;
  87.             when max =>
  88.                 r_next <= r_reg;
  89.                 next_state <= max;
  90.         end case;
  91.        
  92.     end process;
  93.    
  94.     -- output
  95.     process(state, r_reg)
  96.     begin
  97.        
  98.         case state is
  99.             when idle | counting | max =>
  100.                 led <= STD_LOGIC_VECTOR(r_reg);
  101.         end case;
  102.        
  103.     end process;
  104.  
  105. end Behavioral;
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement