------------------
-- version cris para ROSCTRL
-- abril 2006
-- 
-- a)modificado el ciclo de datos para poder leer de la fifo y escribir en el transmisor
--   simultanemente: se duplican las señales txena y read_fifo. 
-- b)limitador de palabras en lectura, word_count y max_word_count: valor fijado por 
--   defecto 200 palabres = 800 bytes => 10us @40MHz.
-- c)se leen las 4 primeras palabras en bloque: header y trailer (o dato) 
-- d)se activa lives con cada fifo: cuando esta inactivo debe resetear el contador, como 
--   hace time_start.
-- e)timer sincrono
------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_misc.or_reduce;
use ieee.std_logic_arith.all;
use work.pack.all;

entity rc_ros is
	port (
	clk: in std_logic;
--	reset: in std_logic;
	soft_reset:in std_logic;
-- 
	valen, hastoken, parcheck: in std_logic;
	evid: in std_logic_vector(11 downto 0);
--	ceros_id: in std_logic_vector (1 downto 0);
	txena_p, read_fifo_p, ros_done: out std_logic;
	lives_c4: out std_logic;
	fout: in std_logic_vector(15 downto 0);
-- modif
	s_data_p: out std_logic_vector(15 downto 0);
--
	ef, mask: in std_logic;--_vector(5 downto 0);
	db_in: in std_logic;--_vector(5 downto 0);
--	sel_read_p: out std_logic_vector(5 downto 0);
	hasunlock, paf, ff, ff_reg, lock: in std_logic;--_vector(5 downto 0);
	timed_out_p: out std_logic;--_vector(5 downto 0);
	evid_mis_p: out std_logic;--_vector(5 downto 0);
   enw_evid_mis: in std_logic;
	dis_timo_mask:in std_logic;
	enw_timed_out, dis_paf_warn, dis_evid_wrong, dis_head_trail: in std_logic;
   dis_evid_bloq: in std_logic;
	dis_ffreg_mask: in std_logic; --feb08
	dis_hul_mask: in std_logic; --16dic08
	dis_maxw_mask: in std_logic; --16dic08
	dis_errorw: in std_logic; --16dic08
	timeout_value: in std_logic_vector(11 downto 0);
	enw_maxw: in std_logic;
	maxw_reached_p: out std_logic;
	nmaxwords_p: in std_logic_vector (7 downto 0); --16dic08
	--
	resynch_to_p, resynch_evidmis_p, warn_PAF_p: in std_logic;
	resynch_FF_p, resynch_maxw_p: in std_logic
	);
end rc_ros;

architecture arch of rc_ros is

--type state_type_1 is (start_event, do_link, w_trans, event_done);
--signal main: state_type_1;
--
type state_type_2 is (
	start_ev,
	--inc_sel,  15 enero08, para ceros 4 no hace falta
	exit_w,	--16dic08
	error_word1,super_exit_w, --16dic08
	w_command, 
	--chk_mask,
	s_error_ff0, s_error_ff1, 
	--s_error_ff2,
	s_warn_paf0, s_warn_paf1, 
	--s_warn_paf2,
-------------
	w_ef_h0, 
	w_ef_h1, r_header1, w_ef_h2, r_header2,
	w_ef_t1, r_trailer1, r_trailer2,
	w_ef_t2,
	w_ef_ft1,
	s_header1, s_header2, s_dato_first1,
------------- 	 
	wait_token,
--	evid_wrong0, 16dic08
	evid_wrong1, evid_wrong2,
	-- evid_wrong3,
	send_dato1, send_dato2,
	w_ef_sd1, w_ef_sd2,
	w_ef_st1,
	forz_trailer1, forz_trailer2, 
   s_trailer1, 
-------
	error_fill, 
	s_timeout0, s_timeout1, s_timeout2,
   s_maxwc1, s_maxwc2             
);
signal state: state_type_2;

constant rob_id: std_logic_vector (4 downto 0):="11000";--24
signal fmt_header_1, fmt_header_2, fmt_timeout_1, fmt_timeout_2: std_logic_vector(15 downto 0);
signal fmt_error_ff_1, fmt_error_ff_2: std_logic_vector(15 downto 0);
signal fmt_warn_paf_1, fmt_warn_paf_2: std_logic_vector(15 downto 0);
--signal fmt_dato_1, fmt_dato_2, fmt_dato_alt_1: std_logic_vector(15 downto 0);
signal fmt_dato_1, fmt_dato_alt_1: std_logic_vector(15 downto 0);
signal fmt_evid_wrong_1, fmt_evid_wrong_2: std_logic_vector(15 downto 0);
signal fmt_trailer_1, fmt_trailer_2, fmt_trailer_alt_1:  std_logic_vector(15 downto 0);
signal fmt_maxwc_1, fmt_maxwc_2:  std_logic_vector(15 downto 0);
signal fmt_errorw_1, fmt_errorw_2:  std_logic_vector(15 downto 0); --16dic08

signal s_data: std_logic_vector(15 downto 0);
signal txena_dir, read_fifo_dir: std_logic;
signal word_count: std_logic_vector (11 downto 0);
--constant max_word_count: std_logic_vector(7 downto 0):= X"C8";	--200 palabras
signal max_word_count: std_logic_vector(7 downto 0);	
signal maxw_reached: std_logic;	
--------                                                   
signal header1_reg: std_logic_vector (7 downto 0);
signal header2_reg, trailer_reg: std_logic_vector (15 downto 0);  
signal time_start, time_out: std_logic;
--16dic08 signal new_fifo, this_ef, this_mask: std_logic;
signal this_ef, this_mask: std_logic;
signal this_hasunlock, this_paf, this_ff, this_ff_reg, this_lock, this_timed_out: std_logic;
signal this_maxw: std_logic;
signal timed_out: std_logic;--_vector ( 5 downto 0);
signal time_counter: std_logic_vector(12 downto 0);
signal trail,this_evid_mis : std_logic;
signal evid_mis: std_logic;--_vector(5 downto 0);
--signal sta_to: std_logic_vector(3 downto 0);
signal error_evid: std_logic;

signal dontread: std_logic; --16dic08 --vector(5 downto 0);
signal this_dontread: std_logic; --16dic08
signal err_dontread: std_logic; --16dic08
signal lives_in: std_logic;	   --16dic08

--16dic08 signal nolimit_maxw: std_logic;

begin

read_fifo_p <= read_fifo_dir and not this_ef; 
txena_p <= txena_dir;
s_data_p <= s_data;
timed_out_p <= timed_out;
evid_mis_p <= evid_mis;
max_word_count <= nmaxwords_p;-- 16dic08(7 downto 0);
--16dic08 nolimit_maxw  <= nmaxwords_p(8);
maxw_reached_p <= maxw_reached;
lives_c4 <= lives_in;	   --16dic08

---- formats ---- 						
fmt_header_1 <= "000" & rob_id & header1_reg;
fmt_header_2 <= header2_reg;

fmt_trailer_1 <= "001" & rob_id & fout(7 downto 0);
fmt_trailer_alt_1 <= "001" & rob_id & trailer_reg(7 downto 0);
fmt_trailer_2 <= fout;

fmt_dato_1 <= fout(14 downto 12) & '0' & parcheck & not(this_paf) & fout(9 downto 0);
fmt_dato_alt_1 <= trailer_reg(14 downto 12) & '0' & parcheck & not(this_paf) & trailer_reg(9 downto 0);
--fmt_dato_2 <= fout;

fmt_error_ff_1 <= "110" & "11111" & "011" & rob_id;
--fmt_error_ff_2 <= X"8000";
fmt_error_ff_2  <= '1' & resynch_FF_p & "00000000" & this_evid_mis & not(this_paf) & this_ef & this_lock & this_hasunlock & this_ff_reg;

fmt_warn_paf_1 <= "110" & "11111" & "010" & rob_id;
fmt_warn_paf_2  <= '1' & warn_PAF_p & "00000000" & this_evid_mis & not(this_paf) & this_ef & this_lock & this_hasunlock & this_ff_reg;

fmt_timeout_1 <= "110" & "11111" & "000" & rob_id;	-- time out 
fmt_timeout_2  <= '1' & resynch_to_p  & "00000000" & this_evid_mis & not(this_paf) & this_ef & this_lock & this_hasunlock & this_ff_reg;
 
fmt_evid_wrong_1 <= "110" & "11111" & "001" & rob_id;
fmt_evid_wrong_2 <= '1' & resynch_evidmis_p & "00" & evid;

-- max word count: error tipo 5 
fmt_maxwc_1 <= "110" & "11111" & "101" & rob_id;
fmt_maxwc_2  <= '1' & resynch_maxw_p & "00000000" & this_evid_mis & not(this_paf) & this_ef & this_lock & this_hasunlock & this_ff_reg;

--16dic08
fmt_errorw_1 <= "111" & "11111" & "011" & "00" & "100";
fmt_errorw_2  <= "1000" & "00000" & evid_mis & "00000"& dontread;

-- first rob identifier
--rob_id_start <= "00000" when ceros_id="00" else	--ceros 0 -> rob 0
--			 "00110" when ceros_id="01" else	--ceros 1 -> rob 6
--			 "01100" when ceros_id="10" else	--ceros 2 -> rob 12
--			 "10010";						--ceros 3 -> rob 18

-- señales del canal seleccionado
this_ef   <= ef;--sumprod(ef, sel_read);
this_mask <= mask;--sumprod (mask, sel_read);
this_hasunlock <= hasunlock;--sumprod (hasunlock, sel_read);	
this_paf <= paf;--sumprod (paf, sel_read);	--este paf es directamente de las fifos y es activo baja (si 0 es que se pasa el límite permitido de palabras minimas)
this_ff <= ff;--sumprod (ff, sel_read);	
this_ff_reg <= ff_reg;--sumprod(ff_reg, sel_read);	
this_lock <= lock;--sumprod (lock, sel_read);	
this_timed_out <= timed_out;--sumprod (timed_out, sel_read);	
this_maxw <= maxw_reached; 
this_evid_mis  <= evid_mis;--sumprod (evid_mis, sel_read);
this_dontread <= dontread; --16dic08

-- main FSM
--mainpr: process (clk, valen)
--begin
--	if valen='0' then
--	  main <= start_event;
--	  new_fifo <= '0';
--	  ros_done <= '0';
--	elsif clk'event and clk='1' then
	
--	case main is
		  
--	  when start_event =>
--	    new_fifo <= '1';
--	    main <= do_link;
	  
--	  when do_link =>
--	    new_fifo <= '0';
--	    main <= w_trans;
	        
--	  when w_trans => 
--	    if state=w_command then
--	         main <= event_done;
--	    end if;
	  
--	  when event_done =>
--	    ros_done <= '1';
    
--	  end case;

--	end if;

--end process mainpr;

-- sequencer
synch: process (clk, valen)
begin
	if valen='0' then
	  error_evid <= '0';
	  state <= start_ev;
	  ros_done <= '0';
	  read_fifo_dir <= '0';
	  txena_dir <= '0';
       word_count <= (others => '0');
 	  time_start <= '0';
	  s_data <= (others => '0');
	  lives_in <= '0';
	  trail <= '0';
	elsif clk'event and clk='1' then
	
  	case state is
	  
	  when start_ev =>
		lives_in <= '0'; --16dic08
		txena_dir <= '0';     
		ros_done <= '0';
 	     state <= w_command; --15dic08	  

--No deberia hacer falta este estado en ceros4
--	  when inc_sel =>
--		 txena_dir <= '0';     
--		 lives_in <= '1'; --16dic08
--	      if sel_read(5)='1' then   
--		    state <= exit_w; --15dic08	  
--	       else
--	          sel_read <= sel_read(4 downto 0) & '0';
  --   	     rob_id <= rob_id +1;
 --	   		state <= w_command; --15dic08	  
--		  end if;

	  when exit_w =>	--16dic08
		lives_in <= '1'; --15ener09 para ceros 4 
		if ((err_dontread='1') and (dis_errorw='0')) then
		    if (hastoken='1') then
		      	txena_dir <= '1';       
			   	s_data <= fmt_errorw_1;
			     state <= error_word1; --16dic08	  
		    else
				txena_dir <= '0';     
		     	ros_done <= '0';
		     	state <= exit_w; --15dic08	  
		    end if;
		else
			txena_dir <= '0';     
		     ros_done <= '1';
		     state <= super_exit_w; --15dic08	  
		end if;

	  when error_word1 =>  --16dic08
			lives_in <= '0'; --15ene09 para ceros 4
	      	txena_dir <= '1';       
		   	s_data <= fmt_errorw_2;
		     state <= super_exit_w; --16dic08	  

	  when super_exit_w =>  --16dic08
	   	lives_in <= '0';  --16dic08
		txena_dir <= '0';     
	     ros_done <= '1';
	     state <= super_exit_w; --15dic08	  
	  
  
	  when w_command =>
		error_evid <= '0';
    	  	lives_in <= '0';
		trail <= '0';
	     time_start <= '0';
	     read_fifo_dir <= '0';
	     txena_dir <= '0';
          word_count <= (others => '0');
		if dontread='0' then
			if this_ff='1' then
				state <= s_error_ff0;
			elsif (this_paf='0' and dis_paf_warn='0') then
				state <= s_warn_paf0;
			else
				state <= w_ef_h0;
			end if;
		else
			state <= exit_w;  --15ene09 ceros 4 no tiene inc_sel
		end if;

	  when s_error_ff0 =>
		lives_in <= '0'; --16dic08
	    if hastoken='1' then
	        txena_dir <= '1';     
		   s_data <= fmt_error_ff_1;
		   state <= s_error_ff1;
	    end if;

	  when s_error_ff1 =>
	    lives_in <= '1'; --16dic08
	    txena_dir <= '1';     
	    s_data <= fmt_error_ff_2;
	    time_start <= '0';
	    if (dis_ffreg_mask='0') then --diciembre08
	      state <= exit_w;	 --diciembre08
	    else
	      state <= w_ef_h0;	 --diciembre08
	    end if;

	  when s_warn_paf0 =>
	    lives_in <= '0'; --16dic08
	    if hastoken='1' then
	        txena_dir <= '1';       
		   s_data <= fmt_warn_paf_1;
		   state <= s_warn_paf1;
	    end if;

	  when s_warn_paf1 =>
	     lives_in <= '1'; --16dic08
       	txena_dir <= '1';       
	     s_data <= fmt_warn_paf_2;
	     state <= w_ef_h0;  --diciembre08
	     time_start <= '0'; --diciembre08

	    
	when w_ef_h0 =>
--  espero a que este lista la fifo y cuando tenga algo 
-- almaceno el dato (ya esta listo porque es modo FWFT) y avanzo a la segunda palabra 
-- el read_fifo no lo va a detectar hasta un ciclo de reloj después de activarlo,
--luego la segunda palabra no está lista en el dout hasta r_header2
--De igual modo, el EF por culpa de este read_fifo_dir que activo aqui no se puede detectar 
--hasta r_header2
		lives_in <= not(lives_in); --16dic08
		txena_dir <= '0'; --diciembre08 vengo de s_warn_paf1
 		time_start <= '1';
		if this_ef='0' then
			read_fifo_dir <= '1';     --1er REN, no lo pilla el reloj hasta el siguiente ciclo
      		state <= r_header1;
		elsif time_out='1' then
			state <= s_timeout0;
		end if;

	when r_header1 =>
 	       lives_in <= '1'; --16dic08
		  time_start <= '0';
		  if this_ef='0' then
            	state <= r_header2;
			read_fifo_dir <= '1';  --2º REN  
  		     header1_reg <= fout(header1_reg'range);  --da igual almacenarla aqui que arriba 
		  else 
            	state <= w_ef_h1;
			read_fifo_dir <= '0';    
		  end if;

	when w_ef_h1 =>
--	Espero a que este vacia y me vuelvo a r_header1
-- Activo el read_fifo pues así venía de w_ef_h0
--y si la fifo ha pasado a EF pues no ha servido de nada 
--asi que hay que volver a dárselo para estar sincronizados
		lives_in <= not(lives_in); --16dic08
		time_start <= '1';
		if this_ef='0' then
      	   state <= r_header1;
		   read_fifo_dir <= '1';    --1º REN por EF
		elsif time_out='1' then
		   state <= s_timeout0;
		   read_fifo_dir <= '0';    
		end if;

	when r_header2 =>
 	   lives_in <= '0'; --16dic08
	   time_start <= '0';
        header2_reg <= fout;	  
        if this_ef='0' then
	          state <= r_trailer1;
 		   	read_fifo_dir <= '1';   --3º REN 
        else
               state <= w_ef_h2;
 		   	read_fifo_dir <= '0'; 
        end if;


	when w_ef_h2 => 	  --  estado de espera
	     lives_in <= not(lives_in); --16dic08
	     time_start <= '1';
		if this_ef ='0' then
			state <= r_header2;
	   		read_fifo_dir <= '1';   --2º REN por EF, dado en r_header1 pero fue bloqueado por el EF
		elsif time_out='1' then
			state <= s_timeout0;
		   	read_fifo_dir <= '0';    
		end if;


	when r_trailer1 =>
 	   	  lives_in <= '1'; --16dic08
       	  time_start <= '0';
		  if this_ef='0' then
			if (fout(15 downto 12)="0001")  then --es TRAILER
				     if dis_head_trail='0' then   --TIRO EL EVENTO
					     state <= r_trailer2;	
			           	read_fifo_dir <= '1'; --4ª REN way 1   
						trail<='0';
					  else   --LO ENVIO
					     state <= wait_token;	
 				          trailer_reg <= fout;
 			           	read_fifo_dir <= '0'; 
						trail<='1';
					  end if;
			else	  --ES UN HIT
			          state <= wait_token; --LO ENVIO
 				     trailer_reg <= fout;
				     read_fifo_dir <= '0'; 
					trail<='0';
			end if;
			if evid=header1_reg & header2_reg(15 downto 12) then
				error_evid <= '0'; --diciembre08
			else
				error_evid <= '1'; --diciembre08
			end if;
		 else 
            	state <= w_ef_t1;
            	read_fifo_dir <= '0';    
			trail<='0';
		 end if;


	when w_ef_t1 =>	  --  estado de espera
	     lives_in <= not(lives_in); --16dic08
	     time_start <= '1';
		if this_ef ='0' then
			state <= r_trailer1;
		     read_fifo_dir <= '1';    --3º REN por EF
		elsif time_out='1' then
			state <= s_timeout0;
		     read_fifo_dir <= '0';    
		end if;

	when r_trailer2 =>  -- termino el evento
 	   	 lives_in <= '0'; --16dic08
           time_start <= '0';
	      read_fifo_dir <= '0';    
		 if this_ef='0' then
		     state <= exit_w; --15ene09 ceros 4 	
		 else 
           	state <= w_ef_t2;
		 end if;

	when w_ef_t2 =>	  --  estado de espera
	     lives_in <= not(lives_in); --16dic08
	     time_start <= '1';
		if this_ef ='0' then
			state <= r_trailer2;
		     read_fifo_dir <= '1';    --4º REN por EF
		elsif time_out='1' then
			state <= s_timeout0;
		     read_fifo_dir <= '0';    
		end if;

-- problema de formatos, a partir de aqui,
-- si trail = 1 --> trailer_reg es trailer_1 y fout es trailer_2
-- si trail = 0 --> trailer_reg es dato_1 y fout es dato_2

-- TOKEN
	  when wait_token =>
 	   	lives_in <= '0'; --16dic08
	     read_fifo_dir <= '0';   
	     time_start <= '0';
	     if hastoken='1' then
 	     	if  error_evid='0' then --16dic08
		   		state <= s_header1;
				error_evid <='0';
	     	elsif dis_evid_wrong='1' then
		   		state <= s_header1;
				error_evid <='1'; --dic08
			else
		   		state <= evid_wrong1;
				error_evid <='1';  --dic08
	     	end if;
          end if;


	  when evid_wrong1 =>
 	   	lives_in <= '1'; --16dic08
		error_evid <= '1';
		s_data <= fmt_evid_wrong_1;
		txena_dir <= '1';               
		state <= evid_wrong2;

	  when evid_wrong2 =>
 	   	lives_in <= '0'; --16dic08
		s_data <= fmt_evid_wrong_2;
		txena_dir <= '1';               
		state <= s_header1;  --16dic08

	      
	  when s_header1 =>
 	    lives_in <= '1'; --16dic08
	    txena_dir <= '1';                 
	    s_data <= fmt_header_1;
	    state <= s_header2;

	  when s_header2 =>                          
 	   	lives_in <= '0'; --16dic08
	   	time_start <= '0';                      
      	txena_dir <= '1';                 
      	s_data <= fmt_header_2;                 
		if trail='1' then                       
    	     	state <= forz_trailer1;
   		else
		     state <= s_dato_first1;
    		end if;

--ENVIO TRAILER SIN HITS Y TERMINO EL EVENTO
     when forz_trailer1 =>   
 	   	lives_in <= '1'; --16dic08
	     time_start <= '0';
  	     if this_ef='0' then 
	        	read_fifo_dir <= '1';   --4º REN	way 2
		     txena_dir <= '1';                 
      	   	s_data <= fmt_trailer_alt_1;
         		state <= forz_trailer2;
		else
	        	read_fifo_dir <= '0'; 
		     txena_dir <= '0';                 
         		state <= w_ef_ft1;
		end if;

	  when w_ef_ft1 =>
	     lives_in <= not(lives_in);  --16dic08 
		time_start <= '1';
          txena_dir <= '0'; 
  	     if this_ef='0' then 
	        	read_fifo_dir <= '1';   --4º REN	way 2
         		state <= forz_trailer1;
		elsif time_out='1' then
         		state <= s_timeout1;
		end if;
	  		
     when forz_trailer2 => 
 	   	lives_in <= '0'; --16dic08
        	read_fifo_dir <= '0';  
          txena_dir <= '1';                 
          s_data <= fmt_trailer_2;
          state <= exit_w;  --15ene09 ceros 4

     when s_dato_first1 =>
 	   	lives_in <= '1'; --16dic08
          s_data <= fmt_dato_alt_1;
	 	read_fifo_dir <= '1';   --REN par
	 	txena_dir <= '1'; 
		state <= send_dato2;
		    
	  when send_dato2 =>
	   	  lives_in <= '0'; --16dic08
            time_start <= '0';
            s_data <= fout;
        	  --if word_count /= max_word_count then
--16dic08		  if ((nolimit_maxw='1') or (word_count(11 downto 4) /= max_word_count)) then
		  if ((dis_maxw_mask='1') or (word_count(11 downto 4) /= max_word_count)) then
			     if this_ef='0' then 
 					read_fifo_dir <= '1';   --REN par
	 		        	txena_dir <= '1'; 
                 		state <= send_dato1;
					word_count <= word_count + 1;
				  else
					read_fifo_dir <= '0'; 
				     txena_dir <= '0'; 
                 		state <= w_ef_sd2;
				  end if;	
			  else
			       read_fifo_dir <= '0';
				  txena_dir <= '1'; 
               	  state <= s_maxwc1;
           end if;

	  when w_ef_sd2 =>
	     lives_in <= not(lives_in);  --16dic08 
	     time_start <= '1';
          txena_dir <= '0'; 
	     if this_ef='0' then	                  
               read_fifo_dir <= '1';
               state <= send_dato2;
        	elsif time_out='1' then
               state <= error_fill;      
        	end if;
	  
	  when send_dato1 =>
 	     lives_in <= '1';  --16dic08 
         	time_start <= '0';
		if this_ef='0' then
        	  read_fifo_dir <= '1';   --REN impar
            if fout(15 downto 12)="0001" then
               txena_dir <= '1';
               s_data <= fmt_trailer_1;
               state <= s_trailer1;
	       else
               txena_dir <= '1';
               state <= send_dato2; 
   		     s_data <= fmt_dato_1;
             end if;  
		 else 
		  read_fifo_dir <= '0'; 
		  txena_dir <= '0'; 
            state <= w_ef_sd1;
		 end if;

	  when w_ef_sd1 =>
	     lives_in <= not(lives_in);  --16dic08 
	    	time_start <= '1';
       	txena_dir <= '0'; 
	    	if this_ef='0' then	                    
          	read_fifo_dir <= '1';
          	state <= send_dato1;
       	elsif time_out='1' then
          	state <= s_timeout0;      
       	end if;


	  when s_trailer1 => 		
	     lives_in <= '0';  --16dic08 
  	     time_start <= '0';                
          read_fifo_dir <= '0';       
    		if this_ef='0' then
		     txena_dir <= '1';           
	    	     s_data <= fmt_trailer_2;
	          state <= exit_w;
	    else
		     txena_dir <= '0';       
		     state <= w_ef_st1;
	     end if;

	  when w_ef_st1 =>   --estado de espera
		lives_in <= not(lives_in);  --16dic08 
		time_start <= '1';
       	txena_dir <= '0'; 
	    	if this_ef='0' then	                    
          	read_fifo_dir <= '1';
            	state <= s_trailer1;
       	elsif time_out='1' then
          	state <= error_fill;      
       	end if;

--TIMEOUTS
	when error_fill =>
	     lives_in <= '0';  --16dic08 
 		time_start <= '0';
		txena_dir <= '1';               
		s_data <= (others=>'0');
		state <= s_timeout1;

	when s_timeout0 =>                    
	     lives_in <= '0';  --16dic08 
	    	read_fifo_dir <= '0';            
		if hastoken='1' then
			state <= s_timeout1;
		end if;
	
	when s_timeout1 =>
	     lives_in <= '1';  --16dic08 
		time_start <='0';
		txena_dir <= '1';                
		s_data <= fmt_timeout_1;
		state <= s_timeout2;

	when s_timeout2 =>
	     lives_in <= '0';  --16dic08 
		s_data <= fmt_timeout_2;
		state <= exit_w;

--MAXIMO DE PALABRAS SIN ENCONTRAR TRAILER
     when s_maxwc1 =>
	     lives_in <= '1';  --16dic08 
		txena_dir <= '1';     
		s_data <= fmt_maxwc_1;
		state <= s_maxwc2;

	when s_maxwc2 =>
	     lives_in <= '0';  --16dic08 
		s_data <= fmt_maxwc_2;
	     state <= exit_w;
		   
  end case;
end if;
	    
end process synch;


-- channel timed out
process (clk, soft_reset)
begin
	if soft_reset='1' then
	  timed_out <='0';-- (others => '0');
	elsif clk'event and clk='1' then
	  if enw_timed_out='1' then
	    timed_out <= db_in;
--diciembre08	  elsif state=s_timeout2 then      
	  elsif state=s_timeout1 then      
	    timed_out <= '1';--timed_out or sel_read(5 downto 0);
	  end if;
	end if;
end process;

-- channel maxwords reached
process (clk, soft_reset)
begin
	if soft_reset='1' then
	  maxw_reached <= '0';
	elsif clk'event and clk='1' then
	  if enw_maxw='1' then
	    maxw_reached <= db_in;
	  elsif state=s_maxwc1 then      
	    maxw_reached <= '1';
	  end if;
	end if;
end process;

-- channel evid_mis
process (clk, soft_reset)
begin
	if soft_reset='1' then
	  evid_mis <= '0';--(others => '0');
	elsif clk'event and clk='1' then
	  if enw_evid_mis='1' then
	    evid_mis <= '0';--(others => '0');
--diciembre08	  elsif (error_evid='1' and (state=forz_trailer2 or state=s_trailer1)) then
	  elsif (error_evid='1' and (state=r_trailer2 or state=wait_token)) then
	    evid_mis <= '1';--evid_mis or sel_read(5 downto 0);
	  end if;
	end if;
end process;

-- timer
process(clk, soft_reset) 
begin

	if soft_reset='1' then
	  time_counter <= (others => '0'); 
	  time_out <= '0';	  
	elsif (clk'event and clk='1') then
	  if time_start='0' then
	    time_counter <= '0' & timeout_value; 
	    time_out <= '0';
	  elsif (this_hasunlock='1' and dis_hul_mask='0') then --16dic08
	    time_counter <= '0' & timeout_value; --16dic08
	    time_out <= '1'; --16dic08
	  elsif time_out='0' then
	    time_counter <= time_counter - 1;
	    time_out <= time_counter(time_counter'high);
       end if;
	end if;

end process;

--diciembre 08 locura
process(clk, soft_reset) 
begin
 	if soft_reset='1' then
	  dontread <= mask; 
	elsif (clk'event and clk='1') then
	  if (state=s_maxwc1 and dis_maxw_mask='0')then       --wordcount
	    dontread <= '1'; --dontread or sel_read(5 downto 0);
	  elsif (error_evid='1' and (state=r_trailer2 or state=wait_token) and dis_evid_bloq='0') then  --evidmis
	    dontread <= '1'; --dontread or sel_read(5 downto 0);
	  elsif (this_hasunlock='1' and state=s_timeout1 and dis_hul_mask='0') then  --unlock
	    dontread <= '1'; --dontread or sel_read(5 downto 0);
	  elsif (this_hasunlock='0' and state=s_timeout1 and dis_timo_mask='0') then 	 --timeout
	    dontread <= '1';--dontread or sel_read(5 downto 0);
	  elsif (state=s_error_ff1 and dis_ffreg_mask='0') then 	 --fifo full
	    dontread <= '1';--dontread or sel_read(5 downto 0);
 	  end if;
	end if;
end process;

--err_dontread <= or_reduce(dontread);	 -- dontread activo alto 15dic08
err_dontread <= dontread;	 -- dontread activo alto 15dic08

end arch;
	
