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

Subversion Repositories wbuart32

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

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
// Copyright (C) 2015-2016, 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, i_rd, o_data,
39
                o_empty_n, o_half_full, o_status, o_err);
40
        parameter       BW=8, LGFLEN=4;
41
        input                   i_clk, i_rst;
42
        input                   i_wr;
43
        input   [(BW-1):0]       i_data;
44
        input                   i_rd;
45
        output  wire [(BW-1):0]  o_data;
46
        output  reg             o_empty_n;
47
        output  wire            o_half_full;
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;
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_last  + {{(LGFLEN-1){1'b0}},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, r_unfl;
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
        initial r_unfl = 1'b0;
119
        initial r_last = 0;
120
        always @(posedge i_clk)
121
                if (i_rst)
122
                begin
123
                        r_last <= { (LGFLEN){1'b0} };
124
                        r_unfl <= 1'b0;
125
                end else if (i_rd)
126
                begin
127
                        if ((i_wr)||(!will_underflow)) // (r_first != r_last)
128
                                r_last <= w_last_plus_one;
129
                                // Last chases first
130
                                // Need to be prepared for a possible two
131
                                // reads in quick succession
132
                                // o_data <= fifo[r_last+1];
133
                        else
134
                                r_unfl <= 1'b1;
135
                end
136
 
137
        reg     [7:0]    fifo_here, fifo_next, r_data;
138
        always @(posedge i_clk)
139
                fifo_here <= fifo[r_last];
140
        always @(posedge i_clk)
141
                fifo_next <= fifo[r_last+{{(LGFLEN-1){1'b0}},1'b1}];
142
        always @(posedge i_clk)
143
                r_data <= i_data;
144
 
145
        reg     [1:0]    osrc;
146
        always @(posedge i_clk)
147
                if (will_underflow)
148
                        // o_data <= i_data;
149
                        osrc <= 2'b00;
150
                else if ((i_rd)&&(r_first == w_last_plus_one))
151
                        osrc <= 2'b01;
152
                else if (i_rd)
153
                        osrc <= 2'b11;
154
                else
155
                        osrc <= 2'b10;
156
        assign o_data = (osrc[1]) ? ((osrc[0])?fifo_next:fifo_here) : r_data;
157
 
158
        // wire [(LGFLEN-1):0]  current_fill;
159
        // assign       current_fill = (r_first-r_last);
160
 
161
        always @(posedge i_clk)
162
                if (i_rst)
163
                        o_empty_n <= 1'b0;
164
                else case({i_wr, i_rd})
165
                        2'b00: o_empty_n <= (r_first != r_last);
166
                        2'b11: o_empty_n <= (r_first != r_last);
167
                        2'b10: o_empty_n <= 1'b1;
168
                        2'b01: o_empty_n <= (r_first != w_last_plus_one);
169
                endcase
170
 
171
        reg     [(LGFLEN-1):0]   r_fill;
172
        always @(posedge i_clk)
173
                if (i_rst)
174
                        r_fill <= 0;
175
                else if ((i_rd)&&(!i_wr))
176
                        r_fill <= r_first - r_last - 1'b1;
177
                else if ((!i_rd)&&(i_wr))
178
                        r_fill <= r_first - r_last + 1'b1;
179
                else
180
                        r_fill <= r_first - r_last;
181
        assign  o_half_full = r_fill[(LGFLEN-1)];
182
 
183
        assign o_err = (r_ovfl) || (r_unfl);
184
 
185
        wire    [3:0]    lglen;
186
        assign lglen = LGFLEN;
187
        assign  o_status = { lglen, {(16-2-4-LGFLEN){1'b0}}, r_fill, o_half_full, o_empty_n };
188
 
189
endmodule

powered by: WebSVN 2.1.0

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