[xiph-commits] r12924 - trunk/theora-fpga/reconrefframes

piga at svn.xiph.org piga at svn.xiph.org
Mon May 7 13:56:53 PDT 2007


Author: piga
Date: 2007-05-07 13:56:52 -0700 (Mon, 07 May 2007)
New Revision: 12924

Added:
   trunk/theora-fpga/reconrefframes/casca_avalon.vhd
   trunk/theora-fpga/reconrefframes/divider.vhd
Modified:
   trunk/theora-fpga/reconrefframes/UpdateUMV.vhd
   trunk/theora-fpga/reconrefframes/clamp.vhd
   trunk/theora-fpga/reconrefframes/copyrecon.vhd
   trunk/theora-fpga/reconrefframes/databuffer.vhd
   trunk/theora-fpga/reconrefframes/dual_syncram.vhd
   trunk/theora-fpga/reconrefframes/expandblock.vhd
   trunk/theora-fpga/reconrefframes/idctslow.vhd
   trunk/theora-fpga/reconrefframes/lflimits.vhd
   trunk/theora-fpga/reconrefframes/loopfilter.vhd
   trunk/theora-fpga/reconrefframes/reconframes.vhd
   trunk/theora-fpga/reconrefframes/reconpixelindex.vhd
   trunk/theora-fpga/reconrefframes/reconrefframes.vhd
   trunk/theora-fpga/reconrefframes/tb_reconrefframes.vhd
Log:


Modified: trunk/theora-fpga/reconrefframes/UpdateUMV.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/UpdateUMV.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/UpdateUMV.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -29,7 +29,22 @@
 
 
 architecture a_UpdateUMV of UpdateUMV is
--- We are using 1024 as the maximum width and height size
+  component ReconPixelIndex
+    port (Clk,
+          Reset_n       : in  std_logic;
+          
+          in_request    : out std_logic;
+          in_valid      : in  std_logic;
+          in_data       : in  signed(31 downto 0);
+          
+          out_requested : in  std_logic;
+          out_valid     : out std_logic;
+          out_data      : out signed(31 downto 0)
+          );
+  end component;
+
+
+  -- We are using 1024 as the maximum width and height size
   -- = ceil(log2(Maximum Size))
   constant LG_MAX_SIZE    : natural := 10;
   constant MEM_ADDR_WIDTH : natural := 20;
@@ -37,52 +52,59 @@
 -------------------------------------------------------------------------------
 -- Signals that must be read at the beginning
 -------------------------------------------------------------------------------
+  signal HFragments       : unsigned(LG_MAX_SIZE-3 downto 0);
+  signal VFragments       : unsigned(LG_MAX_SIZE-3 downto 0);
+  signal YStride          : unsigned(LG_MAX_SIZE+1 downto 0);
+  signal UVStride         : unsigned(LG_MAX_SIZE   downto 0);
+  signal YPlaneFragments  : unsigned(LG_MAX_SIZE*2 downto 0);
+  signal UVPlaneFragments : unsigned(LG_MAX_SIZE*2-2 downto 0);
+  signal ReconYDataOffset : unsigned(MEM_ADDR_WIDTH-1 downto 0);
+  signal ReconUDataOffset : unsigned(MEM_ADDR_WIDTH-1 downto 0);
+  signal ReconVDataOffset : unsigned(MEM_ADDR_WIDTH-1 downto 0);
+  signal info_height      : unsigned(LG_MAX_SIZE-1 downto 0);
 
-  signal HFragments              : unsigned(31 downto 0);
-  signal VFragments              : unsigned(31 downto 0);
-  signal YStride                 : unsigned(31 downto 0);
-  signal UVStride                : unsigned(31 downto 0);
-  signal YPlaneFragments         : unsigned(31 downto 0);
-  signal UVPlaneFragments        : unsigned(31 downto 0);
-  signal ReconYDataOffset        : unsigned(31 downto 0);
-  signal ReconUDataOffset        : unsigned(31 downto 0);
-  signal ReconVDataOffset        : unsigned(31 downto 0);
-  signal info_height             : unsigned(31 downto 0);
 
-
 -------------------------------------------------------------------------------
 -- Signal that must be read for all frames
 -------------------------------------------------------------------------------
-  signal FrameOfs                : unsigned(31 downto 0);
+  signal FrameOfs         : unsigned(MEM_ADDR_WIDTH-1 downto 0);
 
 -------------------------------------------------------------------------------
 -- ReconPixelIndex signal
 -------------------------------------------------------------------------------
-  signal rpi_position     : unsigned(31 downto 0);
-  signal rpi_value        : signed(31 downto 0);
+  constant RPI_DATA_WIDTH : positive := 32;
+  constant RPI_POS_WIDTH  : positive := 17;
+  signal rpi_position     : unsigned(RPI_POS_WIDTH-1 downto 0);
+  signal rpi_value        : signed(RPI_DATA_WIDTH-1 downto 0);
 
+  signal s_rpi_in_request    : std_logic;
+  signal s_rpi_in_valid      : std_logic;
+  signal s_rpi_in_data       : signed(31 downto 0);
+        
+  signal s_rpi_out_requested : std_logic;
+  signal s_rpi_out_valid     : std_logic;
+  signal s_rpi_out_data      : signed(31 downto 0);
+
 -------------------------------------------------------------------------------
 -- Internal Signals
 -------------------------------------------------------------------------------
-  signal s_in_request            : std_logic;
+  signal count                   : integer range 0 to 4095;
+  signal count2                  : unsigned(4 downto 0);
 
-  signal count                   : integer;
-  signal count2                  : integer;
-
   -- VERIFICAR OS TIPOS
-  signal PlaneStride             : unsigned(31 downto 0);
-  signal PlaneBorderWidth        : integer;
-  signal LineFragments           : unsigned(31 downto 0);
-  signal PlaneHeight             : integer;
-  signal BlockVStep              : integer;
-  signal PlaneFragments          : integer;
-  signal position                : unsigned(31 downto 0);
-  signal SrcPtr1                 : integer;
-  signal DestPtr1                : integer;
-  signal DestPtr1_i              : integer;
-  signal SrcPtr2                 : integer;
-  signal DestPtr2                : integer;
-  signal DestPtr2_i              : integer;
+  signal PlaneStride             : unsigned(LG_MAX_SIZE+1 downto 0);
+  signal PlaneBorderWidth        : unsigned(4 downto 0);
+  signal LineFragments           : unsigned(LG_MAX_SIZE-3 downto 0);
+  signal PlaneHeight             : unsigned(LG_MAX_SIZE-1 downto 0);
+  signal BlockVStep              : unsigned(LG_MAX_SIZE+5 downto 0);
+  signal PlaneFragments          : unsigned(LG_MAX_SIZE*2 downto 0);
+  signal position                : unsigned(LG_MAX_SIZE*2 downto 0);
+  signal SrcPtr1                 : unsigned(MEM_ADDR_WIDTH-1 downto 0);
+  signal DestPtr1                : unsigned(MEM_ADDR_WIDTH-1 downto 0);
+  signal DestPtr1_i              : unsigned(MEM_ADDR_WIDTH-1 downto 0);
+  signal SrcPtr2                 : unsigned(MEM_ADDR_WIDTH-1 downto 0);
+  signal DestPtr2                : unsigned(MEM_ADDR_WIDTH-1 downto 0);
+  signal DestPtr2_i              : unsigned(MEM_ADDR_WIDTH-1 downto 0);
 
   signal copy1                   : signed(31 downto 0);
   signal copy2                   : signed(31 downto 0);
@@ -95,13 +117,29 @@
   type layer_t is (stt_Y, stt_U, stt_V);
   signal layer : layer_t;
 
-  type state_t is (stt_readin, stt_Ver, stt_Hor, stt_done, stt_ReadMem, stt_WriteMem);
+  type state_t is (stt_readin, stt_Calc_RPI_Value,
+                   stt_Ver, stt_Hor,
+                   stt_done, stt_ReadMem,
+                   stt_WriteMem);
   signal state : state_t;
   signal save_state : state_t;
 
-  type read_state_t is (stt_onetime, stt_offset );
+  type read_state_t is (stt_read_HFragments,
+                        stt_read_YPlaneFragments,
+                        stt_read_YStride,
+                        stt_read_UVPlaneFragments,
+                        stt_read_UVStride,
+                        stt_read_VFragments,
+                        stt_read_ReconYDataOffset,
+                        stt_read_ReconUDataOffset,
+                        stt_read_ReconVDataOffset,
+                        stt_read_info,
+                        stt_read_offset);
   signal read_state : read_state_t;
 
+  type calc_rpi_state_t is (stt_calc_rpi1, stt_calc_rpi2);
+  signal calc_rpi_state : calc_rpi_state_t;
+
   type update_state_t is (stt_1, stt_2, stt_3, stt_4, stt_5);
   signal update_state : update_state_t;
 
@@ -112,12 +150,13 @@
 -- Constants
 -------------------------------------------------------------------------------
 
-  constant UMV_BORDER  : integer := 16;
-  constant HFRAGPIXELS : integer := 8;
-  constant VFRAGPIXELS : integer := 8;
+  constant UMV_BORDER  : unsigned(4 downto 0) := "10000";
+  constant HFRAGPIXELS : unsigned(3 downto 0) := "1000";
+  constant VFRAGPIXELS : unsigned(3 downto 0) := "1000";
 
 
-
+  signal s_in_request            : std_logic;
+  
   signal s_out_done : std_logic;
   
   signal s_in_sem_request : std_logic;
@@ -129,61 +168,51 @@
   
 begin
 
-
+  in_request <= s_in_request;
+  in_sem_request <= s_in_sem_request;  
+  out_sem_valid <= s_out_sem_valid;
   out_done <= s_out_done;
-  in_request <= s_in_request;
 
 
-  rpi0: entity work.reconpixelindex
-    generic map(32,32,32,32,32,32,32,32)
-    port map (rpi_position, HFragments, VFragments, YStride, UVStride,
-              YPlaneFragments, UVPlaneFragments, ReconYDataOffset,
-              ReconUDataOffset, ReconVDataOffset, rpi_value);
 
+  rpi0: reconpixelindex
+    port map (Clk => Clk,
+              Reset_n => Reset_n,
+              in_request => s_rpi_out_requested,
+              in_valid => s_rpi_out_valid,
+              in_data => s_rpi_out_data,
 
-  in_sem_request <= s_in_sem_request;
-  out_sem_valid <= s_out_sem_valid;
+              out_requested => s_rpi_in_request,
+              out_valid => s_rpi_in_valid,
+              out_data => s_rpi_in_data);
 
+  RPI_HandShake: process (in_data, in_valid,
+                          state, read_state,
+                          calc_rpi_state,
+                          rpi_position,
+                          s_in_request)
+  begin  -- process RPI_HandShake
+    s_rpi_out_data <= x"00000000";
+    s_rpi_out_valid <= '0';
+    if (s_in_request = '1') then
+      if (state = stt_readIn and
+          read_state /= stt_read_info and
+          read_state /= stt_read_offset) then
+        s_rpi_out_data <= in_data;
+        s_rpi_out_valid <= in_valid;
+      end if;
+    else
+      if (state = stt_Calc_RPI_Value and
+          calc_rpi_state = stt_calc_rpi1) then
+        s_rpi_out_data <= resize(signed('0'&rpi_position), 32);
+        s_rpi_out_valid <= '1';
+      end if;
+    end if;
+  end process RPI_HandShake;
+
   
   process (clk)
 
-    procedure read_onetime is
-    begin
-      if (count = 0) then
-        HFragments <= unsigned(in_data);
-        count <= count + 1;
-      elsif (count = 1) then
-        YPlaneFragments <= unsigned(in_data);
-        count <= count + 1;
-      elsif (count = 2) then
-        YStride <= unsigned(in_data);
-        count <= count + 1;
-      elsif (count = 3) then
-        UVPlaneFragments <= unsigned(in_data);
-        count <= count + 1;
-      elsif (count = 4) then
-        UVStride <= unsigned(in_data);
-        count <= count + 1;
-      elsif (count = 5) then
-        VFragments <= unsigned(in_data);
-        count <= count + 1;
-      elsif (count = 6) then
-        ReconYDataOffset <= unsigned(in_data);
-        count <= count + 1;
-      elsif (count = 7) then
-        ReconUDataOffset <= unsigned(in_data);
-        count <= count + 1;
-      elsif (count = 8) then
-        ReconVDataOffset <= unsigned(in_data);
-        count <= count + 1;
-      else
---        assert false report "uu.height = "&integer'image(to_integer(in_data)) severity note;
-        info_height <= unsigned(in_data);
-        read_state <= stt_offset;
-        count <= 0;
-      end if;
-    end procedure read_onetime;
-
     procedure ReadIn is
     begin
       s_in_request <= '1';
@@ -191,16 +220,87 @@
       s_in_sem_request <= '0';
       if (s_in_request = '1' and in_valid = '1') then
         case read_state is
-          when stt_onetime => read_onetime;
+          when stt_read_HFragments =>
+            HFragments <= unsigned(in_data(LG_MAX_SIZE-3 downto 0));
+            read_state <= stt_read_YPlaneFragments;
+
+
+          when stt_read_YPlaneFragments =>
+            YPlaneFragments <= unsigned(in_data(LG_MAX_SIZE*2 downto 0));
+            read_state <= stt_read_YStride;
+
+
+          when stt_read_YStride =>
+            YStride <= unsigned(in_data(LG_MAX_SIZE+1 downto 0));
+            read_state <= stt_read_UVPlaneFragments;
+
+
+          when stt_read_UVPlaneFragments =>
+            UVPlaneFragments <= unsigned(in_data(LG_MAX_SIZE*2-2 downto 0));
+            read_state <= stt_read_UVStride;
+
+
+          when stt_read_UVStride =>
+            UVStride <= unsigned(in_data(LG_MAX_SIZE downto 0));
+            read_state <= stt_read_VFragments;
+
+
+          when stt_read_VFragments =>
+            VFragments <= unsigned(in_data(LG_MAX_SIZE-3 downto 0));
+            read_state <= stt_read_ReconYDataOffset;
+
+
+          when stt_read_ReconYDataOffset =>
+            ReconYDataOffset <= unsigned(in_data(MEM_ADDR_WIDTH-1 downto 0));
+            read_state <= stt_read_ReconUDataOffset;
+
+
+          when stt_read_ReconUDataOffset =>
+            ReconUDataOffset <= unsigned(in_data(MEM_ADDR_WIDTH-1 downto 0));
+            read_state <= stt_read_ReconVDataOffset;
+
+
+          when stt_read_ReconVDataOffset =>
+            ReconVDataOffset <= unsigned(in_data(MEM_ADDR_WIDTH-1 downto 0));
+            read_state <= stt_read_info;
+
+
+          when stt_read_info =>
+            info_height <= unsigned(in_data(LG_MAX_SIZE-1 downto 0));
+            read_state <= stt_read_offset;
+
+
           when others =>                  -- when stt_offset
---            assert false report "uu.FrameOfs = "&integer'image(to_integer(in_data)) severity note;
             state <= stt_Ver;
             s_in_request <= '0';
-            FrameOfs <= unsigned(in_data);
-        end case;  
+            FrameOfs <= unsigned(in_data(MEM_ADDR_WIDTH-1 downto 0));
+            count2 <= "00000";
+            count <= 0;
+        end case;
       end if;
     end procedure ReadIn;
 
+    procedure CalcRPIValue is
+    begin
+      case calc_rpi_state is
+        when stt_calc_rpi1 =>
+          -- Wait until ReconPixelIndex can receive the data
+          if (s_rpi_out_requested = '1') then
+            calc_rpi_state <= stt_calc_rpi2;
+          end if;
+
+
+        when others =>
+          -- Wait until ReconPixelIndex returns the value
+          s_rpi_in_request <= '1';
+          if (s_rpi_in_request = '1' and s_rpi_in_valid = '1') then
+            rpi_value <= s_rpi_in_data;
+            state <= save_state;
+          end if;
+      end case;
+    end procedure CalcRPIValue;
+    
+    
     procedure Vert is
     begin
       case update_state is
@@ -209,51 +309,72 @@
           case layer is
             when stt_Y =>
               PlaneStride <= YStride; 
+--               assert FrameOfs = 15360 report "LastFrame";
+--               assert FrameOfs = 7680 report "--------------------GoldFrame";
               PlaneBorderWidth <= UMV_BORDER; 
               LineFragments <= HFragments; 
-              PlaneHeight <= to_integer(info_height); 
-              rpi_position <= x"00000000";
-              position <= x"00000000";
+              PlaneHeight <= info_height; 
+
+              rpi_position <= resize("00", RPI_POS_WIDTH);
+              state <= stt_Calc_RPI_Value;
+              calc_rpi_state <= stt_calc_rpi1;
+              save_state <= stt_Ver;
+              
+              position <= resize("00", LG_MAX_SIZE*2+1);
             when stt_U =>
-              PlaneStride <= UVStride; 
-              PlaneBorderWidth <= UMV_BORDER / 2; 
-              LineFragments <= HFragments / 2; 
-              PlaneHeight <= to_integer(info_height) / 2; 
-              rpi_position <= YPlaneFragments;
+              PlaneStride <= '0' & UVStride; 
+              PlaneBorderWidth <= SHIFT_RIGHT(UMV_BORDER, 1); 
+              LineFragments <= SHIFT_RIGHT(HFragments, 1); 
+              PlaneHeight <= SHIFT_RIGHT(info_height, 1); 
+
+              rpi_position <= resize(YPlaneFragments, RPI_POS_WIDTH);
+              state <= stt_Calc_RPI_Value;
+              calc_rpi_state <= stt_calc_rpi1;
+              save_state <= stt_Ver;
+
               position <= YPlaneFragments;
-            when stt_V =>
-              PlaneStride <= UVStride;
-              PlaneBorderWidth <= UMV_BORDER / 2;
-              LineFragments <= HFragments / 2;
-              PlaneHeight <= to_integer(info_height) / 2;
-              rpi_position <= YPlaneFragments + UVPlaneFragments;
+            when others =>              -- when stt_V =>
+              PlaneStride <= '0' & UVStride;
+              PlaneBorderWidth <= SHIFT_RIGHT(UMV_BORDER, 1);
+              LineFragments <= SHIFT_RIGHT(HFragments, 1);
+              PlaneHeight <= SHIFT_RIGHT(info_height, 1);
+
+              rpi_position <= resize(YPlaneFragments + UVPlaneFragments, RPI_POS_WIDTH);
+              state <= stt_Calc_RPI_Value;
+              calc_rpi_state <= stt_calc_rpi1;
+              save_state <= stt_Ver;
+              
               position <= YPlaneFragments + UVPlaneFragments;
           end case;
+
         when stt_2 =>
           update_state <= stt_3;
-          SrcPtr1 <= to_integer(FrameOfs) + to_integer(rpi_value);
-          DestPtr1 <= to_integer(FrameOfs) + to_integer(rpi_value) - PlaneBorderWidth;
+          SrcPtr1 <= resize(FrameOfs + ('0' & unsigned(rpi_value)), MEM_ADDR_WIDTH);
+          DestPtr1 <= resize(FrameOfs + ('0' & unsigned(rpi_value)) - PlaneBorderWidth, MEM_ADDR_WIDTH);
+
         when stt_3 => 
           update_state <= stt_4;
-          rpi_position <= position + LineFragments - 1;  -- Por seguranca eu nao
-                                                         -- faco isso no estado
-                                                         -- anterior e espero o
-                                                         -- proximo estado pela resposta
+
+          rpi_position <= resize(position + LineFragments - 1, RPI_POS_WIDTH);
+          state <= stt_Calc_RPI_Value;
+          calc_rpi_state <= stt_calc_rpi1;
+          save_state <= stt_Ver;
+          
         when stt_4 => 
           update_state <= stt_5;
-          SrcPtr2 <= to_integer(FrameOfs) + to_integer(rpi_value) + (HFRAGPIXELS) - 1;
-          DestPtr2 <= to_integer(FrameOfs) + to_integer(rpi_value) + HFRAGPIXELS;
-        when stt_5 => 
+          SrcPtr2 <= resize(FrameOfs + ('0' & unsigned(rpi_value)) + HFRAGPIXELS - 1, MEM_ADDR_WIDTH);
+          DestPtr2 <= resize(FrameOfs + ('0' & unsigned(rpi_value)) + HFRAGPIXELS, MEM_ADDR_WIDTH);
+        when others =>                  -- when stt_5 =>
           if (count = PlaneHeight) then
             count <= 0;
-            count2 <= 0;
+            count2 <= "00000";
             update_state <= stt_1;
             case layer is
               when stt_Y =>
                 layer <= stt_U;
               when stt_U =>
                 layer <= stt_V;
-              when stt_V =>
+              when others =>    -- when stt_V =>
                 layer <= stt_Y;
                 state <= stt_Hor;
             end case;
@@ -263,13 +384,13 @@
               when stt_read1 =>
                 update_int_state <= stt_read2;
                 state <= stt_ReadMem;
-                in_sem_addr <= SHIFT_RIGHT(to_unsigned(SrcPtr1,20),2);
+                in_sem_addr <= SHIFT_RIGHT(SrcPtr1, 2);
                 s_in_sem_request <= '1';
 
               when stt_read2 =>
                 update_int_state <= stt_wait;
                 state <= stt_ReadMem;
-                in_sem_addr <= SHIFT_RIGHT(to_unsigned(SrcPtr2,20), 2);
+                in_sem_addr <= SHIFT_RIGHT(SrcPtr2, 2);
                 s_in_sem_request <= '1';
                 copy1 <= mem_rd_data(31 downto 24) &
                          mem_rd_data(31 downto 24) &
@@ -285,31 +406,30 @@
 
               when stt_write1 =>
                 if (count2 = PlaneBorderWidth) then
-                  count2 <= 0;
+                  count2 <= "00000";
                   count <= count + 1;
                   update_int_state <= stt_read1;
-                  SrcPtr1 <= SrcPtr1 + to_integer(PlaneStride);
-                  SrcPtr2 <= SrcPtr2 + to_integer(PlaneStride);
-                  DestPtr1 <= DestPtr1 + to_integer(PlaneStride);
-                  DestPtr2 <= DestPtr2 + to_integer(PlaneStride);
+                  SrcPtr1 <= SrcPtr1 + PlaneStride;
+                  SrcPtr2 <= SrcPtr2 + PlaneStride;
+                  DestPtr1 <= DestPtr1 + PlaneStride;
+                  DestPtr2 <= DestPtr2 + PlaneStride;
                 else
                   update_int_state <= stt_write2;
-                  out_sem_addr <= SHIFT_RIGHT(to_unsigned(DestPtr1 + count2,20), 2);
+                  out_sem_addr <= SHIFT_RIGHT(DestPtr1 + count2, 2);
                   out_sem_data <= copy1;
                   s_out_sem_valid <= '1';
                   state <= stt_WriteMem;
                 end if;
 
-              when stt_write2 =>
+              when others => -- when stt_write2 =>
                 update_int_state <= stt_write1;
-                out_sem_addr <= SHIFT_RIGHT(to_unsigned(DestPtr2 + count2,20), 2);
+                out_sem_addr <= SHIFT_RIGHT(DestPtr2 + count2, 2);
                 count2 <= count2 + 4;
                 out_sem_data <= copy2;
                 s_out_sem_valid <= '1';
                 state <= stt_WriteMem;
             end case;
           end if;
-        when others => null;
       end case;
     end procedure Vert;
 
@@ -320,55 +440,73 @@
           update_state <= stt_2;
           case layer is
             when stt_Y =>
-              BlockVStep <= to_integer(YStride) * (VFRAGPIXELS - 1);
+              BlockVStep <= YStride * (VFRAGPIXELS - 1);
               PlaneStride <= YStride; 
               PlaneBorderWidth <= UMV_BORDER;
-              PlaneFragments <= to_integer(YPlaneFragments);
+              PlaneFragments <= YPlaneFragments;
               LineFragments <= HFragments; 
-              rpi_position <= x"00000000";
-              position <= x"00000000";
+
+              rpi_position <= resize("00", RPI_POS_WIDTH);
+              state <= stt_Calc_RPI_Value;
+              calc_rpi_state <= stt_calc_rpi1;
+              save_state <= stt_Hor;
+
+              
+              position <= resize("00", LG_MAX_SIZE*2+1);
             when stt_U =>
-              BlockVStep <= to_integer(UVStride) * (VFRAGPIXELS - 1);
-              PlaneStride <= UVStride; 
-              PlaneBorderWidth <= UMV_BORDER / 2 ;
-              PlaneFragments <= to_integer(UVPlaneFragments);
-              LineFragments <= HFragments / 2; 
-              rpi_position <= YPlaneFragments;
+              BlockVStep <= ('0' & UVStride) * (VFRAGPIXELS - 1);
+              PlaneStride <= '0' & UVStride; 
+              PlaneBorderWidth <= SHIFT_RIGHT(UMV_BORDER, 1) ;
+              PlaneFragments <= "00" & UVPlaneFragments;
+              LineFragments <= SHIFT_RIGHT(HFragments, 1); 
+
+              rpi_position <= resize(YPlaneFragments, RPI_POS_WIDTH);
+              state <= stt_Calc_RPI_Value;
+              calc_rpi_state <= stt_calc_rpi1;
+              save_state <= stt_Hor;
+
               position <= YPlaneFragments;
-            when stt_V =>
-              BlockVStep <= to_integer(UVStride) * (VFRAGPIXELS - 1);
-              PlaneStride <= UVStride; 
-              PlaneBorderWidth <= UMV_BORDER / 2 ;
-              PlaneFragments <= to_integer(UVPlaneFragments);
-              LineFragments <= HFragments / 2; 
-              rpi_position <= YPlaneFragments + UVPlaneFragments;
+            when others =>      -- when stt_V =>
+              BlockVStep <= ('0' & UVStride) * (VFRAGPIXELS - 1);
+              PlaneStride <= '0' & UVStride; 
+              PlaneBorderWidth <= SHIFT_RIGHT(UMV_BORDER,1) ;
+              PlaneFragments <= "00" & UVPlaneFragments;
+              LineFragments <= SHIFT_RIGHT(HFragments,1); 
+
+              rpi_position <= resize(YPlaneFragments + UVPlaneFragments, RPI_POS_WIDTH);
+              state <= stt_Calc_RPI_Value;
+              calc_rpi_state <= stt_calc_rpi1;
+              save_state <= stt_Hor;
+              
               position <= YPlaneFragments + UVPlaneFragments;
           end case;
         when stt_2 =>
           update_state <= stt_3;
-          SrcPtr1 <= to_integer(FrameOfs) + to_integer(rpi_value) - PlaneBorderWidth;
-          DestPtr1 <= to_integer(FrameOfs) + to_integer(rpi_value) - PlaneBorderWidth*(to_integer(PlaneStride) + 1); 
+          SrcPtr1 <= resize(FrameOfs + ('0' & unsigned(rpi_value)) - PlaneBorderWidth, MEM_ADDR_WIDTH);
+          DestPtr1 <= resize(FrameOfs + ('0' & unsigned(rpi_value)) - PlaneBorderWidth*(PlaneStride + 1), MEM_ADDR_WIDTH); 
         when stt_3 => 
           update_state <= stt_4;
-          rpi_position <= position + PlaneFragments - LineFragments;  -- Por seguranca eu nao
-                                                         -- faco isso no estado
-                                                         -- anterior e espero o
-                                                         -- proximo estado pela resposta
+
+          rpi_position <= resize(position + PlaneFragments - LineFragments, RPI_POS_WIDTH);
+          state <= stt_Calc_RPI_Value;
+          calc_rpi_state <= stt_calc_rpi1;
+          save_state <= stt_Hor;
+
         when stt_4 => 
           update_state <= stt_5;
-          SrcPtr2 <= to_integer(FrameOfs) + to_integer(rpi_value) + BlockVStep - PlaneBorderWidth;
-          DestPtr2 <= to_integer(FrameOfs) + to_integer(rpi_value) + BlockVStep - PlaneBorderWidth + to_integer(PlaneStride);
-        when stt_5 => 
+          SrcPtr2 <= resize(FrameOfs + ('0' & unsigned(rpi_value)) + BlockVStep - PlaneBorderWidth, MEM_ADDR_WIDTH);
+          DestPtr2 <= resize(FrameOfs + ('0' & unsigned(rpi_value)) + BlockVStep - PlaneBorderWidth + PlaneStride, MEM_ADDR_WIDTH);
+        when others =>    -- when stt_5 => 
           if (count = PlaneStride) then
             count <= 0;
-            count2 <= 0;
+            count2 <= "00000";
             update_state <= stt_1;
             case layer is
               when stt_Y =>
                 layer <= stt_U;
               when stt_U =>
                 layer <= stt_V;
-              when stt_V =>
+              when others =>    -- when stt_V =>
                 layer <= stt_Y;
 		state <= stt_Done;
             end case;
@@ -381,13 +519,13 @@
 
                 update_int_state <= stt_read2;
                 state <= stt_ReadMem;
-                in_sem_addr <= SHIFT_RIGHT(to_unsigned(SrcPtr1,20),2);
+                in_sem_addr <= SHIFT_RIGHT(SrcPtr1, 2);
                 s_in_sem_request <= '1';
                 
               when stt_read2 =>
                 update_int_state <= stt_wait;
                 state <= stt_ReadMem;
-                in_sem_addr <= SHIFT_RIGHT(to_unsigned(SrcPtr2,20), 2);
+                in_sem_addr <= SHIFT_RIGHT(SrcPtr2, 2);
                 s_in_sem_request <= '1';
                 copy1 <= mem_rd_data;
                 
@@ -397,31 +535,30 @@
 
               when stt_write1 =>
                 if (count2 = PlaneBorderWidth) then
-                  count2 <= 0;
+                  count2 <= "00000";
                   count <= count + 4;
                   update_int_state <= stt_read1;
                   SrcPtr1 <= SrcPtr1 + 4;
                   SrcPtr2 <= SrcPtr2 + 4;
                 else
                   update_int_state <= stt_write2;
-                  out_sem_addr <= SHIFT_RIGHT(to_unsigned(DestPtr1_i,20), 2);
+                  out_sem_addr <= SHIFT_RIGHT(DestPtr1_i, 2);
                   out_sem_data <= copy1;
                   s_out_sem_valid <= '1';
                   state <= stt_WriteMem;
                 end if;
 
-              when stt_write2 =>
+              when others =>    -- when stt_write2 =>
                 update_int_state <= stt_write1;
-                out_sem_addr <= SHIFT_RIGHT(to_unsigned(DestPtr2_i,20), 2);
+                out_sem_addr <= SHIFT_RIGHT(DestPtr2_i, 2);
                 count2 <= count2 + 1;
-                DestPtr1_i <= DestPtr1_i + to_integer(PlaneStride);
-                DestPtr2_i <= DestPtr2_i + to_integer(PlaneStride);
+                DestPtr1_i <= DestPtr1_i + PlaneStride;
+                DestPtr2_i <= DestPtr2_i + PlaneStride;
                 out_sem_data <= copy2;
                 s_out_sem_valid <= '1';
                 state <= stt_WriteMem;
             end case;
           end if;
-        when others => null;
       end case;
     end procedure Horz;
 
@@ -434,11 +571,12 @@
 	s_out_done <= '0';
         count <= count+1;
       else
+        assert false report "UpdateUMV is done" severity note;
       	state <= stt_readin;
-      	read_state <= stt_offset;
+      	read_state <= stt_read_offset;
       	count <= 0;
       end if;
-      end procedure Done;
+    end procedure Done;
 
     procedure ReadMemory is
     begin
@@ -463,41 +601,46 @@
 
     
   begin                                 -- process
-    if (Reset_n = '0') then
-      s_out_done <= '0';
-      s_in_request <= '0';
-      layer <= stt_Y;
-      read_state <= stt_onetime;
-      state <= stt_readin;
-      update_int_state <= stt_read1;
-      update_state <= stt_1;
-      count <= 0;
-      count2 <= 0;
-      s_in_sem_request <= '0';
-      s_out_sem_valid <= '0';
-
-      rpi_position <= "00000000000000000000000000000000";
-      HFragments <="11111111111111111111111111111111";
-      VFragments <="00000000000000000000000000000000";
-      YStride <= "00000000000000000000000000000000";
-      UVStride <= "00000000000000000000000000000000";
-      YPlaneFragments <= "00000000000000000000000000000000";
-      UVPlaneFragments <= "00000000000000000000000000000000";
-      ReconYDataOffset <= "00000000000000000000000000000000";
-      ReconUDataOffset <= "00000000000000000000000000000000";
-      ReconVDataOffset <= "00000000000000000000000000000000";
-      info_height	<= "00000000000000000000000000000000";
-    elsif (clk'event and clk = '1') then
-      if (Enable = '1') then
-        case state is
-          when stt_readin => ReadIn;
-          when stt_Ver => Vert;
-          when stt_Hor => Horz;
-          when stt_Done => Done;
-          when stt_WriteMem => WriteMemory;
-          when stt_ReadMem => ReadMemory;
-          when others => ReadIn; state <= stt_readin;
-        end case;
+    if (clk'event and clk = '1') then
+      if (Reset_n = '0') then
+        s_out_done <= '0';
+        s_in_request <= '0';
+        layer <= stt_Y;
+        read_state <= stt_read_HFragments;
+        state <= stt_readin;
+        update_int_state <= stt_read1;
+        update_state <= stt_1;
+        count <= 0;
+        count2 <= "00000";
+        s_in_sem_request <= '0';
+        s_out_sem_valid <= '0';
+        rpi_position <= '0' & x"0000";
+        s_rpi_in_request <= '0';
+        calc_rpi_state <= stt_calc_rpi1;
+        
+        HFragments <= x"11";
+        VFragments <= x"00";
+        YStride <= x"000";
+        UVStride <= "000" & x"00";
+        YPlaneFragments <= '0' & x"00000";
+        UVPlaneFragments <= "000" & x"0000";
+        ReconYDataOffset <= x"00000";
+        ReconUDataOffset <= x"00000";
+        ReconVDataOffset <= x"00000";
+        info_height	 <= resize("00", LG_MAX_SIZE);
+      else
+        if (Enable = '1') then
+          case state is
+            when stt_readin => ReadIn;
+            when stt_Calc_RPI_Value => CalcRPIValue;
+            when stt_Ver => Vert;
+            when stt_Hor => Horz;
+            when stt_Done => Done;
+            when stt_WriteMem => WriteMemory;
+            when stt_ReadMem => ReadMemory;
+            when others => ReadIn; state <= stt_readin;
+          end case;
+        end if;
       end if;
     end if;
   end process;

Added: trunk/theora-fpga/reconrefframes/casca_avalon.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/casca_avalon.vhd	                        (rev 0)
+++ trunk/theora-fpga/reconrefframes/casca_avalon.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -0,0 +1,111 @@
+library std;
+library ieee;
+library work;
+
+use ieee.std_logic_1164.all;
+use ieee.numeric_std.all;
+--use ieee.std_logic_arith.all;
+--use ieee.std_logic_unsigned.all;
+--use ieee.std_logic_signed.all;
+use work.all;
+
+
+
+entity casca_avalon is
+  port (clk,
+        reset_n : in std_logic;
+        address : in std_logic_vector(1 downto 0);
+
+        read : in std_logic;
+        write : in std_logic;
+
+        writedata : in std_logic_vector(31 downto 0);
+        readdata : out std_logic_vector(31 downto 0);
+        
+        chipselect : in std_logic
+
+
+        
+        );
+
+end entity casca_avalon;
+
+
+
+
+architecture rtl of casca_avalon is
+  component ReconRefFrames
+    port (Clk,
+          Reset_n       : in  std_logic;
+          
+          in_request    : out std_logic;
+          in_valid      : in  std_logic;
+          in_data       : in  signed(31 downto 0);
+          
+          out_requested : in  std_logic;
+          out_valid     : out std_logic;
+          out_data      : out signed(31 downto 0)
+
+          );
+  end component;
+  
+  signal in_request : std_logic;
+  signal in_valid : std_logic;
+  signal in_data : signed(31 downto 0);
+
+  signal out_requested : std_logic;
+  signal out_valid : std_logic;
+  signal out_data : signed(31 downto 0);
+
+begin
+
+  recon1 : ReconRefFrames
+    port map ( clk, Reset_n, in_request, in_valid, in_data, out_requested, out_valid, out_data);
+
+   process(chipselect, read, address, in_request, out_valid, out_data)
+   begin
+--     out_requested <= '0';
+     readdata <= "00000000000000000000000000000000";
+
+     out_requested <= '0';
+
+     if (chipselect = '1') then
+       if (read = '1') then
+         case address is
+           when "00" => -- Can software write data to IDCT Module ?
+             readdata <= "0000000000000000000000000000000"&in_request;
+                 
+           when "01" => -- Can software read data from IDCT Module ?
+             readdata <= "0000000000000000000000000000000"&out_valid;
+
+           when others => -- Read data from IDCT Module ?
+             out_requested <= '1';
+             readdata <= std_logic_vector(out_data);
+         end case;
+       end if;
+     end if;
+
+     if (Reset_n = '0') then
+       readdata <= "00000000000000000000000000000000";
+       out_requested <= '0';
+     end if;
+   end process;
+
+
+  process(Reset_n, chipselect, write, writedata)
+   begin
+
+     in_valid <= '0';
+     in_data <= "00000000000000000000000000000000";
+     if (chipselect = '1') then
+       if (write = '1') then
+         in_data <= signed(writedata(31 downto 0));
+         in_valid <= '1';
+       end if;
+     end if;
+     if (Reset_n = '0') then
+       in_valid <= '0';
+       in_data <= "00000000000000000000000000000000";
+     end if;
+   end process;
+end rtl;

Modified: trunk/theora-fpga/reconrefframes/clamp.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/clamp.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/clamp.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -1,3 +1,9 @@
+-------------------------------------------------------------------------------
+--  Description: If x < 0 then sat receives 0.
+--               If x > 255 then sat receives 255
+--               Else sat receives the eights low-order bits. 
+-------------------------------------------------------------------------------
+
 library std;
 library IEEE;
 use IEEE.numeric_std.all;

Modified: trunk/theora-fpga/reconrefframes/copyrecon.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/copyrecon.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/copyrecon.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -1,7 +1,16 @@
+-------------------------------------------------------------------------------
+--  Description: This file implements the CopyRecon.
+--               It copies the visible fragments of the source pointer
+--               to the equivalent pointed by destination pointer.
+-------------------------------------------------------------------------------
+
 library std;
 library ieee;
+library work;
+
 use ieee.std_logic_1164.all;
 use ieee.numeric_std.all;
+use work.all;
 
 entity CopyRecon is
   port (Clk,
@@ -24,10 +33,42 @@
 
         out_done          :   out std_logic
         );
-end CopyRecon;
+end entity CopyRecon;
 
 
 architecture a_copyrecon of CopyRecon is
+  component syncram
+    generic (
+      DEPTH : positive := 64;             -- How many slots
+      DATA_WIDTH : positive := 16;        -- How many bits per slot
+      ADDR_WIDTH : positive := 6          -- = ceil(log2(DEPTH))
+      );
+    port (
+      clk : in std_logic;
+      wr_e  : in std_logic;
+      wr_addr : in unsigned(ADDR_WIDTH-1 downto 0);
+      wr_data : in signed(DATA_WIDTH-1 downto 0);
+      rd_addr : in unsigned(ADDR_WIDTH-1 downto 0);
+      rd_data : out signed(DATA_WIDTH-1 downto 0)
+      );
+  end component;
+
+
+  component ReconPixelIndex
+    port (Clk,
+          Reset_n       : in  std_logic;
+          
+          in_request    : out std_logic;
+          in_valid      : in  std_logic;
+          in_data       : in  signed(31 downto 0);
+          
+          out_requested : in  std_logic;
+          out_valid     : out std_logic;
+          out_data      : out signed(31 downto 0)
+          );
+  end component;
+
+  
   -- We are using 1024 as the maximum width and height size
   -- = ceil(log2(Maximum Size))
   constant LG_MAX_SIZE    : natural := 10;
@@ -55,7 +96,7 @@
 -------------------------------------------------------------------------------
 -- Internal Signals
 -------------------------------------------------------------------------------
-  signal count : integer;
+  signal count : integer range 0 to 2097151;
 
   signal BlockCount : unsigned(2 downto 0);
   signal SlotCount : unsigned(2 downto 0);
@@ -81,7 +122,16 @@
   signal rpi_position : unsigned(RPI_POS_WIDTH-1 downto 0);
   signal rpi_value    : signed(RPI_DATA_WIDTH-1 downto 0);
 
+  signal s_rpi_in_request    : std_logic;
+  signal s_rpi_in_valid      : std_logic;
+  signal s_rpi_in_data       : signed(31 downto 0);
+        
+  signal s_rpi_out_requested : std_logic;
+  signal s_rpi_out_valid     : std_logic;
+  signal s_rpi_out_data      : signed(31 downto 0);
 
+  
+
 -------------------------------------------------------------------------------
 -- display_fragment signals and constants
 -------------------------------------------------------------------------------
@@ -108,7 +158,8 @@
 
   type proc_state_t is (stt_proc1, stt_proc2, stt_proc3,
                         stt_proc4, stt_proc5, stt_proc6,
-                        stt_proc7, stt_proc8);
+                        stt_proc7, stt_proc8, stt_proc9,
+                        stt_proc10);
   signal proc_state : proc_state_t;
 
   type copy_block_state_t is (stt_CopyBlk1, stt_CopyBlk2, stt_CopyBlk3);
@@ -128,18 +179,47 @@
   out_sem_valid <= s_out_sem_valid;
 
   
-  mem_disp_frag: entity work.syncram
+  mem_disp_frag: syncram
     generic map (DPF_DEPTH, DPF_DATA_WIDTH, DPF_ADDR_WIDTH)
     port map (clk, dpf_wr_e, dpf_wr_addr, dpf_wr_data, dpf_rd_addr, dpf_rd_data);
 
   
-  rpi0: entity work.reconpixelindex
-    generic map (17, 8, 12, 11, 21, 19, 20, 20)
-    port map (rpi_position, HFragments, VFragments, YStride, UVStride,
-              YPlaneFragments, UVPlaneFragments, ReconYDataOffset,
-              ReconUDataOffset, ReconVDataOffset, rpi_value);
+  rpi0: reconpixelindex
+    port map (Clk => Clk,
+              Reset_n => Reset_n,
+              in_request => s_rpi_out_requested,
+              in_valid => s_rpi_out_valid,
+              in_data => s_rpi_out_data,
 
+              out_requested => s_rpi_in_request,
+              out_valid => s_rpi_in_valid,
+              out_data => s_rpi_in_data);
 
+
+  RPI_HandShake: process (count, in_data, in_valid,
+                          state, read_state, proc_state,
+                          rpi_position, s_in_request)
+  begin  -- process RPI_HandShake
+    s_rpi_out_data <= x"00000000";
+    s_rpi_out_valid <= '0';
+    if (s_in_request = '1') then
+      if (read_state = stt_32bitsData) then
+        if (count >=0 and count <=8) then
+          s_rpi_out_data <= in_data;
+          s_rpi_out_valid <= in_valid;
+        end if;
+      end if;
+    else
+      if (state = stt_Proc and
+          proc_state = stt_proc7) then
+        s_rpi_out_data <= resize(signed('0'&rpi_position), 32);
+        s_rpi_out_valid <= '1';
+      end if;
+    end if;
+  end process RPI_HandShake;
+
+
+  
   process (clk)
 -------------------------------------------------------------------------------
 -- Procedures called when state is readIn
@@ -189,6 +269,7 @@
 
     procedure ReadDispFrag is
     begin
+--      assert false report "DispFrag Count = "&integer'image(count) severity note;
       dpf_wr_e <= '1';
       dpf_wr_data <= in_data;
       dpf_wr_addr <= dpf_wr_addr + 1;
@@ -206,7 +287,6 @@
    
     procedure ReadOffsets is
     begin
---      assert false report "in_data = "&integer'image(to_integer(in_data)) severity note;
       dpf_wr_e <= '0';
       if (count = 0) then
         OffSetSrcPtr <= unsigned(in_data(MEM_ADDR_WIDTH-1 downto 0));
@@ -224,7 +304,6 @@
     begin
       s_in_request <= '1';
       if (s_in_request = '1' and in_valid = '1') then
---        assert false report "cr.in_data = "&integer'image(to_integer(in_data)) severity note;
         case read_state is
           when stt_32bitsData => Read32bitsData;
           when stt_DispFrag => ReadDispFrag;
@@ -239,10 +318,9 @@
 -------------------------------------------------------------------------------
     procedure Proc is
     begin
+--      assert false report "proc_state = "&proc_state_t'image(proc_state) severity note;
       case proc_state is
         when stt_proc1 =>
---          assert false report "Proc1" severity note;
-          
           PlaneLineStep <= YStride;
           ValueCount <= to_unsigned(0, LG_MAX_SIZE*2+1);
           EndValue <= YPlaneFragments;
@@ -250,7 +328,6 @@
 
 
         when stt_proc2 =>
---          assert false report "Proc2" severity note;
           PlaneLineStep <= '0' & UVStride;
           ValueCount <= YPlaneFragments;
           EndValue <= UnitFragmets;
@@ -258,7 +335,6 @@
 
 
         when stt_proc3 =>
---          assert false report "Proc3" severity note;
           dpf_rd_addr <= resize(SHIFT_RIGHT(ValueCount, 5), DPF_ADDR_WIDTH);
           proc_state <= stt_proc4;
 
@@ -267,31 +343,43 @@
           proc_state <= stt_proc5;
 
         when stt_proc5 =>
-          proc_state <= stt_proc8;
+          proc_state <= stt_proc10;
           -- Now we have 32 display_fragments values
           if (dpf_rd_data(31 - to_integer(ValueCount(4 downto 0))) = '1') then
             proc_state <= stt_proc6;
           end if;
           
         when stt_proc6 =>
---          assert false report "Proc4" severity note;          
-          -- Calculate the recon_pixel_index
+          -- Requet the recon_pixel_index position
           rpi_position <= resize(ValueCount, RPI_POS_WIDTH);
           -- Wait for the memory access
           proc_state <= stt_proc7;
 
+        when stt_proc7 =>
+          -- Wait until ReconPixelIndex can receive the data
+          if (s_rpi_out_requested = '1') then
+            proc_state <= stt_proc8;
+          end if;
 
-        when stt_proc7 =>
---         assert false report "Proc5" severity note;          
+        when stt_proc8 =>
+          -- Wait until ReconPixelIndex returns the value
+          s_rpi_in_request <= '1';
+          if (s_rpi_in_request = '1' and s_rpi_in_valid = '1') then
+            rpi_value <= s_rpi_in_data;
+            s_rpi_in_request <= '0';
+            proc_state <= stt_proc9;
+          end if;
+
+          
+        when stt_proc9 =>
           -- Copying the blocks
           SrcPtr <= OffSetSrcPtr + to_integer(rpi_value);
           DestPtr <= OffSetDestPtr + to_integer(rpi_value);
           state <= stt_CopyBlock;
-          proc_state <= stt_proc8;
+          proc_state <= stt_proc10;
 
 
-        when stt_proc8 =>
---          assert false report "Proc6" severity note;          
+        when stt_proc10 =>
           proc_state <= stt_proc5;
 --          assert false report "ValueCount = "&integer'image(to_integer(ValueCount)) severity note;
           if (ValueCount = EndValue - 1) then
@@ -315,11 +403,13 @@
     end procedure Proc;
 
 
+-------------------------------------------------------------------------------
+-- This procedure do the main job
+-------------------------------------------------------------------------------
     procedure CopyBlock is
     begin
       case copy_block_state is
         when stt_CopyBlk1 =>
---          assert false report "CpBlk1" severity note;
           s_in_sem_request <= '1';
           in_sem_addr <= SHIFT_RIGHT(SrcPtr + SlotCount, 2);
           state <= stt_ReadMemory;
@@ -327,7 +417,6 @@
 
           
         when stt_CopyBlk2 =>
---          assert false report "CpBlk2" severity note;
           out_sem_addr <= SHIFT_RIGHT(DestPtr + SlotCount, 2);
           out_sem_data <= mem_rd_data;
           s_out_sem_valid <= '1';
@@ -342,7 +431,6 @@
 
           
         when stt_CopyBlk3 =>
---          assert false report "CpBlk3" severity note;          
           SrcPtr <= SrcPtr + PlaneLineStep;
           DestPtr <= DestPtr + PlaneLineStep;
           BlockCount <= BlockCount + 1;
@@ -379,56 +467,57 @@
 -------------------------------------------------------------------------------
     procedure EndProc is
     begin
---      if (in_valid = '1') then
-        count <= 0;
-        out_done <= '0';
-        state <= stt_readIn;
---      end if;
+--      assert false report "Writing Data" severity note;
+      count <= 0;
+      out_done <= '0';
+      state <= stt_readIn;
     end procedure EndProc;
 
     
   begin  -- process
-    if (Reset_n = '0') then
-      state <= stt_ReadIn;
-      read_state <= stt_32bitsData;
-      proc_state <= stt_proc1;
-      copy_block_state <= stt_CopyBlk1;
-      BlockCount <= "000";
-      count <= 0;
-      SlotCount <= "000";
+    if (clk'event and clk = '1') then
+      if (Reset_n = '0') then
+        state <= stt_ReadIn;
+        read_state <= stt_32bitsData;
+        proc_state <= stt_proc1;
+        copy_block_state <= stt_CopyBlk1;
+        BlockCount <= "000";
+        count <= 0;
+        SlotCount <= "000";
 
-      s_in_request <= '0';
-      s_in_request <= '0';
-      s_out_sem_valid <= '0';
-      out_done <= '0';
+        s_in_request <= '0';
+        s_in_request <= '0';
+        s_out_sem_valid <= '0';
+        out_done <= '0';
 
-      rpi_position <= '0' & x"0000";
-      HFragments <= x"11";
-      VFragments <= x"00";
-      YStride <= x"000";
-      UVStride <= "000" & x"00";
-      YPlaneFragments <= '0' & x"00000";
-      UVPlaneFragments <= "000" & x"0000";
-      ReconYDataOffset <= x"00000";
-      ReconUDataOffset <= x"00000";
-      ReconVDataOffset <= x"00000";
+        rpi_position <= '0' & x"0000";
+        HFragments <= x"11";
+        VFragments <= x"00";
+        YStride <= x"000";
+        UVStride <= "000" & x"00";
+        YPlaneFragments <= '0' & x"00000";
+        UVPlaneFragments <= "000" & x"0000";
+        ReconYDataOffset <= x"00000";
+        ReconUDataOffset <= x"00000";
+        ReconVDataOffset <= x"00000";
 
-      dpf_wr_e  <= '0';
-      dpf_wr_addr <= to_unsigned(0, DPF_ADDR_WIDTH);
-      dpf_wr_data <= x"00000000";
-      dpf_rd_addr <= to_unsigned(0, DPF_ADDR_WIDTH);
-
-    elsif (clk'event and clk = '1') then
-      s_in_request <= '0';
-      if (Enable = '1') then
-        case state is
-          when stt_ReadIn => ReadIn;
-          when stt_Proc => Proc;
-          when stt_CopyBlock => CopyBlock;
-          when stt_ReadMemory => ReadMemory;
-          when stt_WriteMemory => WriteMemory;
-          when others => EndProc;
-        end case;
+        s_rpi_in_request <= '0';
+        dpf_wr_e  <= '0';
+        dpf_wr_addr <= to_unsigned(0, DPF_ADDR_WIDTH);
+        dpf_wr_data <= x"00000000";
+        dpf_rd_addr <= to_unsigned(0, DPF_ADDR_WIDTH);
+      else
+        s_in_request <= '0';
+        if (Enable = '1') then
+          case state is
+            when stt_ReadIn => ReadIn;
+            when stt_Proc => Proc;
+            when stt_CopyBlock => CopyBlock;
+            when stt_ReadMemory => ReadMemory;
+            when stt_WriteMemory => WriteMemory;
+            when others => EndProc;
+          end case;
+        end if;
       end if;
     end if;
   end process;

Modified: trunk/theora-fpga/reconrefframes/databuffer.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/databuffer.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/databuffer.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -1,9 +1,15 @@
--- atual
+-------------------------------------------------------------------------------
+--  Description: This file implements a big buffer to keep
+--               the roconstructed frames (This, Golden and Last)
+-------------------------------------------------------------------------------
 
 library std;
 library ieee;
+library work;
+
 use ieee.std_logic_1164.all;
 use ieee.numeric_std.all;
+use work.all;
 
 entity DataBuffer is
 
@@ -25,6 +31,21 @@
 
 
 architecture a_DataBuffer of DataBuffer is
+  component syncram
+    generic (
+      DEPTH : positive := 64;             -- How many slots
+      DATA_WIDTH : positive := 16;        -- How many bits per slot
+      ADDR_WIDTH : positive := 6          -- = ceil(log2(DEPTH))
+      );
+    port (
+      clk : in std_logic;
+      wr_e  : in std_logic;
+      wr_addr : in unsigned(ADDR_WIDTH-1 downto 0);
+      wr_data : in signed(DATA_WIDTH-1 downto 0);
+      rd_addr : in unsigned(ADDR_WIDTH-1 downto 0);
+      rd_data : out signed(DATA_WIDTH-1 downto 0)
+      );
+  end component;
 
 
   signal count : integer;
@@ -32,7 +53,7 @@
   signal s_in_request : std_logic;
   signal s_out_valid : std_logic;
 
-  constant MEM_DEPTH : natural := 65536;
+  constant MEM_DEPTH : natural := 16384;
   constant MEM_DATA_WIDTH : natural := 32;
   constant MEM_ADDR_WIDTH : natural := 20;
   
@@ -46,7 +67,7 @@
   in_request <= s_in_request;
   out_valid <= s_out_valid;
   
-  mem_int32: entity work.syncram
+  mem_int32: syncram
     generic map (MEM_DEPTH, MEM_DATA_WIDTH, MEM_ADDR_WIDTH)
     port map (clk, mem_wr_e, mem_wr_addr, mem_wr_data,
               mem_rd_addr, mem_rd_data);
@@ -54,40 +75,41 @@
   process (clk)
 
   begin  -- process
-    if (Reset_n = '0') then
-      s_in_request <= '0';
-      s_out_valid <= '0';
+    
+    if (clk'event and clk = '1') then
+      if (Reset_n = '0') then
+        s_in_request <= '0';
+        s_out_valid <= '0';
 
-      count <= 0;
+        count <= 0;
 --memory's signals
-      mem_wr_e <= '0';
-      mem_wr_addr <= x"00000";
-      mem_wr_data <= x"00000000";
-      mem_rd_addr <= x"00000";
+        mem_wr_e <= '0';
+        mem_wr_addr <= x"00000";
+        mem_wr_data <= x"00000000";
+        mem_rd_addr <= x"00000";
+      else
 
-      
-    elsif (clk'event and clk = '1') then
-
-      s_out_valid <= '0';
-      s_in_request <= '1';
-      mem_wr_e <= '0';
-      if (s_in_request = '1' and in_valid = '1') then
-        mem_wr_e <= '1';
-        mem_wr_data <= in_data;
-        mem_wr_addr <= in_addr;
-      end if;
-      count <= 0;
-      if (out_requested = '1' and s_out_valid = '0') then
-        if (count = 0) then
-          mem_rd_addr <= out_addr;
-          count <= count + 1;
-        elsif (count = 1) then
-          count <= count + 1;
-        else
-          out_data <= mem_rd_data;
-          s_out_valid <= '1';
-          count <= 0;
+        s_out_valid <= '0';
+        s_in_request <= '1';
+        mem_wr_e <= '0';
+        if (s_in_request = '1' and in_valid = '1') then
+          mem_wr_e <= '1';
+          mem_wr_data <= in_data;
+          mem_wr_addr <= in_addr;
         end if;
+        count <= 0;
+        if (out_requested = '1' and s_out_valid = '0') then
+          if (count = 0) then
+            mem_rd_addr <= out_addr;
+            count <= count + 1;
+          elsif (count = 1) then
+            count <= count + 1;
+          else
+            out_data <= mem_rd_data;
+            s_out_valid <= '1';
+            count <= 0;
+          end if;
+        end if;
       end if;
     end if;
   end process;

Added: trunk/theora-fpga/reconrefframes/divider.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/divider.vhd	                        (rev 0)
+++ trunk/theora-fpga/reconrefframes/divider.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -0,0 +1,118 @@
+library std;
+library ieee;
+library work;
+
+use ieee.std_logic_1164.all;
+use ieee.numeric_std.all;
+use work.all;
+
+
+entity divider is
+  generic (           
+    WIDTH : positive := 32);
+  port (Clk,
+        Reset_n       : in  std_logic;
+        
+        in_request    : out std_logic;
+        in_valid      : in  std_logic;
+        dividend      : in  unsigned(WIDTH-1 downto 0);
+        divisor       : in  unsigned(WIDTH-1 downto 0);
+        
+        out_requested : in  std_logic;
+        out_valid     : out std_logic;
+        quotient      : out unsigned(WIDTH-1 downto 0);
+        remainder     : out unsigned(WIDTH-1 downto 0)
+        );
+end entity divider;
+
+architecture a_divider of divider is
+  type state_t is (stt_readIn, stt_divide, stt_writeOut);
+  signal state : state_t;
+
+  type divide_state_t is (stt_divide1, stt_divide2, stt_divide3);
+  signal divide_state : divide_state_t;
+
+  signal s_dividend  :   unsigned(WIDTH-1 downto 0);
+  signal s_divisor   :   unsigned(WIDTH*2-1 downto 0);
+  signal s_quotient  :   unsigned(WIDTH-1 downto 0);
+  signal s_remainder :   unsigned(WIDTH*2-1 downto 0);
+
+  signal s_in_request : std_logic;
+  signal s_out_valid  : std_logic;
+  signal s_repetition : integer range 0 to WIDTH+1;
+
+begin  -- a_divider
+
+  in_request <= s_in_request;
+  out_valid <= s_out_valid;
+
+  process (clk)
+
+    procedure ReadIn is
+    begin
+      s_out_valid <= '0';            -- came from WriteOut, out_valid must be 0
+      s_in_request <= '1';
+      if( s_in_request = '1' and in_valid = '1' )then
+        s_dividend <= dividend;
+        s_remainder <= resize("00", WIDTH) & dividend;
+        s_divisor <= divisor & resize("00", WIDTH);
+        s_quotient <= resize("00", WIDTH);
+        s_repetition <= 0;
+        s_in_request <= '0';
+        state <= stt_divide;
+      end if;
+    end procedure ReadIn;
+
+    procedure Divide is
+      variable v_subtractor : unsigned(WIDTH*2-1 downto 0);
+    begin
+      v_subtractor := s_remainder - s_divisor;
+
+      s_divisor <= SHIFT_RIGHT(s_divisor, 1);
+      s_quotient <= SHIFT_LEFT(s_quotient, 1);
+      if (v_subtractor(WIDTH*2-1) = '0') then  -- positive
+        s_quotient(0) <= '1';
+        s_remainder <= v_subtractor;
+      else
+        s_quotient(0) <= '0';
+      end if;
+      s_repetition <= s_repetition + 1;
+      if (s_repetition = WIDTH) then
+        state <= stt_writeOut;
+        s_repetition <= 0;
+      end if;
+    end procedure Divide;
+
+
+    procedure WriteOut is
+    begin
+      s_out_valid <= '1';
+      quotient <= s_quotient;
+      remainder <= s_remainder(WIDTH-1 downto 0);
+      if (out_requested = '1') then
+        state <= stt_readIn;
+      end if;
+    end procedure WriteOut;
+
+  begin 
+    if (clk'event and clk = '1') then
+      if (Reset_n = '0') then
+        s_in_request <= '0';
+        s_out_valid <= '0';
+        
+        s_repetition <= 0;
+        divide_state <= stt_divide1;
+        state <= stt_readIn;
+        
+      else
+        case state is
+          when stt_readIn => ReadIn;
+          when stt_divide => Divide;
+          when stt_writeOut => WriteOut;
+          when others => ReadIn; state <= stt_readIn;
+        end case;
+      end if;
+    end if;
+  end process;
+
+end a_divider;

Modified: trunk/theora-fpga/reconrefframes/dual_syncram.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/dual_syncram.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/dual_syncram.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -1,3 +1,7 @@
+-------------------------------------------------------------------------------
+--  Description: This file implements a dual-SRAM
+-------------------------------------------------------------------------------
+
 library IEEE;
 use IEEE.std_logic_1164.all;
 use IEEE.numeric_std.all;

Modified: trunk/theora-fpga/reconrefframes/expandblock.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/expandblock.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/expandblock.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -1,7 +1,10 @@
 library std;
 library ieee;
+library work;
+
 use ieee.std_logic_1164.all;
 use ieee.numeric_std.all;
+use work.all;
 
 entity ExpandBlock is
   
@@ -29,6 +32,57 @@
 end ExpandBlock;
 
 architecture a_ExpandBlock of ExpandBlock is
+  component syncram
+    generic (
+      DEPTH : positive := 64;             -- How many slots
+      DATA_WIDTH : positive := 16;        -- How many bits per slot
+      ADDR_WIDTH : positive := 6          -- = ceil(log2(DEPTH))
+      );
+    port (
+      clk : in std_logic;
+      wr_e  : in std_logic;
+      wr_addr : in unsigned(ADDR_WIDTH-1 downto 0);
+      wr_data : in signed(DATA_WIDTH-1 downto 0);
+      rd_addr : in unsigned(ADDR_WIDTH-1 downto 0);
+      rd_data : out signed(DATA_WIDTH-1 downto 0)
+      );
+  end component;
+
+  component ReconPixelIndex
+    port (Clk,
+          Reset_n       : in  std_logic;
+          
+          in_request    : out std_logic;
+          in_valid      : in  std_logic;
+          in_data       : in  signed(31 downto 0);
+          
+          out_requested : in  std_logic;
+          out_valid     : out std_logic;
+          out_data      : out signed(31 downto 0)
+          );
+  end component;
+
+  component clamp
+    port (
+      x   : in  SIGNED(16 downto 0);
+      sat : out UNSIGNED(7 downto 0));
+  end component;
+
+  component IDctSlow
+    port (Clk,
+          Reset_n : in std_logic;
+          
+          in_request : out std_logic;
+          in_valid : in std_logic;
+          in_data : in signed(15 downto 0);
+          in_quantmat : in signed(15 downto 0);
+          
+          out_requested : in std_logic;
+          out_valid : out std_logic;
+          out_data : out signed(15 downto 0)
+          );
+  end component;
+  
   -- We are using 1024 as the maximum width and height size
   -- = ceil(log2(Maximum Size))
   constant LG_MAX_SIZE    : natural := 10;
@@ -78,7 +132,7 @@
 
   signal s_in_request : std_logic;
 
-  signal count : integer;
+  signal count : integer range 0 to 511;
 -------------------------------------------------------------------------------
 -- dequant_coeffs Offsets 
 -------------------------------------------------------------------------------
@@ -112,13 +166,21 @@
 
 
 -------------------------------------------------------------------------------
--- ReconPixelIndex signal and constants
+-- ReconPixelIndex signal
 -------------------------------------------------------------------------------
   constant RPI_DATA_WIDTH : positive := 32;
   constant RPI_POS_WIDTH  : positive := 17;
   signal rpi_position     : unsigned(RPI_POS_WIDTH-1 downto 0);
   signal rpi_value        : signed(RPI_DATA_WIDTH-1 downto 0);
 
+  signal s_rpi_in_request    : std_logic;
+  signal s_rpi_in_valid      : std_logic;
+  signal s_rpi_in_data       : signed(31 downto 0);
+        
+  signal s_rpi_out_requested : std_logic;
+  signal s_rpi_out_valid     : std_logic;
+  signal s_rpi_out_data      : signed(31 downto 0);
+
   
 -------------------------------------------------------------------------------
 -- Constants
@@ -141,9 +203,13 @@
   signal read_state : read_state_t;
 
   type pre_recon_state_t is (stt_PrepIDct, stt_CallIDct, stt_RecIDct,
-                             stt_SelectRecons);
+                             stt_Calc_RPI_Value, stt_SelectRecons);
   signal pre_recon_state : pre_recon_state_t;
 
+  type calc_rpi_state_t is (stt_calc_rpi1, stt_calc_rpi2);
+  signal calc_rpi_state : calc_rpi_state_t;
+
+  
   type select_recons_state_t is (stt_SelectRecons_1, stt_SelectRecons_2,
                                  stt_SelectRecons_3, stt_SelectRecons_4,
                                  stt_SelectRecons_5, stt_SelectRecons_6);
@@ -266,38 +332,70 @@
   in_sem_request <= s_in_sem_request;
   out_sem_valid <= s_out_sem_valid;
 
-  clamp255_0: entity work.clamp port map (sum, sat); 
+  clamp255_0: clamp port map (sum, sat); 
 
-  syncram_384_16: entity work.syncram
+  syncram_384_16: syncram
     generic map (DEPTH => 384, DATA_WIDTH => 16, ADDR_WIDTH => 9)
     port map (clk, dqc_wr_e, dqc_wr_addr, dqc_wr_data,
               dqc_rd_addr, dqc_rd_data);
 
-  syncram_64_16: entity work.syncram
+  syncram_64_16: syncram
     generic map (DEPTH => 64, DATA_WIDTH => 16, ADDR_WIDTH => 6)
     port map (clk, rdb_wr_e, rdb_wr_addr, rdb_wr_data,
               rdb_rd_addr, rdb_rd_data);
 
   
-  mem_64_int16_: entity work.syncram
+  mem_64_int16: syncram
     generic map (64, 16, 6)
     port map (clk, qtl_wr_e, qtl_wr_addr, qtl_wr_data,
               qtl_rd_addr, qtl_rd_data);
 
-  rpi0: entity work.reconpixelindex
-    port map (rpi_position, HFragments, VFragments, YStride, UVStride,
-              YPlaneFragments, UVPlaneFragments, ReconYDataOffset,
-              ReconUDataOffset, ReconVDataOffset, rpi_value);
+  rpi0: reconpixelindex
+    port map (Clk => Clk,
+              Reset_n => Reset_n,
+              in_request => s_rpi_out_requested,
+              in_valid => s_rpi_out_valid,
+              in_data => s_rpi_out_data,
 
-  idctslow0: entity work.IDctSlow
+              out_requested => s_rpi_in_request,
+              out_valid => s_rpi_in_valid,
+              out_data => s_rpi_in_data);
+
+  idctslow0: IDctSlow
     port map(clk, Reset_n,
              out_idct_requested, out_idct_valid, out_idct_data,
              out_idct_quantmat,
              in_idct_request, in_idct_valid, in_idct_data);
 
+
   in_idct_request <= s_in_idct_request;
   out_idct_valid <= s_out_idct_valid;
 
+
+  RPI_HandShake: process (in_data, in_valid,
+                          state, read_state, pre_recon_state,
+                          calc_rpi_state, rpi_position,
+                          s_in_request)
+  begin  -- process RPI_HandShake
+    s_rpi_out_data <= x"00000000";
+    s_rpi_out_valid <= '0';
+    if (s_in_request = '1') then
+      if (state = stt_readIn and read_state = stt_read1) then
+        s_rpi_out_data <= in_data;
+        s_rpi_out_valid <= in_valid;
+      end if;
+    else
+      if (state = stt_PreRecon and
+          pre_recon_state = stt_Calc_RPI_Value and
+          calc_rpi_state = stt_calc_rpi1) then
+        s_rpi_out_data <= resize(signed('0'&rpi_position), 32);
+        s_rpi_out_valid <= '1';
+      end if;
+    end if;
+  end process RPI_HandShake;
+
+  
+
   process (clk)
 -------------------------------------------------------------------------------
 -- Procedures called when state is stt_readIn
@@ -392,19 +490,15 @@
         FragMVect_FragNumber_y <= in_data;
         count <= count + 1;
       elsif (count = 4) then
---        assert false report "eb.FragmentNumber = "&integer'image(to_integer(in_data)) severity note;
         FragmentNumber <= unsigned(in_data);
         count <= count + 1;
       elsif (count = 5) then
---        assert false report "eb.FrameType = "&integer'image(to_integer(in_data)) severity note;
         FrameType <= unsigned(in_data(7 downto 0));
         count <= count + 1;
       elsif (count = 6) then
---        assert false report "eb.GoldenFrameOfs = "&integer'image(to_integer(in_data)) severity note;
         GoldenFrameOfs <= unsigned(in_data(MEM_ADDR_WIDTH-1 downto 0));
         count <= count + 1;
       elsif (count = 7) then
---        assert false report "eb.LastFrameReconOfs = "&integer'image(to_integer(in_data)) severity note;
         LastFrameReconOfs <= unsigned(in_data(MEM_ADDR_WIDTH-1 downto 0));
         count <= count + 1;
       --elsif (count = 8) then
@@ -422,8 +516,7 @@
     procedure ReadIn is
     begin
       s_in_request <= '1';
-      if (s_in_request = '1' and in_valid = '1') then
---        assert false report "eb.in_data = "&integer'image(to_integer(in_data)) severity note;
+      if (s_in_request = '1' and in_valid = '1') then 
         case read_state is
           when stt_read1 => read1;
           when stt_read_dqc => read_dqc;
@@ -469,10 +562,18 @@
           MvModMask <= x"00000003";
 
           -- Select appropriate dequantiser matrix. 
-          if (FragmentNumber < YPlaneFragments + UVPlaneFragments) then
-            dequant_coeffs_Ofs <= INTERU_COEFFS_OFS;
+          if (CodingMode = CODE_INTRA) then
+            if (FragmentNumber < YPlaneFragments + UVPlaneFragments) then
+              dequant_coeffs_Ofs <= U_COEFFS_OFS;
+            else
+              dequant_coeffs_Ofs <= V_COEFFS_OFS;
+            end if;
           else
-            dequant_coeffs_Ofs <= INTERV_COEFFS_OFS;
+            if (FragmentNumber < YPlaneFragments + UVPlaneFragments) then
+              dequant_coeffs_Ofs <= INTERU_COEFFS_OFS;
+            else
+              dequant_coeffs_Ofs <= INTERV_COEFFS_OFS;
+            end if;
           end if;
         end if;
         count <= 0;
@@ -522,27 +623,51 @@
       s_out_idct_valid <= '0';
       s_in_idct_request <= '1';
       if (count = 64) then
-        pre_recon_state <= stt_SelectRecons;
-        select_recons_state <= stt_SelectRecons_1;
+        pre_recon_state <= stt_Calc_RPI_Value;
+        calc_rpi_state <= stt_calc_rpi1;
         count <= 0;
         s_in_idct_request <= '0';
         -- Convert fragment number to a pixel offset in a reconstruction buffer.
         rpi_position <= resize(FragmentNumber, RPI_POS_WIDTH);
-        
-      elsif (s_in_idct_request = '1' and in_idct_valid = '1') then
-        rdb_wr_e <= '1';
-        rdb_wr_addr <= rdb_wr_addr + 1;
-        rdb_wr_data <= in_idct_data;
-        if (count = 0) then
+      else
+        if (s_in_idct_request = '1' and in_idct_valid = '1') then
+          rdb_wr_e <= '1';
+          rdb_wr_addr <= rdb_wr_addr + 1;
+          rdb_wr_data <= in_idct_data;
+          if (count = 0) then
 --          assert false report "Receiving IDct" severity note;
-          rdb_wr_addr <= "000000";
+            rdb_wr_addr <= "000000";
+          end if;
+          count <= count + 1;
         end if;
-        count <= count + 1;
-      else
-        -- Nothing to do
       end if;
     end procedure RecIDct;
 
+
+    procedure CalcRPIValue is
+    begin
+      case calc_rpi_state is
+        when stt_calc_rpi1 =>
+          -- Wait until ReconPixelIndex can receive the data
+          if (s_rpi_out_requested = '1') then
+            calc_rpi_state <= stt_calc_rpi2;
+          end if;
+
+
+        when others =>
+          -- Wait until ReconPixelIndex returns the value
+          s_rpi_in_request <= '1';
+          if (s_rpi_in_request = '1' and s_rpi_in_valid = '1') then
+            rpi_value <= s_rpi_in_data;
+            s_rpi_in_request <= '0';
+            pre_recon_state <= stt_SelectRecons;
+            select_recons_state <= stt_SelectRecons_1;
+          end if;
+      end case;
+    end procedure CalcRPIValue;
+
+
+    
     procedure SelectRecons is
     begin
       if (select_recons_state = stt_SelectRecons_1) then
@@ -551,6 +676,7 @@
 
         rdb_wr_e <= '0';
         -- Action depends on decode mode.
+--        assert false report "CodingMode1 = "&integer'image(to_integer('0' & CodingMode)) severity note;
         if (CodingMode = CODE_INTER_NO_MV) then
           -- Inter with no motion vector
           
@@ -690,6 +816,7 @@
         when stt_PrepIDct => PrepareToCallIDct;
         when stt_CallIDct => CallIDct;
         when stt_RecIDct => RecIDct;
+        when stt_Calc_RPI_Value => CalcRPIValue;
      -- when stt_SelectRecons = other
         when others => SelectRecons;
       end case;  
@@ -712,7 +839,6 @@
     --   offset_RefPtr2 - Must be zero
     procedure Calculate_ReconIntra is
     begin
---      assert false report "ReconIntra" severity note;
       if (count = 8) then
         out_done <= '1';
         recon_calc_state <= stt_CalcRecon1;
@@ -1089,72 +1215,72 @@
 -------------------------------------------------------------------------------
     procedure EndProc is
     begin
---      if (in_valid = '1') then
-        count <= 0;
-        out_done <= '0';
-        state <= stt_readIn;
---      end if;
+      count <= 0;
+      out_done <= '0';
+      state <= stt_readIn;
     end procedure EndProc;
     
   begin  -- process
-    if (Reset_n = '0') then
-      state <= stt_readIn;
-      read_state <= stt_read1;
-      pre_recon_state <= stt_PrepIDct;
-      select_recons_state <= stt_SelectRecons_1;
-      read_pixel_state <= stt_ReadPixels1;
-      write_recon_state <= stt_WriteRecon1;
-      
-      s_in_request <= '0';
-      s_in_sem_request <= '0';
-      count <= 0;
-      s_out_sem_valid <= '0';
-      out_done <= '0';
+    if (clk'event and clk = '1') then
+      if (Reset_n = '0') then
+        state <= stt_readIn;
+        read_state <= stt_read1;
+        pre_recon_state <= stt_PrepIDct;
+        select_recons_state <= stt_SelectRecons_1;
+        read_pixel_state <= stt_ReadPixels1;
+        write_recon_state <= stt_WriteRecon1;
+        
+        s_in_request <= '0';
+        s_in_sem_request <= '0';
+        count <= 0;
+        s_out_sem_valid <= '0';
+        out_done <= '0';
 
-      s_out_idct_valid <= '0';
-      s_in_idct_request <= '0';
+        s_out_idct_valid <= '0';
+        s_in_idct_request <= '0';
 
 
-      colcount <= 0;
-      sum <= '0' & x"0000";
-      
-      rpi_position <= '0' & x"0000";
-      HFragments <= x"11";
-      VFragments <= x"00";
-      YStride <= x"000";
-      UVStride <= "000" & x"00";
-      YPlaneFragments <= '0' & x"00000";
-      UVPlaneFragments <= "000" & x"0000";
-      ReconYDataOffset <= x"00000";
-      ReconUDataOffset <= x"00000";
-      ReconVDataOffset <= x"00000";
+        colcount <= 0;
+        sum <= '0' & x"0000";
+        
+        rpi_position <= '0' & x"0000";
+        HFragments <= x"11";
+        VFragments <= x"00";
+        YStride <= x"000";
+        UVStride <= "000" & x"00";
+        YPlaneFragments <= '0' & x"00000";
+        UVPlaneFragments <= "000" & x"0000";
+        ReconYDataOffset <= x"00000";
+        ReconUDataOffset <= x"00000";
+        ReconVDataOffset <= x"00000";
 
-      qtl_wr_e <= '0';
-      qtl_wr_addr <= "000000";
-      qtl_wr_data <= "0000000000000000";
-      qtl_rd_addr <= "000000";
+        qtl_wr_e <= '0';
+        qtl_wr_addr <= "000000";
+        qtl_wr_data <= "0000000000000000";
+        qtl_rd_addr <= "000000";
 
-      dqc_wr_e <= '0';
-      dqc_wr_addr <= "000000000";
-      dqc_wr_data <= "0000000000000000";
-      dqc_rd_addr <= "000000000";
+        dqc_wr_e <= '0';
+        dqc_wr_addr <= "000000000";
+        dqc_wr_data <= "0000000000000000";
+        dqc_rd_addr <= "000000000";
 
-      rdb_wr_e <= '0';
-      rdb_wr_addr <= "000000";
-      rdb_wr_data <= "0000000000000000";
-      rdb_rd_addr <= "000000";
+        rdb_wr_e <= '0';
+        rdb_wr_addr <= "000000";
+        rdb_wr_data <= "0000000000000000";
+        rdb_rd_addr <= "000000";
 
 
-      dbgSelRecon1 <= '0';
-    elsif (clk'event and clk = '1') then
-      s_in_request <= '0';
-      if (Enable = '1') then
-        case state is
-          when stt_readIn => ReadIn;
-          when stt_PreRecon => PreRecon;
-          when stt_Recon => Recon;
-          when others => EndProc;
-        end case;
+        dbgSelRecon1 <= '0';
+      else
+        s_in_request <= '0';
+        if (Enable = '1') then
+          case state is
+            when stt_readIn => ReadIn;
+            when stt_PreRecon => PreRecon;
+            when stt_Recon => Recon;
+            when others => EndProc;
+          end case;
+        end if;
       end if;
     end if;
   end process;

Modified: trunk/theora-fpga/reconrefframes/idctslow.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/idctslow.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/idctslow.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -1,8 +1,16 @@
+-------------------------------------------------------------------------------
+--  Description: Do the iDCTSlow job.
+-------------------------------------------------------------------------------
+
 library std;
 library ieee;
+library work;
+
 use ieee.std_logic_1164.all;
 use ieee.numeric_std.all;
+use work.all;
 
+
 entity IDctSlow is
   port (Clk,
         Reset_n : in std_logic;
@@ -20,6 +28,24 @@
 
 
 architecture rtl of IDctSlow is
+  component dual_syncram
+    generic (
+      DEPTH : positive := 64;             -- How many slots
+      DATA_WIDTH : positive := 16;        -- How many bits per slot
+      ADDR_WIDTH : positive := 6          -- = ceil(log2(DEPTH))
+      );
+    port (
+      clk : in std_logic;
+      wr_e  : in std_logic;
+      wr_addr : in unsigned(ADDR_WIDTH-1 downto 0);
+      wr_data : in signed(DATA_WIDTH-1 downto 0);
+      rd1_addr : in unsigned(ADDR_WIDTH-1 downto 0);
+      rd1_data : out signed(DATA_WIDTH-1 downto 0);
+      rd2_addr : in unsigned(ADDR_WIDTH-1 downto 0);
+      rd2_data : out signed(DATA_WIDTH-1 downto 0)
+      );
+  end component;
+
   
   subtype ogg_int_16_t is signed(15 downto 0);
   subtype ogg_int_32_t is signed(31 downto 0);
@@ -92,7 +118,7 @@
 begin
   
   -- Data matrix 8 x 8 x 16 bits
-  mem : entity work.dual_syncram
+  mem : dual_syncram
      generic map( DEPTH => 64, DATA_WIDTH => 16, ADDR_WIDTH => 6 )
      port map(clk, mem_we, mem_waddr, mem_wdata, mem_raddr1, mem_rdata1, mem_raddr2, mem_rdata2 );
   
@@ -292,30 +318,31 @@
     
     
   begin
-    if( Reset_n = '0' ) then
-       state <= readIn;
-       idct_state <= idct_st1;
-       wout_state <= wout_st1;
-       s_in_request <= '0';
-       count <= 0;
-       s_out_valid <= '0';
-       row_loop <= '1';
-       mem_we <= '0';
+    
+    if(clk'event and clk = '1') then
+      if( Reset_n = '0' ) then
+        state <= readIn;
+        idct_state <= idct_st1;
+        wout_state <= wout_st1;
+        s_in_request <= '0';
+        count <= 0;
+        s_out_valid <= '0';
+        row_loop <= '1';
+        mem_we <= '0';
 
-       mem_waddr <= "000000";
-       mem_wdata <= "0000000000000000";
-       mem_raddr1 <= "000000";
-       mem_raddr2 <= "000000";
-       
-     elsif(clk'event and clk = '1') then
-       mem_we <= '0';
-       case state is
-         when readIn => ReadIn;
-         when idct => Idct;
-         when writeOut => WriteOut;
-         when others => ReadIn; state <= readIn;
-       end case;  
-
+        mem_waddr <= "000000";
+        mem_wdata <= "0000000000000000";
+        mem_raddr1 <= "000000";
+        mem_raddr2 <= "000000";
+      else
+        mem_we <= '0';
+        case state is
+          when readIn => ReadIn;
+          when idct => Idct;
+          when writeOut => WriteOut;
+          when others => ReadIn; state <= readIn;
+        end case;  
+      end if;
      end if;
   end process;
 

Modified: trunk/theora-fpga/reconrefframes/lflimits.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/lflimits.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/lflimits.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -1,3 +1,8 @@
+-------------------------------------------------------------------------------
+--  Description: This file implements a component that calculate
+--               the filtbounding on-the-fly.
+-------------------------------------------------------------------------------
+
 library std;
 library ieee;
 use ieee.std_logic_1164.all;

Modified: trunk/theora-fpga/reconrefframes/loopfilter.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/loopfilter.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/loopfilter.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -1,12 +1,16 @@
--- display_fragments memory depth      : ((height/8)*(width/8)*3/2)/4
--- recon_pixel_index_table memory depth: (height/8)*(width/8)*3/2*4
--- FiltBoundingValues memory depth     : 512
--- LastFrameRecon  memory depth        : (3/2*(width + 2*16)*(height + 2*16))/4
+-------------------------------------------------------------------------------
+--  Description: This file implements the loopfilter. A filter that do
+--               a deblocking on the fragments.
+-------------------------------------------------------------------------------
 library std;
 library ieee;
+library work;
+  
 use ieee.std_logic_1164.all;
 use ieee.numeric_std.all;
+use work.all;
 
+
 entity LoopFilter is
 
   port (Clk,
@@ -32,6 +36,44 @@
 end LoopFilter;
 
 architecture a_LoopFilter of LoopFilter is
+  component syncram
+    generic (
+      DEPTH : positive := 64;             -- How many slots
+      DATA_WIDTH : positive := 16;        -- How many bits per slot
+      ADDR_WIDTH : positive := 6          -- = ceil(log2(DEPTH))
+      );
+    port (
+      clk : in std_logic;
+      wr_e  : in std_logic;
+      wr_addr : in unsigned(ADDR_WIDTH-1 downto 0);
+      wr_data : in signed(DATA_WIDTH-1 downto 0);
+      rd_addr : in unsigned(ADDR_WIDTH-1 downto 0);
+      rd_data : out signed(DATA_WIDTH-1 downto 0)
+      );
+  end component;
+
+  component ReconPixelIndex
+    port (Clk,
+          Reset_n       : in  std_logic;
+          
+          in_request    : out std_logic;
+          in_valid      : in  std_logic;
+          in_data       : in  signed(31 downto 0);
+          
+          out_requested : in  std_logic;
+          out_valid     : out std_logic;
+          out_data      : out signed(31 downto 0)
+          );
+  end component;
+
+  component LFLimits
+    port (
+      parameter : in  unsigned(8 downto 0);
+      FLimit    : in  signed(8 downto 0);
+      fbv_value : out signed(9 downto 0));
+  end component;
+
+  
   -- We are using 1024 as the maximum width and height size
   -- = ceil(log2(Maximum Size))
   constant LG_MAX_SIZE    : natural := 10;
@@ -63,11 +105,20 @@
   signal fbv_position  : unsigned(8 downto 0);
   signal fbv_value     : signed(9 downto 0);
 
--- ReconPixelIndex signal
+-- ReconPixelIndex signals and constants
   constant RPI_DATA_WIDTH : positive := 32;
   constant RPI_POS_WIDTH : positive := 17;
   signal rpi_position : unsigned(RPI_POS_WIDTH-1 downto 0);
   signal rpi_value    : signed(RPI_DATA_WIDTH-1 downto 0);
+
+  signal s_rpi_in_request    : std_logic;
+  signal s_rpi_in_valid      : std_logic;
+  signal s_rpi_in_data       : signed(31 downto 0);
+        
+  signal s_rpi_out_requested : std_logic;
+  signal s_rpi_out_valid     : std_logic;
+  signal s_rpi_out_data      : signed(31 downto 0);
+
   
 -- Memories
   signal LoopFilterLimits : mem_64_8bits_t;
@@ -118,10 +169,15 @@
                         stt_SelectColor, stt_ApplyFilter,
                         stt_CalcDispFragPos,
                         stt_CallFilterHoriz, stt_CalcFilterHoriz,
-                        stt_CallFilterVert, stt_CalcFilterVert);
+                        stt_CallFilterVert, stt_CalcFilterVert,
+                        stt_Calc_RPI_Value);
   signal proc_state : proc_state_t;
   signal back_proc_state : proc_state_t;
+  signal next_proc_state : proc_state_t;
 
+  type calc_rpi_state_t is (stt_calc_rpi1, stt_calc_rpi2);
+  signal calc_rpi_state : calc_rpi_state_t;
+
   type set_bound_val_state_t is (stt_SetBVal1, stt_SetBVal2, stt_SetBVal3, stt_SetBVal4);
   signal set_bound_val_state : set_bound_val_state_t;
 
@@ -146,7 +202,7 @@
                                 stt_ApplyFilter_27, stt_ApplyFilter_28,
                                 stt_ApplyFilter_29, stt_ApplyFilter_30,
                                 stt_ApplyFilter_31, stt_ApplyFilter_32,
-                                stt_ApplyFilter_33);
+                                stt_ApplyFilter_33, stt_ApplyFilter_34);
   signal apply_filter_state : apply_filter_state_t;
   signal next_apply_filter_state : apply_filter_state_t;
 
@@ -162,6 +218,7 @@
                               stt_DispFrag17, stt_DispFrag18,
                               stt_DispFrag19, stt_DispFrag20,
                               stt_DispFrag21, stt_DispFrag22);
+                              
   signal disp_frag_state : disp_frag_state_t;
   signal next_disp_frag_state : disp_frag_state_t;
 
@@ -171,7 +228,7 @@
   signal calc_disp_frag_state : calc_disp_frag_state_t;
   
 -- Handshake
-  signal count : integer;
+  signal count : integer range 0 to 2097151;
   
   signal s_in_request : std_logic;
 
@@ -213,6 +270,14 @@
   signal dpf_rd_addr : unsigned(DPF_ADDR_WIDTH-1 downto 0);
   signal dpf_rd_data : signed(DPF_DATA_WIDTH-1 downto 0);
 
+  
+--   signal T_Pixel1 : ogg_int32_t;
+--   signal T_Pixel2 : ogg_int32_t;
+
+--   signal applyfilter_states : integer;
+--   signal dispfragstates_states : integer;
+
+  
 begin  -- a_LoopFilter
 
   in_request <= s_in_request;
@@ -220,22 +285,53 @@
   out_sem_valid <= s_out_sem_valid;
   out_done <= s_out_done;
 
-  mem_64_int32: entity work.syncram
+  mem_64_int32: syncram
     generic map (QTT_DEPTH, QTT_DATA_WIDTH, QTT_ADDR_WIDTH)
     port map (clk, qtt_wr_e, qtt_wr_addr, qtt_wr_data, qtt_rd_addr, qtt_rd_data);
 
-  mem_512_int32_1: entity work.syncram
+  mem_512_int32_1: syncram
     generic map (DPF_DEPTH, DPF_DATA_WIDTH, DPF_ADDR_WIDTH)
     port map (clk, dpf_wr_e, dpf_wr_addr, dpf_wr_data, dpf_rd_addr, dpf_rd_data);
 
-  lflimits0: entity work.lflimits
+  lflimits0: lflimits
     port map (fbv_position, FLimit, fbv_value);
 
-  rpi0: entity work.reconpixelindex
-    generic map (17, 8, 12, 11, 21, 19, 20, 20)
-    port map (rpi_position, HFragments, VFragments, YStride, UVStride,
-              YPlaneFragments, UVPlaneFragments, ReconYDataOffset,
-              ReconUDataOffset, ReconVDataOffset, rpi_value);
+  rpi0: reconpixelindex
+    port map (Clk => Clk,
+              Reset_n => Reset_n,
+              in_request => s_rpi_out_requested,
+              in_valid => s_rpi_out_valid,
+              in_data => s_rpi_out_data,
+
+              out_requested => s_rpi_in_request,
+              out_valid => s_rpi_in_valid,
+              out_data => s_rpi_in_data);
+
+
+  RPI_HandShake: process (count, in_data, in_valid,
+                          state, read_state, proc_state,
+                          calc_rpi_state, rpi_position,
+                          s_in_request)
+  begin  -- process RPI_HandShake
+    s_rpi_out_data <= x"00000000";
+    s_rpi_out_valid <= '0';
+    if (s_in_request = '1') then
+      if (state = readIn and read_state = stt_32bitsData) then
+        if (count >=0 and count <=8) then
+          s_rpi_out_data <= in_data;
+          s_rpi_out_valid <= in_valid;
+        end if;
+      end if;
+    else
+      if (state = proc and
+          proc_state = stt_Calc_RPI_Value and
+          calc_rpi_state = stt_calc_rpi1) then
+        s_rpi_out_data <= resize(signed('0'&rpi_position), 32);
+        s_rpi_out_valid <= '1';
+      end if;
+    end if;
+  end process RPI_HandShake;
+
   
   process (clk)
 -------------------------------------------------------------------------------
@@ -243,6 +339,7 @@
 -------------------------------------------------------------------------------
     procedure Read32bitsData is
     begin
+--      assert false report "in_data = "&integer'image(to_integer(in_data)) severity note;
       if (count = 0) then
         HFragments <= unsigned(in_data(LG_MAX_SIZE-3 downto 0));
         count <= count + 1;
@@ -271,6 +368,7 @@
         ReconVDataOffset <= unsigned(in_data(MEM_ADDR_WIDTH-1 downto 0));
         count <= count + 1;
       else
+        assert false report "UnitFragments = "&integer'image(to_integer(in_data)) severity note;
         UnitFragmets <= unsigned(in_data(LG_MAX_SIZE*2 downto 0));
 
         MaxDPFCount <= SHIFT_RIGHT(
@@ -284,6 +382,10 @@
       end if;
     end procedure Read32bitsData;
 
+-------------------------------------------------------------------------------
+-- Procedure that receives the QThreashTable matrice and keep the data
+-- in a SRAM memory
+-------------------------------------------------------------------------------    
     procedure QThreTab is
     begin
       qtt_wr_e <= '1';
@@ -302,6 +404,10 @@
       end if;
     end procedure QThreTab;
 
+-------------------------------------------------------------------------------
+-- Procedure that receives the loop filter limits values and keep the data
+-- in an internal memory
+-------------------------------------------------------------------------------    
     procedure LfLim is
     begin
       qtt_wr_e <= '0';
@@ -318,7 +424,10 @@
       end if;
     end procedure LfLim;
 
- 
+-------------------------------------------------------------------------------
+-- Procedure that receives the display fragments matrice and keep the data
+-- in a SRAM memory
+-------------------------------------------------------------------------------    
     procedure DispFrag is
     begin
       dpf_wr_e <= '1';
@@ -335,12 +444,13 @@
       end if;
     end procedure DispFrag;
 
-
+-------------------------------------------------------------------------------
+-- Procedure that receives the ThisFrameQualityValue and the Last Reconstructed
+-- Frame offset
+-------------------------------------------------------------------------------    
     procedure ReadOthers is
     begin
-      dpf_wr_e <= '0';
       if (count = 0) then
---        assert false report "lf.ThisFrameQualityValue = "&integer'image(to_integer(in_data)) severity note;
         ThisFrameQualityValue <= in_data;
         count <= count + 1;
       else
@@ -354,7 +464,9 @@
       end if;
     end procedure ReadOthers;
     
-    
+-------------------------------------------------------------------------------
+-- Procedure that controls the read state machine
+-------------------------------------------------------------------------------    
     procedure ReadIn is
     begin
       s_out_done <= '0';
@@ -374,9 +486,9 @@
       end if;
     end procedure ReadIn;
     
-    -- *****************************************************
-    -- Procedures called when state is proc
-    -- *****************************************************
+-- *****************************************************
+-- Procedures called when state is proc
+-- *****************************************************
 
     procedure ReadMemory is
     begin
@@ -400,6 +512,33 @@
       end if;
     end procedure WriteMemory;
 
+    procedure CalcRPIValue is
+    begin
+      case calc_rpi_state is
+        when stt_calc_rpi1 =>
+          -- Wait until ReconPixelIndex can receive the data
+          if (s_rpi_out_requested = '1') then
+            calc_rpi_state <= stt_calc_rpi2;
+          end if;
+
+
+        when others =>
+          -- Wait until ReconPixelIndex returns the value
+          s_rpi_in_request <= '1';
+          if (s_rpi_in_request = '1' and s_rpi_in_valid = '1') then
+            rpi_value <= s_rpi_in_data;
+            s_rpi_in_request <= '0';
+            proc_state <= next_proc_state;
+          end if;
+      end case;
+    end procedure CalcRPIValue;
+    
+-------------------------------------------------------------------------------
+-- Initialize QIndex with 63.
+-- For each element i of qtt in decreasing order, if i >= ThisFrameQualityValue
+-- then stop, else subtract one of the QIndex and read the i-1 element of qtt,
+-- until QIndex = 0
+-------------------------------------------------------------------------------    
     procedure FindQIndex is
     begin
       if (count = 0) then
@@ -421,10 +560,13 @@
       end if;
     end procedure FindQIndex;
 
+-------------------------------------------------------------------------------
+-- If LoopFilterLimits[QIndex] is not zero do the loopfiltering in the frame.
+-- The next procedure is SelectColor
+-------------------------------------------------------------------------------
     procedure CalcFLimit is
     begin
       if (LoopFilterLimits(to_integer(QIndex)) /= "00000000000000000000000000000000") then
-
         proc_state <= stt_SelectColor;
         FLimit <= '0' & signed(LoopFilterLimits(to_integer(QIndex)));
       else
@@ -440,6 +582,9 @@
       end if;      
     end procedure CalcFLimit;
 
+-------------------------------------------------------------------------------
+-- Adjust some parameters according the plane of color
+-------------------------------------------------------------------------------    
     procedure SelectColor is
     begin
       if (pli = "00") then
@@ -449,7 +594,9 @@
         FragsDown <= VFragments;
         Fragment <= "000000000000000000000";
         proc_state <= stt_ApplyFilter;
+        disp_frag_state <= stt_DispFrag1;
         pli <= pli + 1;
+
       elsif (pli = "01") then
 
         FragsAcross <= SHIFT_RIGHT(HFragments, 1);
@@ -458,7 +605,9 @@
         FragsDown <= SHIFT_RIGHT(VFragments, 1);
         Fragment <= YPlaneFragments;
         proc_state <= stt_ApplyFilter;
+        disp_frag_state <= stt_DispFrag1;
         pli <= pli + 1;
+        
       elsif (pli = "10") then
 
         FragsAcross <= SHIFT_RIGHT(HFragments, 1);
@@ -467,9 +616,10 @@
         FragsDown <= SHIFT_RIGHT(VFragments, 1);
         Fragment <= YPlaneFragments + UVPlaneFragments;
         proc_state <= stt_ApplyFilter;
+        disp_frag_state <= stt_DispFrag1;
         pli <= pli + 1;
       else
---        assert false report "SelectColor 4" severity note;
+        assert false report "SelectColor 4" severity note;
         pli <= "00";
         count <= 0;
         s_out_done <= '1';
@@ -551,7 +701,8 @@
         
         Pixel1 := NULL_24bits &
                   signed(Pixel(1)) + fbv_value;
-
+--        T_Pixel1 <= Pixel1;
+        
         out_sem_addr <= lfr_OffSet +
                         resize(
                           SHIFT_RIGHT(
@@ -598,6 +749,8 @@
         Pixel2 := NULL_24bits &
                   signed(Pixel(2)) - fbv_value;
 
+--        T_Pixel2 <= Pixel2;
+        
         out_sem_addr <= lfr_OffSet +
                         resize(
                           SHIFT_RIGHT(
@@ -749,7 +902,7 @@
 
         Pixel1 := (NULL_24bits &
                    signed(Pixel(1))) + fbv_value;
-
+--        T_Pixel1 <= Pixel1;
         out_sem_addr <= lfr_OffSet +
                         resize(
                           SHIFT_RIGHT(
@@ -796,8 +949,8 @@
 
         Pixel2 := (NULL_24bits &
                    signed(Pixel(2))) - fbv_value;
-
-
+--        T_Pixel2 <= Pixel2;
+        
         out_sem_addr <= lfr_OffSet +
                         resize(
                           SHIFT_RIGHT(
@@ -875,7 +1028,9 @@
     procedure ApplyFilter is
       variable NextFragment : unsigned(LG_MAX_SIZE*2 downto 0);
     begin
+
       if (apply_filter_state = stt_ApplyFilter_1) then
+--        applyfilter_states <= 1;
         -- ******************************************************
         -- First Row
         -- ******************************************************
@@ -883,6 +1038,7 @@
         -- only do 2 prediction if fragment coded and on non intra
         -- or if all fragments are intra
         if (disp_frag_state = stt_DispFrag1) then
+--          dispfragstates_states <= 1;
           -- dpf_rd_addr <= resize(SHIFT_RIGHT(Fragment,5),  DPF_ADDR_WIDTH);
           dpf_rd_addr <= Fragment((4+DPF_ADDR_WIDTH) downto 5);
           dpf_position <= Fragment;
@@ -891,31 +1047,43 @@
           next_disp_frag_state <= stt_DispFrag2;
         else
           if (disp_frag_value = '1') then
-            -- Filter right hand border only if the block to the right
-            -- is not coded
-            if (disp_frag_state = stt_DispFrag2) then
-              NextFragment := Fragment + 1;
-              dpf_rd_addr <= NextFragment((4+DPF_ADDR_WIDTH) downto 5);
-              dpf_position <= Fragment + 1;
-              proc_state <= stt_CalcDispFragPos;
-              next_apply_filter_state <= stt_ApplyFilter_1;
-              next_disp_frag_state <= stt_DispFrag3;
-            else
-              if (disp_frag_value = '0') then
-                rpi_position <= resize(Fragment, RPI_POS_WIDTH);
-                DeltaHorizFilter <= x"6";
-                proc_state <= stt_CallFilterHoriz;
-                next_apply_filter_state <= stt_ApplyFilter_2;
-              else
-                apply_filter_state <= stt_ApplyFilter_2;
-              end if;
-            end if;
+            apply_filter_state <= stt_ApplyFilter_34;
           else
             apply_filter_state <= stt_ApplyFilter_3;
           end if;
         end if;
         
+      elsif (apply_filter_state = stt_ApplyFilter_34) then
+--        applyfilter_states <= 34;
+        -- Filter right hand border only if the block to the right
+        -- is not coded
+        if (disp_frag_state = stt_DispFrag2) then
+--          dispfragstates_states <= 2;
+          NextFragment := Fragment + 1;
+          dpf_rd_addr <= NextFragment((4+DPF_ADDR_WIDTH) downto 5);
+          dpf_position <= Fragment + 1;
+          proc_state <= stt_CalcDispFragPos;
+          next_apply_filter_state <= stt_ApplyFilter_34;
+          next_disp_frag_state <= stt_DispFrag3;
+        else
+          if (disp_frag_value = '0') then
+            rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+            -- Horizontal Filter Parameter
+            DeltaHorizFilter <= x"6";
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterHoriz;
+            next_apply_filter_state <= stt_ApplyFilter_2;
+          else
+            apply_filter_state <= stt_ApplyFilter_2;
+          end if;
+        end if;
+  
       elsif (apply_filter_state = stt_ApplyFilter_2) then
+--        applyfilter_states <= 2;
         -- Bottom done if next row set
         if (disp_frag_state = stt_DispFrag3) then
           NextFragment := Fragment + LineFragments;
@@ -927,7 +1095,12 @@
         else
           if (disp_frag_value = '0') then
             rpi_position <= resize(Fragment +  LineFragments, RPI_POS_WIDTH);
-            proc_state <= stt_CallFilterVert;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterVert;
             next_apply_filter_state <= stt_ApplyFilter_3;
           else
             apply_filter_state <= stt_ApplyFilter_3;
@@ -935,7 +1108,7 @@
         end if;
         
       elsif (apply_filter_state = stt_ApplyFilter_3) then
-
+--        applyfilter_states <= 3;
         Fragment <= Fragment + 1;
         CountMiddles <= '0' & x"00001";
         apply_filter_state <= stt_ApplyFilter_4;
@@ -959,8 +1132,15 @@
             if (disp_frag_value = '1') then
               -- Filter Left edge always
               rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+              -- Horizontal Filter Parameter
               DeltaHorizFilter <= x"E";
-              proc_state <= stt_CallFilterHoriz;
+              
+              -- Calculate RPI_Value before continue
+              proc_state <= stt_Calc_RPI_Value;
+              calc_rpi_state <= stt_calc_rpi1;
+              -- Next state after RPI_Value calculation
+              next_proc_state <= stt_CallFilterHoriz;
+              
               next_apply_filter_state <= stt_ApplyFilter_5;
             else
               apply_filter_state <= stt_ApplyFilter_7;  -- Increment CountMiddles
@@ -993,8 +1173,14 @@
             -- Filter right hand border only if the block to the right is
             -- not coded
             rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+            -- Horizontal Filter Parameter
             DeltaHorizFilter <= x"6";
-            proc_state <= stt_CallFilterHoriz;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterHoriz;
             next_apply_filter_state <= stt_ApplyFilter_6;
 
           else
@@ -1021,7 +1207,12 @@
 
             -- Bottom done if next row set
             rpi_position <= resize(Fragment + LineFragments, RPI_POS_WIDTH);
-            proc_state <= stt_CallFilterVert;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterVert;
             next_apply_filter_state <= stt_ApplyFilter_7;
           else
             apply_filter_state <= stt_ApplyFilter_7;
@@ -1054,8 +1245,14 @@
           if (disp_frag_value = '1') then
             -- Filter Left edge always
             rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+            -- Horizontal Filter Parameter
             DeltaHorizFilter <= x"E";
-            proc_state <= stt_CallFilterHoriz;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterHoriz;
             next_apply_filter_state <= stt_ApplyFilter_9;
           else
             apply_filter_state <= stt_ApplyFilter_10;
@@ -1077,7 +1274,12 @@
           if (disp_frag_value = '0') then
             -- Bottom done if next row set
             rpi_position <= resize(Fragment + LineFragments, RPI_POS_WIDTH);
-            proc_state <= stt_CallFilterVert;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterVert;
             next_apply_filter_state <= stt_ApplyFilter_10;
           else
             apply_filter_state <= stt_ApplyFilter_10;
@@ -1112,7 +1314,12 @@
             if (disp_frag_value = '1') then
               -- TopRow is always done
               rpi_position <= resize(Fragment, RPI_POS_WIDTH);
-              proc_state <= stt_CallFilterVert;
+
+              -- Calculate RPI_Value before continue
+              proc_state <= stt_Calc_RPI_Value;
+              calc_rpi_state <= stt_calc_rpi1;
+              -- Next state after RPI_Value calculation
+              next_proc_state <= stt_CallFilterVert;
               next_apply_filter_state <= stt_ApplyFilter_12;
             else
               apply_filter_state <= stt_ApplyFilter_14;  -- Do middle columns
@@ -1142,8 +1349,14 @@
             -- Filter right hand border only if the block to the right is
             -- not coded
             rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+            -- Horizontal Filter Parameter
             DeltaHorizFilter <= x"6";
-            proc_state <= stt_CallFilterHoriz;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterHoriz;
             next_apply_filter_state <= stt_ApplyFilter_13;
           else
             apply_filter_state <= stt_ApplyFilter_13;
@@ -1167,7 +1380,12 @@
           if (disp_frag_value = '0') then
             -- Bottom done if next row set
             rpi_position <= resize(Fragment + LineFragments, RPI_POS_WIDTH);
-            proc_state <= stt_CallFilterVert;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterVert;
             next_apply_filter_state <= stt_ApplyFilter_14;
           else
             apply_filter_state <= stt_ApplyFilter_14;
@@ -1200,8 +1418,14 @@
             if (disp_frag_value = '1') then
               -- Filter Left edge always
               rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+              -- Horizontal Filter Parameter
               DeltaHorizFilter <= x"E";
-              proc_state <= stt_CallFilterHoriz;
+
+              -- Calculate RPI_Value before continue
+              proc_state <= stt_Calc_RPI_Value;
+              calc_rpi_state <= stt_calc_rpi1;
+              -- Next state after RPI_Value calculation
+              next_proc_state <= stt_CallFilterHoriz;
               next_apply_filter_state <= stt_ApplyFilter_16;
             else
               apply_filter_state <= stt_ApplyFilter_19;  -- Increment CountMidCols
@@ -1222,7 +1446,12 @@
         
         -- TopRow is always done
         rpi_position <= resize(Fragment, RPI_POS_WIDTH);
-        proc_state <= stt_CallFilterVert;
+
+        -- Calculate RPI_Value before continue
+        proc_state <= stt_Calc_RPI_Value;
+        calc_rpi_state <= stt_calc_rpi1;
+        -- Next state after RPI_Value calculation
+        next_proc_state <= stt_CallFilterVert;
         next_apply_filter_state <= stt_ApplyFilter_17;
 
 
@@ -1244,8 +1473,14 @@
         else
           if (disp_frag_value = '0') then
             rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+            -- Horizontal Filter Parameter
             DeltaHorizFilter <= x"6";
-            proc_state <= stt_CallFilterHoriz;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterHoriz;
             next_apply_filter_state <= stt_ApplyFilter_18;
           else
             apply_filter_state <= stt_ApplyFilter_18;
@@ -1269,7 +1504,12 @@
         else
           if (disp_frag_value = '0') then
             rpi_position <= resize(Fragment + LineFragments, RPI_POS_WIDTH);
-            proc_state <= stt_CallFilterVert;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterVert;
             next_apply_filter_state <= stt_ApplyFilter_19;
           else
             apply_filter_state <= stt_ApplyFilter_19;
@@ -1298,8 +1538,14 @@
           if (disp_frag_value = '1') then
             -- Filter Left edge always
             rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+            -- Horizontal Filter Parameter
             DeltaHorizFilter <= x"E";
-            proc_state <= stt_CallFilterHoriz;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterHoriz;
             next_apply_filter_state <= stt_ApplyFilter_21;
           else
             apply_filter_state <= stt_ApplyFilter_23;
@@ -1312,7 +1558,12 @@
 
         -- TopRow is always done
         rpi_position <= resize(Fragment, RPI_POS_WIDTH);
-        proc_state <= stt_CallFilterVert;
+
+        -- Calculate RPI_Value before continue
+        proc_state <= stt_Calc_RPI_Value;
+        calc_rpi_state <= stt_calc_rpi1;
+        -- Next state after RPI_Value calculation
+        next_proc_state <= stt_CallFilterVert;
         next_apply_filter_state <= stt_ApplyFilter_22;
 
         
@@ -1333,7 +1584,12 @@
 
           if (disp_frag_value = '0') then
             rpi_position <= resize(Fragment + LineFragments, RPI_POS_WIDTH);
-            proc_state <= stt_CallFilterVert;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterVert;
             next_apply_filter_state <= stt_ApplyFilter_23;
           else
             apply_filter_state <= stt_ApplyFilter_23;
@@ -1367,7 +1623,12 @@
           if (disp_frag_value = '1') then
             -- TopRow is always done
             rpi_position <= resize(Fragment, RPI_POS_WIDTH);
-            proc_state <= stt_CallFilterVert;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterVert;
             next_apply_filter_state <= stt_ApplyFilter_25;
           else
             apply_filter_state <= stt_ApplyFilter_26;
@@ -1391,8 +1652,14 @@
 
           if (disp_frag_value = '0') then
             rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+            -- Horizontal Filter Parameter
             DeltaHorizFilter <= x"6";
-            proc_state <= stt_CallFilterHoriz;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterHoriz;
             next_apply_filter_state <= stt_ApplyFilter_26;
           else
             apply_filter_state <= stt_ApplyFilter_26;
@@ -1420,9 +1687,15 @@
           else
             if (disp_frag_value = '1') then
               -- Filter Left edge always
-              rpi_position <= resize(Fragment, RPI_POS_WIDTH);            
+              rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+              -- Horizontal Filter Parameter
               DeltaHorizFilter <= x"E";
-              proc_state <= stt_CallFilterHoriz;
+
+              -- Calculate RPI_Value before continue
+              proc_state <= stt_Calc_RPI_Value;
+              calc_rpi_state <= stt_calc_rpi1;
+              -- Next state after RPI_Value calculation
+              next_proc_state <= stt_CallFilterHoriz;
               next_apply_filter_state <= stt_ApplyFilter_28;
             else
               apply_filter_state <= stt_ApplyFilter_30;  -- Increment CountMiddles
@@ -1439,7 +1712,12 @@
 
         -- TopRow is always done
         rpi_position <= resize(Fragment, RPI_POS_WIDTH);
-        proc_state <= stt_CallFilterVert;
+
+        -- Calculate RPI_Value before continue
+        proc_state <= stt_Calc_RPI_Value;
+        calc_rpi_state <= stt_calc_rpi1;
+        -- Next state after RPI_Value calculation
+        next_proc_state <= stt_CallFilterVert;
         next_apply_filter_state <= stt_ApplyFilter_29;
         
       elsif (apply_filter_state = stt_ApplyFilter_29) then
@@ -1460,8 +1738,14 @@
         else
           if (disp_frag_value = '0') then
             rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+            --Horizontal Filter Parameter
             DeltaHorizFilter <= x"6";
-            proc_state <= stt_CallFilterHoriz;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterHoriz;
             next_apply_filter_state <= stt_ApplyFilter_30;
           else
             apply_filter_state <= stt_ApplyFilter_30;
@@ -1493,8 +1777,14 @@
           if (disp_frag_value = '1') then
             -- Filter Left edge always
             rpi_position <= resize(Fragment, RPI_POS_WIDTH);
+            -- Horizontal Filter Parameter
             DeltaHorizFilter <= x"E";
-            proc_state <= stt_CallFilterHoriz;
+
+            -- Calculate RPI_Value before continue
+            proc_state <= stt_Calc_RPI_Value;
+            calc_rpi_state <= stt_calc_rpi1;
+            -- Next state after RPI_Value calculation
+            next_proc_state <= stt_CallFilterHoriz;
             next_apply_filter_state <= stt_ApplyFilter_32;
           else
             apply_filter_state <= stt_ApplyFilter_33;
@@ -1507,7 +1797,12 @@
 
         -- TopRow is always done
         rpi_position <= resize(Fragment, RPI_POS_WIDTH);
-        proc_state <= stt_CallFilterVert;
+
+        -- Calculate RPI_Value before continue
+        proc_state <= stt_Calc_RPI_Value;
+        calc_rpi_state <= stt_calc_rpi1;
+        -- Next state after RPI_Value calculation
+        next_proc_state <= stt_CallFilterVert;
         next_apply_filter_state <= stt_ApplyFilter_33;
 
 
@@ -1526,6 +1821,7 @@
       case proc_state is
         when stt_ReadMemory => ReadMemory;
         when stt_WriteMemory => WriteMemory;
+        when stt_Calc_RPI_Value => CalcRPIValue;
         when stt_FindQIndex => FindQIndex;
         when stt_CalcFLimit => CalcFLimit;
         when stt_SelectColor => SelectColor;
@@ -1540,63 +1836,66 @@
     end procedure Proc;
     
   begin  -- process
-    if (Reset_n = '0') then
-      state <= readIn;
-      read_state <= stt_32bitsData;
-      proc_state <= stt_FindQIndex;
-      apply_filter_state <= stt_ApplyFilter_1;
-      calc_filter_state <= stt_CalcFilter1;
 
-      s_in_request <= '0';
-      s_in_sem_request <= '0';
-      count <= 0;
-      pli <=  "00";
-      s_out_sem_valid <= '0';
-      s_out_done <= '0';
+    
+    if (clk'event and clk = '1') then
+      if (Reset_n = '0') then
+        state <= readIn;
+        read_state <= stt_32bitsData;
+        proc_state <= stt_FindQIndex;
+        apply_filter_state <= stt_ApplyFilter_1;
+        calc_filter_state <= stt_CalcFilter1;
+        calc_rpi_state <= stt_calc_rpi1;
+        
+        s_in_request <= '0';
+        s_in_sem_request <= '0';
+        count <= 0;
+        pli <=  "00";
+        s_out_sem_valid <= '0';
+        s_out_done <= '0';
 
-      mem_rd_valid <= '0';
-      
-      CountFilter <= "000";
-      CountColumns <= "000";
-      pixelPtr <= "00000000000000000000";
+        mem_rd_valid <= '0';
+        
+        CountFilter <= "000";
+        CountColumns <= "000";
+        pixelPtr <= "00000000000000000000";
 
-      rpi_position <= '0' & x"0000";
-      HFragments <= x"11";
-      VFragments <= x"00";
-      YStride <= x"000";
-      UVStride <= "000" & x"00";
-      YPlaneFragments <= '0' & x"00000";
-      UVPlaneFragments <= "000" & x"0000";
-      ReconYDataOffset <= x"00000";
-      ReconUDataOffset <= x"00000";
-      ReconVDataOffset <= x"00000";
+        rpi_position <= '0' & x"0000";
+        HFragments <= x"11";
+        VFragments <= x"00";
+        YStride <= x"000";
+        UVStride <= "000" & x"00";
+        YPlaneFragments <= '0' & x"00000";
+        UVPlaneFragments <= "000" & x"0000";
+        ReconYDataOffset <= x"00000";
+        ReconUDataOffset <= x"00000";
+        ReconVDataOffset <= x"00000";
 
 -- FLimits signals initialiation
-      fbv_position <= "000000000";
-      FLimit <= "000000000";
+        fbv_position <= "000000000";
+        FLimit <= "000000000";
 
-      
+        
 -- QThreshTable signal memories
-      qtt_wr_e <= '0';
-      qtt_wr_addr <= "000000";
-      qtt_wr_data <= "00000000000000000000000000000000";
-      qtt_rd_addr <= "000000";
+        qtt_wr_e <= '0';
+        qtt_wr_addr <= "000000";
+        qtt_wr_data <= "00000000000000000000000000000000";
+        qtt_rd_addr <= "000000";
 
 
 --display_fragments signal memories
-      dpf_wr_e <= '0';
-      dpf_wr_addr <= to_unsigned(0, DPF_ADDR_WIDTH);
-      dpf_wr_data <= "00000000000000000000000000000000";
-      dpf_rd_addr <= to_unsigned(0, DPF_ADDR_WIDTH);
-
-      
-    elsif (clk'event and clk = '1') then
-      if (Enable = '1') then
-        case state is
-          when readIn => ReadIn;
-          when proc => Proc;
-          when others => ReadIn; state <= readIn;
-        end case;
+        dpf_wr_e <= '0';
+        dpf_wr_addr <= to_unsigned(0, DPF_ADDR_WIDTH);
+        dpf_wr_data <= "00000000000000000000000000000000";
+        dpf_rd_addr <= to_unsigned(0, DPF_ADDR_WIDTH);
+      else
+        if (Enable = '1') then
+          case state is
+            when readIn => ReadIn;
+            when proc => Proc;
+            when others => ReadIn; state <= readIn;
+          end case;
+        end if;
       end if;
     end if;
   end process;

Modified: trunk/theora-fpga/reconrefframes/reconframes.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/reconframes.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/reconframes.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -1,7 +1,10 @@
 library std;
 library ieee;
+library work;
+
 use ieee.std_logic_1164.all;
 use ieee.numeric_std.all;
+use work.all;
 
 entity ReconFrames is
     port (
@@ -28,6 +31,29 @@
 end ReconFrames;
 
 architecture a_ReconFrames of ReconFrames is
+  component ExpandBlock
+    port (
+      Clk,
+      Reset_n           : in  std_logic;
+      Enable            : in  std_logic;
+      
+      in_request        : out std_logic;
+      in_valid          : in  std_logic;
+      in_data           : in  signed(31 downto 0);
+
+      in_sem_request    : out std_logic;
+      in_sem_valid      : in  std_logic;
+      in_sem_addr       : out unsigned(19 downto 0);
+      in_sem_data       : in  signed(31 downto 0);
+
+      out_sem_requested : in  std_logic;
+      out_sem_valid     : out std_logic;
+      out_sem_addr      : out unsigned(19 downto 0);
+      out_sem_data      : out signed(31 downto 0);
+
+      out_done          : out std_logic);
+  end component;
+
   constant LG_MAX_SIZE    : natural := 10;
   constant MEM_ADDR_WIDTH : natural := 20;
 -------------------------------------------------------------------------------
@@ -59,19 +85,28 @@
   signal countExpand : unsigned(LG_MAX_SIZE*2-1 downto 0);
   signal s_in_request : std_logic;
 
-  signal s_in_valid : std_logic;
 -------------------------------------------------------------------------------
 -- States and sub-states
 -------------------------------------------------------------------------------
   type state_t is (stt_Read, stt_Proc);
   signal state : state_t;
   
-  type read_state_t is (stt_read_Others, stt_read_QuantDispFrags);
+  type read_state_t is (stt_read_HFragments,
+                        stt_read_YPlaneFragments,
+                        stt_read_YStride,
+                        stt_read_UVPlaneFragments,
+                        stt_read_UVStride,
+                        stt_read_VFragments,
+                        stt_read_ReconYDataOffset,
+                        stt_read_ReconUDataOffset,
+                        stt_read_ReconVDataOffset,
+                        stt_read_QuantDispFrags,
+                        stt_read_Others);
   signal read_state : read_state_t;
 
 begin  -- a_ReconFrames
 
-  expandblock0: entity work.expandblock
+  expandblock0: expandblock
     port map(clk, reset_n, eb_enable,
              out_eb_request, out_eb_valid, out_eb_data,
              out_eb_DtBuf_request, out_eb_DtBuf_valid, out_eb_DtBuf_addr, out_eb_DtBuf_data,
@@ -95,13 +130,13 @@
 -----------------------------------------------------------------------------
   -- Switch the in_request
   -----------------------------------------------------------------------------
-  process(read_state, out_eb_request, in_valid, s_in_valid)
+  process(read_state, out_eb_request, in_valid)
   begin
     s_in_request <= out_eb_request;
     out_eb_valid <= in_valid;
     if (read_state = stt_read_QuantDispFrags) then
       s_in_request <= '1';
-      out_eb_valid <= s_in_valid;
+      out_eb_valid <= '0';
     end if;
   end process;
 
@@ -109,63 +144,98 @@
   
   process(clk)
   begin
-    if (Reset_n = '0') then
-      count <= 0;
-      countExpand <= to_unsigned(0, LG_MAX_SIZE*2);
-      eb_enable <= '1';
-      QuantDispFrags <= to_unsigned(0, LG_MAX_SIZE*2);
-      read_state <= stt_read_Others;
-      
-    elsif (clk'event and clk = '1') then
-      out_done <= '0';
-      out_eb_done <= '0';
-      if (Enable = '1') then
-        case state is
+   
+    if (clk'event and clk = '1') then
+      if (Reset_n = '0') then
+        count <= 0;
+        countExpand <= to_unsigned(0, LG_MAX_SIZE*2);
+        eb_enable <= '1';
+        QuantDispFrags <= to_unsigned(0, LG_MAX_SIZE*2);
+        read_state <= stt_read_HFragments;
+      else
+        out_done <= '0';
+        out_eb_done <= '0';
+        if (Enable = '1') then
+          case state is
 
-          when stt_Read =>
+            when stt_Read =>
 
 --            assert false report "rf.in_valid = "&std_logic'image(in_valid) severity note;
 
-            if (s_in_request = '1' and in_valid = '1') then
+              if (s_in_request = '1' and in_valid = '1') then
 --              assert false report "rf.in_data = "&integer'image(to_integer(in_data)) severity note;
-              count <= count + 1;
-              if (count = 8) then
-                read_state <= stt_read_QuantDispFrags;
-                s_in_valid <= '0';
+                count <= count + 1;
+                case read_state is
+                  when stt_read_HFragments =>
+                    read_state <= stt_read_YPlaneFragments;
 
-              elsif (read_state = stt_read_QuantDispFrags) then
---                assert false report "rf.QuantDispFrags = "&integer'image(to_integer(in_data)) severity note;
-                -- One per Frame
-                read_state <= stt_read_Others;
-                QuantDispFrags <= unsigned(in_data(LG_MAX_SIZE*2-1 downto 0));
-              elsif (count = 466) then
-                state <= stt_Proc;
-                count <= 10;
-              end if;
-            end if;
 
+                  when stt_read_YPlaneFragments =>
+                    read_state <= stt_read_YStride;
 
-          when stt_Proc =>
-            if (eb_done = '1') then
-              out_eb_done <= '1';
-              if ((to_integer(countExpand) mod 100) = 0) then
---                assert false report "Fragmento = "&integer'image(to_integer(countExpand)) severity note;
+
+                  when stt_read_YStride =>
+                    read_state <= stt_read_UVPlaneFragments;
+
+
+                  when stt_read_UVPlaneFragments =>
+                    read_state <= stt_read_UVStride;
+
+
+                  when stt_read_UVStride =>
+                    read_state <= stt_read_VFragments;
+
+
+                  when stt_read_VFragments =>
+                    read_state <= stt_read_ReconYDataOffset;
+
+
+                  when stt_read_ReconYDataOffset =>
+                    read_state <= stt_read_ReconUDataOffset;
+
+
+                  when stt_read_ReconUDataOffset =>
+                    read_state <= stt_read_ReconVDataOffset;
+
+
+                  when stt_read_ReconVDataOffset =>
+                    read_state <= stt_read_QuantDispFrags;
+                    
+
+                  when stt_read_QuantDispFrags =>
+                    -- One per Frame
+                    read_state <= stt_read_Others;
+                    QuantDispFrags <= unsigned(in_data(LG_MAX_SIZE*2-1 downto 0));
+
+                  when others =>
+                    if (count = 466) then
+                      state <= stt_Proc;
+                      count <= 10;
+                    end if;
+                end case;
               end if;
-              countExpand <= countExpand + 1;
-              state <= stt_Read;
-              if (countExpand = TO_INTEGER(QuantDispFrags-1)) then
-                countExpand <= to_unsigned(0, LG_MAX_SIZE*2);
-                out_done <= '1';
 
-                s_in_valid <= '0';
-                count <= 9;
-                read_state <= stt_read_QuantDispFrags;
+            when stt_Proc =>
+              if (eb_done = '1') then
+                out_eb_done <= '1';
+                if ((to_integer(countExpand) mod 100) = 0) then
+--                assert false report "Fragmento = "&integer'image(to_integer(countExpand)) severity note;
+                end if;
+                countExpand <= countExpand + 1;
+                state <= stt_Read;
+                if (countExpand = TO_INTEGER(QuantDispFrags-1)) then
+                  countExpand <= to_unsigned(0, LG_MAX_SIZE*2);
+                  out_done <= '1';
+
+                  count <= 9;
+                  read_state <= stt_read_QuantDispFrags;
+                end if;
               end if;
-            end if;
-            
-          when others => null;
+              
+            when others => null;
 
-        end case;
+          end case;
+        end if;
       end if;
     end if;
   end process;                   

Modified: trunk/theora-fpga/reconrefframes/reconpixelindex.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/reconpixelindex.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/reconpixelindex.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -4,59 +4,253 @@
 use ieee.numeric_std.all;
 
 entity ReconPixelIndex is
+  port (Clk,
+        Reset_n       : in  std_logic;
+        
+        in_request    : out std_logic;
+        in_valid      : in  std_logic;
+        in_data       : in  signed(31 downto 0);
+        
+        out_requested : in  std_logic;
+        out_valid     : out std_logic;
+        out_data      : out signed(31 downto 0)
+        );
+end entity ReconPixelIndex;
+
+architecture a_ReconPixelIndex of ReconPixelIndex is
+  component Divider
+    generic (           
+      WIDTH : positive := 32);
+    port (Clk,
+          Reset_n       : in  std_logic;
+          
+          in_request    : out std_logic;
+          in_valid      : in  std_logic;
+          dividend      : in  unsigned(WIDTH-1 downto 0);
+          divisor       : in  unsigned(WIDTH-1 downto 0);
+          
+          out_requested : in  std_logic;
+          out_valid     : out std_logic;
+          quotient      : out unsigned(WIDTH-1 downto 0);
+          remainder     : out unsigned(WIDTH-1 downto 0));
+  end component;
+
+
   
-  generic (
-    RPI_POS_WIDTH           : positive := 17;
-    HV_FRAG_WIDTH           : positive := 8;
-    Y_STRIDE_WIDTH          : positive := 12;
-    UV_STRIDE_WIDTH         : positive := 11;
-    Y_PL_FRAG_WIDTH         : positive := 21;
-    UV_PL_FRAG_WIDTH        : positive := 19;
-    RECON_Y_DATA_OFS_WIDTH  : positive := 20;
-    RECON_UV_DATA_OFS_WIDTH : positive := 20
-    
-    );
+  constant VFRAGPIXELS             : unsigned(3 downto 0) := x"8";
+  constant HFRAGPIXELS             : unsigned(3 downto 0) := x"8";
+  
+  constant RPI_POS_WIDTH           : positive := 17;
+  constant HV_FRAG_WIDTH           : positive := 8;
+  constant Y_STRIDE_WIDTH          : positive := 12;
+  constant UV_STRIDE_WIDTH         : positive := 11;
+  constant Y_PL_FRAG_WIDTH         : positive := 21;
+  constant UV_PL_FRAG_WIDTH        : positive := 19;
+  constant RECON_Y_DATA_OFS_WIDTH  : positive := 20;
+  constant RECON_UV_DATA_OFS_WIDTH : positive := 20;
+                                                 
+  -- States machines
+  type state_t is (stt_readIn, stt_Proc, stt_WriteOut);
+  signal state : state_t;
+  
+  type read_state_t is (stt_read_HFragments,
+                        stt_read_YPlaneFragments,
+                        stt_read_YStride,
+                        stt_read_UVPlaneFragments,
+                        stt_read_UVStride,
+                        stt_read_VFragments,
+                        stt_read_ReconYDataOffset,
+                        stt_read_ReconUDataOffset,
+                        stt_read_ReconVDataOffset,
+                        stt_read_Position);
+  signal read_state : read_state_t;
 
-  port (
-    rpi_position : in unsigned(RPI_POS_WIDTH-1 downto 0);
-    HFragments : in unsigned(HV_FRAG_WIDTH-1 downto 0);
-    VFragments : in unsigned(HV_FRAG_WIDTH-1 downto 0);
-    YStride    : in unsigned(Y_STRIDE_WIDTH-1 downto 0);
-    UVStride   : in unsigned(UV_STRIDE_WIDTH-1 downto 0);
-    YPlaneFragments : in unsigned(Y_PL_FRAG_WIDTH-1 downto 0);
-    UVPlaneFragments : in unsigned(UV_PL_FRAG_WIDTH-1 downto 0);
-    ReconYDataOffset : in unsigned(RECON_Y_DATA_OFS_WIDTH-1 downto 0);
-    ReconUDataOffset : in unsigned(RECON_UV_DATA_OFS_WIDTH-1 downto 0);
-    ReconVDataOffset : in unsigned(RECON_UV_DATA_OFS_WIDTH-1 downto 0);
+  type proc_state_t is (stt_proc_1, stt_proc_2);
+  signal proc_state : proc_state_t;
+  
+  -- Signals that will be received
+  signal rpi_position     : unsigned(RPI_POS_WIDTH-1 downto 0);
+  signal HFragments       : unsigned(HV_FRAG_WIDTH-1 downto 0);
+  signal VFragments       : unsigned(HV_FRAG_WIDTH-1 downto 0);
+  signal YStride          : unsigned(Y_STRIDE_WIDTH-1 downto 0);
+  signal UVStride         : unsigned(UV_STRIDE_WIDTH-1 downto 0);
+  signal YPlaneFragments  : unsigned(Y_PL_FRAG_WIDTH-1 downto 0);
+  signal UVPlaneFragments : unsigned(UV_PL_FRAG_WIDTH-1 downto 0);
+  signal ReconYDataOffset : unsigned(RECON_Y_DATA_OFS_WIDTH-1 downto 0);
+  signal ReconUDataOffset : unsigned(RECON_UV_DATA_OFS_WIDTH-1 downto 0);
+  signal ReconVDataOffset : unsigned(RECON_UV_DATA_OFS_WIDTH-1 downto 0);
 
-    rpi_value : out signed(31 downto 0)
-    );
-end ReconPixelIndex;
+  -- Calculated value
+  signal rpi_value        : signed(31 downto 0);
 
-architecture a_ReconPixelIndex of ReconPixelIndex is
-  constant VFRAGPIXELS : unsigned(3 downto 0) := x"8";
-  constant HFRAGPIXELS : unsigned(3 downto 0) := x"8";
+  -- Handshake signals
+  signal s_in_request : std_logic;
+  signal s_out_valid  : std_logic;
 
+  -- Divider signals
+  signal s_divider_in_request    : std_logic;
+  signal s_divider_in_valid      : std_logic;
+  signal s_divider_dividend      : unsigned(RPI_POS_WIDTH-1 downto 0);
+  signal s_divider_divisor       : unsigned(RPI_POS_WIDTH-1 downto 0);
+  signal s_divider_out_requested : std_logic;
+  signal s_divider_out_valid     : std_logic;
+  signal s_divider_quotient      : unsigned(RPI_POS_WIDTH-1 downto 0);
+  signal s_divider_remainder     : unsigned(RPI_POS_WIDTH-1 downto 0);
+
+  
 begin  -- a_ReconPixelIndex
 
-  rpi_value <=
-    resize(
-      signed(
+  divider0: divider
+    generic map (WIDTH  => RPI_POS_WIDTH)
+    port map(Clk            => Clk,
+             Reset_n        => Reset_n,
+             in_request     => s_divider_out_requested,
+             in_valid       => s_divider_out_valid,
+             dividend       => s_divider_dividend,
+             divisor        => s_divider_divisor,
+             out_requested  => s_divider_in_request,
+             out_valid      => s_divider_in_valid,
+             quotient       => s_divider_quotient,
+             remainder      => s_divider_remainder);
 
-        ((rpi_position / HFragments) * VFRAGPIXELS * YStride) +
-        ((rpi_position mod HFragments) * HFRAGPIXELS) +
-        ReconYDataOffset), 32)
-    when rpi_position < YPlaneFragments else
-    resize(
-      signed(
-        (((rpi_position - YPlaneFragments) / SHIFT_RIGHT(HFragments, 1)) * VFRAGPIXELS * UVStride) +
-        (((rpi_position - YPlaneFragments) mod SHIFT_RIGHT(HFragments, 1)) * HFRAGPIXELS) +
-        ReconUDataOffset), 32)
-    when rpi_position < YPlaneFragments + UVPlaneFragments else
-    resize(
-      signed(
-        (((rpi_position - (YPlaneFragments + UVPlaneFragments))/ SHIFT_RIGHT(HFragments, 1)) * VFRAGPIXELS * UVStride) +
-        (((rpi_position - (YPlaneFragments + UVPlaneFragments)) mod SHIFT_RIGHT(HFragments, 1)) * HFRAGPIXELS) +
-        ReconVDataOffset), 32);
+  in_request <= s_in_request;
+  out_valid <= s_out_valid;
+  process(clk)
 
+    procedure ReadIn is
+    begin
+      s_in_request <= '1';
+      s_out_valid <= '0';
+
+      if (s_in_request = '1' and in_valid = '1') then
+
+        case read_state is
+          when stt_read_HFragments =>
+            read_state <= stt_read_YPlaneFragments;
+            HFragments <= unsigned(in_data(HV_FRAG_WIDTH-1 downto 0));
+
+          when stt_read_YPlaneFragments =>
+            read_state <= stt_read_YStride;
+            YPlaneFragments <= unsigned(in_data(Y_PL_FRAG_WIDTH-1 downto 0));
+
+          when stt_read_YStride =>
+            read_state <= stt_read_UVPlaneFragments;
+            YStride <= unsigned(in_data(Y_STRIDE_WIDTH-1 downto 0));
+            
+          when stt_read_UVPlaneFragments =>
+            read_state <= stt_read_UVStride;
+            UVPlaneFragments <= unsigned(in_data(UV_PL_FRAG_WIDTH-1 downto 0));
+
+          when stt_read_UVStride =>
+            read_state <= stt_read_VFragments;
+            UVStride <= unsigned(in_data(UV_STRIDE_WIDTH-1 downto 0));
+
+          when stt_read_VFragments =>
+            read_state <= stt_read_ReconYDataOffset;
+            VFragments <= unsigned(in_data(HV_FRAG_WIDTH-1 downto 0));
+
+          when stt_read_ReconYDataOffset =>
+            read_state <= stt_read_ReconUDataOffset;
+            ReconYDataOffset <= unsigned(in_data(RECON_Y_DATA_OFS_WIDTH-1 downto 0));
+
+          when stt_read_ReconUDataOffset =>
+            read_state <= stt_read_ReconVDataOffset;
+            ReconUDataOffset <= unsigned(in_data(RECON_UV_DATA_OFS_WIDTH-1 downto 0));
+
+          when stt_read_ReconVDataOffset =>
+            read_state <= stt_read_Position;
+            ReconVDataOffset <= unsigned(in_data(RECON_UV_DATA_OFS_WIDTH-1 downto 0));
+
+          when others =>                -- stt_read_Position
+            read_state <= stt_read_Position;
+            state <= stt_Proc;
+            proc_state <= stt_proc_1;
+            rpi_position <= unsigned(in_data(RPI_POS_WIDTH-1 downto 0));
+            s_in_request <= '0';
+        end case;
+      end if;
+    end procedure ReadIn;
+
+    procedure Proc is
+    begin
+      s_divider_out_valid <= '0';
+      s_divider_in_request <= '0';
+      case proc_state is
+        when stt_proc_1 =>
+          if (s_divider_out_requested = '1') then
+            s_divider_out_valid <= '1';
+            s_divider_in_request <= '1';
+            proc_state <= stt_proc_2;
+            if (rpi_position < YPlaneFragments) then
+              s_divider_dividend <= rpi_position;
+              s_divider_divisor <= resize(HFragments, RPI_POS_WIDTH);
+              rpi_value <= resize(signed('0' & ReconYDataOffset), 32);
+
+            elsif (rpi_position < YPlaneFragments + UVPlaneFragments) then
+              s_divider_dividend <= resize(rpi_position - YPlaneFragments, RPI_POS_WIDTH);
+              s_divider_divisor <= resize(SHIFT_RIGHT(HFragments, 1), RPI_POS_WIDTH);
+              rpi_value <= resize(signed('0' & ReconUDataOffset) , 32);
+            else
+              s_divider_dividend <= resize(rpi_position - (YPlaneFragments + UVPlaneFragments), RPI_POS_WIDTH);
+              s_divider_divisor <= resize(SHIFT_RIGHT(HFragments, 1), RPI_POS_WIDTH);
+              rpi_value <= resize(signed('0' & ReconVDataOffset), 32);
+            end if;
+          end if;
+
+
+
+        when others =>
+          s_divider_in_request <= '1';
+          if (s_divider_in_request = '1' and s_divider_in_valid = '1') then
+            s_divider_in_request <= '0';
+            proc_state <= stt_proc_1;
+            state <= stt_WriteOut;
+            
+            if (rpi_position < YPlaneFragments) then
+              rpi_value <= rpi_value +
+                           resize(signed('0' &
+                                  (s_divider_quotient * VFRAGPIXELS * YStride +
+                                   s_divider_remainder * HFRAGPIXELS)), 32);
+            else
+              rpi_value <= rpi_value +
+                           resize(signed('0' &
+                                  (s_divider_quotient * VFRAGPIXELS * UVStride +
+                                   s_divider_remainder * HFRAGPIXELS)), 32);
+
+            end if;
+            
+          end if;
+      end case;
+    end procedure Proc;
+        
+    procedure WriteOut is
+    begin
+      s_out_valid <= '1';
+      out_data <= rpi_value;
+      if (out_requested = '1') then
+        state <= stt_readIn;
+      end if;
+    end procedure WriteOut;
+
+
+    
+  begin
+    if (clk'event and clk = '1') then
+      if (Reset_n = '0') then
+        state <= stt_readIn;
+        read_state <= stt_read_HFragments;
+        proc_state <= stt_proc_1;
+        s_in_request <= '0';
+        s_out_valid <= '0';
+      else
+        case state is
+          when stt_readIn => ReadIn;
+          when stt_Proc => Proc;
+          when others => WriteOut;
+        end case;
+      end if;
+    end if;
+
+  end process;
+  
 end a_ReconPixelIndex;

Modified: trunk/theora-fpga/reconrefframes/reconrefframes.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/reconrefframes.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/reconrefframes.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -1,8 +1,12 @@
 library std;
 library ieee;
+library work;
+
 use ieee.std_logic_1164.all;
 use ieee.numeric_std.all;
+use work.all;
 
+
 entity ReconRefFrames is
   port (Clk,
         Reset_n       : in  std_logic;
@@ -18,6 +22,118 @@
 end entity ReconRefFrames;
 
 architecture a_ReconRefFrames of ReconRefFrames is
+  component ReconFrames
+    port (
+      Clk,
+      Reset_n           : in  std_logic;
+      Enable            : in  std_logic;
+      
+      in_request        : out std_logic;
+      in_valid          : in  std_logic;
+      in_data           : in  signed(31 downto 0);
+
+      in_sem_request    : out std_logic;
+      in_sem_valid      : in  std_logic;
+      in_sem_addr       : out unsigned(19 downto 0);
+      in_sem_data       : in  signed(31 downto 0);
+
+      out_sem_requested : in  std_logic;
+      out_sem_valid     : out std_logic;
+      out_sem_addr      : out unsigned(19 downto 0);
+      out_sem_data      : out signed(31 downto 0);
+
+      out_done          : out std_logic;
+      out_eb_done       : out std_logic);
+  end component;
+
+
+  component LoopFilter
+    port (Clk,
+          Reset_n       :       in std_logic;
+          Enable        :       in std_logic;
+          
+          in_request    :       out std_logic;
+          in_valid      :       in std_logic;
+          in_data       :       in signed(31 downto 0);
+
+          in_sem_request    :   out std_logic;
+          in_sem_valid      :   in  std_logic;
+          in_sem_addr       :   out unsigned(19 downto 0);
+          in_sem_data       :   in  signed(31 downto 0);
+
+          out_sem_requested :   in  std_logic;
+          out_sem_valid     :   out std_logic;
+          out_sem_addr      :   out unsigned(19 downto 0);
+          out_sem_data      :   out signed(31 downto 0);
+
+          out_done          :   out std_logic
+          );
+  end component;
+
+  component CopyRecon
+    port (Clk,
+          Reset_n       :       in std_logic;
+          Enable        :       in std_logic;
+          
+          in_request    :       out std_logic;
+          in_valid      :       in std_logic;
+          in_data       :       in signed(31 downto 0);
+
+          in_sem_request    :   out std_logic;
+          in_sem_valid      :   in  std_logic;
+          in_sem_addr       :   out unsigned(19 downto 0);
+          in_sem_data       :   in  signed(31 downto 0);
+
+          out_sem_requested :   in  std_logic;
+          out_sem_valid     :   out std_logic;
+          out_sem_addr      :   out unsigned(19 downto 0);
+          out_sem_data      :   out signed(31 downto 0);
+
+          out_done          :   out std_logic
+          );
+  end component;
+
+
+  component UpdateUMV
+    port (Clk,
+          Reset_n           :   in std_logic;
+          Enable            :   in std_logic;
+          
+          in_request        :   out std_logic;
+          in_valid          :   in std_logic;
+          in_data           :   in signed(31 downto 0);
+
+          in_sem_request    :   out std_logic;
+          in_sem_valid      :   in  std_logic;
+          in_sem_addr       :   out unsigned(19 downto 0);
+          in_sem_data       :   in  signed(31 downto 0);
+
+          out_sem_requested :   in  std_logic;
+          out_sem_valid     :   out std_logic;
+          out_sem_addr      :   out unsigned(19 downto 0);
+          out_sem_data      :   out signed(31 downto 0);
+
+          out_done          :   out std_logic
+          );
+  end component;
+
+  component DataBuffer
+    port (Clk,
+          Reset_n       :       in std_logic;
+
+          in_request    :       out std_logic;
+          in_valid      :       in std_logic;
+          in_addr       :       in unsigned(19 downto 0);
+          in_data       :       in signed(31 downto 0);
+
+          
+          out_requested :       in std_logic;
+          out_valid     :       out std_logic;
+          out_addr      :       in unsigned(19 downto 0);
+          out_data      :       out signed(31 downto 0)
+          );
+  end component;
+
   constant LG_MAX_SIZE    : natural := 10;
   constant MEM_ADDR_WIDTH : natural := 20;
 -------------------------------------------------------------------------------
@@ -114,6 +230,11 @@
   signal uu_done                : std_logic;
   signal uu_enable : std_logic;
 
+-------------------------------------------------------------------------------
+  signal in_rr_DtBuf_request    : std_logic;
+  signal in_rr_DtBuf_valid      : std_logic;
+  signal in_rr_DtBuf_addr       : unsigned(MEM_ADDR_WIDTH-1 downto 0);
+
   signal out_rr_DtBuf_request   : std_logic;
   signal out_rr_DtBuf_valid     : std_logic;
   signal out_rr_DtBuf_addr      : unsigned(MEM_ADDR_WIDTH-1 downto 0);
@@ -129,18 +250,19 @@
   signal FrameOfsAuxSrc    : unsigned(MEM_ADDR_WIDTH-1 downto 0);
   
   signal FrameType         : unsigned(7 downto 0);
-  signal MaxDPFCount : unsigned(LG_MAX_SIZE*2 downto 0);
+  signal MaxDPFCount       : unsigned(LG_MAX_SIZE*2 downto 0);
 
   signal s_in_request : std_logic;
   signal s_in_valid   : std_logic;
 
   signal CountCopies  : std_logic;
   signal CountUpdates : std_logic;
-  signal count : integer;
+  signal count : integer range 0 to 2097151;
   
   constant KEY_FRAME : unsigned(7 downto 0) := "00000000";
 
-  type state_t is (stt_Send, stt_ReconFrames,
+  type state_t is (stt_CleanBuffer,
+                   stt_Send, stt_ReconFrames,
                    stt_CopyRecon, stt_LoopFilter,
                    stt_UpdateUMV, stt_WriteOut);
   signal state : state_t;
@@ -166,75 +288,90 @@
                         stt_send_none);
   signal send_state : send_state_t;
 
-  type write_state_t is (stt_write1, stt_write2);
+  type write_state_t is (stt_write1, stt_write2, stt_write3);
   signal write_state : write_state_t;
-  
+
+  signal s_out_valid : std_logic;
+  signal s_out_data : signed(31 downto 0);
+
+
+
+--  signal dbg_temp : integer := 0;
+
+
 begin  -- a_ReconRefFrames
 
-  reconframes0: entity work.reconframes
+  reconframes0: reconframes
     port map(clk, Reset_n, rf_enable,
              out_rf_request, out_rf_valid, out_rf_data,
              out_rf_DtBuf_request, out_rf_DtBuf_valid, out_rf_DtBuf_addr, out_rf_DtBuf_data,
              in_rf_DtBuf_request, in_rf_DtBuf_valid, in_rf_DtBuf_addr, in_rf_DtBuf_data,
              rf_done, rf_eb_done);
 
-  loopfilter0: entity work.loopfilter
+  loopfilter0: loopfilter
     port map(clk, Reset_n, rf_enable,
              out_lf_request, out_lf_valid, out_lf_data,
              out_lf_DtBuf_request, out_lf_DtBuf_valid, out_lf_DtBuf_addr, out_lf_DtBuf_data,
              in_lf_DtBuf_request, in_lf_DtBuf_valid, in_lf_DtBuf_addr, in_lf_DtBuf_data,
              lf_done);
   
-  copyrecon0: entity work.copyrecon
+  copyrecon0: copyrecon
     port map(clk, Reset_n, cr_enable,
              out_cr_request, out_cr_valid, out_cr_data,
              out_cr_DtBuf_request, out_cr_DtBuf_valid, out_cr_DtBuf_addr, out_cr_DtBuf_data,
              in_cr_DtBuf_request, in_cr_DtBuf_valid, in_cr_DtBuf_addr, in_cr_DtBuf_data,
              cr_done);
 
-  updateumv0: entity work.UpdateUMV
+  updateumv0: UpdateUMV
     port map(clk, Reset_n, uu_enable,
              out_uu_request, out_uu_valid, out_uu_data,
              out_uu_DtBuf_request, out_uu_DtBuf_valid, out_uu_DtBuf_addr, out_uu_DtBuf_data,
              in_uu_DtBuf_request, in_uu_DtBuf_valid, in_uu_DtBuf_addr, in_uu_DtBuf_data,
              uu_done);
 
-  databuffer0: entity work.databuffer
+  databuffer0: databuffer
     port map(clk, Reset_n,
              out_DtBuf_request, out_DtBuf_valid, out_DtBuf_addr, out_DtBuf_data,
              in_DtBuf_request, in_DtBuf_valid, in_DtBuf_addr, in_DtBuf_data);
 
 
-
+  out_valid <= s_out_valid;
   -----------------------------------------------------------------------------
   -- Switch the in_request
   -----------------------------------------------------------------------------
-  in_request <= '0' when (send_state = stt_send_golden_ofs_rf or
-                          send_state = stt_send_last_ofs_rf or
-                          send_state = stt_send_this_ofs_rf or
-                          send_state = stt_send_source_ofs_cr or
-                          send_state = stt_send_dest_ofs_cr or
-                          send_state = stt_send_offset_lf or
-                          send_state = stt_send_offset_uu or
-                          send_state = stt_send_dispfrag_golden or
-                          send_state = stt_send_none) else
-                s_in_request;
-
-
+  -- If send_state is a state that doesn't need external data then
+  -- in_request will be turned off
+  with send_state select in_request <=
+    '0' when stt_send_golden_ofs_rf,
+    '0' when stt_send_last_ofs_rf,
+    '0' when stt_send_this_ofs_rf,
+    '0' when stt_send_source_ofs_cr,
+    '0' when stt_send_dest_ofs_cr,
+    '0' when stt_send_offset_lf,
+    '0' when stt_send_offset_uu,
+    '0' when stt_send_dispfrag_golden,
+    '0' when stt_send_none,
+    s_in_request when others;
   
   -----------------------------------------------------------------------------
   -- Switch the in_data and the in_valid
   -----------------------------------------------------------------------------
-  process (send_state,
+  process (Reset_n,
+           send_state,
            out_rf_request,
            out_cr_request,
            out_lf_request,
            out_uu_request,
            in_valid,
-           in_data)
+           in_data,
+           GoldenFrameOfs,
+           LastFrameReconOfs,
+           ThisFrameReconOfs,
+           FrameOfsAuxSrc,
+           FrameOfsAux)
 
   begin
-
+   
     out_rf_valid <= '0';
     out_rf_data <= in_data;
     out_cr_valid <= '0';
@@ -246,20 +383,14 @@
 
     s_in_valid <= in_valid;
     s_in_request <= '0';
---    assert false report "in_data = "&integer'image(to_integer(in_data)) severity note;
---    assert false report "in_valid = "&std_logic'image(in_valid) severity note;
+
     -----------------------------------------------------------------------------
     -- Unique Parameters
     -----------------------------------------------------------------------------    
     if (send_state = stt_rec_framesize) then
       s_in_request <= '1';
-      FrameSize <= unsigned(in_data(MEM_ADDR_WIDTH-1 downto 0));
-
+      
     elsif (send_state = stt_send_uniq_common) then
---      assert false report "out_rf_request = "&std_logic'image(out_rf_request) severity note;
---      assert false report "out_cr_request = "&std_logic'image(out_cr_request) severity note;
---      assert false report "out_lf_request = "&std_logic'image(out_lf_request) severity note;
---      assert false report "out_uu_request = "&std_logic'image(out_uu_request) severity note;
       s_in_request <= out_rf_request and
                  out_cr_request and
                  out_lf_request and
@@ -271,23 +402,13 @@
 
     elsif (send_state = stt_send_uniq_cr_lf) then
       -------------------------------------------------------------------------
-      -- UnitFragment is sent to CopyRecon and LoopFilter and read internal
+      -- UnitFragment is sent to CopyRecon and LoopFilter and read internaly
       -------------------------------------------------------------------------
---      assert false report "out_cr_request = "&std_logic'image(out_cr_request) severity note;
---      assert false report "out_lf_request = "&std_logic'image(out_lf_request) severity note;
       s_in_request <= out_cr_request and
                  out_lf_request;
       
       out_cr_valid <= in_valid;
       out_lf_valid <= in_valid;
-
-      MaxDPFCount <= SHIFT_RIGHT(
-        unsigned(in_data(LG_MAX_SIZE*2 downto 0)), 5) + 1;
-      if (in_data(4 downto 0) = "00000") then
-        MaxDPFCount <= SHIFT_RIGHT(
-          unsigned(in_data(LG_MAX_SIZE*2 downto 0)), 5);
-      end if;
-
       
     elsif (send_state = stt_send_uniq_lf) then
       s_in_request <= out_lf_request;
@@ -305,11 +426,11 @@
 
     elsif (send_state = stt_frametype) then
       -------------------------------------------------------------------------
-      -- FrameType is sent to ReconFrames and read internal
+      -- FrameType is sent to ReconFrames and read internaly
       -------------------------------------------------------------------------
       s_in_request <= out_rf_request;
       out_rf_valid <= in_valid;
-      FrameType <= unsigned(in_data(7 downto 0));
+--      FrameType <= unsigned(in_data(7 downto 0));
                    
     elsif (send_state = stt_send_golden_ofs_rf) then
       s_in_request <= out_rf_request;
@@ -332,8 +453,14 @@
     elsif (send_state = stt_send_dispfrag) then
       s_in_request <= out_cr_request and
                       out_lf_request;
-      out_cr_valid <= in_valid;
-      out_lf_valid <= in_valid;
+      out_cr_valid <= '0';
+      out_lf_valid <= '0';
+      if ((out_cr_request and out_lf_request)= '1') then
+        out_cr_valid <= in_valid;
+        out_lf_valid <= in_valid;
+      else
+        assert false report "Somebody doesn't want read" severity note;
+      end if;
       
     elsif (send_state = stt_send_source_ofs_cr) then
       s_in_request <= out_cr_request;
@@ -372,13 +499,21 @@
       s_in_valid <= '1';
       out_cr_valid <= '1';
       out_cr_data <= x"FFFFFFFF";
-     
+    else
+      null;
     end if;
-   
+    if (Reset_n = '0') then
+          out_rf_valid <= '0';
+          out_cr_valid <= '0';
+          out_lf_valid <= '0';
+          out_uu_valid <= '0';
+          s_in_request <= '0';
+    end if;
   end process;
 
 
-  process (state,
+  process (Reset_n,
+           state,
            in_DtBuf_valid,
            in_DtBuf_data,
            out_DtBuf_request,
@@ -408,81 +543,172 @@
            in_uu_DtBuf_data,
 
            out_rr_DtBuf_request,
-           out_rr_DtBuf_addr
+           out_rr_DtBuf_addr,
+           in_rr_DtBuf_valid,
+           in_rr_DtBuf_addr
            )
   begin  -- process state
-    case state is
-      when stt_ReconFrames =>
-        in_DtBuf_request <= out_rf_DtBuf_request;
-        out_rf_DtBuf_valid <= in_DtBuf_valid;
-        in_DtBuf_addr <= out_rf_DtBuf_addr;
-        out_rf_DtBuf_data  <= in_DtBuf_data;
+    out_rr_DtBuf_data  <= x"00000000";
+    out_rr_DtBuf_valid <= '0';
+    
+    out_cr_DtBuf_data  <= x"00000000";
+    out_cr_DtBuf_valid <= '0';
+    
+    out_lf_DtBuf_data  <= x"00000000";
+    out_lf_DtBuf_valid <= '0';
+    
+    out_rf_DtBuf_data <= x"00000000";
+    out_rf_DtBuf_valid <= '0';
+    
+    out_uu_DtBuf_data <= x"00000000";
+    out_uu_DtBuf_valid <= '0';
 
-        in_rf_DtBuf_request <= out_DtBuf_request;
-        out_DtBuf_valid <= in_rf_DtBuf_valid;
-        out_DtBuf_addr <= in_rf_DtBuf_addr;
-        out_DtBuf_data <= in_rf_DtBuf_data;
-        
-      when stt_CopyRecon =>
-        in_DtBuf_request <= out_cr_DtBuf_request;
-        out_cr_DtBuf_valid <= in_DtBuf_valid;
-        in_DtBuf_addr <= out_cr_DtBuf_addr;
-        out_cr_DtBuf_data  <= in_DtBuf_data;
+    in_uu_DtBuf_request <= '0';
+    in_cr_DtBuf_request <= '0';
+    in_lf_DtBuf_request <= '0';
+    in_rf_DtBuf_request <= '0';
+   
+    if (state = stt_ReconFrames) then
+      in_DtBuf_request <= out_rf_DtBuf_request;
+      out_rf_DtBuf_valid <= in_DtBuf_valid;
+      in_DtBuf_addr <= out_rf_DtBuf_addr;
+      out_rf_DtBuf_data  <= in_DtBuf_data;
 
-        in_cr_DtBuf_request <= out_DtBuf_request;
-        out_DtBuf_valid <= in_cr_DtBuf_valid;
-        out_DtBuf_addr <= in_cr_DtBuf_addr;
-        out_DtBuf_data <= in_cr_DtBuf_data;
+      in_rf_DtBuf_request <= out_DtBuf_request;
+      out_DtBuf_valid <= in_rf_DtBuf_valid;
+      out_DtBuf_addr <= in_rf_DtBuf_addr;
+      out_DtBuf_data <= in_rf_DtBuf_data;
+      
+    elsif (state = stt_CopyRecon) then
+      in_DtBuf_request <= out_cr_DtBuf_request;
+      out_cr_DtBuf_valid <= in_DtBuf_valid;
+      in_DtBuf_addr <= out_cr_DtBuf_addr;
+      out_cr_DtBuf_data  <= in_DtBuf_data;
 
-      when stt_LoopFilter =>
-        in_DtBuf_request <= out_lf_DtBuf_request;
-        out_lf_DtBuf_valid <= in_DtBuf_valid;
-        in_DtBuf_addr <= out_lf_DtBuf_addr;
-        out_lf_DtBuf_data  <= in_DtBuf_data;
+      in_cr_DtBuf_request <= out_DtBuf_request;
+      out_DtBuf_valid <= in_cr_DtBuf_valid;
+      out_DtBuf_addr <= in_cr_DtBuf_addr;
+      out_DtBuf_data <= in_cr_DtBuf_data;
 
-        in_lf_DtBuf_request <= out_DtBuf_request;
-        out_DtBuf_valid <= in_lf_DtBuf_valid;
-        out_DtBuf_addr <= in_lf_DtBuf_addr;
-        out_DtBuf_data <= in_lf_DtBuf_data;
+    elsif (state = stt_LoopFilter) then
+      in_DtBuf_request <= out_lf_DtBuf_request;
+      out_lf_DtBuf_valid <= in_DtBuf_valid;
+      in_DtBuf_addr <= out_lf_DtBuf_addr;
+      out_lf_DtBuf_data  <= in_DtBuf_data;
 
-      when stt_UpdateUMV =>
-        in_DtBuf_request <= out_uu_DtBuf_request;
-        out_uu_DtBuf_valid <= in_DtBuf_valid;
-        in_DtBuf_addr <= out_uu_DtBuf_addr;
-        out_uu_DtBuf_data  <= in_DtBuf_data;
+      in_lf_DtBuf_request <= out_DtBuf_request;
+      out_DtBuf_valid <= in_lf_DtBuf_valid;
+      out_DtBuf_addr <= in_lf_DtBuf_addr;
+      out_DtBuf_data <= in_lf_DtBuf_data;
 
-        in_uu_DtBuf_request <= out_DtBuf_request;
-        out_DtBuf_valid <= in_uu_DtBuf_valid;
-        out_DtBuf_addr <= in_uu_DtBuf_addr;
-        out_DtBuf_data <= in_uu_DtBuf_data;
+    elsif (state = stt_UpdateUMV) then
+      in_DtBuf_request <= out_uu_DtBuf_request;
+      out_uu_DtBuf_valid <= in_DtBuf_valid;
+      in_DtBuf_addr <= out_uu_DtBuf_addr;
+      out_uu_DtBuf_data  <= in_DtBuf_data;
 
-      when others =>
-        in_DtBuf_request <= out_rr_DtBuf_request;
-        out_rr_DtBuf_valid <= in_DtBuf_valid;
-        in_DtBuf_addr <= out_rr_DtBuf_addr;
-        out_rr_DtBuf_data  <= in_DtBuf_data;
+      in_uu_DtBuf_request <= out_DtBuf_request;
+      out_DtBuf_valid <= in_uu_DtBuf_valid;
+      out_DtBuf_addr <= in_uu_DtBuf_addr;
+      out_DtBuf_data <= in_uu_DtBuf_data;
 
-        out_DtBuf_valid <= '0';
-        out_DtBuf_addr <= x"00000";
-        out_DtBuf_data <= x"00000000";
+    elsif (state = stt_CleanBuffer) then
+      
+      in_DtBuf_request <= out_rr_DtBuf_request;
+      out_rr_DtBuf_valid <= in_DtBuf_valid;
+      in_DtBuf_addr <= out_rr_DtBuf_addr;
+      out_rr_DtBuf_data  <= in_DtBuf_data;
 
-    end case;
+      in_rr_DtBuf_request <= out_DtBuf_request;
+      out_DtBuf_valid <= in_rr_DtBuf_valid;
+      out_DtBuf_addr <= in_rr_DtBuf_addr;
+      out_DtBuf_data <= x"00000000";
+      
+    else
+      in_DtBuf_request <= out_rr_DtBuf_request;
+      out_rr_DtBuf_valid <= in_DtBuf_valid;
+      in_DtBuf_addr <= out_rr_DtBuf_addr;
+      out_rr_DtBuf_data  <= in_DtBuf_data;
+
+      out_DtBuf_valid <= '0';
+      out_DtBuf_addr <= x"00000";
+      out_DtBuf_data <= x"00000000";
+
+    end if;
+
+    if (Reset_n = '0') then
+      out_rr_DtBuf_data  <= x"00000000";
+      out_rr_DtBuf_valid <= '0';
+     
+      out_cr_DtBuf_data  <= x"00000000";
+      out_cr_DtBuf_valid <= '0';
+      
+      out_lf_DtBuf_data  <= x"00000000";
+      out_lf_DtBuf_valid <= '0';
+      
+      out_rf_DtBuf_data <= x"00000000";
+      out_rf_DtBuf_valid <= '0';
+      
+      out_uu_DtBuf_data <= x"00000000";
+      out_uu_DtBuf_valid <= '0';
+
+      in_uu_DtBuf_request <= '0';
+      in_cr_DtBuf_request <= '0';
+      in_lf_DtBuf_request <= '0';
+      in_rf_DtBuf_request <= '0';
+    end if;
   end process;
 
   
   process(clk)
+
+    procedure CleanBuffer is
+    begin
+      in_rr_DtBuf_valid <= '1';
+      if (count = 0) then
+        in_rr_DtBuf_addr <= x"00000";
+      else
+        in_rr_DtBuf_addr <= in_rr_DtBuf_addr + 1;
+      end if;
+
+      if (in_rr_DtBuf_request = '1') then
+        count <= count + 1;
+      end if;
+
+      if (count = SHIFT_RIGHT(3*FrameSize,2)) then
+        state <= stt_Send;
+        send_state <= stt_send_uniq_common;
+        in_rr_DtBuf_addr <= x"00000";
+        count <= 0;
+        in_rr_DtBuf_valid <= '0';
+      end if;
+
+    end CleanBuffer;
+
+    
 -------------------------------------------------------------------------------
--- Change the states syncronous
+-- Change the states syncronously
 -------------------------------------------------------------------------------
     procedure SendControl is
     begin
+--      dbg_temp <= dbg_temp + 1;
+      
 --    assert false report "s_in_request = "&std_logic'image(s_in_request) severity note;
 --    assert false report "in_request = "&std_logic'image(in_request) severity note;
---    assert false report "count = "&integer'image(count) severity note;
+--       assert false report "count = "&integer'image(count) severity note;
+--       assert false report "send_state ="&send_state_t'image(send_state) severity NOTE;
       if (s_in_request = '1' and s_in_valid = '1') then
         count <= 0;
+      
         if (send_state = stt_rec_framesize) then
-          send_state <= stt_send_uniq_common;
+          FrameSize <= unsigned(in_data(MEM_ADDR_WIDTH-1 downto 0));
+          if (count = 0) then
+            count <= 1;
+          else
+            count <= 0;
+            send_state <= stt_send_none;
+            state <= stt_CleanBuffer;
+          end if;
           
         elsif (send_state = stt_send_uniq_common) then
           GoldenFrameOfs <= x"00000";
@@ -499,6 +725,13 @@
         elsif (send_state = stt_send_uniq_cr_lf) then
           send_state <= stt_send_uniq_lf;
 
+          MaxDPFCount <= SHIFT_RIGHT(
+            unsigned(in_data(LG_MAX_SIZE*2 downto 0)), 5) + 1;
+          if (in_data(4 downto 0) = "00000") then
+            MaxDPFCount <= SHIFT_RIGHT(
+              unsigned(in_data(LG_MAX_SIZE*2 downto 0)), 5);
+          end if;
+
           
         elsif (send_state = stt_send_uniq_lf) then
           count <= count + 1;
@@ -520,6 +753,7 @@
           
         elsif (send_state = stt_frametype) then
           send_state <= stt_send_golden_ofs_rf;
+          FrameType <= unsigned(in_data(7 downto 0));
 
         elsif (send_state = stt_send_golden_ofs_rf) then
           send_state <= stt_send_last_ofs_rf;
@@ -534,6 +768,10 @@
 
         elsif (send_state = stt_send_dispfrag or
                send_state = stt_send_dispfrag_golden) then
+--           assert false report "send_state = "&send_state_t'image(send_state) severity note;
+--           assert false report "Count = "&integer'image(count) severity note;
+--           assert false report "MaxDPFCount = "&integer'image(to_integer(MaxDPFCount)) severity note;
+
           count <= count + 1;
           if (count = MaxDPFCount - 1) then
             send_state <= stt_send_source_ofs_cr;
@@ -541,9 +779,11 @@
           end if;
 
         elsif (send_state = stt_send_source_ofs_cr) then
+--           assert false report "send_state = "&send_state_t'image(send_state) severity note;
           send_state <= stt_send_dest_ofs_cr;
 
         elsif (send_state = stt_send_dest_ofs_cr) then
+--           assert false report "send_state = "&send_state_t'image(send_state) severity note;
           send_state <= stt_send_none;
           state <= stt_CopyRecon;
 
@@ -559,7 +799,8 @@
           assert false report "Calling UU" severity note;
           send_state <= stt_send_none;
           state <= stt_UpdateUMV;
-
+        else
+          null;
         end if;
       end if;
     end procedure SendControl;
@@ -578,6 +819,8 @@
         send_state <= stt_send_rf;
         state <= stt_Send;
         count <= 1;
+      else
+        null;
       end if;
     end procedure ReconFrames;
     
@@ -585,15 +828,18 @@
     begin
       if (cr_done = '1') then
         assert false report "CopyRecon Concluido" severity note;
+      
         send_state <= stt_send_lf;
         state <= stt_Send;
         CountCopies <= '0';
-        if (FrameType = KEY_FRAME and CountCopies = '0') then
-          CountCopies <= '1';
-        elsif (FrameType = KEY_FRAME and CountCopies = '1') then
-          send_state <= stt_send_offset_uu;
-          state <= stt_Send;
-        end if;
+         if (FrameType = KEY_FRAME and CountCopies = '0') then
+           CountCopies <= '1';
+         elsif (FrameType = KEY_FRAME and CountCopies = '1') then
+           send_state <= stt_send_offset_uu;
+           state <= stt_Send;
+         else
+           null;
+         end if;
       end if;
     end procedure CopyRecon;
     
@@ -610,83 +856,92 @@
     begin
       if (uu_done = '1') then
         assert false report "UpdateUMV Concluido" severity note;
-        count <= 0;
-        state <= stt_WriteOut;
-        send_state <= stt_send_none;
-        CountUpdates <= '0';
-        if (FrameType = KEY_FRAME and CountUpdates = '0') then
-          FrameOfsAux <= GoldenFrameOfs;
-          FrameOfsAuxSrc <= LastFrameReconOfs;
-          send_state <= stt_send_dispfrag_golden;
-          state <= stt_Send;
-          CountUpdates <= '1';
-        end if;
+         count <= 0;
+         state <= stt_WriteOut;
+         write_state <= stt_write1;
+         send_state <= stt_send_none;
+         CountUpdates <= '0';
+         if (FrameType = KEY_FRAME and CountUpdates = '0') then
+           FrameOfsAux <= GoldenFrameOfs;
+           FrameOfsAuxSrc <= LastFrameReconOfs;
+           send_state <= stt_send_dispfrag_golden;
+           state <= stt_Send;
+           CountUpdates <= '1';
+         end if;
       end if;
     end procedure UpdateUMV;
 
     procedure WriteOut is
     begin
-
-      out_valid <= '0';
+      s_out_valid <= '0';
       if (write_state = stt_write1) then
-        
         write_state <= stt_write2;
         out_rr_DtBuf_request <= '1';
         out_rr_DtBuf_addr <= out_rr_DtBuf_addr + 1;
 
         count <= count + 4;
         if (count = 0) then
---          assert false report "Writing Data" severity note;
---          out_rr_DtBuf_addr <= SHIFT_RIGHT(LastFrameReconOfs, 2);
+          assert false report "Writing Data" severity note;
           out_rr_DtBuf_addr <= SHIFT_RIGHT(LastFrameReconOfs, 2);
+        else
+          if (count = FrameSize) then
+            count <= 0;
+            send_state <= stt_send_rf;
+            state <= stt_Send;
+            write_state <= stt_write1;
+            out_rr_DtBuf_request <= '0';
+            out_rr_DtBuf_addr <= SHIFT_RIGHT(LastFrameReconOfs, 2);
+          end if;
+        end if;
 
-        elsif (count = FrameSize) then
---          assert false report "Data Wrote" severity note;
-          count <= 0;
-          send_state <= stt_send_rf;
-          state <= stt_Send;
-          write_state <= stt_write1;
+      elsif (write_state = stt_write2) then
+        if (out_rr_DtBuf_valid = '1') then
+          s_out_data <= out_rr_DtBuf_data;
           out_rr_DtBuf_request <= '0';
-          out_rr_DtBuf_addr <= SHIFT_RIGHT(LastFrameReconOfs, 2);
-          
+          write_state <= stt_write3;
         end if;
-
-      elsif (write_state = stt_write2) then
-        if (out_rr_DtBuf_valid = '1' and out_requested = '1') then
-          out_valid <= '1';
-          out_data <= out_rr_DtBuf_data;
+      else
+        s_out_valid <= '1';
+        out_data <= s_out_data;
+        if (out_requested = '1') then
           write_state <= stt_write1;
           out_rr_DtBuf_request <= '0';
         end if;
       end if;
-
     end procedure WriteOut;
 
   begin
-    if (Reset_n = '0') then
-      rf_enable <= '1';
-      cr_enable <= '1';
-      lf_enable <= '1';
-      uu_enable <= '1';
-      
-      write_state <= stt_write1;
-      send_state <= stt_rec_framesize;
-      state <= stt_Send;
+    if (clk'event and clk = '1') then
+      if (Reset_n = '0') then
+        rf_enable <= '1';
+        cr_enable <= '1';
+        lf_enable <= '1';
+        uu_enable <= '1';
+        
+        write_state <= stt_write1;
+        send_state <= stt_rec_framesize;
+        state <= stt_Send;
 
-      CountCopies  <= '0';
-      CountUpdates <= '0';
-      count <= 0;
+        
+        CountCopies  <= '0';
+        CountUpdates <= '0';
+        count <= 0;
+        out_data <= x"00000000";
+        s_out_data <= x"00000000";
+        s_out_valid <= '0';
 
-      
-    elsif (clk'event and clk = '1') then
-      case state is
-        when stt_Send => SendControl;
-        when stt_ReconFrames => ReconFrames;
-        when stt_CopyRecon => CopyRecon;
-        when stt_LoopFilter => LoopFilter;
-        when stt_UpdateUMV => UpdateUMV;
-        when others => WriteOut;
-      end case;
+        out_rr_DtBuf_request <= '0';
+      else
+        case state is
+          when stt_CleanBuffer => CleanBuffer;
+          when stt_Send => SendControl;
+          when stt_ReconFrames => ReconFrames;
+          when stt_CopyRecon => CopyRecon;
+          when stt_LoopFilter => LoopFilter;
+          when stt_UpdateUMV => UpdateUMV;
+          when others => WriteOut;
+        end case;
+      end if;
     end if;
   end process;
 

Modified: trunk/theora-fpga/reconrefframes/tb_reconrefframes.vhd
===================================================================
--- trunk/theora-fpga/reconrefframes/tb_reconrefframes.vhd	2007-05-07 20:54:02 UTC (rev 12923)
+++ trunk/theora-fpga/reconrefframes/tb_reconrefframes.vhd	2007-05-07 20:56:52 UTC (rev 12924)
@@ -37,7 +37,10 @@
   signal out_request : std_logic := '0';
   signal out_valid : std_logic;
   signal out_data : signed(31 downto 0);
-
+  signal count_entrada : integer := 0;
+  signal count_saida : integer := 0;
+  signal frames : integer := 0;
+  shared variable flag : std_logic := '0';
 begin  -- behavior
 
   ReconRefFrame0: entity work.reconrefframes
@@ -63,8 +66,9 @@
       else
 
         if( in_requested = '1' )then
-
-          
+          flag := '0';
+--          assert frames < 2 report "2 frames: count_entrada="&integer'image(count_entrada) severity failure;
+          count_entrada <= count_entrada + 1;
           ReadLine( DataInFile, input_line);
           Read( input_line, aux );
 --          assert false report "testbench = "&integer'image(aux) severity note;
@@ -78,8 +82,6 @@
 
 
 
-
-
   Output : process(clk, resetn)
 
     variable output_line	  : line;
@@ -88,9 +90,16 @@
     elsif clk'EVENT and clk = '1' then
       out_request <= '1' after delta;
       if( out_request = '1' and out_valid = '1' )then
+        if (flag = '0') then
+          flag := '1';
+          frames <= frames + 1;
+        end if;
+        count_saida <= count_saida + 1;
         --Write(output_line, now, left, 15);
 	Write(output_line, to_integer(out_data));
 	WriteLine(OutFile, output_line);
+        
+--        assert false report "count_entrada = "&integer'image(count_entrada) severity failure;
       end if;
     end if;
 



More information about the commits mailing list