Index: firmware/FAD/FACT_FAD_20MHz_VAR_PS/FACT_FAD_lib/hdl/w5300_modul.vhd
===================================================================
--- firmware/FAD/FACT_FAD_20MHz_VAR_PS/FACT_FAD_lib/hdl/w5300_modul.vhd	(revision 10136)
+++ firmware/FAD/FACT_FAD_20MHz_VAR_PS/FACT_FAD_lib/hdl/w5300_modul.vhd	(revision 10137)
@@ -1,21 +1,3 @@
-----------------------------------------------------------------------------------
--- Company: 
--- Engineer: 
--- 
--- Create Date:    11:48:48 11/10/2009 
--- Design Name: 
 -- Module Name:    w5300_modul - Behavioral 
--- Project Name: 
--- Target Devices: 
--- Tool versions: 
--- Description: 
---
--- Dependencies: 
---
--- Revision: 
--- Revision 0.01 - File Created
--- Additional Comments: 
---
-----------------------------------------------------------------------------------
 library IEEE;
 use IEEE.STD_LOGIC_1164.ALL;
@@ -24,9 +6,4 @@
 library FACT_FAD_lib;
 use FACT_FAD_lib.fad_definitions.ALL;
-
----- Uncomment the following library declaration if instantiating
----- any Xilinx primitives in this code.
---library UNISIM;
---use UNISIM.VComponents.all;
 
 ENTITY w5300_modul IS
@@ -57,37 +34,52 @@
       c_trigger_enable: out std_logic := '0';
       c_trigger_mult: out std_logic_vector (7 DOWNTO 0) := (OTHERS => '1'); --subject to changes
-      --
+
+	  -- FAD configuration signals:
+	  ------------------------------------------------------------------------------
+		-- start entire configuration chain
       new_config : OUT std_logic := '0';
       config_started : in std_logic;
+		-- read/write configRAM
       config_addr : out std_logic_vector (7 downto 0);
       config_data : inout std_logic_vector (15 downto 0) := (others => 'Z');
       config_wr_en : out std_logic := '0';
       config_rd_en : out std_logic := '0';
-      -- --
       config_rw_ack, config_rw_ready : in std_logic;
-      -- --
       config_busy : in std_logic;
+	  ------------------------------------------------------------------------------
       
+	  -- MAC/IP calculation signals:
+	  ------------------------------------------------------------------------------
       MAC_jumper : in std_logic_vector (1 downto 0);
       BoardID : in std_logic_vector (3 downto 0);
       CrateID : in std_logic_vector (1 downto 0);
+	  ------------------------------------------------------------------------------
       
+	  -- user controllable enable signals
+	  ------------------------------------------------------------------------------
+	  trigger_enable : out std_logic := '0'; -- default triggers are NOT accepted
       denable : out std_logic := '0'; -- default domino wave off
       dwrite_enable : out std_logic := '0'; -- default DWRITE low.
       sclk_enable : out std_logic := '1'; -- default DWRITE HIGH.
-      ps_direction : out std_logic := '1'; -- default phase shift upwards
+      srclk_enable : out std_logic := '1'; -- default SRCLK on.
+	  ------------------------------------------------------------------------------
+	  
+	  -- ADC CLK generator, is able to shift phase with respect to X_50M
+	  -- these signals control the behavior of the digital clock manager (DCM)
+	  ------------------------------------------------------------------------------
+	  ps_direction : out std_logic := '1'; -- default phase shift upwards
       ps_do_phase_shift : out std_logic := '0'; --pulse this to phase shift once
       ps_reset : out std_logic := '0'; -- pulse this to reset the variable phase shift
-      
-      srclk_enable : out std_logic := '1'; -- default SRCLK on.
-      
-      trigger_enable : out std_logic := '0'; -- default triggers are NOT accepted
-      
+      ------------------------------------------------------------------------------
+      	  
+	  -- signals used to control FAD LED bahavior:
+	  -- one of the three LEDs is used for com-status info
+	  ------------------------------------------------------------------------------
       socks_waiting : out std_logic;
       socks_connected: out std_logic
+	  ------------------------------------------------------------------------------
+	  
+	  
    );
-
--- Declarations
-
 END w5300_modul ;
 
@@ -154,19 +146,13 @@
 signal data_valid_int : std_logic := '0';
 
+
+-- signals used for MAC/IP calculation:
+-- these 2 synch in, the jumper state.
 signal FAD_in_cam : std_logic := '0';
 signal FAD_at_ETHZ : std_logic := '0';
+-- these 2 synch in the FMP lines encoding the FAD position in the cam. 
 signal bid : std_logic_vector (3 downto 0);
 signal cid : std_logic_vector (1 downto 0);
-
-
--- only for debugging
---signal error_cnt : std_logic_vector (7 downto 0) := (others => '0');
---signal last_trigger_id : std_logic_vector (15 downto 0) := (others => '0');
-
-
--- signals for different socket modes: DN 04.01.11
-signal socket_nr_counter : integer range 1 to 7 :=1; --used to determine which socket is used for data sending
-signal socket_send_mode : std_logic := '0'; -- if 0 data is send via socket 0; if 1 data is send via the other sockets.
-
+-- these are just used as local variables, to make reading easier.
 signal mac_loc : mac_type;
 signal ip_loc : ip_type;
@@ -174,4 +160,18 @@
 signal netmask_loc : ip_type;
 
+	-- signals for synching in asynchronous input signals
+	------------------------------------------------------------------------------
+	signal w5300_interrupt_sr : std_logic_vector(1 downto 0); 
+	------------------------------------------------------------------------------
+
+
+-- only for debugging
+--signal error_cnt : std_logic_vector (7 downto 0) := (others => '0');
+--signal last_trigger_id : std_logic_vector (15 downto 0) := (others => '0');
+
+
+-- signals for different socket modes: DN 04.01.11
+signal socket_nr_counter : integer range 1 to 7 :=1; --used to determine which socket is used for data sending
+signal socket_send_mode : std_logic := '0'; -- if 0 data is send via socket 0; if 1 data is send via the other sockets.
 
 begin
@@ -182,16 +182,20 @@
 
 
-	w5300_init_proc : process (clk, int)
+	w5300_proc : process (clk)
 	begin
+		if rising_edge (clk) then
+		-- synch asynchronous input in:
+		w5300_interrupt_sr <= w5300_interrupt_sr(1) & int;
 		
-		if rising_edge (clk) then
-
-			-- Interrupt low
-			if (int = '0') and (interrupt_ignore = '0') then
+			-- interrupt is handled synchronously
+			-- W5300 pulls low its interrpt line in case of:
+			--	??? I don't know ... connection loss ???
+			
+			if (w5300_interrupt_sr = "01") and (interrupt_ignore = '0') then
 				case state_interrupt_1 is
 					when IR1_01 =>
 						int_flag <= '1';
 						busy <= '1';
-						state_interrupt_1 <= IR1_02;
+						state_interrupt_1 <= IR1_02;  --wait one cycle
 					when IR1_02 =>
 						state_interrupt_1 <= IR1_03;
@@ -200,9 +204,9 @@
 						socket_cnt <= "000";
 						ram_access <= '0';
-						zaehler <= X"00000";
-						count <= "000";
+						zaehler <= X"00000";  	--what is this zaehler counting?
+						count <= "000";			--what is this count counting?
 						int_flag <= '0';
 						interrupt_ignore <= '1';
-						state_interrupt_1 <= IR1_04;
+						state_interrupt_1 <= IR1_04;  --this state is not existing? bad coding habit???
 					when others =>
 						null;
@@ -220,15 +224,22 @@
 								next_state <= INTERRUPT;
 								state_interrupt_2 <= IR2_02;
-							when IR2_02 =>
+								
+							-- check if it was a Sx Interrupt of Socket n
+							-- therfor: loop over all channel.
+							-- if the loop ended but no Sx Interrupt was found --> IR2_06
+							-- if an Sx Interrupt was found go to --> IR2_03 and check what happened.
+							when IR2_02 => 
 								if (data_read (conv_integer(socket_cnt)) = '1') then -- Sx Interrupt
 									state_interrupt_2 <= IR2_03;
 								else
-                  socket_cnt <= socket_cnt + 1;
-                  if (socket_cnt = 7) then
-                    state_interrupt_2 <= IR2_06;
-                  else
-                    state_interrupt_2 <= IR2_02;
-                  end if; 
+									socket_cnt <= socket_cnt + 1;
+									if (socket_cnt = 7) then
+										state_interrupt_2 <= IR2_06;
+									else
+										state_interrupt_2 <= IR2_02;  -- go on with loop
+									end if; 
 								end if;
+								
+							-- check the Interrupt register of the Socket, which caused the Interrupt.
 							when IR2_03 =>
 								par_addr <= W5300_S0_IR + socket_cnt * W5300_S_INC; -- Sx Interrupt Register
@@ -236,4 +247,5 @@
 								next_state <= INTERRUPT;
 								state_interrupt_2 <= IR2_04;
+							-- before checking what happened, clear the Interrupt register, so we can proceed.
 							when IR2_04 =>
 								par_addr <= W5300_S0_IR + socket_cnt * W5300_S_INC;
@@ -242,4 +254,7 @@
 								next_state <= INTERRUPT;
 								state_interrupt_2 <= IR2_05;
+							-- send the command to close this socket
+							-- then go back and read the main Interrupt register again, 
+							-- if this was not Socket 7 ... if it was Socket 7, we're done anyway.
 							when IR2_05 =>
 								par_addr <= W5300_S0_CR + socket_cnt * W5300_S_INC;
@@ -254,4 +269,5 @@
 								end if; 
 
+							-- we go on and reset, the W5300 and this entire state machine.
 							when IR2_06 =>
 								state_interrupt_1 <= IR1_01;
@@ -263,11 +279,11 @@
 					-- reset W5300
 					when RESET =>
-					  socket_send_mode <= '0';
-					  busy <= '1';
+						socket_send_mode <= '0';
+						busy <= '1';
 						zaehler <= zaehler + 1;
 						socks_waiting <= '0';
-            socks_connected <= '0';
-            wiz_reset <= '0';
---            led <= X"FF";
+						socks_connected <= '0';
+						wiz_reset <= '0';
+						--led <= X"FF";
 						if (zaehler >= X"00064") then -- wait 2µs
 							wiz_reset <= '1';
@@ -288,60 +304,60 @@
 					-- Init
 					when INIT =>
-					  -- status of MAC jumpers is synched in
-					  -- and Board- and CrateID are synched in
-					  FAD_in_cam <= MAC_jumper(1); -- see position of jumpers in FACT logbook
-            FAD_at_ETHZ <= MAC_jumper(0); -- MAC_jumper(1) is where D_T(7) was; MAC_jumper(0) is where D_T(6) was;
-            bid <= BoardID;
-            cid <= CrateID;
-            --
+						-- status of MAC jumpers is synched in
+						-- and Board- and CrateID are synched in
+						FAD_in_cam <= MAC_jumper(1); -- see position of jumpers in FACT logbook
+						FAD_at_ETHZ <= MAC_jumper(0); -- MAC_jumper(1) is where D_T(7) was; MAC_jumper(0) is where D_T(6) was;
+						bid <= BoardID;
+						cid <= CrateID;
 						par_addr <= W5300_MR;
 						par_data <= X"0000";
 						state_init <= WRITE_REG;
 						next_state <= LOCATE;
-					
+				
 					when LOCATE =>					  
-					  state_init <= IM;
-					  
-					  if (FAD_in_cam = '1') then
-					    -- if BID = "1111" and CID="11" then FAD is not really in cam
-              -- back to INIT !! endless loop
-              if (bid = "1111" and cid="11") then
-               -- this should never happen!!!!!
-               -- impossible to find this out, if in cam
-                state_init <= INIT;
-              else -- everything is fine
-                -- IP & MAC are calculated from BID & CID
-                -- code here
-                gateway_loc <= CAM_GATEWAY;
-                netmask_loc <= CAM_NETMASK;					        
-                mac_loc <= (CAM_MAC_prefix (0), CAM_MAC_prefix (1)  , conv_std_logic_vector ( conv_integer(cid)*10+conv_integer(bid)  , 16)   ); 
-                ip_loc <= ( CAM_IP_PREFIX(0) , CAM_IP_PREFIX(1) , IP_offset + conv_integer(cid) , IP_offset + conv_integer(bid) );
-              end if;
-					  else -- FAD is tested, either at ETHZ or at TUDO AND either with FMP or without.
-					    if ( FAD_at_ETHZ = '0' ) then
-					     -- easy FAD is at TUDO -> only one choice.
-					     mac_loc <= MAC_FAD0;
-               ip_loc <= IP_TUDO;
-               gateway_loc <= TUDO_GATEWAY;
-               netmask_loc <= TUDO_NETMASK;
-					    else -- FAD is at ETHZ but not in cam --> IP lookup table is needed.
-					      if (bid = "1111" and cid="11") then -- FAD is not in crate
-                  mac_loc <= MAC_FAD0;
-                  ip_loc <= IP_ETHZ_FAD0;
-                  gateway_loc <= ETHZ_GATEWAY;
-                  netmask_loc <= ETHZ_NETMASK;					        
-					      else
-					        -- FAD is at ETHZ and in crate:
-					        -- crate ID is not of importance.
-					        -- we only have 3 MACs and IPs so far, so only the first boardIDs are allowed.
-					        if ( conv_integer(bid) < MAC_LIST'length) then
-                    gateway_loc <= ETHZ_GATEWAY;
-                    netmask_loc <= ETHZ_NETMASK;					        
-                    mac_loc <= MAC_LIST(conv_integer(bid));
-      					        ip_loc <= IP_LIST(conv_integer(bid));
-   					        end if; -- conv_integer
-					      end if; -- bid=1111 & cid=11
-					    end if; --FAD_at_ETHZ = 0
-					  end if; --FAD_in_cam = 1
+						state_init <= IM;
+
+						if (FAD_in_cam = '1') then
+							if (bid = "1111" and cid="11") then
+								-- if BID = "1111" and CID="11" then FAD is not really in cam
+								-- I don't know what to do now.
+								-- I could wait a long time, and then just assume, I was not in cam.
+								-- this should never happen!!!!!
+								-- impossible to find this out, if in cam
+								state_init <= INIT;
+							else -- everything is fine
+								-- IP & MAC are calculated from BID & CID
+								-- code here
+								gateway_loc <= CAM_GATEWAY;
+								netmask_loc <= CAM_NETMASK;					        
+								mac_loc <= (CAM_MAC_prefix (0), CAM_MAC_prefix (1)  , conv_std_logic_vector ( conv_integer(cid)*10+conv_integer(bid)  , 16)   ); 
+								ip_loc <= ( CAM_IP_PREFIX(0) , CAM_IP_PREFIX(1) , IP_offset + conv_integer(cid) , IP_offset + conv_integer(bid) );
+							end if;
+						else -- FAD is tested, either at ETHZ or at TUDO AND either with FMP or without.
+							if ( FAD_at_ETHZ = '0' ) then
+								-- easy FAD is at TUDO -> only one choice.
+								mac_loc <= MAC_FAD0;
+								ip_loc <= IP_TUDO;
+								gateway_loc <= TUDO_GATEWAY;
+								netmask_loc <= TUDO_NETMASK;
+							else -- FAD is at ETHZ but not in cam --> IP lookup table is needed.
+								if (bid = "1111" and cid="11") then -- FAD is not in crate
+									mac_loc <= MAC_FAD0;
+									ip_loc <= IP_ETHZ_FAD0;
+									gateway_loc <= ETHZ_GATEWAY;
+									netmask_loc <= ETHZ_NETMASK;					        
+								else
+									-- FAD is at ETHZ and in crate:
+									-- crate ID is not of importance.
+									-- we only have 3 MACs and IPs so far, so only the first boardIDs are allowed.
+									if ( conv_integer(bid) < MAC_LIST'length) then
+										gateway_loc <= ETHZ_GATEWAY;
+										netmask_loc <= ETHZ_NETMASK;					        
+										mac_loc <= MAC_LIST(conv_integer(bid));
+										ip_loc <= IP_LIST(conv_integer(bid));
+									end if; -- conv_integer
+								end if; -- bid=1111 & cid=11
+							end if; --FAD_at_ETHZ = 0
+						end if; --FAD_in_cam = 1
 						
 					-- Interrupt Mask
@@ -1003,5 +1019,5 @@
 		end if; -- rising_edge (clk)
 
-	end process w5300_init_proc;
+	end process w5300_proc;
 
 end Behavioral;
