----------------------------------------------------------------------------------
-- 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.03- 64 bits command, 2 lsb of command_i are cmd type,  others bits are command data
-- Revision 0.04- add change UDP port command
-- 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 -- lowest value
		 );
    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';
			  set_dst_ip_addr_o	: out STD_LOGIC := '0';
			  spi_en_o 				: out STD_LOGIC := '1';
			  set_dst_port_o		: out STD_LOGIC := '0';
			  dst_port_o 	 		: out STD_LOGIC_VECTOR (15 downto 0);
		     dst_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);
			  dst_ip_addr_o		: out STD_LOGIC_VECTOR (31 downto 0)
           );
end command_decoder;

architecture Behavioral of command_decoder is

	signal cmd : STD_LOGIC_VECTOR(3 downto 0) := x"0";
	signal dst_port 	 		: STD_LOGIC_VECTOR (15 downto 0):= (others => '0');
	signal dst_mac_addr 	 	: STD_LOGIC_VECTOR (47 downto 0):= (others => '0');
	signal dst_ip_addr		: STD_LOGIC_VECTOR (31 downto 0):= (others => '0');
	--signal ext_trig_date 	: STD_LOGIC_VECTOR (52 downto 0):= (others => '0');
	--signal throttle_val  	: STD_LOGIC_VECTOR (15 downto 0):= (others => '0');
	
begin

	cmd <= command_i(3 downto 0);
	dst_mac_addr_o <= dst_mac_addr;
	dst_ip_addr_o	<= dst_ip_addr;
	dst_port_o 		<= dst_port;

cmd_proc : PROCESS(clk_i)		  
   BEGIN
   		
   	if rising_edge(clk_i)then
			ext_trig_en_o	 	  <= '0';
			set_throttle_val_o  <= '0';	
			set_dst_ip_addr_o	  <= '0';
			set_dst_port_o		  <= '0';
			
			if data_valid_i = '1' then				
				case cmd is		
				when  x"0" => 						-- 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  x"1" => 				-- set destination MAC address
					dst_mac_addr	 		<= command_i(51 downto 4);
					ext_trig_en_o	 		<= '0';
					
				when  x"2" => 				-- external trigger date
					ext_trig_date_o 		<= command_i(56 downto 4);
					ext_trig_en_o	 		<= '1';
				
				when  x"3" => 				--  set new throttle value	
					throttle_val_o  		<= command_i(19 downto 4);
					set_throttle_val_o	<= '1';
					ext_trig_en_o	 		<= '0';
					
				when  x"4" => 				--  set destination IP address
					dst_ip_addr 			<= command_i(35 downto 4);
					set_dst_ip_addr_o		<= '1';
					ext_trig_en_o	 		<= '0';
					
				when  x"5" => 				--  enable/disable SPI
					--'1' enable SPI: default state
					--'0' disable SPI
					spi_en_o 				<= command_i(4);
					ext_trig_en_o	 		<= '0';
					
				when  x"6" => 				--  set destination UDP port
					dst_port 				<= command_i(19 downto 4);
					set_dst_port_o			<= '1';
					ext_trig_en_o	 		<= '0';
					
				when others => null;
						
				end case; 				
			end if;	
	end if;	
 END PROCESS;
	
		
	END Behavioral;
