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

Subversion Repositories pci

[/] [pci/] [tags/] [rel_3/] [rtl/] [verilog/] [pciw_fifo_control.v] - Blame information for rev 154

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 mihad
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  File name "pciw_fifo_control.v"                             ////
4
////                                                              ////
5
////  This file is part of the "PCI bridge" project               ////
6
////  http://www.opencores.org/cores/pci/                         ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////      - Miha Dolenc (mihad@opencores.org)                     ////
10
////                                                              ////
11
////  All additional information is avaliable in the README       ////
12
////  file.                                                       ////
13
////                                                              ////
14
////                                                              ////
15
//////////////////////////////////////////////////////////////////////
16
////                                                              ////
17
//// Copyright (C) 2001 Miha Dolenc, mihad@opencores.org          ////
18
////                                                              ////
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. ////
23
////                                                              ////
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.                                               ////
29
////                                                              ////
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 ////
34
//// details.                                                     ////
35
////                                                              ////
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                     ////
39
////                                                              ////
40
//////////////////////////////////////////////////////////////////////
41
//
42
// CVS Revision History
43
//
44
// $Log
45
//
46
 
47
/* FIFO_CONTROL module provides read/write address and status generation for
48
   FIFOs implemented with standard dual port SRAM cells in ASIC or FPGA designs */
49 21 mihad
`include "pci_constants.v"
50
// synopsys translate_off
51 6 mihad
`include "timescale.v"
52 21 mihad
// synopsys translate_on
53 6 mihad
 
54 2 mihad
module PCIW_FIFO_CONTROL
55 21 mihad
(
56
    rclock_in,
57
    wclock_in,
58
    renable_in,
59
    wenable_in,
60
    reset_in,
61 58 mihad
//    flush_in,         // not used
62 21 mihad
    almost_full_out,
63
    full_out,
64
    almost_empty_out,
65
    empty_out,
66
    waddr_out,
67
    raddr_out,
68
    rallow_out,
69 2 mihad
    wallow_out,
70
    two_left_out
71
);
72
 
73
parameter ADDR_LENGTH = 7 ;
74
 
75
// independent clock inputs - rclock_in = read clock, wclock_in = write clock
76
input  rclock_in, wclock_in;
77
 
78
// enable inputs - read address changes on rising edge of rclock_in when reads are allowed
79
//                 write address changes on rising edge of wclock_in when writes are allowed
80
input  renable_in, wenable_in;
81
 
82
// reset input
83
input  reset_in;
84
 
85
// flush input
86 58 mihad
//input flush_in ;      // not used
87 2 mihad
 
88
// almost full and empy status outputs
89
output almost_full_out, almost_empty_out;
90
 
91
// full and empty status outputs
92
output full_out, empty_out;
93
 
94
// read and write addresses outputs
95
output [(ADDR_LENGTH - 1):0] waddr_out, raddr_out;
96
 
97
// read and write allow outputs
98
output rallow_out, wallow_out ;
99
 
100
// two locations left output indicator
101
output two_left_out ;
102
 
103
// read address register
104
reg [(ADDR_LENGTH - 1):0] raddr ;
105
 
106
// write address register
107
reg [(ADDR_LENGTH - 1):0] waddr;
108
assign waddr_out = waddr ;
109
 
110
// grey code registers
111
// grey code pipeline for write address
112
reg [(ADDR_LENGTH - 1):0] wgrey_minus1 ; // current
113
reg [(ADDR_LENGTH - 1):0] wgrey_addr ; // current
114
reg [(ADDR_LENGTH - 1):0] wgrey_next ; // next
115
 
116
// next write gray address calculation - bitwise xor between address and shifted address
117
wire [(ADDR_LENGTH - 2):0] calc_wgrey_next  = waddr[(ADDR_LENGTH - 1):1] ^ waddr[(ADDR_LENGTH - 2):0] ;
118
 
119
// grey code pipeline for read address
120
reg [(ADDR_LENGTH - 1):0] rgrey_minus3 ; // three before current
121
reg [(ADDR_LENGTH - 1):0] rgrey_minus2 ; // two before current
122
reg [(ADDR_LENGTH - 1):0] rgrey_minus1 ; // one before current
123
reg [(ADDR_LENGTH - 1):0] rgrey_addr ; // current
124
reg [(ADDR_LENGTH - 1):0] rgrey_next ; // next
125
 
126
// next read gray address calculation - bitwise xor between address and shifted address
127
wire [(ADDR_LENGTH - 2):0] calc_rgrey_next  = raddr[(ADDR_LENGTH - 1):1] ^ raddr[(ADDR_LENGTH - 2):0] ;
128
 
129
// FFs for registered empty and full flags
130 59 mihad
wire empty ;
131
wire full ;
132 2 mihad
 
133
// registered almost_empty and almost_full flags
134 59 mihad
wire almost_empty ;
135
wire almost_full ;
136 2 mihad
 
137
// write allow wire - writes are allowed when fifo is not full
138 59 mihad
wire wallow = wenable_in && !full ;
139 2 mihad
 
140
// write allow output assignment
141
assign wallow_out = wallow ;
142
 
143
// read allow wire
144
wire rallow ;
145
 
146
// full output assignment
147
assign full_out  = full ;
148
 
149
// almost full output assignment
150 59 mihad
assign almost_full_out  = almost_full && !full ;
151 2 mihad
 
152
// clear generation for FFs and registers
153 58 mihad
wire clear = reset_in /*|| flush_in*/ ;     // flush not used for write fifo
154 2 mihad
 
155 21 mihad
reg wclock_nempty_detect ;
156
always@(posedge reset_in or posedge wclock_in)
157
begin
158
    if (reset_in)
159
        wclock_nempty_detect <= #`FF_DELAY 1'b0 ;
160
    else
161
        wclock_nempty_detect <= #`FF_DELAY (rgrey_addr != wgrey_addr) ;
162
end
163 2 mihad
 
164 59 mihad
wire stretched_empty ;
165 2 mihad
 
166 59 mihad
wire stretched_empty_flop_i = empty && ~wclock_nempty_detect ;
167
 
168
meta_flop #(1) i_meta_flop_stretched_empty
169
(
170
    .rst_i      (clear),
171
    .clk_i      (rclock_in),
172
    .ld_i       (1'b0),
173
    .ld_val_i   (1'b0),
174
    .en_i       (1'b1),
175
    .d_i        (stretched_empty_flop_i),
176
    .meta_q_o   (stretched_empty)
177
) ;
178
 
179 21 mihad
// empty output is actual empty + 1 read clock cycle ( stretched empty )
180
assign empty_out = empty  || stretched_empty ;
181 2 mihad
 
182 21 mihad
//rallow generation
183 59 mihad
assign rallow = renable_in && !empty && !stretched_empty ; // reads allowed if read enable is high and FIFO is not empty
184 2 mihad
 
185 21 mihad
// rallow output assignment
186
assign rallow_out = rallow ;
187 2 mihad
 
188 21 mihad
// almost empty output assignment
189 59 mihad
assign almost_empty_out = almost_empty && !empty && !stretched_empty ;
190 2 mihad
 
191 21 mihad
// at any clock edge that rallow is high, this register provides next read address, so wait cycles are not necessary
192
// when FIFO is empty, this register provides actual read address, so first location can be read
193
reg [(ADDR_LENGTH - 1):0] raddr_plus_one ;
194 2 mihad
 
195
 
196 21 mihad
// read address mux - when read is performed, next address is driven, so next data is available immediately after read
197
// this is convenient for zero wait stait bursts
198
assign raddr_out = rallow ? raddr_plus_one : raddr ;
199 2 mihad
 
200 21 mihad
always@(posedge rclock_in or posedge clear)
201
begin
202
    if (clear)
203 2 mihad
    begin
204 71 mihad
        // initial values seem a bit odd - they are this way to allow easier grey pipeline implementation and to allow min fifo size of 8
205 21 mihad
        raddr_plus_one <= #`FF_DELAY 6 ;
206 71 mihad
        raddr          <= #`FF_DELAY 5 ;
207 2 mihad
    end
208 21 mihad
    else if (rallow)
209 71 mihad
    begin
210 21 mihad
        raddr_plus_one <= #`FF_DELAY raddr_plus_one + 1'b1 ;
211 71 mihad
        raddr          <= #`FF_DELAY raddr_plus_one ;
212
    end
213 21 mihad
end
214 2 mihad
 
215
/*-----------------------------------------------------------------------------------------------
216
Read address control consists of Read address counter and Grey Address pipeline
217 21 mihad
There are 5 Grey addresses:
218 2 mihad
    - rgrey_minus3 is Grey Code of address three before current address
219
    - rgrey_minus2 is Grey Code of address two before current address
220
    - rgrey_minus1 is Grey Code of address one before current address
221
    - rgrey_addr is Grey Code of current read address
222
    - rgrey_next is Grey Code of next read address
223
--------------------------------------------------------------------------------------------------*/
224 71 mihad
// grey coded address pipeline for status generation in read clock domain
225 2 mihad
always@(posedge rclock_in or posedge clear)
226
begin
227 21 mihad
    if (clear)
228 2 mihad
    begin
229 21 mihad
        rgrey_minus3 <= #`FF_DELAY 0 ;
230 71 mihad
        rgrey_minus2 <= #`FF_DELAY 1 ;
231
        rgrey_minus1 <= #`FF_DELAY 3 ;
232
        rgrey_addr   <= #`FF_DELAY 2 ;
233
        rgrey_next   <= #`FF_DELAY 6 ;
234 2 mihad
    end
235 21 mihad
    else
236
    if (rallow)
237 71 mihad
    begin
238 21 mihad
        rgrey_minus3 <= #`FF_DELAY rgrey_minus2 ;
239
        rgrey_minus2 <= #`FF_DELAY rgrey_minus1 ;
240
        rgrey_minus1 <= #`FF_DELAY rgrey_addr ;
241 71 mihad
        rgrey_addr   <= #`FF_DELAY rgrey_next ;
242
        rgrey_next   <= #`FF_DELAY {raddr[ADDR_LENGTH - 1], calc_rgrey_next} ;
243 2 mihad
    end
244
end
245
 
246
/*--------------------------------------------------------------------------------------------
247
Write address control consists of write address counter and three Grey Code Registers:
248
    - wgrey_minus1 holds grey coded address of one before current write address
249
    - wgrey_addr represents current Grey Coded write address
250
    - wgrey_next represents Grey Coded next write address
251
----------------------------------------------------------------------------------------------*/
252 71 mihad
// grey coded address pipeline for status generation in write clock domain
253 2 mihad
always@(posedge wclock_in or posedge clear)
254
begin
255 21 mihad
    if (clear)
256 2 mihad
    begin
257 21 mihad
        wgrey_minus1 <= #`FF_DELAY 3 ;
258 71 mihad
        wgrey_addr   <= #`FF_DELAY 2 ;
259
        wgrey_next   <= #`FF_DELAY 6 ;
260 2 mihad
    end
261 21 mihad
    else
262 2 mihad
    if (wallow)
263 71 mihad
    begin
264 21 mihad
        wgrey_minus1 <= #`FF_DELAY wgrey_addr ;
265 71 mihad
        wgrey_addr   <= #`FF_DELAY wgrey_next ;
266
        wgrey_next   <= #`FF_DELAY {waddr[(ADDR_LENGTH - 1)], calc_wgrey_next} ;
267 2 mihad
    end
268
end
269
 
270 21 mihad
// write address counter - nothing special except initial value
271 2 mihad
always@(posedge wclock_in or posedge clear)
272
begin
273 21 mihad
    if (clear)
274
        // initial value 5
275
        waddr <= #`FF_DELAY 5 ;
276
    else
277
    if (wallow)
278
        waddr <= #`FF_DELAY waddr + 1'b1 ;
279 2 mihad
end
280
 
281
/*------------------------------------------------------------------------------------------------------------------------------
282
Registered full control:
283
registered full is set on rising edge of wclock_in, when one location is left in fifo and another is written
284
It's kept high until something is read from FIFO, which is registered on
285
next rising write clock edge.
286
 
287
Registered almost full control:
288
registered almost full is set on rising edge of write clock when two locations are left in fifo and another is written to it.
289
it's kept high until something is read/written from/to fifo
290
 
291
Registered two left control:
292
registered two left is set on rising edge of write clock when three locations are left in fifo and another is written to it.
293
it's kept high until something is read/written from/to fifo.
294
--------------------------------------------------------------------------------------------------------------------------------*/
295
wire comb_full          = wgrey_next == rgrey_addr ;
296
wire comb_almost_full   = wgrey_addr == rgrey_minus2 ;
297
wire comb_two_left      = wgrey_next == rgrey_minus2 ;
298
wire comb_three_left    = wgrey_next == rgrey_minus3 ;
299
 
300
//combinatorial input to Registered full FlipFlop
301
wire reg_full = (wallow && comb_almost_full) || (comb_full) ;
302
 
303 59 mihad
meta_flop #(0) i_meta_flop_full
304
(
305
    .rst_i       (clear),
306
    .clk_i       (wclock_in),
307
    .ld_i        (1'b0),
308
    .ld_val_i    (1'b0),
309
    .en_i        (1'b1),
310
    .d_i         (reg_full),
311
    .meta_q_o    (full)
312
) ;
313 2 mihad
 
314
// input for almost full flip flop
315
wire reg_almost_full_in = wallow && comb_two_left || comb_almost_full ;
316
 
317 59 mihad
meta_flop #(0) i_meta_flop_almost_full
318
(
319
    .rst_i       (clear),
320
    .clk_i       (wclock_in),
321
    .ld_i        (1'b0),
322
    .ld_val_i    (1'b0),
323
    .en_i        (1'b1),
324
    .d_i         (reg_almost_full_in),
325
    .meta_q_o    (almost_full)
326
) ;
327 2 mihad
 
328
wire reg_two_left_in = wallow && comb_three_left || comb_two_left ;
329
 
330 59 mihad
meta_flop #(0) i_meta_flop_two_left
331
(
332
    .rst_i       (clear),
333
    .clk_i       (wclock_in),
334
    .ld_i        (1'b0),
335
    .ld_val_i    (1'b0),
336
    .en_i        (1'b1),
337
    .d_i         (reg_two_left_in),
338
    .meta_q_o    (two_left_out)
339
) ;
340 2 mihad
 
341
/*------------------------------------------------------------------------------------------------------------------------------
342
Registered empty control:
343 21 mihad
registered empty is set on rising edge of rclock_in,
344 2 mihad
when only one location is used in and read from fifo. It's kept high until something is written to FIFO, which is registered on
345
the next read clock.
346
 
347
Registered almost empty control:
348
almost empty is set on rising clock edge of rclock when two locations are used and one read from FIFO. It's kept high until
349
something is read/written from/to fifo.
350
--------------------------------------------------------------------------------------------------------------------------------*/
351
wire comb_almost_empty  = rgrey_next == wgrey_addr ;
352
wire comb_empty         = rgrey_addr == wgrey_addr ;
353
wire comb_two_used      = rgrey_next == wgrey_minus1 ;
354
 
355
// combinatorial input for registered emty FlipFlop
356 66 mihad
//wire reg_empty = (rallow && comb_almost_empty) || comb_empty ;
357
wire reg_empty = (rallow && almost_empty) || comb_empty ;
358 2 mihad
 
359 59 mihad
meta_flop #(1) i_meta_flop_empty
360
(
361
    .rst_i      (clear),
362
    .clk_i      (rclock_in),
363
    .ld_i       (1'b0),
364
    .ld_val_i   (1'b0),
365
    .en_i       (1'b1),
366
    .d_i        (reg_empty),
367
    .meta_q_o   (empty)
368
) ;
369 2 mihad
 
370
// input for almost empty flip flop
371
wire reg_almost_empty = rallow && comb_two_used || comb_almost_empty ;
372
 
373 59 mihad
meta_flop #(0) i_meta_flop_almost_empty
374
(
375
    .rst_i      (clear),
376
    .clk_i      (rclock_in),
377
    .ld_i       (1'b0),
378
    .ld_val_i   (1'b0),
379
    .en_i       (1'b1),
380
    .d_i        (reg_almost_empty),
381
    .meta_q_o   (almost_empty)
382
) ;
383
 
384 2 mihad
endmodule

powered by: WebSVN 2.1.0

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