----------------------------------------------------------------------------------
-- Company: 
-- Engineer: CC
-- 
-- Create Date:    10:06:08 16/06/2017
-- Design Name: 
-- Module Name:    event_counter_flag - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 0.01
-- Revision 0.01 - File Created
-- Additional Comments: 
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;

entity event_counter_flag is

    Port (
		clk_i				: in std_logic;
		rst_i				: in std_logic;
		event_i 			: in std_logic;
--		ack_flag_i		: in STD_LOGIC;
		flag_o			: out std_logic;
		event_cnt_o		: out std_logic_vector(31 downto 0) 
	 );
end event_counter_flag;

architecture Behavioral of event_counter_flag is

	signal event_cnt 		: unsigned(31 downto 0) := (others =>'0');
	signal event_detect 	: std_logic_vector(2 downto 0) := "111";
	signal event_pulse	: std_logic := '0';
	--place FF iin the same slice for timing 
	attribute ASYNC_REG : string;
	attribute ASYNC_REG of event_detect : signal is "TRUE";
	
begin

--flag_o	<= event_pulse;
	
cnt_proc: PROCESS (clk_i)
       BEGIN	  
			
			if rising_edge(clk_i)  then
				if (rst_i = '1') then
					event_cnt 	<= (others =>'0');
					flag_o		<= '0';
				elsif (event_pulse = '1') then  				-- event rising edge	sync
					event_cnt 	<=  event_cnt + 1;
					flag_o		<= '1';
				else
					flag_o		<= '0';				
				end if;
			end if;
  end process;	
    
  
evt_detect_proc: PROCESS (clk_i)
       BEGIN	  
			
			if rising_edge(clk_i)  then	
				if (rst_i = '1') then
					event_detect	<= (others => '1');					
				else 
--					event_detect(0) 	<= event_i;
--					event_detect(1)	<= event_detect(0);
--					event_detect(2)	<= event_detect(1);
					event_detect	 	<= event_detect(1 downto 0) & event_i;
					event_pulse			<= not event_detect(2) and event_detect(1);
				end if;
			end if;
  end process;		
  
	event_cnt_o <= std_logic_vector(event_cnt);	
	

	
end Behavioral;
