| 1 | 3 | Andrewski | --------------------------------------------------------------------------------
 | 
      
         | 2 | 13 | Andrewski | --This file is part of fpga_gpib_controller.
 | 
      
         | 3 |  |  | --
 | 
      
         | 4 |  |  | -- Fpga_gpib_controller is free software: you can redistribute it and/or modify
 | 
      
         | 5 |  |  | -- it under the terms of the GNU General Public License as published by
 | 
      
         | 6 |  |  | -- the Free Software Foundation, either version 3 of the License, or
 | 
      
         | 7 |  |  | -- (at your option) any later version.
 | 
      
         | 8 |  |  | --
 | 
      
         | 9 |  |  | -- Fpga_gpib_controller is distributed in the hope that it will be useful,
 | 
      
         | 10 |  |  | -- but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
      
         | 11 |  |  | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
      
         | 12 |  |  | -- GNU General Public License for more details.
 | 
      
         | 13 |  |  |  
 | 
      
         | 14 |  |  | -- You should have received a copy of the GNU General Public License
 | 
      
         | 15 |  |  | -- along with Fpga_gpib_controller.  If not, see <http://www.gnu.org/licenses/>.
 | 
      
         | 16 |  |  | --------------------------------------------------------------------------------
 | 
      
         | 17 | 3 | Andrewski | -- Entity: Fifo8b
 | 
      
         | 18 |  |  | -- Date:2011-11-28  
 | 
      
         | 19 | 13 | Andrewski | -- Author: Andrzej Paluch
 | 
      
         | 20 | 3 | Andrewski | --
 | 
      
         | 21 |  |  | -- Description ${cursor}
 | 
      
         | 22 |  |  | --------------------------------------------------------------------------------
 | 
      
         | 23 |  |  | library ieee;
 | 
      
         | 24 |  |  | use ieee.std_logic_1164.all;
 | 
      
         | 25 |  |  | use ieee.std_logic_unsigned.all;
 | 
      
         | 26 |  |  | use ieee.std_logic_arith.all;
 | 
      
         | 27 |  |  |  
 | 
      
         | 28 |  |  | use work.utilPkg.all;
 | 
      
         | 29 |  |  | use work.helperComponents.all;
 | 
      
         | 30 |  |  |  
 | 
      
         | 31 |  |  |  
 | 
      
         | 32 |  |  | entity Fifo8b is
 | 
      
         | 33 |  |  |         generic (
 | 
      
         | 34 |  |  |                 MAX_ADDR_BIT_NUM : integer := 10
 | 
      
         | 35 |  |  |         );
 | 
      
         | 36 |  |  |         port (
 | 
      
         | 37 |  |  |                 reset : in std_logic;
 | 
      
         | 38 |  |  |                 clk : in std_logic;
 | 
      
         | 39 |  |  |                 -------------- fifo --------------------
 | 
      
         | 40 |  |  |                 bytesAvailable : out std_logic;
 | 
      
         | 41 |  |  |                 availableBytesCount : out std_logic_vector(MAX_ADDR_BIT_NUM downto 0);
 | 
      
         | 42 |  |  |                 bufferFull : out std_logic;
 | 
      
         | 43 |  |  |                 resetFifo : in std_logic;
 | 
      
         | 44 |  |  |                 ----------------------------------------
 | 
      
         | 45 |  |  |                 data_in : in std_logic_vector(7 downto 0);
 | 
      
         | 46 |  |  |                 ready_to_write :out std_logic;
 | 
      
         | 47 |  |  |                 strobe_write : in std_logic;
 | 
      
         | 48 |  |  |                 ----------------------------------------
 | 
      
         | 49 |  |  |                 data_out : out std_logic_vector(7 downto 0);
 | 
      
         | 50 |  |  |                 ready_to_read : out std_logic;
 | 
      
         | 51 |  |  |                 strobe_read : in std_logic
 | 
      
         | 52 |  |  |         );
 | 
      
         | 53 |  |  | end Fifo8b;
 | 
      
         | 54 |  |  |  
 | 
      
         | 55 |  |  | architecture arch of Fifo8b is
 | 
      
         | 56 |  |  |  
 | 
      
         | 57 |  |  |         constant ADDR_BITS_COUNT : integer := MAX_ADDR_BIT_NUM + 1;
 | 
      
         | 58 |  |  |         constant MEMORY_CELLS_COUNT : integer := 2**ADDR_BITS_COUNT;
 | 
      
         | 59 |  |  |         constant MAX_DATA_LENGTH : integer := MEMORY_CELLS_COUNT - 1;
 | 
      
         | 60 |  |  |         constant MAX_ADDR : integer := MAX_DATA_LENGTH;
 | 
      
         | 61 |  |  |  
 | 
      
         | 62 |  |  |         -------------- memory ----------------
 | 
      
         | 63 |  |  |         signal n_clk : std_logic;
 | 
      
         | 64 |  |  |         signal p1_addr : std_logic_vector(MAX_ADDR_BIT_NUM downto 0);
 | 
      
         | 65 |  |  |         signal p1_data_in : std_logic_vector(7 downto 0);
 | 
      
         | 66 |  |  |         signal p1_strobe : std_logic;
 | 
      
         | 67 |  |  |         signal p1_data_out : std_logic_vector(7 downto 0);
 | 
      
         | 68 |  |  |         -------------------------------------------------
 | 
      
         | 69 |  |  |         signal p2_addr : std_logic_vector(MAX_ADDR_BIT_NUM downto 0);
 | 
      
         | 70 |  |  |         signal p2_data_in : std_logic_vector(7 downto 0);
 | 
      
         | 71 |  |  |         signal p2_strobe : std_logic;
 | 
      
         | 72 |  |  |         signal p2_data_out : std_logic_vector(7 downto 0);
 | 
      
         | 73 |  |  |  
 | 
      
         | 74 |  |  |         ------------- fifo --------------------
 | 
      
         | 75 |  |  |         signal writeAddr : integer range 0 to MAX_ADDR;
 | 
      
         | 76 |  |  |         signal readAddr : integer range 0 to MAX_ADDR;
 | 
      
         | 77 |  |  |         signal readAddrValid : std_logic;
 | 
      
         | 78 |  |  |         signal currentDataLen : integer range 0 to MAX_DATA_LENGTH;
 | 
      
         | 79 |  |  |  
 | 
      
         | 80 |  |  |         -------- control ----------------------
 | 
      
         | 81 |  |  |         signal ss_r, sr_r, ss_w, sr_w : std_logic;
 | 
      
         | 82 |  |  |  
 | 
      
         | 83 |  |  |  
 | 
      
         | 84 |  |  | begin
 | 
      
         | 85 |  |  |  
 | 
      
         | 86 |  |  |         n_clk <= not clk;
 | 
      
         | 87 |  |  |  
 | 
      
         | 88 |  |  |         p2_strobe <= '0';
 | 
      
         | 89 |  |  |  
 | 
      
         | 90 |  |  |         ready_to_write <= to_stdl((ss_w = sr_w) and currentDataLen < MAX_DATA_LENGTH);
 | 
      
         | 91 |  |  |         ready_to_read <= to_stdl((ss_r = sr_r) and currentDataLen > 0);
 | 
      
         | 92 |  |  |  
 | 
      
         | 93 |  |  |         bytesAvailable <= to_stdl(currentDataLen > 0);
 | 
      
         | 94 |  |  |         availableBytesCount <= conv_std_logic_vector(currentDataLen, ADDR_BITS_COUNT);
 | 
      
         | 95 |  |  |  
 | 
      
         | 96 |  |  |         p1_data_in <= data_in;
 | 
      
         | 97 |  |  |         data_out <= p2_data_out;
 | 
      
         | 98 |  |  |  
 | 
      
         | 99 |  |  |         bufferFull <= to_stdl(currentDataLen = MAX_DATA_LENGTH);
 | 
      
         | 100 |  |  |  
 | 
      
         | 101 |  |  |         p1_addr <= conv_std_logic_vector(writeAddr, ADDR_BITS_COUNT);
 | 
      
         | 102 |  |  |         p2_addr <= conv_std_logic_vector(readAddr, ADDR_BITS_COUNT);
 | 
      
         | 103 |  |  |  
 | 
      
         | 104 |  |  |  
 | 
      
         | 105 |  |  |         process (reset, clk) begin
 | 
      
         | 106 |  |  |                 if reset = '1' then
 | 
      
         | 107 |  |  |                         writeAddr <= 1;
 | 
      
         | 108 |  |  |                         readAddr <= 0;
 | 
      
         | 109 |  |  |                         readAddrValid <= '0';
 | 
      
         | 110 |  |  |  
 | 
      
         | 111 |  |  |                         sr_w <= '0';
 | 
      
         | 112 |  |  |                         sr_r <= '0';
 | 
      
         | 113 |  |  |  
 | 
      
         | 114 |  |  |                         p1_strobe <= '0';
 | 
      
         | 115 |  |  |                 elsif rising_edge(clk) then
 | 
      
         | 116 |  |  |                         if resetFifo = '1' then
 | 
      
         | 117 |  |  |                                 writeAddr <= 1;
 | 
      
         | 118 |  |  |                                 readAddr <= 0;
 | 
      
         | 119 |  |  |                                 readAddrValid <= '0';
 | 
      
         | 120 |  |  |  
 | 
      
         | 121 |  |  |                                 sr_w <= ss_w;
 | 
      
         | 122 |  |  |                                 sr_r <= ss_r;
 | 
      
         | 123 |  |  |  
 | 
      
         | 124 |  |  |                                 p1_strobe <= '0';
 | 
      
         | 125 |  |  |                         else
 | 
      
         | 126 |  |  |                                 if sr_w /= ss_w and currentDataLen < MAX_DATA_LENGTH and
 | 
      
         | 127 |  |  |                                                 p1_strobe = '0' then
 | 
      
         | 128 |  |  |                                         p1_strobe <= '1';
 | 
      
         | 129 |  |  |                                 elsif sr_w /= ss_w and currentDataLen < MAX_DATA_LENGTH and
 | 
      
         | 130 |  |  |                                                 p1_strobe = '1' then
 | 
      
         | 131 |  |  |                                         p1_strobe <= '0';
 | 
      
         | 132 |  |  |                                         sr_w <= ss_w;
 | 
      
         | 133 |  |  |  
 | 
      
         | 134 |  |  |                                         if writeAddr < MAX_ADDR then
 | 
      
         | 135 |  |  |                                                 writeAddr <= writeAddr + 1;
 | 
      
         | 136 |  |  |                                         else
 | 
      
         | 137 |  |  |                                                 writeAddr <= 0;
 | 
      
         | 138 |  |  |                                         end if;
 | 
      
         | 139 |  |  |  
 | 
      
         | 140 |  |  |                                         if readAddrValid = '0' then
 | 
      
         | 141 |  |  |                                                 if readAddr < MAX_ADDR then
 | 
      
         | 142 |  |  |                                                         readAddr <= readAddr + 1;
 | 
      
         | 143 |  |  |                                                 else
 | 
      
         | 144 |  |  |                                                         readAddr <= 0;
 | 
      
         | 145 |  |  |                                                 end if;
 | 
      
         | 146 |  |  |  
 | 
      
         | 147 |  |  |                                                 readAddrValid <= '1';
 | 
      
         | 148 |  |  |                                         end if;
 | 
      
         | 149 |  |  |                                 end if;
 | 
      
         | 150 |  |  |  
 | 
      
         | 151 |  |  |                                 if sr_r /= ss_r and currentDataLen > 0 and
 | 
      
         | 152 |  |  |                                                 readAddrValid = '1' then
 | 
      
         | 153 |  |  |                                         sr_r <= ss_r;
 | 
      
         | 154 |  |  |  
 | 
      
         | 155 |  |  |                                         if currentDataLen = 1 and
 | 
      
         | 156 |  |  |                                                 -- and last writing phase is not ongoing
 | 
      
         | 157 |  |  |                                                 not(sr_w /= ss_w and p1_strobe = '1') then
 | 
      
         | 158 |  |  |                                                         -- if writing is not ongoing
 | 
      
         | 159 |  |  |                                                         readAddrValid <= '0';
 | 
      
         | 160 |  |  |                                         else
 | 
      
         | 161 |  |  |                                                 if readAddr < MAX_ADDR then
 | 
      
         | 162 |  |  |                                                         readAddr <= readAddr + 1;
 | 
      
         | 163 |  |  |                                                 else
 | 
      
         | 164 |  |  |                                                         readAddr <= 0;
 | 
      
         | 165 |  |  |                                                 end if;
 | 
      
         | 166 |  |  |                                         end if;
 | 
      
         | 167 |  |  |                                 end if;
 | 
      
         | 168 |  |  |                         end if;
 | 
      
         | 169 |  |  |                 end if;
 | 
      
         | 170 |  |  |         end process;
 | 
      
         | 171 |  |  |  
 | 
      
         | 172 |  |  |         -- calculate current length
 | 
      
         | 173 |  |  |         process(writeAddr, readAddr, readAddrValid) begin
 | 
      
         | 174 |  |  |                 if readAddrValid = '0' then
 | 
      
         | 175 |  |  |                         currentDataLen <= 0;
 | 
      
         | 176 |  |  |                 elsif readAddr < writeAddr then
 | 
      
         | 177 |  |  |                         currentDataLen <= writeAddr - readAddr;
 | 
      
         | 178 |  |  |                 else -- readAddr > writeAddr, readAddr = writeAddr shoud never happen
 | 
      
         | 179 |  |  |                         currentDataLen <= (MEMORY_CELLS_COUNT - readAddr) + writeAddr;
 | 
      
         | 180 |  |  |                 end if;
 | 
      
         | 181 |  |  |         end process;
 | 
      
         | 182 |  |  |  
 | 
      
         | 183 |  |  |         -- subscribe write
 | 
      
         | 184 |  |  |         process (reset, strobe_write) begin
 | 
      
         | 185 |  |  |                 if reset = '1' then
 | 
      
         | 186 |  |  |                         ss_w <= '0';
 | 
      
         | 187 |  |  |                 elsif rising_edge(strobe_write) then
 | 
      
         | 188 |  |  |                         if ss_w = sr_w then
 | 
      
         | 189 |  |  |                                 ss_w <= not sr_w;
 | 
      
         | 190 |  |  |                         end if;
 | 
      
         | 191 |  |  |                 end if;
 | 
      
         | 192 |  |  |         end process;
 | 
      
         | 193 |  |  |  
 | 
      
         | 194 |  |  |         -- subscribe read
 | 
      
         | 195 |  |  |         process (reset, strobe_read) begin
 | 
      
         | 196 |  |  |                 if reset = '1' then
 | 
      
         | 197 |  |  |                         ss_r <= '0';
 | 
      
         | 198 |  |  |                 elsif rising_edge(strobe_read) then
 | 
      
         | 199 |  |  |                         if ss_r = sr_r then
 | 
      
         | 200 |  |  |                                 ss_r <= not sr_r;
 | 
      
         | 201 |  |  |                         end if;
 | 
      
         | 202 |  |  |                 end if;
 | 
      
         | 203 |  |  |         end process;
 | 
      
         | 204 |  |  |  
 | 
      
         | 205 |  |  |         -- target memory
 | 
      
         | 206 |  |  |         mb: MemoryBlock port map (
 | 
      
         | 207 |  |  |                 reset => reset,
 | 
      
         | 208 |  |  |                 clk => n_clk,
 | 
      
         | 209 |  |  |                 -------------------------------------------------
 | 
      
         | 210 |  |  |                 p1_addr => p1_addr,
 | 
      
         | 211 |  |  |                 p1_data_in => p1_data_in,
 | 
      
         | 212 |  |  |                 p1_strobe => p1_strobe,
 | 
      
         | 213 |  |  |                 p1_data_out => p1_data_out,
 | 
      
         | 214 |  |  |                 -------------------------------------------------
 | 
      
         | 215 |  |  |                 p2_addr => p2_addr,
 | 
      
         | 216 |  |  |                 p2_data_in => p2_data_in,
 | 
      
         | 217 |  |  |                 p2_strobe => p2_strobe,
 | 
      
         | 218 |  |  |                 p2_data_out => p2_data_out
 | 
      
         | 219 |  |  |         );
 | 
      
         | 220 |  |  |  
 | 
      
         | 221 |  |  | end arch;
 | 
      
         | 222 |  |  |  
 |