LIBRARY ieee;
USE ieee.std_logic_1164. all;
USE ieee.numeric_std.all;
 
ENTITY event_trigger IS

	PORT (
    TBCLK		: 	IN std_logic;
	 
	 CTR_CMPA	:	IN std_logic;
	 CTR_CMPB	:	IN std_logic;
	 CTR_dir 	:  IN std_logic;
	 CTR_zero	:  IN std_logic;
	 CTR_prd 	:  IN std_logic;
	 
	 EPWMxSOCA	:  OUT std_logic;
	 EPWMxSOCB	:  OUT std_logic;
	 EPWMxINTn	:  OUT std_logic;
	 
	 SOCB_EN		: 	IN std_logic;
	 SOCB_SEL	:	IN	std_logic_vector(2 DOWNTO 0);
	 SOCA_EN		: 	IN std_logic;
	 SOCA_SEL	:	IN	std_logic_vector(2 DOWNTO 0);
	 
	 INT_EN		:	IN	std_logic;
	 INT_SEL		:	IN std_logic_vector(2 DOWNTO 0);
	 
	 SOCB_CNT	:	OUT std_logic_vector(1 DOWNTO 0);
	 SOCB_PRD	:	IN std_logic_vector(1 DOWNTO 0);
	 SOCA_CNT	:	OUT std_logic_vector(1 DOWNTO 0);
	 SOCA_PRD	:	IN std_logic_vector(1 DOWNTO 0);
	 INT_CNT		:	OUT std_logic_vector(1 DOWNTO 0);
	 INT_PRD		:	IN std_logic_vector(1 DOWNTO 0);
	 
	 SOCB_ETFLG	:	OUT	std_logic;
	 SOCA_ETFLG	:	OUT	std_logic;
	 INT_ETFLG	:	OUT 	std_logic;
	 
	 SOCB_ETCLR	:	IN	std_logic;
	 SOCA_ETCLR	:	IN	std_logic;
	 INT_ETCLR	:	IN std_logic;

	 SOCB_ETFRC	:	IN std_logic;
	 SOCA_ETFRC	:	IN std_logic;
	 INT_ETFRC	:	IN std_logic		  
	);
END event_trigger;
 

ARCHITECTURE Behavioral OF event_trigger IS
	
	SIGNAL CTRU_CMPA		:  std_logic := '0';
	SIGNAL CTRU_CMPB		:  std_logic := '0';		
	SIGNAL CTRD_CMPA		:  std_logic := '0';
	SIGNAL CTRD_CMPB		:  std_logic := '0';
	
	
BEGIN

	-- direction qualifier
	
	CTRU_CMPA <= '1' WHEN (CTR_CMPA = '1' AND CTR_dir = '1') ELSE '0';
	CTRD_CMPA <= '1' WHEN (CTR_CMPA = '1' AND CTR_dir = '0') ELSE '0';
	CTRU_CMPB <= '1' WHEN (CTR_CMPB = '1' AND CTR_dir = '1') ELSE '0';
	CTRD_CMPB <= '1' WHEN (CTR_CMPB = '1' AND CTR_dir = '0') ELSE '0';
	
	-- pulse generators
	
	interrupt_generator: ENTITY work.event_trigger_generator GENERIC MAP(INT_GEN => TRUE)	PORT MAP(TBCLK => TBCLK,
																																	CTRU_CMPA => CTRU_CMPA,
																																	CTRD_CMPA => CTRD_CMPA,
																																	CTRU_CMPB => CTRU_CMPB,
																																	CTRD_CMPB => CTRD_CMPB,
																																	CTR_zero => CTR_zero,
																																	CTR_prd => CTR_prd,
																																	ETSEL => INT_SEL,
																																	ETFRC => INT_ETFRC,
																																	ETPS => INT_PRD,
																																	ETSEN => INT_EN,
																																	ETCLR => INT_ETCLR,
																																	ETCNT => INT_CNT,
																																	OUTPUT => EPWMxINTn, 
																																	ETFLG => INT_ETFLG);
																																	
	SOCA_generator: ENTITY work.event_trigger_generator GENERIC MAP(INT_GEN => FALSE	)		PORT MAP(TBCLK => TBCLK,
																																	CTRU_CMPA => CTRU_CMPA,
																																	CTRD_CMPA => CTRD_CMPA,
																																	CTRU_CMPB => CTRU_CMPB,
																																	CTRD_CMPB => CTRD_CMPB,
																																	CTR_zero => CTR_zero,
																																	CTR_prd => CTR_prd,
																																	ETSEL => SOCA_SEL,
																																	ETFRC => SOCA_ETFRC,
																																	ETPS => SOCA_PRD,
																																	ETSEN => SOCA_EN,
																																	ETCLR => SOCA_ETCLR,
																																	ETCNT => SOCA_CNT,
																																	OUTPUT => EPWMxSOCA, 
																																	ETFLG => SOCA_ETFLG);	
																																	
	SOCB_generator: ENTITY work.event_trigger_generator GENERIC MAP(INT_GEN => FALSE	)		PORT MAP(TBCLK => TBCLK,
																																	CTRU_CMPA => CTRU_CMPA,
																																	CTRD_CMPA => CTRD_CMPA,
																																	CTRU_CMPB => CTRU_CMPB,
																																	CTRD_CMPB => CTRD_CMPB,
																																	CTR_zero => CTR_zero,
																																	CTR_prd => CTR_prd,
																																	ETSEL => SOCB_SEL,
																																	ETFRC => SOCB_ETFRC,
																																	ETPS => SOCB_PRD,
																																	ETSEN => SOCB_EN,
																																	ETCLR => SOCB_ETCLR,
																																	ETCNT => SOCB_CNT,
																																	OUTPUT => EPWMxSOCB, 
																																	ETFLG => SOCB_ETFLG);
	

END Behavioral;