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.storage/] [fifos/] [synch_fifos/] [1.0/] [vhd/] [fifo_im.vhd] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
-------------------------------------------------------------------------------
2
-- File        : fifo_im.vhdl
3
-- Description : Fifo buffer for hibi interface
4
-- Author      : Erno Salminen
5
-- Date        : 29.04.2002
6
-- Modified    : 30.04.2002 Vesa Lahtinen Optimized for synthesis
7
--
8
--              02.06 ES: default assignment Fifo_Buffer <= Fifo_Buffer
9
--                      smaller and faster implementation
10
--              Effect on synthesis is uncertain :
11
--              small fifos seem to gets smaller and faster, but big fifos
12
--              get bigger and slower. Strange.
13
-------------------------------------------------------------------------------
14
library ieee;
15
use ieee.std_logic_1164.all;
16
use ieee.std_logic_arith.all;
17
use ieee.std_logic_unsigned.all;
18
 
19
entity fifo is
20
 
21
  generic (
22
    width : integer := 0;
23
    depth : integer := 0);
24
 
25
  port (
26
    Clk            : in  std_logic;
27
    Rst_n          : in  std_logic;
28
    Data_In        : in  std_logic_vector (width-1 downto 0);
29
    Write_Enable   : in  std_logic;
30
    One_Place_Left : out std_logic;
31
    Full           : out std_logic;
32
    Data_Out       : out std_logic_vector (width-1 downto 0);
33
    Read_Enable    : in  std_logic;
34
    Empty          : out std_logic;
35
    One_Data_Left  : out std_logic
36
    );
37
 
38
end fifo;
39
 
40
architecture in_mux of fifo is
41
 
42
  type data_array is array (depth-1 downto 0) of std_logic_vector (width-1 downto 0);
43
  signal Fifo_Buffer : data_array;
44
 
45
  -- Registers
46
  signal Full_reg           : std_logic;
47
  signal Empty_reg          : std_logic;
48
  signal One_Data_Left_reg  : std_logic;
49
  signal One_Place_Left_reg : std_logic;
50
  --signal Data_Amount        : std_logic_vector (depth-1 downto 0);
51
  signal Data_Amount        : integer range 0 to depth-1;
52
 
53
 
54
begin  -- in_mux
55
 
56
  -- Continuous assignments
57
  -- Assigns register values to outputs
58
  Full           <= Full_reg;
59
  Empty          <= Empty_reg;
60
  One_Data_Left  <= One_Data_Left_reg;
61
  One_Place_Left <= One_Place_Left_reg;
62
  Data_Out       <= Fifo_Buffer (0);
63
  -- Note! There is some old value in data output when fifo is empty.
64
 
65
 
66
Main : process (Clk, Rst_n)
67
begin  -- process Main
68
  if Rst_n = '0' then                   -- asynchronous reset (active low)
69
 
70
    -- Reset all registers
71
    -- Fifo is empty at first
72
    Full_reg           <= '0';
73
    Empty_reg          <= '1';
74
    One_Data_Left_reg  <= '0';
75
    Data_Amount        <= 0;
76
 
77
    if depth =1 then                    -- 30.07
78
      One_Place_Left_reg <= '1';
79
    else
80
      One_Place_Left_reg <= '0';
81
    end if;
82
 
83
    for i in 0 to depth-1 loop
84
      Fifo_Buffer (i)  <= (others => '0');
85
    end loop;  -- i
86
 
87
  elsif Clk'event and Clk = '1' then    -- rising clock edge
88
 
89
 
90
    -- 1) Write data to fifo
91
    if Write_Enable = '1' and Read_Enable = '0' then
92
 
93
      if Full_reg = '0' then
94
        Empty_reg                 <= '0';
95
        Data_Amount               <= Data_Amount +1;
96
 
97
        Fifo_Buffer               <= Fifo_Buffer;  --02.06
98
        Fifo_Buffer (Data_Amount) <= Data_In;
99
 
100
        -- Check if the fifo is getting full
101
        if Data_Amount + 2 = depth then
102
          Full_reg           <= '0';
103
          One_Place_Left_reg <= '1';
104
        elsif Data_Amount +1 = depth then
105
          Full_reg           <= '1';
106
          One_Place_Left_reg <= '0';
107
        else
108
          Full_reg           <= '0';
109
          One_Place_Left_reg <= '0';
110
        end if;
111
 
112
 
113
        -- If fifo was empty, it has now one data 
114
        if Empty_reg = '1' then
115
          One_Data_Left_reg <= '1';
116
        else
117
          One_Data_Left_reg <= '0';
118
        end if;
119
 
120
      else
121
        Full_reg           <= Full_reg;
122
        Empty_reg          <= Empty_reg;
123
        Fifo_Buffer        <= Fifo_Buffer;
124
        Data_Amount        <= Data_Amount;
125
        One_Data_Left_reg  <= One_Data_Left_reg;
126
        One_Place_Left_reg <= One_Place_Left_reg;
127
      end if;
128
 
129
 
130
    -- 2) Read data from fifo  
131
    elsif Write_Enable = '0' and Read_Enable = '1' then
132
 
133
      if Empty_reg = '0' then
134
 
135
        -- Shiftaus
136
        Fifo_Buffer               <= Fifo_Buffer;  --02.06
137
        for i in 0 to depth-2 loop
138
          Fifo_Buffer (i) <= Fifo_Buffer (i+1);
139
        end loop;  -- i
140
 
141
        Full_reg    <= '0';
142
        Data_Amount <= Data_Amount -1;
143
 
144
        -- Debug
145
        -- Fifo_Buffer (out_ptr) <= (others => '1');
146
 
147
 
148
 
149
 
150
        -- Check if the fifo is getting empty
151
        if Data_Amount = 2 then
152
          Empty_reg         <= '0';
153
          One_data_Left_reg <= '1';
154
        elsif Data_Amount = 1 then
155
          Empty_reg         <= '1';
156
          One_Data_Left_reg <= '0';
157
        else
158
          Empty_reg         <= '0';
159
          One_Data_Left_reg <= '0';
160
        end if;
161
 
162
        -- If fifo was full, it is no more 
163
        if Full_reg = '1' then
164
          One_Place_Left_reg <= '1';
165
        else
166
          One_Place_Left_reg <= '0';
167
        end if;
168
 
169
      else
170
        Full_reg           <= Full_reg;
171
        Empty_reg          <= Empty_reg;
172
        Fifo_Buffer        <= Fifo_Buffer;
173
        Data_Amount        <= Data_Amount;
174
        One_Data_Left_reg  <= One_Data_Left_reg;
175
        One_Place_Left_reg <= One_Place_Left_reg;
176
      end if;
177
 
178
 
179
    -- 3) Write and read at the same time  
180
    elsif Write_Enable = '1' and Read_Enable = '1' then
181
 
182
 
183
      if Full_reg = '0' and Empty_reg = '0' then
184
        Full_reg           <= '0';
185
        Empty_reg          <= '0';
186
        Data_Amount        <= Data_Amount;
187
        One_Data_Left_reg  <= One_Data_Left_reg;
188
        One_Place_Left_reg <= One_Place_Left_reg;
189
 
190
        Fifo_Buffer               <= Fifo_Buffer;  --02.06
191
        -- Shiftaus
192
        for i in 0 to depth-2 loop
193
          Fifo_Buffer (i) <= Fifo_Buffer (i+1);
194
        end loop;  -- i
195
        Fifo_Buffer (Data_Amount-1)  <= Data_In;  --vai amount-1 ?? koska pitaa
196
                                                --shiftata samalla
197
 
198
        -- Fifo_Buffer (out_ptr) <= (others => '1');  --debug
199
 
200
 
201
      elsif Full_reg = '1' and Empty_reg = '0' then
202
        -- Fifo is full, only reading is possible
203
 
204
        Full_reg              <= '0';
205
        One_Place_Left_reg    <= '1';
206
        --Fifo_Buffer (out_ptr) <= (others => '1');  -- Debug
207
        Data_Amount           <= Data_Amount -1;
208
 
209
        Fifo_Buffer               <= Fifo_Buffer;  --02.06
210
        -- Shiftaus
211
        for i in 0 to depth-2 loop
212
          Fifo_Buffer (i) <= Fifo_Buffer (i+1);
213
        end loop;  -- i
214
 
215
        -- Check if the fifo is getting empty
216
        if Data_Amount = 2 then
217
          Empty_reg         <= '0';
218
          One_data_Left_reg <= '1';
219
        elsif Data_Amount = 1 then
220
          Empty_reg         <= '1';
221
          One_Data_Left_reg <= '0';
222
        else
223
          Empty_reg         <= '0';
224
          One_Data_Left_reg <= '0';
225
        end if;
226
 
227
 
228
      elsif Full_reg = '0' and Empty_reg = '1' then
229
        -- Fifo is empty, only writing is possible
230
        Empty_reg                 <= '0';
231
        One_Data_Left_reg         <= '1';
232
        Fifo_Buffer               <= Fifo_Buffer;  --02.06
233
        Fifo_Buffer (Data_Amount) <= Data_In;  --Data_Amount =0
234
        Data_Amount               <= Data_Amount +1;
235
 
236
        -- Check if the fifo is getting full
237
        if Data_Amount + 2 = depth then
238
          Full_reg           <= '0';
239
          One_Place_Left_reg <= '1';
240
        elsif Data_Amount +1 = depth then
241
          Full_reg           <= '1';
242
          One_Place_Left_reg <= '0';
243
        else
244
          Full_reg           <= '0';
245
          One_Place_Left_reg <= '0';
246
        end if;
247
 
248
 
249
      else
250
        -- Fifo is full and empty at the same time => impossible
251
        Full_reg           <= Full_reg;
252
        Empty_reg          <= Empty_reg;
253
        Fifo_Buffer        <= Fifo_Buffer;
254
        Data_Amount        <= Data_Amount;
255
        One_Data_Left_reg  <= One_Data_Left_reg;
256
        One_Place_Left_reg <= One_Place_Left_reg;
257
      end if;                           --Full_reg & Empty_reg
258
 
259
 
260
    -- 4) Do nothing, fifo remains idle 
261
    else
262
 
263
      Full_reg           <= Full_reg;
264
      Empty_reg          <= Empty_reg;
265
      Fifo_Buffer        <= Fifo_Buffer;
266
      Data_Amount        <= Data_Amount;
267
      One_Data_Left_reg  <= One_Data_Left_reg;
268
      One_Place_Left_reg <= One_Place_Left_reg;
269
    end if;
270
 
271
  end if;
272
end process Main;
273
 
274
end in_mux;

powered by: WebSVN 2.1.0

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