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

Subversion Repositories neorv32

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

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

powered by: WebSVN 2.1.0

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