1 //////////////////////////////////////////////////////////////////////
3 //// File name "pciw_fifo_control.v" ////
5 //// This file is part of the "PCI bridge" project ////
6 //// http://www.opencores.org/cores/pci/ ////
9 //// - Miha Dolenc (mihad@opencores.org) ////
11 //// All additional information is avaliable in the README ////
15 //////////////////////////////////////////////////////////////////////
17 //// Copyright (C) 2001 Miha Dolenc, mihad@opencores.org ////
19 //// This source file may be used and distributed without ////
20 //// restriction provided that this copyright statement is not ////
21 //// removed from the file and that any derivative work contains ////
22 //// the original copyright notice and the associated disclaimer. ////
24 //// This source file is free software; you can redistribute it ////
25 //// and/or modify it under the terms of the GNU Lesser General ////
26 //// Public License as published by the Free Software Foundation; ////
27 //// either version 2.1 of the License, or (at your option) any ////
28 //// later version. ////
30 //// This source is distributed in the hope that it will be ////
31 //// useful, but WITHOUT ANY WARRANTY; without even the implied ////
32 //// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
33 //// PURPOSE. See the GNU Lesser General Public License for more ////
36 //// You should have received a copy of the GNU Lesser General ////
37 //// Public License along with this source; if not, download it ////
38 //// from http://www.opencores.org/lgpl.shtml ////
40 //////////////////////////////////////////////////////////////////////
42 // CVS Revision History
44 // $Log: pci_pciw_fifo_control.v,v $
45 // Revision 1.1 2007-03-20 17:50:56 sithglan
48 // Revision 1.5 2003/08/14 13:06:03 simons
49 // synchronizer_flop replaced with pci_synchronizer_flop, artisan ram instance updated.
51 // Revision 1.4 2003/08/08 16:36:33 tadejm
52 // Added 'three_left_out' to pci_pciw_fifo signaling three locations before full. Added comparison between current registered cbe and next unregistered cbe to signal wb_master whether it is allowed to performe burst or not. Due to this, I needed 'three_left_out' so that writing to pci_pciw_fifo can be registered, otherwise timing problems would occure.
54 // Revision 1.3 2003/07/29 08:20:11 mihad
55 // Found and simulated the problem in the synchronization logic.
56 // Repaired the synchronization logic in the FIFOs.
60 /* FIFO_CONTROL module provides read/write address and status generation for
61 FIFOs implemented with standard dual port SRAM cells in ASIC or FPGA designs */
62 `include "pci_constants.v"
63 // synopsys translate_off
64 `include "timescale.v"
65 // synopsys translate_on
67 module pci_pciw_fifo_control
86 parameter ADDR_LENGTH = 7 ;
88 // independent clock inputs - rclock_in = read clock, wclock_in = write clock
89 input rclock_in, wclock_in;
91 // enable inputs - read address changes on rising edge of rclock_in when reads are allowed
92 // write address changes on rising edge of wclock_in when writes are allowed
93 input renable_in, wenable_in;
98 // almost full and empy status outputs
99 output almost_full_out, almost_empty_out;
101 // full and empty status outputs
102 output full_out, empty_out;
104 // read and write addresses outputs
105 output [(ADDR_LENGTH - 1):0] waddr_out, raddr_out;
107 // read and write allow outputs
108 output rallow_out, wallow_out ;
110 // three and two locations left output indicator
111 output three_left_out ;
112 output two_left_out ;
114 // read address register
115 reg [(ADDR_LENGTH - 1):0] raddr ;
117 // write address register
118 reg [(ADDR_LENGTH - 1):0] waddr;
119 reg [(ADDR_LENGTH - 1):0] waddr_plus1;
120 assign waddr_out = waddr ;
122 // grey code registers
123 // grey code pipeline for write address
124 reg [(ADDR_LENGTH - 1):0] wgrey_minus1 ; // previous
125 reg [(ADDR_LENGTH - 1):0] wgrey_addr ; // current
126 reg [(ADDR_LENGTH - 1):0] wgrey_next ; // next
128 reg [(ADDR_LENGTH - 1):0] wgrey_next_plus1 ; // next plus 1
131 // next write gray address calculation - bitwise xor between address and shifted address
132 wire [(ADDR_LENGTH - 2):0] calc_wgrey_next = waddr[(ADDR_LENGTH - 1):1] ^ waddr[(ADDR_LENGTH - 2):0] ;
133 wire [(ADDR_LENGTH - 2):0] calc_wgrey_next_plus1 = waddr_plus1[(ADDR_LENGTH - 1):1] ^ waddr_plus1[(ADDR_LENGTH - 2):0] ;
135 // grey code pipeline for read address
136 reg [(ADDR_LENGTH - 1):0] rgrey_minus2 ; // two before current
137 reg [(ADDR_LENGTH - 1):0] rgrey_minus1 ; // one before current
138 reg [(ADDR_LENGTH - 1):0] rgrey_addr ; // current
139 reg [(ADDR_LENGTH - 1):0] rgrey_next ; // next
141 // next read gray address calculation - bitwise xor between address and shifted address
142 wire [(ADDR_LENGTH - 2):0] calc_rgrey_next = raddr[(ADDR_LENGTH - 1):1] ^ raddr[(ADDR_LENGTH - 2):0] ;
144 // write allow - writes are allowed when fifo is not full
145 assign wallow_out = wenable_in & ~full_out ;
147 // clear generation for FFs and registers
148 wire clear = reset_in ;
151 assign rallow_out = renable_in & ~empty_out ; // reads allowed if read enable is high and FIFO is not empty
153 // at any clock edge that rallow is high, this register provides next read address, so wait cycles are not necessary
154 // when FIFO is empty, this register provides actual read address, so first location can be read
155 reg [(ADDR_LENGTH - 1):0] raddr_plus_one ;
158 // read address mux - when read is performed, next address is driven, so next data is available immediately after read
159 // this is convenient for zero wait stait bursts
160 assign raddr_out = rallow_out ? raddr_plus_one : raddr ;
162 always@(posedge rclock_in or posedge clear)
166 // initial values seem a bit odd - they are this way to allow easier grey pipeline implementation and to allow min fifo size of 8
167 raddr_plus_one <= #`FF_DELAY 5 ;
168 raddr <= #`FF_DELAY 4 ;
169 // raddr_plus_one <= #`FF_DELAY 6 ;
170 // raddr <= #`FF_DELAY 5 ;
174 raddr_plus_one <= #`FF_DELAY raddr_plus_one + 1'b1 ;
175 raddr <= #`FF_DELAY raddr_plus_one ;
179 /*-----------------------------------------------------------------------------------------------
180 Read address control consists of Read address counter and Grey Address pipeline
181 There are 4 Grey addresses:
182 - rgrey_minus2 is Grey Code of address two before current address
183 - rgrey_minus1 is Grey Code of address one before current address
184 - rgrey_addr is Grey Code of current read address
185 - rgrey_next is Grey Code of next read address
186 --------------------------------------------------------------------------------------------------*/
187 // grey coded address pipeline for status generation in read clock domain
188 always@(posedge rclock_in or posedge clear)
192 rgrey_minus2 <= #1 0 ;
193 rgrey_minus1 <= #`FF_DELAY 1 ;
195 rgrey_next <= #`FF_DELAY 2 ;
200 rgrey_minus2 <= #1 rgrey_minus1 ;
201 rgrey_minus1 <= #`FF_DELAY rgrey_addr ;
202 rgrey_addr <= #1 rgrey_next ;
203 rgrey_next <= #`FF_DELAY {raddr[ADDR_LENGTH - 1], calc_rgrey_next} ;
207 /*--------------------------------------------------------------------------------------------
208 Write address control consists of write address counter and 3 Grey Code Registers:
209 - wgrey_minus1 represents previous Grey coded write address
210 - wgrey_addr represents current Grey Coded write address
211 - wgrey_next represents next Grey Coded write address
213 - wgrey_next_plus1 represents second next Grey Coded write address
215 ----------------------------------------------------------------------------------------------*/
216 // grey coded address pipeline for status generation in write clock domain
217 always@(posedge wclock_in or posedge clear)
221 wgrey_minus1 <= #`FF_DELAY 1 ;
222 wgrey_addr <= #`FF_DELAY 3 ;
223 wgrey_next <= #`FF_DELAY 2 ;
225 wgrey_next_plus1 <= #`FF_DELAY 6;
231 wgrey_minus1 <= #`FF_DELAY wgrey_addr ;
232 wgrey_addr <= #`FF_DELAY wgrey_next ;
234 wgrey_next <= #`FF_DELAY {waddr[(ADDR_LENGTH - 1)], calc_wgrey_next} ;
235 // wgrey_next <= #`FF_DELAY wgrey_next_plus1 ;
236 wgrey_next_plus1 <= #`FF_DELAY {waddr_plus1[(ADDR_LENGTH - 1)], calc_wgrey_next_plus1} ;
241 // write address counter - nothing special except initial value
242 always@(posedge wclock_in or posedge clear)
248 waddr <= #`FF_DELAY 4 ;
249 waddr_plus1 <= #`FF_DELAY 5 ;
254 waddr <= #`FF_DELAY waddr + 1'b1 ;
255 waddr_plus1 <= #`FF_DELAY waddr_plus1 + 1'b1 ;
259 /*------------------------------------------------------------------------------------------------------------------------------
260 Gray coded address of read address decremented by two is synchronized to write clock domain and compared to:
261 - previous grey coded write address - if they are equal, the fifo is full
263 - gray coded write address. If they are equal, fifo is almost full.
265 - grey coded next write address. If they are equal, the fifo has two free locations left.
266 --------------------------------------------------------------------------------------------------------------------------------*/
267 wire [(ADDR_LENGTH - 1):0] wclk_sync_rgrey_minus2 ;
268 reg [(ADDR_LENGTH - 1):0] wclk_rgrey_minus2 ;
270 pci_synchronizer_flop #(ADDR_LENGTH, 0) i_synchronizer_reg_rgrey_minus2
272 .data_in (rgrey_minus2),
273 .clk_out (wclock_in),
274 .sync_data_out (wclk_sync_rgrey_minus2),
278 always@(posedge wclock_in or posedge clear)
282 wclk_rgrey_minus2 <= #`FF_DELAY 0 ;
286 wclk_rgrey_minus2 <= #`FF_DELAY wclk_sync_rgrey_minus2 ;
290 assign full_out = (wgrey_minus1 == wclk_rgrey_minus2) ;
291 assign almost_full_out = (wgrey_addr == wclk_rgrey_minus2) ;
292 assign two_left_out = (wgrey_next == wclk_rgrey_minus2) ;
294 assign three_left_out = (wgrey_next_plus1 == wclk_rgrey_minus2) ;
297 /*------------------------------------------------------------------------------------------------------------------------------
299 Gray coded write address pointer is synchronized to read clock domain and compared to Gray coded read address pointer.
300 If they are equal, fifo is empty.
302 Almost empty control:
303 Synchronized write pointer is also compared to Gray coded next read address. If these two are
304 equal, fifo is almost empty.
305 --------------------------------------------------------------------------------------------------------------------------------*/
306 wire [(ADDR_LENGTH - 1):0] rclk_sync_wgrey_addr ;
307 reg [(ADDR_LENGTH - 1):0] rclk_wgrey_addr ;
308 pci_synchronizer_flop #(ADDR_LENGTH, 3) i_synchronizer_reg_wgrey_addr
310 .data_in (wgrey_addr),
311 .clk_out (rclock_in),
312 .sync_data_out (rclk_sync_wgrey_addr),
316 always@(posedge rclock_in or posedge clear)
319 rclk_wgrey_addr <= #`FF_DELAY 3 ;
321 rclk_wgrey_addr <= #`FF_DELAY rclk_sync_wgrey_addr ;
324 assign almost_empty_out = (rgrey_next == rclk_wgrey_addr) ;
325 assign empty_out = (rgrey_addr == rclk_wgrey_addr) ;