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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [ufifo.v] - Blame information for rev 50

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 50 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    ufifo.v
4
//
5
// Project:     wbuart32, a full featured UART with simulator
6
//
7
// Purpose:     
8
//
9
// Creator:     Dan Gisselquist, Ph.D.
10
//              Gisselquist Technology, LLC
11
//
12
////////////////////////////////////////////////////////////////////////////////
13
//
14
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
15
//
16
// This program is free software (firmware): you can redistribute it and/or
17
// modify it under the terms of  the GNU General Public License as published
18
// by the Free Software Foundation, either version 3 of the License, or (at
19
// your option) any later version.
20
//
21
// This program is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License along
27
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
28
// target there if the PDF file isn't present.)  If not, see
29
// <http://www.gnu.org/licenses/> for a copy.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
module ufifo(i_clk, i_rst, i_wr, i_data, o_empty_n, i_rd, o_data, o_status, o_err);
39
        parameter       BW=8;   // Byte/data width
40
        parameter [3:0]  LGFLEN=4;
41
        parameter       RXFIFO=1'b0;
42
        input                   i_clk, i_rst;
43
        input                   i_wr;
44
        input   [(BW-1):0]       i_data;
45
        output  wire            o_empty_n;      // True if something is in FIFO
46
        input                   i_rd;
47
        output  wire [(BW-1):0]  o_data;
48
        output  wire    [15:0]   o_status;
49
        output  wire            o_err;
50
 
51
        localparam      FLEN=(1<<LGFLEN);
52
 
53
        reg     [(BW-1):0]       fifo[0:(FLEN-1)];
54
        reg     [(LGFLEN-1):0]   r_first, r_last, r_next;
55
 
56
        wire    [(LGFLEN-1):0]   w_first_plus_one, w_first_plus_two,
57
                                w_last_plus_one;
58
        assign  w_first_plus_two = r_first + {{(LGFLEN-2){1'b0}},2'b10};
59
        assign  w_first_plus_one = r_first + {{(LGFLEN-1){1'b0}},1'b1};
60
        assign  w_last_plus_one  = r_next; // r_last  + 1'b1;
61
 
62
        reg     will_overflow;
63
        initial will_overflow = 1'b0;
64
        always @(posedge i_clk)
65
                if (i_rst)
66
                        will_overflow <= 1'b0;
67
                else if (i_rd)
68
                        will_overflow <= (will_overflow)&&(i_wr);
69
                else if (i_wr)
70
                        will_overflow <= (w_first_plus_two == r_last);
71
                else if (w_first_plus_one == r_last)
72
                        will_overflow <= 1'b1;
73
 
74
        // Write
75
        reg     r_ovfl;
76
        initial r_first = 0;
77
        initial r_ovfl  = 0;
78
        always @(posedge i_clk)
79
                if (i_rst)
80
                begin
81
                        r_ovfl <= 1'b0;
82
                        r_first <= { (LGFLEN){1'b0} };
83
                end else if (i_wr)
84
                begin // Cowardly refuse to overflow
85
                        if ((i_rd)||(!will_overflow)) // (r_first+1 != r_last)
86
                                r_first <= w_first_plus_one;
87
                        else
88
                                r_ovfl <= 1'b1;
89
                end
90
        always @(posedge i_clk)
91
                if (i_wr) // Write our new value regardless--on overflow or not
92
                        fifo[r_first] <= i_data;
93
 
94
        // Reads
95
        //      Following a read, the next sample will be available on the
96
        //      next clock
97
        //      Clock   ReadCMD ReadAddr        Output
98
        //      0        0        0                fifo[0]
99
        //      1       1       0                fifo[0]
100
        //      2       0        1               fifo[1]
101
        //      3       0        1               fifo[1]
102
        //      4       1       1               fifo[1]
103
        //      5       1       2               fifo[2]
104
        //      6       0        3               fifo[3]
105
        //      7       0        3               fifo[3]
106
        reg     will_underflow;
107
        initial will_underflow = 1'b1;
108
        always @(posedge i_clk)
109
                if (i_rst)
110
                        will_underflow <= 1'b1;
111
                else if (i_wr)
112
                        will_underflow <= (will_underflow)&&(i_rd);
113
                else if (i_rd)
114
                        will_underflow <= (w_last_plus_one == r_first);
115
                else
116
                        will_underflow <= (r_last == r_first);
117
 
118
        //
119
        // Don't report FIFO underflow errors.  These'll be caught elsewhere
120
        // in the system, and the logic below makes it hard to reset them.
121
        // We'll still report FIFO overflow, however.
122
        //
123
        // reg          r_unfl;
124
        // initial      r_unfl = 1'b0;
125
        initial r_last = 0;
126
        always @(posedge i_clk)
127
                if (i_rst)
128
                begin
129
                        r_last <= 0;
130
                        r_next <= { {(LGFLEN-1){1'b0}}, 1'b1 };
131
                        // r_unfl <= 1'b0;
132
                end else if (i_rd)
133
                begin
134
                        if ((i_wr)||(!will_underflow)) // (r_first != r_last)
135
                        begin
136
                                r_last <= r_next;
137
                                r_next <= r_last +{{(LGFLEN-2){1'b0}},2'b10};
138
                                // Last chases first
139
                                // Need to be prepared for a possible two
140
                                // reads in quick succession
141
                                // o_data <= fifo[r_last+1];
142
                        end
143
                        // else r_unfl <= 1'b1;
144
                end
145
 
146
        reg     [7:0]    fifo_here, fifo_next, r_data;
147
        always @(posedge i_clk)
148
                fifo_here <= fifo[r_last];
149
        always @(posedge i_clk)
150
                fifo_next <= fifo[r_next];
151
        always @(posedge i_clk)
152
                r_data <= i_data;
153
 
154
        reg     [1:0]    osrc;
155
        always @(posedge i_clk)
156
                if (will_underflow)
157
                        // o_data <= i_data;
158
                        osrc <= 2'b00;
159
                else if ((i_rd)&&(r_first == w_last_plus_one))
160
                        osrc <= 2'b01;
161
                else if (i_rd)
162
                        osrc <= 2'b11;
163
                else
164
                        osrc <= 2'b10;
165
        assign o_data = (osrc[1]) ? ((osrc[0])?fifo_next:fifo_here) : r_data;
166
 
167
        // wire [(LGFLEN-1):0]  current_fill;
168
        // assign       current_fill = (r_first-r_last);
169
 
170
        reg     r_empty_n;
171
        initial r_empty_n = 1'b0;
172
        always @(posedge i_clk)
173
                if (i_rst)
174
                        r_empty_n <= 1'b0;
175
                else case({i_wr, i_rd})
176
                        2'b00: r_empty_n <= (r_first != r_last);
177
                        2'b11: r_empty_n <= (r_first != r_last);
178
                        2'b10: r_empty_n <= 1'b1;
179
                        2'b01: r_empty_n <= (r_first != w_last_plus_one);
180
                endcase
181
 
182
        wire    w_full_n;
183
        assign  w_full_n = will_overflow;
184
 
185
        //
186
        // If this is a receive FIFO, the FIFO count that matters is the number
187
        // of values yet to be read.  If instead this is a transmit FIFO, then 
188
        // the FIFO count that matters is the number of empty positions that
189
        // can still be filled before the FIFO is full.
190
        //
191
        // Adjust for these differences here.
192
        reg     [(LGFLEN-1):0]   r_fill;
193
        always @(posedge i_clk)
194
                if (RXFIFO!=0) begin
195
                        // Calculate the number of elements in our FIFO
196
                        //
197
                        // Although used for receive, this is actually the more
198
                        // generic answer--should you wish to use the FIFO in
199
                        // another context.
200
                        if (i_rst)
201
                                r_fill <= 0;
202
                        else case({i_wr, i_rd})
203
                        2'b01:   r_fill <= r_first - r_next;
204
                        2'b10:   r_fill <= r_first - r_last + 1'b1;
205
                        default: r_fill <= r_first - r_last;
206
                        endcase
207
                end else begin
208
                        // Calculate the number of elements that are empty and
209
                        // can be filled within our FIFO
210
                        if (i_rst)
211
                                r_fill <= { (LGFLEN){1'b1} };
212
                        else case({i_wr, i_rd})
213
                        2'b01:   r_fill <= r_last - r_first;
214
                        2'b10:   r_fill <= r_last - w_first_plus_two;
215
                        default: r_fill <= r_last - w_first_plus_one;
216
                        endcase
217
                end
218
 
219
        // We don't report underflow errors.  These
220
        assign o_err = (r_ovfl); //  || (r_unfl);
221
 
222
        wire    [3:0]    lglen;
223
        assign lglen = LGFLEN;
224
 
225
        wire    [9:0]    w_fill;
226
        assign  w_fill[(LGFLEN-1):0] = r_fill;
227
        generate if (LGFLEN < 10)
228
                assign w_fill[9:(LGFLEN)] = 0;
229
        endgenerate
230
 
231
        wire    w_half_full;
232
        assign  w_half_full = r_fill[(LGFLEN-1)];
233
 
234
        assign  o_status = {
235
                // Our status includes a 4'bit nibble telling anyone reading
236
                // this the size of our FIFO.  The size is then given by
237
                // 2^(this value).  Hence a 4'h4 in this position means that the
238
                // FIFO has 2^4 or 16 values within it.
239
                lglen,
240
                // The FIFO fill--for a receive FIFO the number of elements
241
                // left to be read, and for a transmit FIFO the number of
242
                // empty elements within the FIFO that can yet be filled.
243
                w_fill,
244
                // A '1' here means a half FIFO length can be read (receive
245
                // FIFO) or written to (not a receive FIFO).
246
                // receive FIFO), or be written to (if it isn't).
247
                (RXFIFO!=0)?w_half_full:w_half_full,
248
                // A '1' here means the FIFO can be read from (if it is a
249
                // receive FIFO), or be written to (if it isn't).
250
                (RXFIFO!=0)?r_empty_n:w_full_n
251
        };
252
 
253
        assign  o_empty_n = r_empty_n;
254
 
255
endmodule

powered by: WebSVN 2.1.0

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