----------------------------------------------------------------------------------
-- Company: APC
-- Engineer: CC
-- 
-- Create Date:    16:23:52 09/06/2016 
-- Design Name: 
-- Module Name:    command_decoder - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: decode commands reeived from UDP
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Revision 0.02- add reset counters procedure
-- Revision 0.04- 64 bits command, 2 lsb of command_i are cmd type,  others bits are command data
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
--use IEEE.NUMERIC_STD.ALL;


entity command_decoder is
	generic(
		 g_data_rx_width  : natural := 16
		 );
    Port ( clk_i 					: in  STD_LOGIC;
			  command_i				: in  STD_LOGIC_VECTOR (g_data_rx_width-1 downto 0);
		     data_valid_i			: in  STD_LOGIC;
		     cnt_en_o				: out STD_LOGIC := '0';
			  ext_trig_en_o		: out STD_LOGIC := '0';
			  set_throttle_val_o : out STD_LOGIC := '0';
		     mac_addr_o 	 		: out STD_LOGIC_VECTOR (47 downto 0);
			  ext_trig_date_o 	: out STD_LOGIC_VECTOR (52 downto 0):= (others => '0');
			  throttle_val_o  	: out STD_LOGIC_VECTOR (15 downto 0)
           );
end command_decoder;

architecture Behavioral of command_decoder is

	signal cmd : STD_LOGIC_VECTOR(1 downto 0) := "00";
	
begin

	cmd <= command_i(1 downto 0);


cmd_proc : PROCESS(clk_i)		  
   BEGIN
   		
   	if rising_edge(clk_i)then
			ext_trig_en_o	 	  <= '0';
			set_throttle_val_o  <= '0';	
			
			if data_valid_i = '1' then				
				case cmd is		
				when  "00" => 						-- set/reset command					
					--'1' enable trigggers, start counters	& send an external trigger
					--'0' reset counters : default state
					cnt_en_o  		 <= command_i(4);
					ext_trig_en_o	 <= '0';
					
				when  "01" => 						-- retrieve destination MAC address
					mac_addr_o 		 <= command_i(51 downto 4);
					ext_trig_en_o	 <= '0';
					
				when  "10" => 						-- external trigger date
					ext_trig_date_o <= command_i(56 downto 4);
					ext_trig_en_o	 <= '1';
				
				when  "11" => 						--  set new throttle value	
					throttle_val_o  		<= command_i(19 downto 4);
					set_throttle_val_o	<= '1';
					
				when others => null;
						
				end case; 				
			end if;	
	end if;	
 END PROCESS;
	
		
	END Behavioral;
