OpenCores
URL https://opencores.org/ocsvn/uart16750/uart16750/trunk

Subversion Repositories uart16750

[/] [uart16750/] [trunk/] [rtl/] [vhdl/] [slib_fifo.vhd] - Blame information for rev 18

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 hasw
--
2
-- FIFO
3
--
4
-- Author:   Sebastian Witt
5
-- Date:     29.01.2008
6 8 hasw
-- Version:  1.3
7 2 hasw
--
8
-- This code is free software; you can redistribute it and/or
9
-- modify it under the terms of the GNU Lesser General Public
10
-- License as published by the Free Software Foundation; either
11
-- version 2.1 of the License, or (at your option) any later version.
12
--
13
-- This code is distributed in the hope that it will be useful,
14
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
15
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
-- Lesser General Public License for more details.
17
--
18
-- You should have received a copy of the GNU Lesser General Public
19
-- License along with this library; if not, write to the
20
-- Free Software  Foundation, Inc., 59 Temple Place, Suite 330,
21
-- Boston, MA  02111-1307  USA
22
--
23
 
24
LIBRARY IEEE;
25
USE IEEE.std_logic_1164.all;
26
USE IEEE.numeric_std.all;
27
 
28
 
29
entity slib_fifo is
30
    generic (
31
        WIDTH       : integer := 8;                             -- FIFO width
32
        SIZE_E      : integer := 6                              -- FIFO size (2^SIZE_E)
33
    );
34
    port (
35
        CLK         : in std_logic;                             -- Clock
36
        RST         : in std_logic;                             -- Reset
37
        CLEAR       : in std_logic;                             -- Clear FIFO
38
        WRITE       : in std_logic;                             -- Write to FIFO
39
        READ        : in std_logic;                             -- Read from FIFO
40
        D           : in std_logic_vector(WIDTH-1 downto 0);    -- FIFO input
41
        Q           : out std_logic_vector(WIDTH-1 downto 0);   -- FIFO output
42
        EMPTY       : out std_logic;                            -- FIFO is empty
43
        FULL        : out std_logic;                            -- FIFO is full
44
        USAGE       : out std_logic_vector(SIZE_E-1 downto 0)   -- FIFO usage
45
    );
46
end slib_fifo;
47
 
48
architecture rtl of slib_fifo is
49
    -- Signals
50
    signal iEMPTY   : std_logic;                                -- Internal EMPTY
51
    signal iFULL    : std_logic;                                -- Internal FULL
52 10 hasw
    signal iWRAddr  : unsigned(SIZE_E downto 0);                -- FIFO write address
53
    signal iRDAddr  : unsigned(SIZE_E downto 0);                -- FIFO read address
54
    signal iUSAGE   : unsigned(SIZE_E-1 downto 0);              -- FIFO usage
55 2 hasw
    -- FIFO memory
56
    type FIFO_Mem_Type is array (2**SIZE_E-1 downto 0) of std_logic_vector(WIDTH-1 downto 0);
57 8 hasw
    signal iFIFOMem : FIFO_Mem_Type := (others => (others => '0'));
58 2 hasw
 
59
begin
60
    -- Full signal (biggest difference of read and write address)
61
    iFULL <= '1' when (iRDAddr(SIZE_E-1 downto 0) = iWRAddr(SIZE_E-1 downto 0)) and
62
                      (iRDAddr(SIZE_E)           /= iWRAddr(SIZE_E)) else '0';
63
 
64 8 hasw
    -- Write/read address counter and empty signal
65 2 hasw
    FF_ADDR: process (RST, CLK)
66
    begin
67
        if (RST = '1') then
68
            iWRAddr <= (others => '0');
69
            iRDAddr <= (others => '0');
70 8 hasw
            iEMPTY  <= '1';
71 2 hasw
        elsif (CLK'event and CLK='1') then
72
            if (WRITE = '1' and iFULL = '0') then       -- Write to FIFO
73 10 hasw
                iWRAddr <= iWRAddr + 1;
74 2 hasw
            end if;
75
 
76
            if (READ = '1' and iEMPTY = '0') then       -- Read from FIFO
77 10 hasw
                iRDAddr <= iRDAddr + 1;
78 2 hasw
            end if;
79
 
80
            if (CLEAR = '1') then                       -- Reset FIFO
81
                iWRAddr <= (others => '0');
82
                iRDAddr <= (others => '0');
83
            end if;
84 8 hasw
 
85
            if (iRDAddr = iWRAddr) then                 -- Empty signal (read address same as write address)
86
                iEMPTY <= '1';
87
            else
88
                iEMPTY <= '0';
89
            end if;
90 2 hasw
        end if;
91
    end process;
92
 
93
    -- FIFO memory process
94
    FF_MEM: process (RST, CLK)
95
    begin
96
        if (RST = '1') then
97 7 hasw
            --iFIFOMem(2**SIZE_E-1 downto 0) <= (others => (others => '0'));
98 2 hasw
        elsif (CLK'event and CLK = '1') then
99
            if (WRITE = '1' and iFULL = '0') then
100 10 hasw
                iFIFOMem(to_integer(iWRAddr(SIZE_E-1 downto 0))) <= D;
101 2 hasw
            end if;
102 10 hasw
            Q <= iFIFOMem(to_integer(iRDAddr(SIZE_E-1 downto 0)));
103 2 hasw
        end if;
104
    end process;
105
 
106
    -- Usage counter
107
    FF_USAGE: process (RST, CLK)
108
    begin
109
        if (RST = '1') then
110
            iUSAGE <= (others => '0');
111
        elsif (CLK'event and CLK = '1') then
112
            if (CLEAR = '1') then
113
                iUSAGE <= (others => '0');
114
            else
115
                if (READ = '0' and WRITE = '1' and iFULL = '0') then
116 10 hasw
                    iUSAGE <= iUSAGE + 1;
117 2 hasw
                end if;
118
                if (WRITE = '0' and READ = '1' and iEMPTY = '0') then
119 10 hasw
                    iUSAGE <= iUSAGE - 1;
120 2 hasw
                end if;
121
            end if;
122
        end if;
123
    end process;
124
 
125
    -- Output signals
126
    EMPTY <= iEMPTY;
127
    FULL  <= iFULL;
128 10 hasw
    USAGE <= std_logic_vector(iUSAGE);
129 2 hasw
 
130
end rtl;
131
 
132
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.