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

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.hwp.communication/] [hibi/] [2.0/] [vhd/] [fifo.vhd] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
-------------------------------------------------------------------------------
2
-- Funbase IP library Copyright (C) 2011 TUT Department of Computer Systems
3
--
4
-- This source file may be used and distributed without
5
-- restriction provided that this copyright statement is not
6
-- removed from the file and that any derivative work contains
7
-- the original copyright notice and the associated disclaimer.
8
--
9
-- This source file is free software; you can redistribute it
10
-- and/or modify it under the terms of the GNU Lesser General
11
-- Public License as published by the Free Software Foundation;
12
-- either version 2.1 of the License, or (at your option) any
13
-- later version.
14
--
15
-- This source is distributed in the hope that it will be
16
-- useful, but WITHOUT ANY WARRANTY; without even the implied
17
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18
-- PURPOSE.  See the GNU Lesser General Public License for more
19
-- details.
20
--
21
-- You should have received a copy of the GNU Lesser General
22
-- Public License along with this source; if not, download it
23
-- from http://www.opencores.org/lgpl.shtml
24
-------------------------------------------------------------------------------
25
-------------------------------------------------------------------------------
26
-- File        : fifo.vhdl
27
-- Description : Fifo buffer for hibi interface
28
-- Author      : Erno Salminen
29
-- e-mail      : erno.salminen@tut.fi
30
-- Project     : mikälie
31
-- Design      : Do not use term design when you mean system
32
-- Date        : 29.04.2002
33
-- Modified    : 30.04.2002 Vesa Lahtinen Optimized for synthesis
34
--
35
-- 15.12.04     ES: names changed
36
-------------------------------------------------------------------------------
37
library ieee;
38
use ieee.std_logic_1164.all;
39
use ieee.std_logic_arith.all;
40
use ieee.std_logic_unsigned.all;
41
 
42
entity fifo is
43
 
44
  generic (
45
    data_width_g :    integer := 32;
46
    depth_g      :    integer := 5
47
    );
48
  port (
49
    clk          : in std_logic;
50
    rst_n        : in std_logic;
51
 
52
    data_in   : in  std_logic_vector (data_width_g-1 downto 0);
53
    we_in     : in  std_logic;
54
    full_out  : out std_logic;
55
    one_p_out : out std_logic;
56
 
57
    re_in     : in  std_logic;
58
    data_out  : out std_logic_vector (data_width_g-1 downto 0);
59
    empty_out : out std_logic;
60
    one_d_out : out std_logic
61
    );
62
 
63
end fifo;
64
 
65
architecture behavioral of fifo is
66
 
67
 
68
  -- Registers
69
  signal full_r        : std_logic;
70
  signal empty_r       : std_logic;
71
  signal one_d_r       : std_logic;
72
  signal one_p_r       : std_logic;
73
  --signal data_amount_r : std_logic_vector (depth_g-1 downto 0);
74
  signal data_amount_r : std_logic_vector (16-1 downto 0);
75
 
76
  signal in_ptr_r  : integer range 0 to depth_g-1;
77
  signal out_ptr_r : integer range 0 to depth_g-1;
78
 
79
  type data_arr_type is array (depth_g-1 downto 0) of std_logic_vector (data_width_g-1 downto 0);
80
  signal fifo_buffer_r : data_arr_type;
81
 
82
 
83
begin  -- behavioral
84
 
85
  -- Continuous assignments
86
  -- Assigns register values to outputs
87
  full_out  <= full_r;
88
  empty_out <= empty_r;
89
  one_d_out <= one_d_r;
90
  one_p_out <= one_p_r;
91
  data_out  <= fifo_buffer_r (out_ptr_r);   -- mux at output!
92
  -- Note! There is some old value in data output when fifo is empty.
93
 
94
 
95
Main : process (clk, rst_n)
96
begin  -- process Main
97
  if rst_n = '0' then                   -- asynchronous reset (active low)
98
 
99
    -- Reset all registers
100
    -- Fifo is empty at first
101
    full_r        <= '0';
102
    empty_r       <= '1';
103
    one_d_r       <= '0';
104
    in_ptr_r      <= 0;
105
    out_ptr_r     <= 0;
106
    data_amount_r <= (others => '0');
107
 
108
    if depth_g =1 then                    -- 30.07
109
      one_p_r <= '1';
110
    else
111
      one_p_r <= '0';
112
    end if;
113
 
114
    for i in 0 to depth_g-1 loop
115
      fifo_buffer_r (i) <= (others => '0');
116
    end loop;  -- i
117
 
118
  elsif clk'event and clk = '1' then    -- rising clock edge
119
 
120
 
121
    -- 1) Write data to fifo
122
    if we_in = '1' and re_in = '0' then
123
 
124
      if full_r = '0' then
125
        empty_r                <= '0';
126
        if (in_ptr_r = (depth_g-1)) then
127
          in_ptr_r               <= 0;
128
        else
129
          in_ptr_r               <= in_ptr_r + 1;
130
        end if;
131
        out_ptr_r                <= out_ptr_r;
132
        data_amount_r          <= data_amount_r +1;
133
        fifo_buffer_r (in_ptr_r) <= data_in;
134
 
135
        -- Check if the fifo is getting full
136
        if data_amount_r + 2 = depth_g then
137
          full_r  <= '0';
138
          one_p_r <= '1';
139
        elsif data_amount_r +1 = depth_g then
140
          full_r  <= '1';
141
          one_p_r <= '0';
142
        else
143
          full_r  <= '0';
144
          one_p_r <= '0';
145
        end if;
146
 
147
        -- If fifo was empty, it has now one data 
148
        if empty_r = '1' then
149
          one_d_r <= '1';
150
        else
151
          one_d_r <= '0';
152
        end if;
153
 
154
      else
155
--         in_ptr_r      <= in_ptr_r;
156
--         out_ptr_r     <= out_ptr_r;
157
--         full_r        <= full_r;
158
--         empty_r       <= empty_r;
159
--         fifo_buffer_r <= fifo_buffer_r;
160
--         data_amount_r <= data_amount_r;
161
--         one_d_r       <= one_d_r;
162
--         one_p_r       <= one_p_r;
163
      end if;
164
 
165
 
166
    -- 2) Read data from fifo  
167
    elsif we_in = '0' and re_in = '1' then
168
 
169
      if empty_r = '0' then
170
        in_ptr_r        <= in_ptr_r;
171
        if (out_ptr_r = (depth_g-1)) then
172
          out_ptr_r     <= 0;
173
        else
174
          out_ptr_r     <= out_ptr_r + 1;
175
        end if;
176
        full_r        <= '0';
177
        data_amount_r <= data_amount_r -1;
178
 
179
        -- Debug
180
        -- fifo_buffer_r (out_ptr_r) <= (others => '1');
181
 
182
        -- Check if the fifo is getting empty
183
        if data_amount_r = 2 then
184
          empty_r <= '0';
185
          one_d_r <= '1';
186
        elsif data_amount_r = 1 then
187
          empty_r <= '1';
188
          one_d_r <= '0';
189
        else
190
          empty_r <= '0';
191
          one_d_r <= '0';
192
        end if;
193
 
194
        -- If fifo was full, it is no more 
195
        if full_r = '1' then
196
          one_p_r <= '1';
197
        else
198
          one_p_r <= '0';
199
        end if;
200
 
201
      else
202
--         in_ptr_r      <= in_ptr_r;
203
--         out_ptr_r     <= out_ptr_r;
204
--         full_r        <= full_r;
205
--         empty_r       <= empty_r;
206
--         fifo_buffer_r <= fifo_buffer_r;
207
--         data_amount_r <= data_amount_r;
208
--         one_d_r       <= one_d_r;
209
--         one_p_r       <= one_p_r;
210
      end if;
211
 
212
 
213
    -- 3) Write and read at the same time  
214
    elsif we_in = '1' and re_in = '1' then
215
 
216
 
217
      if full_r = '0' and empty_r = '0' then
218
        if (in_ptr_r = (depth_g-1)) then
219
          in_ptr_r    <= 0;
220
        else
221
          in_ptr_r    <= in_ptr_r + 1;
222
        end if;
223
        if (out_ptr_r = (depth_g-1)) then
224
          out_ptr_r   <= 0;
225
        else
226
          out_ptr_r   <= out_ptr_r + 1;
227
        end if;
228
        full_r        <= '0';
229
        empty_r       <= '0';
230
        data_amount_r <= data_amount_r;
231
        one_d_r       <= one_d_r;
232
        one_p_r       <= one_p_r;
233
 
234
        fifo_buffer_r (in_ptr_r)  <= data_in;
235
        -- fifo_buffer_r (out_ptr_r) <= (others => '1');  --debug
236
 
237
 
238
      elsif full_r = '1' and empty_r = '0' then
239
        -- Fifo is full, only reading is possible
240
        in_ptr_r        <= in_ptr_r;
241
        if (out_ptr_r = (depth_g-1)) then
242
          out_ptr_r     <= 0;
243
        else
244
          out_ptr_r     <= out_ptr_r + 1;
245
        end if;
246
        full_r        <= '0';
247
        one_p_r       <= '1';
248
        --fifo_buffer_r (out_ptr_r) <= (others => '1');  -- Debug
249
        data_amount_r <= data_amount_r -1;
250
 
251
        -- Check if the fifo is getting empty
252
        if data_amount_r = 2 then
253
          empty_r <= '0';
254
          one_d_r <= '1';
255
        elsif data_amount_r = 1 then
256
          empty_r <= '1';
257
          one_d_r <= '0';
258
        else
259
          empty_r <= '0';
260
          one_d_r <= '0';
261
        end if;
262
 
263
 
264
      elsif full_r = '0' and empty_r = '1' then
265
        -- Fifo is empty, only writing is possible
266
        if (in_ptr_r = (depth_g-1)) then
267
          in_ptr_r               <= 0;
268
        else
269
          in_ptr_r               <= in_ptr_r + 1;
270
        end if;
271
        out_ptr_r                <= out_ptr_r;
272
        empty_r                  <= '0';
273
        one_d_r                  <= '1';
274
        fifo_buffer_r (in_ptr_r) <= data_in;
275
        data_amount_r            <= data_amount_r +1;
276
 
277
        -- Check if the fifo is getting full
278
        if data_amount_r + 2 = depth_g then
279
          full_r  <= '0';
280
          one_p_r <= '1';
281
        elsif data_amount_r +1 = depth_g then
282
          full_r  <= '1';
283
          one_p_r <= '0';
284
        else
285
          full_r  <= '0';
286
          one_p_r <= '0';
287
        end if;
288
 
289
 
290
      -- 4) Do nothing, fifo remains idle 
291
      else
292
 
293
--         in_ptr_r      <= in_ptr_r;
294
--         out_ptr_r     <= out_ptr_r;
295
--         full_r        <= full_r;
296
--         empty_r       <= empty_r;
297
--         fifo_buffer_r <= fifo_buffer_r;
298
--         data_amount_r <= data_amount_r;
299
--         one_d_r       <= one_d_r;
300
--         one_p_r       <= one_p_r;
301
      end if;
302
 
303
    else
304
      -- Fifo is idle
305
--       in_ptr_r      <= in_ptr_r;
306
--       out_ptr_r     <= out_ptr_r;
307
--       full_r        <= full_r;
308
--       empty_r       <= empty_r;
309
--       fifo_buffer_r <= fifo_buffer_r;
310
--       data_amount_r <= data_amount_r;
311
--       one_d_r       <= one_d_r;
312
--       one_p_r       <= one_p_r;
313
    end if;
314
 
315
  end if;
316
end process Main;
317
 
318
end behavioral;

powered by: WebSVN 2.1.0

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