2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use ieee.std_logic_unsigned.all;
6 --------------------------------------------------------------------------------
7 -- This is a behavioral model of FIFO (Fisrt In First Out) memory.
9 -- All operations (except for reset) are synchronous to 'clk' rising edges.
10 -- Reset makes fifo empty but actually does not change the content of memory.
12 -- The generic parameter 'width' determines the width of address vector used to
13 -- access memory and so the size of memory.
15 -- When overflow occurs, the 'overflow' flag is set to 1 and the least recent
16 -- data is rewritten by new data.
18 -- Underflow is not handled currently and causes misfunction.
19 --------------------------------------------------------------------------------
28 we : in std_logic; -- write enable
29 re : in std_logic; -- read enable
30 clear_ow : in std_logic; -- clear overflow flag
31 d_in : in std_logic_vector (7 downto 0);
32 d_out : out std_logic_vector (7 downto 0);
33 full : out std_logic; -- fifo is full
34 hfull : out std_logic; -- fifo is half full
35 empty : out std_logic; -- fifo is empty
36 overflow : out std_logic := '0'
40 --------------------------------------------------------------------------------
42 architecture behavioral of fifo is
44 subtype mem_addr_t is std_logic_vector (width-1 downto 0);
45 type mem_t is array (3 downto 0) of std_logic_vector (7 downto 0);
47 signal memory : mem_t;
49 signal read_addr : mem_addr_t := (others => '0');
50 signal write_addr : mem_addr_t := (others => '0');
52 signal length : std_logic_vector (width downto 0) := (others => '0');
54 signal full_s : std_logic;
56 --------------------------------------------------------------------------------
60 -- Handling of overflow output signal and internal length signal, storing
61 -- the number of occupied memory positions.
64 if (rising_edge(clk)) then
66 length <= (others => '0');
70 if ((re = '1') and (we = '0')) then
72 elsif ((re = '0') and (we = '1')) then
73 if (full_s = '1') then
80 if (clear_ow = '1') then
88 -- Handling of address registers and writing to memory.
92 read_addr <= (others => '0');
93 write_addr <= (others => '0');
95 elsif (rising_edge(clk)) then
97 read_addr <= read_addr + 1;
101 write_addr <= write_addr + 1;
102 memory (conv_integer(write_addr)) <= d_in;
104 if (full_s = '1') then
105 read_addr <= read_addr + 1;
111 --------------------------------------------------------------------------------
113 d_out <= memory (conv_integer(read_addr));
115 full_s <= '1' when (length >= 2**width) else '0';
118 hfull <= '1' when (length >= 2**(width-1)) else '0';
119 empty <= '1' when (length = 0) else '0';