-------------------------------------------------------------------------------
-- Title      : Interrupt Generation
-------------------------------------------------------------------------------
-- File       : Interrupt_Generation.vhd
-- Author     : Haris Akkool
-- Company    : Microsemi Corporation
-- Device     : SmartFusion
-- Standard   : VHDL
-------------------------------------------------------------------------------
-- Description: This code creates two timers Timer1 and Timer2. Timer 1 generates
--              interrupt for every 2000001 clock cycles of FAB_CLK
--              Timer 2 generates interrupt for every 10000004 clock cycles.
--              Asynchronous input SWITCH_N is taken as interrupt. This input is
--              synchronized and detected using pulse detection logic.
-------------------------------------------------------------------------------
-- Copyright (c) 2011   Microsemi Corporation
--                      All rights reserved.
-------------------------------------------------------------------------------
-- Revisions  : V1.0
-------------------------------------------------------------------------------

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
use IEEE.std_logic_arith.all;

entity Interrupt_Generation is
  port ( RESET_N : in std_logic;
         FAB_CLK : in std_logic;
         FABINT  : out std_logic;
         GPI_0   : out std_logic);


end Interrupt_Generation;

architecture behav of Interrupt_Generation is

signal counter1,counter2   : std_logic_vector(31 downto 0);

begin

-- Timer1 implemetation and generation of Fabric intrrupt upon the expiry of
-- timer value. The timer value is 950000 clock cycles of FAB_CLK 

process (FAB_CLK,RESET_N)
begin
   if(RESET_N = '0') then
       counter1  <= (others => '0');
       FABINT    <= '0';
   elsif (FAB_CLK'event and FAB_CLK='1') then
      if(counter1 = X"000E7EF0") then
        counter1 <= (others => '0');
        FABINT   <= '1';
      else
        counter1 <= counter1 + '1';
        FABINT   <= '0';
      end if;
    end if;
end process;

-- Timer2 implemetation and generation of Fabric intrrupt upon the expiry of
-- timer value. The timer value is 2950000 clock cycles of FAB_CLK 

process (FAB_CLK, RESET_N)
begin
   if(RESET_N = '0') then
       counter2   <= (others => '0');
       GPI_0      <= '0';
   elsif (FAB_CLK'event and FAB_CLK='1') then
      if(counter2 = X"002D0370") then
        counter2  <= (others => '0');
        GPI_0     <= '1';
      else
        counter2  <= counter2 + '1';
        GPI_0     <= '0';
      end if;
    end if;
end process;

end behav;






