| 1 |
-- Copyright (c) 2013 Nuand LLC |
| 2 |
-- |
| 3 |
-- Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
-- of this software and associated documentation files (the "Software"), to deal |
| 5 |
-- in the Software without restriction, including without limitation the rights |
| 6 |
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
-- copies of the Software, and to permit persons to whom the Software is |
| 8 |
-- furnished to do so, subject to the following conditions: |
| 9 |
-- |
| 10 |
-- The above copyright notice and this permission notice shall be included in |
| 11 |
-- all copies or substantial portions of the Software. |
| 12 |
-- |
| 13 |
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 |
-- THE SOFTWARE. |
| 20 |
|
| 21 |
library ieee ; |
| 22 |
use ieee.std_logic_1164.all ; |
| 23 |
use ieee.numeric_std.all ; |
| 24 |
|
| 25 |
entity sync_fifo is |
| 26 |
generic ( |
| 27 |
DEPTH : positive := 32 ; |
| 28 |
WIDTH : positive := 16 ; |
| 29 |
READ_AHEAD : boolean := true |
| 30 |
) ; |
| 31 |
port ( |
| 32 |
areset : in std_logic ; |
| 33 |
clock : in std_logic ; |
| 34 |
|
| 35 |
-- FIFO Status information |
| 36 |
full : out std_logic ; |
| 37 |
empty : out std_logic ; |
| 38 |
used_words : out natural range 0 to DEPTH ; |
| 39 |
|
| 40 |
-- FIFO Input Side |
| 41 |
data_in : in std_logic_vector(WIDTH-1 downto 0) ; |
| 42 |
write_en : in std_logic ; |
| 43 |
|
| 44 |
-- FIFO Output Side |
| 45 |
data_out : out std_logic_vector(WIDTH-1 downto 0) ; |
| 46 |
read_en : in std_logic |
| 47 |
) ; |
| 48 |
end entity ; -- sync_fifo |
| 49 |
|
| 50 |
architecture arch of sync_fifo is |
| 51 |
|
| 52 |
-- House keeping |
| 53 |
signal write_address : natural range 0 to DEPTH-1 ; |
| 54 |
signal read_address : natural range 0 to DEPTH-1 ; |
| 55 |
signal used : natural range 0 to DEPTH ; |
| 56 |
|
| 57 |
-- Dual port RAM |
| 58 |
type ram_t is array(0 to DEPTH-1) of std_logic_vector(WIDTH-1 downto 0) ; |
| 59 |
signal ram : ram_t ; |
| 60 |
|
| 61 |
begin |
| 62 |
|
| 63 |
empty <= '1' when used = 0 else '0' ; |
| 64 |
full <= '1' when used = DEPTH else '0' ; |
| 65 |
used_words <= used ; |
| 66 |
|
| 67 |
follow_used_words : process( clock, areset ) |
| 68 |
begin |
| 69 |
if( areset = '1' ) then |
| 70 |
used <= 0 ; |
| 71 |
write_address <= 0 ; |
| 72 |
read_address <= 0 ; |
| 73 |
elsif( rising_edge(clock) ) then |
| 74 |
if( write_en = '1' ) then |
| 75 |
if( read_en = '0' ) then |
| 76 |
if( used < DEPTH ) then |
| 77 |
write_address <= (write_address + 1) mod DEPTH ; |
| 78 |
used <= used + 1 ; |
| 79 |
else |
| 80 |
report "Trying to write a full FIFO!" severity error ; |
| 81 |
end if ; |
| 82 |
end if ; |
| 83 |
else |
| 84 |
if( read_en = '1' ) then |
| 85 |
if( used > 0 ) then |
| 86 |
read_address <= (read_address + 1) mod DEPTH ; |
| 87 |
used <= used - 1 ; |
| 88 |
else |
| 89 |
report "Trying to read an empty FIFO!" severity error ; |
| 90 |
end if ; |
| 91 |
end if ; |
| 92 |
end if ; |
| 93 |
end if ; |
| 94 |
end process ; |
| 95 |
|
| 96 |
access_ram : process( clock ) |
| 97 |
begin |
| 98 |
if( rising_edge( clock ) ) then |
| 99 |
data_out <= ram(read_address) ; |
| 100 |
if( write_en = '1' ) then |
| 101 |
ram(write_address) <= data_in ; |
| 102 |
end if ; |
| 103 |
end if ; |
| 104 |
end process ; |
| 105 |
|
| 106 |
end architecture ; -- arch |