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

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [examples/] [memfifo/] [fpga-2.04b/] [ipcore_dir/] [mem0/] [user_design/] [sim/] [afifo.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
//*****************************************************************************
2
// (c) Copyright 2008-2009 Xilinx, Inc. All rights reserved.
3
//
4
// This file contains confidential and proprietary information
5
// of Xilinx, Inc. and is protected under U.S. and
6
// international copyright and other intellectual property
7
// laws.
8
//
9
// DISCLAIMER
10
// This disclaimer is not a license and does not grant any
11
// rights to the materials distributed herewith. Except as
12
// otherwise provided in a valid license issued to you by
13
// Xilinx, and to the maximum extent permitted by applicable
14
// law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
15
// WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
16
// AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
17
// BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
18
// INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
19
// (2) Xilinx shall not be liable (whether in contract or tort,
20
// including negligence, or under any other theory of
21
// liability) for any loss or damage of any kind or nature
22
// related to, arising under or in connection with these
23
// materials, including for any direct, or any indirect,
24
// special, incidental, or consequential loss or damage
25
// (including loss of data, profits, goodwill, or any type of
26
// loss or damage suffered as a result of any action brought
27
// by a third party) even if such damage or loss was
28
// reasonably foreseeable or Xilinx had been advised of the
29
// possibility of the same.
30
//
31
// CRITICAL APPLICATIONS
32
// Xilinx products are not designed or intended to be fail-
33
// safe, or for use in any application requiring fail-safe
34
// performance, such as life-support or safety devices or
35
// systems, Class III medical devices, nuclear facilities,
36
// applications related to the deployment of airbags, or any
37
// other applications that could lead to death, personal
38
// injury, or severe property or environmental damage
39
// (individually and collectively, "Critical
40
// Applications"). Customer assumes the sole risk and
41
// liability of any use of Xilinx products in Critical
42
// Applications, subject only to applicable laws and
43
// regulations governing limitations on product liability.
44
//
45
// THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
46
// PART OF THIS FILE AT ALL TIMES.
47
//
48
//*****************************************************************************
49
//   ____  ____
50
//  /   /\/   /
51
// /___/  \  /    Vendor: Xilinx
52
// \   \   \/     Version: %version
53
//  \   \         Application: MIG
54
//  /   /         Filename: afifo.v
55
// /___/   /\     Date Last Modified: $Date: 2011/06/02 07:16:32 $
56
// \   \  /  \    Date Created: Oct 21 2008
57
//  \___\/\___\
58
//
59
//Device: Spartan6
60
//Design Name: DDR/DDR2/DDR3/LPDDR 
61
//Purpose:  A generic synchronous fifo.
62
//Reference:
63
//Revision History:
64
 
65
//*****************************************************************************
66
 
67
`timescale 1ps/1ps
68
 
69
module afifo #
70
(
71
 parameter TCQ           = 100,
72
 parameter DSIZE = 32,
73
 parameter FIFO_DEPTH = 16,
74
 parameter ASIZE = 4,
75
 parameter SYNC = 1   // only has always '1' logic.
76
)
77
(
78
input              wr_clk,
79
input              rst,
80
input              wr_en,
81
input [DSIZE-1:0]  wr_data,
82
input              rd_en,
83
input              rd_clk,
84
output [DSIZE-1:0] rd_data,
85
output reg         full,
86
output reg         empty,
87
output reg         almost_full
88
);
89
 
90
// memory array
91
reg [DSIZE-1:0] mem [0:FIFO_DEPTH-1];
92
 
93
//Read Capture Logic
94
// if Sync = 1, then no need to remove metastability logic because wrclk = rdclk
95
reg [ASIZE:0] rd_gray_nxt;
96
reg [ASIZE:0]    rd_gray;
97
reg [ASIZE:0]    rd_capture_ptr;
98
reg [ASIZE:0]    pre_rd_capture_gray_ptr;
99
reg [ASIZE:0]    rd_capture_gray_ptr;
100
reg [ASIZE:0]    wr_gray;
101
reg [ASIZE:0] wr_gray_nxt;
102
 
103
reg [ASIZE:0] wr_capture_ptr;
104
reg [ASIZE:0] pre_wr_capture_gray_ptr;
105
reg [ASIZE:0] wr_capture_gray_ptr;
106
wire [ASIZE:0] buf_avail;
107
wire [ASIZE:0] buf_filled;
108
wire [ASIZE-1:0] wr_addr, rd_addr;
109
 
110
reg [ASIZE:0]   wr_ptr, rd_ptr;
111
integer i,j,k;
112
 
113
 
114
// for design that use the same clock for both read and write
115
generate
116
if (SYNC == 1) begin: RDSYNC
117
   always @ (rd_ptr)
118
     rd_capture_ptr = rd_ptr;
119
end
120
endgenerate
121
 
122
 
123
 
124
//capture the wr_gray_pointers to rd_clk domains and convert the gray pointers to binary pointers 
125
// before do comparison.
126
 
127
 
128
 
129
// if Sync = 1, then no need to remove metastability logic because wrclk = rdclk
130
generate
131
if (SYNC == 1) begin: WRSYNC
132
always @ (wr_ptr)
133
    wr_capture_ptr = wr_ptr;
134
end
135
endgenerate
136
 
137
// dualport ram 
138
// Memory (RAM) that holds the contents of the FIFO
139
 
140
 
141
assign wr_addr = wr_ptr;
142
assign rd_data = mem[rd_addr];
143
always @(posedge wr_clk)
144
begin
145
if (wr_en && !full)
146
  mem[wr_addr] <= #TCQ wr_data;
147
 
148
end
149
 
150
 
151
// Read Side Logic
152
 
153
 
154
assign rd_addr = rd_ptr[ASIZE-1:0];
155
assign rd_strobe = rd_en && !empty;
156
 
157
integer n;
158
reg [ASIZE:0] rd_ptr_tmp;
159
    // change the binary pointer to gray pointer
160
always @ (rd_ptr)
161
begin
162
//  rd_gray_nxt[ASIZE] = rd_ptr_tmp[ASIZE];
163
//  for (n=0; n < ASIZE; n=n+1) 
164
//       rd_gray_nxt[n] = rd_ptr_tmp[n] ^ rd_ptr_tmp[n+1];
165
 
166
  rd_gray_nxt[ASIZE] = rd_ptr[ASIZE];
167
  for (n=0; n < ASIZE; n=n+1)
168
       rd_gray_nxt[n] = rd_ptr[n] ^ rd_ptr[n+1];
169
 
170
 
171
 
172
end
173
 
174
 
175
always @(posedge rd_clk)
176
begin
177
if (rst)
178
   begin
179
        rd_ptr <= #TCQ 'b0;
180
        rd_gray <= #TCQ 'b0;
181
   end
182
else begin
183
    if (rd_strobe)
184
        rd_ptr <= #TCQ rd_ptr + 1;
185
 
186
    rd_ptr_tmp <= #TCQ rd_ptr;
187
 
188
    // change the binary pointer to gray pointer
189
    rd_gray <= #TCQ rd_gray_nxt;
190
end
191
 
192
end
193
 
194
//generate empty signal
195
assign buf_filled = wr_capture_ptr - rd_ptr;
196
 
197
always @ (posedge rd_clk )
198
begin
199
   if (rst)
200
        empty <= #TCQ 1'b1;
201
   else if ((buf_filled == 0) || (buf_filled == 1 && rd_strobe))
202
        empty <= #TCQ 1'b1;
203
   else
204
        empty <= #TCQ 1'b0;
205
end
206
 
207
 
208
// write side logic;
209
 
210
reg [ASIZE:0] wbin;
211
wire [ASIZE:0] wgraynext, wbinnext;
212
 
213
 
214
 
215
always @(posedge rd_clk)
216
begin
217
if (rst)
218
   begin
219
        wr_ptr <= #TCQ 'b0;
220
        wr_gray <= #TCQ 'b0;
221
   end
222
else begin
223
    if (wr_en)
224
        wr_ptr <= #TCQ wr_ptr + 1;
225
 
226
    // change the binary pointer to gray pointer
227
    wr_gray <= #TCQ wr_gray_nxt;
228
end
229
 
230
end
231
 
232
 
233
// change the write pointer to gray pointer
234
always @ (wr_ptr)
235
begin
236
    wr_gray_nxt[ASIZE] = wr_ptr[ASIZE];
237
    for (n=0; n < ASIZE; n=n+1)
238
       wr_gray_nxt[n] = wr_ptr[n] ^ wr_ptr[n+1];
239
end
240
// calculate how many buf still available
241
assign buf_avail = (rd_capture_ptr + FIFO_DEPTH) - wr_ptr;
242
 
243
always @ (posedge wr_clk )
244
begin
245
   if (rst)
246
        full <= #TCQ 1'b0;
247
   else if ((buf_avail == 0) || (buf_avail == 1 && wr_en))
248
        full <= #TCQ 1'b1;
249
   else
250
        full <= #TCQ 1'b0;
251
end
252
 
253
 
254
always @ (posedge wr_clk )
255
begin
256
   if (rst)
257
        almost_full <= #TCQ 1'b0;
258
   else if ((buf_avail == FIFO_DEPTH - 2 ) || ((buf_avail == FIFO_DEPTH -3) && wr_en))
259
        almost_full <= #TCQ 1'b1;
260
   else
261
        almost_full <= #TCQ 1'b0;
262
end
263
 
264
endmodule
265
 
266
 

powered by: WebSVN 2.1.0

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