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

Subversion Repositories wbuart32

[/] [wbuart32/] [trunk/] [rtl/] [ufifo.v] - Blame information for rev 23

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    ufifo.v
4
//
5
// Project:     wbuart32, a full featured UART with simulator
6
//
7 18 dgisselq
// Purpose:     A synchronous data FIFO, designed for supporting the Wishbone
8
//              UART.  Particular features include the ability to read and
9
//      write on the same clock, while maintaining the correct output FIFO
10
//      parameters.  Two versions of the FIFO exist within this file, separated
11
//      by the RXFIFO parameter's value.  One, where RXFIFO = 1, produces status
12
//      values appropriate for reading and checking a read FIFO from logic,
13
//      whereas the RXFIFO = 0 applies to writing to the FIFO from bus logic
14
//      and reading it automatically any time the transmit UART is idle.
15 5 dgisselq
//
16
// Creator:     Dan Gisselquist, Ph.D.
17
//              Gisselquist Technology, LLC
18
//
19
////////////////////////////////////////////////////////////////////////////////
20
//
21 21 dgisselq
// Copyright (C) 2015-2018, Gisselquist Technology, LLC
22 5 dgisselq
//
23
// This program is free software (firmware): you can redistribute it and/or
24
// modify it under the terms of  the GNU General Public License as published
25
// by the Free Software Foundation, either version 3 of the License, or (at
26
// your option) any later version.
27
//
28
// This program is distributed in the hope that it will be useful, but WITHOUT
29
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
30
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31
// for more details.
32
//
33
// You should have received a copy of the GNU General Public License along
34 9 dgisselq
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
35 5 dgisselq
// target there if the PDF file isn't present.)  If not, see
36
// <http://www.gnu.org/licenses/> for a copy.
37
//
38
// License:     GPL, v3, as defined and found on www.gnu.org,
39
//              http://www.gnu.org/licenses/gpl.html
40
//
41
//
42
////////////////////////////////////////////////////////////////////////////////
43
//
44
//
45 17 dgisselq
`default_nettype        none
46
//
47 9 dgisselq
module ufifo(i_clk, i_rst, i_wr, i_data, o_empty_n, i_rd, o_data, o_status, o_err);
48
        parameter       BW=8;   // Byte/data width
49
        parameter [3:0]  LGFLEN=4;
50 14 dgisselq
        parameter       RXFIFO=1'b0;
51 21 dgisselq
        parameter       [0:0]     F_OPT_CLK2FFLOGIC = 1'b0;
52 17 dgisselq
        input   wire            i_clk, i_rst;
53
        input   wire            i_wr;
54
        input   wire [(BW-1):0]  i_data;
55 9 dgisselq
        output  wire            o_empty_n;      // True if something is in FIFO
56 17 dgisselq
        input   wire            i_rd;
57 5 dgisselq
        output  wire [(BW-1):0]  o_data;
58
        output  wire    [15:0]   o_status;
59
        output  wire            o_err;
60
 
61
        localparam      FLEN=(1<<LGFLEN);
62
 
63
        reg     [(BW-1):0]       fifo[0:(FLEN-1)];
64 6 dgisselq
        reg     [(LGFLEN-1):0]   r_first, r_last, r_next;
65 5 dgisselq
 
66
        wire    [(LGFLEN-1):0]   w_first_plus_one, w_first_plus_two,
67
                                w_last_plus_one;
68
        assign  w_first_plus_two = r_first + {{(LGFLEN-2){1'b0}},2'b10};
69
        assign  w_first_plus_one = r_first + {{(LGFLEN-1){1'b0}},1'b1};
70 6 dgisselq
        assign  w_last_plus_one  = r_next; // r_last  + 1'b1;
71 5 dgisselq
 
72
        reg     will_overflow;
73
        initial will_overflow = 1'b0;
74
        always @(posedge i_clk)
75
                if (i_rst)
76
                        will_overflow <= 1'b0;
77
                else if (i_rd)
78
                        will_overflow <= (will_overflow)&&(i_wr);
79
                else if (i_wr)
80 17 dgisselq
                        will_overflow <= (will_overflow)||(w_first_plus_two == r_last);
81 5 dgisselq
                else if (w_first_plus_one == r_last)
82
                        will_overflow <= 1'b1;
83
 
84
        // Write
85
        reg     r_ovfl;
86
        initial r_first = 0;
87
        initial r_ovfl  = 0;
88
        always @(posedge i_clk)
89
                if (i_rst)
90
                begin
91
                        r_ovfl <= 1'b0;
92
                        r_first <= { (LGFLEN){1'b0} };
93
                end else if (i_wr)
94
                begin // Cowardly refuse to overflow
95
                        if ((i_rd)||(!will_overflow)) // (r_first+1 != r_last)
96
                                r_first <= w_first_plus_one;
97
                        else
98
                                r_ovfl <= 1'b1;
99
                end
100
        always @(posedge i_clk)
101
                if (i_wr) // Write our new value regardless--on overflow or not
102
                        fifo[r_first] <= i_data;
103
 
104
        // Reads
105
        //      Following a read, the next sample will be available on the
106
        //      next clock
107
        //      Clock   ReadCMD ReadAddr        Output
108
        //      0        0        0                fifo[0]
109
        //      1       1       0                fifo[0]
110
        //      2       0        1               fifo[1]
111
        //      3       0        1               fifo[1]
112
        //      4       1       1               fifo[1]
113
        //      5       1       2               fifo[2]
114
        //      6       0        3               fifo[3]
115
        //      7       0        3               fifo[3]
116 9 dgisselq
        reg     will_underflow;
117 5 dgisselq
        initial will_underflow = 1'b1;
118
        always @(posedge i_clk)
119
                if (i_rst)
120
                        will_underflow <= 1'b1;
121
                else if (i_wr)
122 18 dgisselq
                        will_underflow <= 1'b0;
123 5 dgisselq
                else if (i_rd)
124 17 dgisselq
                        will_underflow <= (will_underflow)||(w_last_plus_one == r_first);
125 5 dgisselq
                else
126
                        will_underflow <= (r_last == r_first);
127
 
128 9 dgisselq
        //
129
        // Don't report FIFO underflow errors.  These'll be caught elsewhere
130
        // in the system, and the logic below makes it hard to reset them.
131
        // We'll still report FIFO overflow, however.
132
        //
133
        // reg          r_unfl;
134
        // initial      r_unfl = 1'b0;
135 5 dgisselq
        initial r_last = 0;
136 18 dgisselq
        initial r_next = { {(LGFLEN-1){1'b0}}, 1'b1 };
137 5 dgisselq
        always @(posedge i_clk)
138
                if (i_rst)
139
                begin
140 6 dgisselq
                        r_last <= 0;
141
                        r_next <= { {(LGFLEN-1){1'b0}}, 1'b1 };
142 9 dgisselq
                        // r_unfl <= 1'b0;
143 5 dgisselq
                end else if (i_rd)
144
                begin
145 18 dgisselq
                        if (!will_underflow) // (r_first != r_last)
146 6 dgisselq
                        begin
147
                                r_last <= r_next;
148
                                r_next <= r_last +{{(LGFLEN-2){1'b0}},2'b10};
149 5 dgisselq
                                // Last chases first
150
                                // Need to be prepared for a possible two
151
                                // reads in quick succession
152
                                // o_data <= fifo[r_last+1];
153 9 dgisselq
                        end
154
                        // else r_unfl <= 1'b1;
155 5 dgisselq
                end
156
 
157 17 dgisselq
        reg     [(BW-1):0]       fifo_here, fifo_next, r_data;
158 5 dgisselq
        always @(posedge i_clk)
159
                fifo_here <= fifo[r_last];
160
        always @(posedge i_clk)
161 6 dgisselq
                fifo_next <= fifo[r_next];
162 5 dgisselq
        always @(posedge i_clk)
163
                r_data <= i_data;
164
 
165
        reg     [1:0]    osrc;
166
        always @(posedge i_clk)
167
                if (will_underflow)
168
                        // o_data <= i_data;
169
                        osrc <= 2'b00;
170
                else if ((i_rd)&&(r_first == w_last_plus_one))
171
                        osrc <= 2'b01;
172
                else if (i_rd)
173
                        osrc <= 2'b11;
174
                else
175
                        osrc <= 2'b10;
176
        assign o_data = (osrc[1]) ? ((osrc[0])?fifo_next:fifo_here) : r_data;
177
 
178
        // wire [(LGFLEN-1):0]  current_fill;
179
        // assign       current_fill = (r_first-r_last);
180
 
181 9 dgisselq
        reg     r_empty_n;
182
        initial r_empty_n = 1'b0;
183 5 dgisselq
        always @(posedge i_clk)
184
                if (i_rst)
185 9 dgisselq
                        r_empty_n <= 1'b0;
186 17 dgisselq
                else casez({i_wr, i_rd, will_underflow})
187
                        3'b00?: r_empty_n <= (r_first != r_last);
188 18 dgisselq
                        3'b010: r_empty_n <= (r_first != w_last_plus_one);
189 17 dgisselq
                        3'b10?: r_empty_n <= 1'b1;
190 18 dgisselq
                        3'b110: r_empty_n <= (r_first != r_last);
191
                        3'b111: r_empty_n <= 1'b1;
192 17 dgisselq
                        default: begin end
193 5 dgisselq
                endcase
194
 
195 9 dgisselq
        wire    w_full_n;
196
        assign  w_full_n = will_overflow;
197
 
198
        //
199
        // If this is a receive FIFO, the FIFO count that matters is the number
200
        // of values yet to be read.  If instead this is a transmit FIFO, then 
201
        // the FIFO count that matters is the number of empty positions that
202
        // can still be filled before the FIFO is full.
203
        //
204
        // Adjust for these differences here.
205 5 dgisselq
        reg     [(LGFLEN-1):0]   r_fill;
206 18 dgisselq
        generate
207
        if (RXFIFO != 0)
208
                initial r_fill = 0;
209
        else
210
                initial r_fill = -1;
211
        endgenerate
212
 
213 14 dgisselq
        always @(posedge i_clk)
214
                if (RXFIFO!=0) begin
215
                        // Calculate the number of elements in our FIFO
216
                        //
217
                        // Although used for receive, this is actually the more
218
                        // generic answer--should you wish to use the FIFO in
219
                        // another context.
220 9 dgisselq
                        if (i_rst)
221
                                r_fill <= 0;
222 18 dgisselq
                        else casez({(i_wr), (!will_overflow), (i_rd)&&(!will_underflow)})
223
                        3'b0?1:   r_fill <= r_first - r_next;
224
                        3'b110:   r_fill <= r_first - r_last + 1'b1;
225
                        3'b1?1:   r_fill <= r_first - r_last;
226 9 dgisselq
                        default: r_fill <= r_first - r_last;
227
                        endcase
228 14 dgisselq
                end else begin
229
                        // Calculate the number of elements that are empty and
230 17 dgisselq
                        // can be filled within our FIFO.  Hence, this is really
231
                        // not the fill, but (SIZE-1)-fill.
232 9 dgisselq
                        if (i_rst)
233
                                r_fill <= { (LGFLEN){1'b1} };
234 18 dgisselq
                        else casez({i_wr, (!will_overflow), (i_rd)&&(!will_underflow)})
235
                        3'b0?1:   r_fill <= r_fill + 1'b1;
236
                        3'b110:   r_fill <= r_fill - 1'b1;
237
                        default: r_fill  <= r_fill;
238 9 dgisselq
                        endcase
239 14 dgisselq
                end
240 5 dgisselq
 
241 9 dgisselq
        // We don't report underflow errors.  These
242
        assign o_err = (r_ovfl); //  || (r_unfl);
243 5 dgisselq
 
244
        wire    [3:0]    lglen;
245
        assign lglen = LGFLEN;
246 9 dgisselq
 
247 18 dgisselq
        wire    w_half_full;
248 9 dgisselq
        wire    [9:0]    w_fill;
249
        assign  w_fill[(LGFLEN-1):0] = r_fill;
250 14 dgisselq
        generate if (LGFLEN < 10)
251
                assign w_fill[9:(LGFLEN)] = 0;
252 9 dgisselq
        endgenerate
253
 
254
        assign  w_half_full = r_fill[(LGFLEN-1)];
255
 
256
        assign  o_status = {
257
                // Our status includes a 4'bit nibble telling anyone reading
258
                // this the size of our FIFO.  The size is then given by
259
                // 2^(this value).  Hence a 4'h4 in this position means that the
260
                // FIFO has 2^4 or 16 values within it.
261
                lglen,
262
                // The FIFO fill--for a receive FIFO the number of elements
263
                // left to be read, and for a transmit FIFO the number of
264
                // empty elements within the FIFO that can yet be filled.
265
                w_fill,
266
                // A '1' here means a half FIFO length can be read (receive
267
                // FIFO) or written to (not a receive FIFO).
268
                // receive FIFO), or be written to (if it isn't).
269
                (RXFIFO!=0)?w_half_full:w_half_full,
270
                // A '1' here means the FIFO can be read from (if it is a
271
                // receive FIFO), or be written to (if it isn't).
272
                (RXFIFO!=0)?r_empty_n:w_full_n
273
        };
274
 
275
        assign  o_empty_n = r_empty_n;
276 18 dgisselq
 
277
//
278
//
279
//
280
// FORMAL METHODS
281
//
282
//
283
//
284
`ifdef  FORMAL
285
 
286
`ifdef  UFIFO
287
`define ASSUME  assume
288
`else
289
`define ASSUME  assert
290
`endif
291
 
292
//
293
// Assumptions about our input(s)
294
//
295
//
296
        reg     f_past_valid, f_last_clk;
297
 
298
        initial restrict(i_rst);
299
 
300 21 dgisselq
        generate if (F_OPT_CLK2FFLOGIC)
301 18 dgisselq
        begin
302 21 dgisselq
                always @($global_clock)
303 18 dgisselq
                begin
304 21 dgisselq
                        restrict(i_clk == !f_last_clk);
305
                        f_last_clk <= i_clk;
306
                        if (!$rose(i_clk))
307
                        begin
308
                                `ASSUME($stable(i_rst));
309
                                `ASSUME($stable(i_wr));
310
                                `ASSUME($stable(i_data));
311
                                `ASSUME($stable(i_rd));
312
                        end
313 18 dgisselq
                end
314 21 dgisselq
        end endgenerate
315 18 dgisselq
 
316
        //
317
        // Underflows are a very real possibility, should the user wish to
318
        // read from this FIFO while it is empty.  Our parent module will need
319
        // to deal with this.
320
        //
321
        // always @(posedge i_clk)
322
        //      `ASSUME((!will_underflow)||(!i_rd)||(i_rst));
323
//
324
// Assertions about our outputs
325
//
326
//
327
 
328
        initial f_past_valid = 1'b0;
329
        always @(posedge i_clk)
330
                f_past_valid <= 1'b1;
331
 
332
        wire    [(LGFLEN-1):0]   f_fill, f_next, f_empty;
333
        assign  f_fill = r_first - r_last;
334
        assign  f_empty = {(LGFLEN){1'b1}} -f_fill;
335
        assign  f_next = r_last + 1'b1;
336
        always @(posedge i_clk)
337
        begin
338
                if (RXFIFO)
339
                        assert(f_fill == r_fill);
340
                else
341
                        assert(f_empty== r_fill);
342
                if (f_fill == 0)
343
                begin
344
                        assert(will_underflow);
345
                        assert(!o_empty_n);
346
                end else begin
347
                        assert(!will_underflow);
348
                        assert(o_empty_n);
349
                end
350
 
351
                if (f_fill == {(LGFLEN){1'b1}})
352
                        assert(will_overflow);
353
                else
354
                        assert(!will_overflow);
355
 
356
                assert(r_next == f_next);
357
        end
358
 
359
        always @(posedge i_clk)
360
        if (f_past_valid)
361
        begin
362
                if ($past(i_rst))
363
                        assert(!o_err);
364
                else begin
365
                        // No underflow detection in this core
366
                        //
367
                        // if (($past(i_rd))&&($past(r_fill == 0)))
368
                        //      assert(o_err);
369
                        //
370
                        // We do, though, have overflow detection
371
                        if (($past(i_wr))&&(!$past(i_rd))
372
                                        &&($past(will_overflow)))
373
                                assert(o_err);
374
                end
375
        end
376
 
377 23 dgisselq
        always @(posedge i_clk)
378
        if (RXFIFO)
379
        begin
380
                assert(o_status[0] == (f_fill != 0));
381
                assert(o_status[1] == (f_fill[LGFLEN-1]));
382
        end
383
 
384
        always @(posedge i_clk)
385
        if (!RXFIFO) // Transmit FIFO interrupt flags
386
        begin
387
                assert(o_status[0] != (!w_full_n));
388
                assert(o_status[1] == (!f_fill[LGFLEN-1]));
389
        end
390
 
391 18 dgisselq
`endif
392 5 dgisselq
endmodule

powered by: WebSVN 2.1.0

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