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

	PORT (
    TBCLK		: 	IN std_logic; 								--! Time-base clock.
	 CTR			:	IN std_logic_vector(15 DOWNTO 0); 	--! Time-base counter value.
	 CTR_PRD 	: 	IN std_logic;								--! Time-base counter equal to the specified period (TBCTR = TBPRD).
	 CTR_ZERO	: 	IN std_logic;								--! Time-base counter equal to zero (TBCTR = 0x0000).
	 
	 CMPA			:	IN std_logic_vector(15 DOWNTO 0); 	--! The value in the active CMPA register
	 SHDWAMODE 	: 	IN std_logic; 								--! Counter-compare A (CMPA) Register Operating Mode.	
	 LOADAMODE 	: 	IN std_logic_vector(1 DOWNTO 0);		--! Active Counter-Compare A (CMPA) Load From Shadow Select Mode
	 SHDWAFULL 	: 	OUT std_logic;
	 CMPAE		:	OUT std_logic; 							--! CTR = CMPA
	 
	 CMPB			:	IN std_logic_vector(15 DOWNTO 0); 	--! The value in the active CMPB register
	 SHDWBMODE 	: 	IN std_logic; 								--! Counter-compare B (CMPB) Register Operating Mode.
	 LOADBMODE 	: 	IN std_logic_vector(1 DOWNTO 0); 	--! Active Counter-Compare B (CMPB) Load From Shadow Select Mode
	 SHDWBFULL 	: 	OUT std_logic;
	 CMPBE		:	OUT std_logic);						 	--! CTR = CMPB
	
END counter_compare;
 

ARCHITECTURE Behavioral OF counter_compare IS
	
	SIGNAL CMPA_active: std_logic_vector(15 DOWNTO 0) := (OTHERS => '0');
	SIGNAL CMPB_active: std_logic_vector(15 DOWNTO 0) := (OTHERS => '0');

	SIGNAL CMPA_event: std_logic := '0';
	SIGNAL CMPB_event: std_logic := '0';

BEGIN

	--! Shadow register load mode select
	
	CMPA_event <= 	CTR_ZERO 				WHEN LOADAMODE = "00" ELSE
						CTR_PRD  				WHEN LOADAMODE = "01" ELSE
						CTR_ZERO OR CTR_PRD  WHEN LOADAMODE = "10" ELSE
						CMPA_event;
	
	CMPB_event <= 	CTR_ZERO 				WHEN LOADBMODE = "00" ELSE
						CTR_PRD  				WHEN LOADBMODE = "01" ELSE
						CTR_ZERO OR CTR_PRD  WHEN LOADBMODE = "10" ELSE
						CMPB_event;		
	
	--! Assign value to CMPA_active from shadow register
	
	CMPA_active <= CMPA WHEN (CMPA_event = '1' AND SHDWAMODE = '0') ELSE
						CMPA WHEN (SHDWAMODE = '1'); 
						
	CMPB_active <= CMPB WHEN (CMPB_event = '1' AND SHDWBMODE = '0') ELSE
						CMPB WHEN (SHDWBMODE = '1'); 
						
	--! Main Counter Compare process
	
	CMPAE <= '1' WHEN CTR = CMPA_active ELSE '0';
	CMPBE <= '1' WHEN CTR = CMPB_active ELSE '0';

	
END Behavioral;