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

Subversion Repositories wbuart32

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

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

powered by: WebSVN 2.1.0

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