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_case.vhd] - Blame information for rev 145

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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