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

Subversion Repositories neorv32

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

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 61 zero_gravi
    -- write port --
56
    wdata_i : in  std_ulogic_vector(FIFO_WIDTH-1 downto 0); -- write data
57
    we_i    : in  std_ulogic; -- write enable
58
    free_o  : out std_ulogic; -- at least one entry is free when set
59
    -- read port --
60
    re_i    : in  std_ulogic; -- read enable
61
    rdata_o : out std_ulogic_vector(FIFO_WIDTH-1 downto 0); -- read data
62
    avail_o : out std_ulogic  -- data available when set
63
  );
64
end neorv32_fifo;
65
 
66
architecture neorv32_fifo_rtl of neorv32_fifo is
67
 
68
  -- FIFO --
69
  type fifo_data_t is array (0 to FIFO_DEPTH-1) of std_ulogic_vector(FIFO_WIDTH-1 downto 0);
70
  type fifo_t is record
71
    we    : std_ulogic; -- write enable
72
    re    : std_ulogic; -- read enable
73
    w_pnt : std_ulogic_vector(index_size_f(FIFO_DEPTH) downto 0); -- write pointer
74
    r_pnt : std_ulogic_vector(index_size_f(FIFO_DEPTH) downto 0); -- read pointer
75 62 zero_gravi
    level : std_ulogic_vector(index_size_f(FIFO_DEPTH) downto 0); -- fill count
76 61 zero_gravi
    data  : fifo_data_t; -- fifo memory
77 62 zero_gravi
    datas : std_ulogic_vector(FIFO_WIDTH-1 downto 0);
78 61 zero_gravi
    match : std_ulogic;
79
    empty : std_ulogic;
80
    full  : std_ulogic;
81
    free  : std_ulogic;
82
    avail : std_ulogic;
83
  end record;
84
  signal fifo : fifo_t;
85
 
86
begin
87
 
88
  -- Sanity Checks --------------------------------------------------------------------------
89
  -- -------------------------------------------------------------------------------------------
90
  assert not (FIFO_DEPTH = 0) report "NEORV32 CONFIG ERROR: FIFO depth has to be > 0." severity error;
91
  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;
92
 
93
 
94
  -- Access Control -------------------------------------------------------------------------
95
  -- -------------------------------------------------------------------------------------------
96
  fifo.re <= re_i when (FIFO_SAFE = false) else (re_i and fifo.avail);
97
  fifo.we <= we_i when (FIFO_SAFE = false) else (we_i and fifo.free);
98
 
99
 
100
  -- FIFO Control ---------------------------------------------------------------------------
101
  -- -------------------------------------------------------------------------------------------
102
  fifo_control: process(rstn_i, clk_i)
103
  begin
104
    if (rstn_i = '0') then
105
      fifo.w_pnt <= (others => '0');
106
      fifo.r_pnt <= (others => '0');
107
    elsif rising_edge(clk_i) then
108
      -- write port --
109
      if (clear_i = '1') then
110
        fifo.w_pnt <= (others => '0');
111
      elsif (fifo.we = '1') then
112
        fifo.w_pnt <= std_ulogic_vector(unsigned(fifo.w_pnt) + 1);
113
      end if;
114
      -- read port --
115
      if (clear_i = '1') then
116
        fifo.r_pnt <= (others => '0');
117
      elsif (fifo.re = '1') then
118
        fifo.r_pnt <= std_ulogic_vector(unsigned(fifo.r_pnt) + 1);
119
      end if;
120
    end if;
121
  end process fifo_control;
122
 
123
  -- status --
124 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';
125 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';
126
  fifo.empty <= '1' when (fifo.r_pnt(fifo.r_pnt'left)  = fifo.w_pnt(fifo.w_pnt'left)) and (fifo.match = '1') else '0';
127
  fifo.free  <= not fifo.full;
128
  fifo.avail <= not fifo.empty;
129 62 zero_gravi
  fifo.level <= std_ulogic_vector(to_unsigned(FIFO_DEPTH, fifo.level'length)) when (fifo.full = '1') else std_ulogic_vector(unsigned(fifo.w_pnt) - unsigned(fifo.r_pnt));
130 61 zero_gravi
 
131
  -- status output --
132 62 zero_gravi
  level_o <= fifo.level;
133 61 zero_gravi
  free_o  <= fifo.free;
134
  avail_o <= fifo.avail;
135
 
136
 
137
  -- FIFO Memory ----------------------------------------------------------------------------
138
  -- -------------------------------------------------------------------------------------------
139
  fifo_memory_write: process(clk_i)
140
  begin
141
    if rising_edge(clk_i) then
142
      if (fifo.we = '1') then
143 62 zero_gravi
        if (FIFO_DEPTH = 1) then
144
          fifo.datas <= wdata_i;
145
        else
146
          fifo.data(to_integer(unsigned(fifo.w_pnt(fifo.w_pnt'left-1 downto 0)))) <= wdata_i;
147
        end if;
148 61 zero_gravi
      end if;
149
    end if;
150
  end process fifo_memory_write;
151
 
152
  -- asynchronous read --
153
  fifo_read_async:
154
  if (FIFO_RSYNC = false) generate
155 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))));
156 61 zero_gravi
  end generate;
157
 
158
  -- synchronous read --
159
  fifo_read_sync:
160
  if (FIFO_RSYNC = true) generate
161
    fifo_memory_read: process(clk_i)
162
    begin
163
      if rising_edge(clk_i) then
164
        if (fifo.re = '1') then
165 62 zero_gravi
          if (FIFO_DEPTH = 1) then
166
            rdata_o <= fifo.datas;
167
          else
168
            rdata_o <= fifo.data(to_integer(unsigned(fifo.r_pnt(fifo.r_pnt'left-1 downto 0))));
169
          end if;
170 61 zero_gravi
        end if;
171
      end if;
172
    end process fifo_memory_read;
173
  end generate;
174
 
175
 
176
end neorv32_fifo_rtl;

powered by: WebSVN 2.1.0

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