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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_fifo.vhd] - Blame information for rev 65

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

Line No. Rev Author Line
1 61 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - General Purpose FIFO Component >>                                                #
3
-- # ********************************************************************************************* #
4
-- # BSD 3-Clause License                                                                          #
5
-- #                                                                                               #
6
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
7
-- #                                                                                               #
8
-- # Redistribution and use in source and binary forms, with or without modification, are          #
9
-- # permitted provided that the following conditions are met:                                     #
10
-- #                                                                                               #
11
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
-- #    conditions and the following disclaimer.                                                   #
13
-- #                                                                                               #
14
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
16
-- #    provided with the distribution.                                                            #
17
-- #                                                                                               #
18
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
-- #    endorse or promote products derived from this software without specific prior written      #
20
-- #    permission.                                                                                #
21
-- #                                                                                               #
22
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
-- # ********************************************************************************************* #
32
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
-- #################################################################################################
34
 
35
library ieee;
36
use ieee.std_logic_1164.all;
37
use ieee.numeric_std.all;
38
 
39
library neorv32;
40
use neorv32.neorv32_package.all;
41
 
42
entity neorv32_fifo is
43
  generic (
44 62 zero_gravi
    FIFO_DEPTH : natural; -- number of fifo entries; has to be a power of two; min 1
45
    FIFO_WIDTH : natural; -- size of data elements in fifo
46
    FIFO_RSYNC : boolean; -- false = async read; true = sync read
47
    FIFO_SAFE  : boolean  -- true = allow read/write only if entry available
48 61 zero_gravi
  );
49
  port (
50
    -- control --
51
    clk_i   : in  std_ulogic; -- clock, rising edge
52
    rstn_i  : in  std_ulogic; -- async reset, low-active
53
    clear_i : in  std_ulogic; -- sync reset, high-active
54 62 zero_gravi
    level_o : out std_ulogic_vector(index_size_f(FIFO_DEPTH) downto 0); -- fill level
55 65 zero_gravi
    half_o  : out std_ulogic; -- FIFO is at least half full
56 61 zero_gravi
    -- write port --
57
    wdata_i : in  std_ulogic_vector(FIFO_WIDTH-1 downto 0); -- write data
58
    we_i    : in  std_ulogic; -- write enable
59
    free_o  : out std_ulogic; -- at least one entry is free when set
60
    -- read port --
61
    re_i    : in  std_ulogic; -- read enable
62
    rdata_o : out std_ulogic_vector(FIFO_WIDTH-1 downto 0); -- read data
63
    avail_o : out std_ulogic  -- data available when set
64
  );
65
end neorv32_fifo;
66
 
67
architecture neorv32_fifo_rtl of neorv32_fifo is
68
 
69
  -- FIFO --
70
  type fifo_data_t is array (0 to FIFO_DEPTH-1) of std_ulogic_vector(FIFO_WIDTH-1 downto 0);
71
  type fifo_t is record
72
    we    : std_ulogic; -- write enable
73
    re    : std_ulogic; -- read enable
74
    w_pnt : std_ulogic_vector(index_size_f(FIFO_DEPTH) downto 0); -- write pointer
75
    r_pnt : std_ulogic_vector(index_size_f(FIFO_DEPTH) downto 0); -- read pointer
76 62 zero_gravi
    level : std_ulogic_vector(index_size_f(FIFO_DEPTH) downto 0); -- fill count
77 61 zero_gravi
    data  : fifo_data_t; -- fifo memory
78 62 zero_gravi
    datas : std_ulogic_vector(FIFO_WIDTH-1 downto 0);
79 61 zero_gravi
    match : std_ulogic;
80
    empty : std_ulogic;
81
    full  : std_ulogic;
82
    free  : std_ulogic;
83
    avail : std_ulogic;
84
  end record;
85
  signal fifo : fifo_t;
86
 
87 65 zero_gravi
  signal level_diff : std_ulogic_vector(index_size_f(FIFO_DEPTH) downto 0);
88
 
89 61 zero_gravi
begin
90
 
91
  -- Sanity Checks --------------------------------------------------------------------------
92
  -- -------------------------------------------------------------------------------------------
93
  assert not (FIFO_DEPTH = 0) report "NEORV32 CONFIG ERROR: FIFO depth has to be > 0." severity error;
94
  assert not (is_power_of_two_f(FIFO_DEPTH) = false) report "NEORV32 CONFIG ERROR: FIFO depth has to be a power of two." severity error;
95
 
96
 
97
  -- Access Control -------------------------------------------------------------------------
98
  -- -------------------------------------------------------------------------------------------
99 65 zero_gravi
  fifo.re <= re_i when (FIFO_SAFE = false) else (re_i and fifo.avail); -- read only if data available
100
  fifo.we <= we_i when (FIFO_SAFE = false) else (we_i and fifo.free); -- write only if space left
101 61 zero_gravi
 
102
 
103
  -- FIFO Control ---------------------------------------------------------------------------
104
  -- -------------------------------------------------------------------------------------------
105
  fifo_control: process(rstn_i, clk_i)
106
  begin
107
    if (rstn_i = '0') then
108
      fifo.w_pnt <= (others => '0');
109
      fifo.r_pnt <= (others => '0');
110
    elsif rising_edge(clk_i) then
111
      -- write port --
112
      if (clear_i = '1') then
113
        fifo.w_pnt <= (others => '0');
114
      elsif (fifo.we = '1') then
115
        fifo.w_pnt <= std_ulogic_vector(unsigned(fifo.w_pnt) + 1);
116
      end if;
117
      -- read port --
118
      if (clear_i = '1') then
119
        fifo.r_pnt <= (others => '0');
120
      elsif (fifo.re = '1') then
121
        fifo.r_pnt <= std_ulogic_vector(unsigned(fifo.r_pnt) + 1);
122
      end if;
123
    end if;
124
  end process fifo_control;
125
 
126
  -- status --
127 62 zero_gravi
  fifo.match <= '1' when (fifo.r_pnt(fifo.r_pnt'left-1 downto 0) = fifo.w_pnt(fifo.w_pnt'left-1 downto 0)) or (FIFO_DEPTH = 1) else '0';
128 61 zero_gravi
  fifo.full  <= '1' when (fifo.r_pnt(fifo.r_pnt'left) /= fifo.w_pnt(fifo.w_pnt'left)) and (fifo.match = '1') else '0';
129
  fifo.empty <= '1' when (fifo.r_pnt(fifo.r_pnt'left)  = fifo.w_pnt(fifo.w_pnt'left)) and (fifo.match = '1') else '0';
130
  fifo.free  <= not fifo.full;
131
  fifo.avail <= not fifo.empty;
132 65 zero_gravi
  level_diff <= std_ulogic_vector(unsigned(fifo.w_pnt) - unsigned(fifo.r_pnt));
133
  fifo.level <= std_ulogic_vector(to_unsigned(FIFO_DEPTH, fifo.level'length)) when (fifo.full = '1') else level_diff;
134 61 zero_gravi
 
135
  -- status output --
136 62 zero_gravi
  level_o <= fifo.level;
137 61 zero_gravi
  free_o  <= fifo.free;
138
  avail_o <= fifo.avail;
139
 
140 65 zero_gravi
  fifo_half_level:
141
  if (FIFO_DEPTH > 1) generate
142
    half_o <= level_diff(level_diff'left-1) or fifo.full;
143
  end generate;
144 61 zero_gravi
 
145 65 zero_gravi
  fifo_half_level_simple:
146
  if (FIFO_DEPTH = 1) generate
147
    half_o <= fifo.full;
148
  end generate;
149
 
150
 
151 61 zero_gravi
  -- FIFO Memory ----------------------------------------------------------------------------
152
  -- -------------------------------------------------------------------------------------------
153
  fifo_memory_write: process(clk_i)
154
  begin
155
    if rising_edge(clk_i) then
156
      if (fifo.we = '1') then
157 62 zero_gravi
        if (FIFO_DEPTH = 1) then
158
          fifo.datas <= wdata_i;
159
        else
160
          fifo.data(to_integer(unsigned(fifo.w_pnt(fifo.w_pnt'left-1 downto 0)))) <= wdata_i;
161
        end if;
162 61 zero_gravi
      end if;
163
    end if;
164
  end process fifo_memory_write;
165
 
166
  -- asynchronous read --
167
  fifo_read_async:
168
  if (FIFO_RSYNC = false) generate
169 62 zero_gravi
    rdata_o <= fifo.datas when (FIFO_DEPTH = 1) else fifo.data(to_integer(unsigned(fifo.r_pnt(fifo.r_pnt'left-1 downto 0))));
170 61 zero_gravi
  end generate;
171
 
172
  -- synchronous read --
173
  fifo_read_sync:
174
  if (FIFO_RSYNC = true) generate
175
    fifo_memory_read: process(clk_i)
176
    begin
177
      if rising_edge(clk_i) then
178 65 zero_gravi
        if (FIFO_DEPTH = 1) then
179
          rdata_o <= fifo.datas;
180
        else
181
          rdata_o <= fifo.data(to_integer(unsigned(fifo.r_pnt(fifo.r_pnt'left-1 downto 0))));
182 61 zero_gravi
        end if;
183
      end if;
184
    end process fifo_memory_read;
185
  end generate;
186
 
187
 
188
end neorv32_fifo_rtl;

powered by: WebSVN 2.1.0

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