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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [rtl/] [wbufifo.v] - Blame information for rev 113

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    wbufifo.v
4
//
5 102 dgisselq
// Project:     FPGA library
6 2 dgisselq
//
7
// Purpose:     This was once a FIFO for a UART ... but now it works as a
8 59 dgisselq
//              synchronous FIFO for JTAG-wishbone conversion 36-bit codewords. 
9 2 dgisselq
//
10
//
11
// Creator:     Dan Gisselquist, Ph.D.
12
//              Gisselquist Technology, LLC
13
//
14
////////////////////////////////////////////////////////////////////////////////
15
//
16
// Copyright (C) 2015, Gisselquist Technology, LLC
17
//
18
// This program is free software (firmware): you can redistribute it and/or
19
// modify it under the terms of  the GNU General Public License as published
20
// by the Free Software Foundation, either version 3 of the License, or (at
21
// your option) any later version.
22
//
23
// This program is distributed in the hope that it will be useful, but WITHOUT
24
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
25
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26
// for more details.
27
//
28
// License:     GPL, v3, as defined and found on www.gnu.org,
29
//              http://www.gnu.org/licenses/gpl.html
30
//
31
//
32
////////////////////////////////////////////////////////////////////////////////
33
//
34
//
35
module wbufifo(i_clk, i_rst, i_wr, i_data, i_rd, o_data, o_empty_n, o_err);
36
        parameter       BW=66, LGFLEN=10, FLEN=(1<<LGFLEN);
37
        input                   i_clk, i_rst;
38
        input                   i_wr;
39
        input   [(BW-1):0]       i_data;
40
        input                   i_rd;
41
        output  reg [(BW-1):0]   o_data;
42
        output  reg             o_empty_n;
43
        output  wire            o_err;
44
 
45
        reg     [(BW-1):0]       fifo[0:(FLEN-1)];
46
        reg     [(LGFLEN-1):0]   r_first, r_last;
47
 
48 113 dgisselq
        wire    [(LGFLEN-1):0]   nxt_first;
49
        assign  nxt_first = r_first+{{(LGFLEN-1){1'b0}},1'b1};
50
 
51 59 dgisselq
        reg     will_overflow;
52
        initial will_overflow = 1'b0;
53
        always @(posedge i_clk)
54
                if (i_rst)
55
                        will_overflow <= 1'b0;
56
                else if (i_rd)
57
                        will_overflow <= (will_overflow)&&(i_wr);
58
                else if (i_wr)
59
                        will_overflow <= (r_first+2 == r_last);
60 113 dgisselq
                else if (nxt_first == r_last)
61 59 dgisselq
                        will_overflow <= 1'b1;
62
 
63 2 dgisselq
        // Write
64
        initial r_first = 0;
65
        always @(posedge i_clk)
66
                if (i_rst)
67
                        r_first <= { (LGFLEN){1'b0} };
68
                else if (i_wr)
69
                begin // Cowardly refuse to overflow
70 59 dgisselq
                        if ((i_rd)||(~will_overflow)) // (r_first+1 != r_last)
71 113 dgisselq
                                r_first <= nxt_first;
72 2 dgisselq
                        // else o_ovfl <= 1'b1;
73
                end
74
        always @(posedge i_clk)
75
                if (i_wr) // Write our new value regardless--on overflow or not
76
                        fifo[r_first] <= i_data;
77
 
78
        // Reads
79
        //      Following a read, the next sample will be available on the
80
        //      next clock
81
        //      Clock   ReadCMD ReadAddr        Output
82
        //      0        0        0                fifo[0]
83
        //      1       1       0                fifo[0]
84
        //      2       0        1               fifo[1]
85
        //      3       0        1               fifo[1]
86
        //      4       1       1               fifo[1]
87
        //      5       1       2               fifo[2]
88
        //      6       0        3               fifo[3]
89
        //      7       0        3               fifo[3]
90 59 dgisselq
        reg     will_underflow;
91
        initial will_underflow = 1'b0;
92 2 dgisselq
        always @(posedge i_clk)
93
                if (i_rst)
94 59 dgisselq
                        will_underflow <= 1'b0;
95
                else if (i_wr)
96
                        will_underflow <= (will_underflow)&&(i_rd);
97
                else if (i_rd)
98
                        will_underflow <= (r_last+1==r_first);
99
                else
100
                        will_underflow <= (r_last == r_first);
101
 
102
        initial r_last = 0;
103
        always @(posedge i_clk)
104
                if (i_rst)
105 2 dgisselq
                        r_last <= { (LGFLEN){1'b0} };
106
                else if (i_rd)
107
                begin
108 59 dgisselq
                        if ((i_wr)||(~will_underflow)) // (r_first != r_last)
109 2 dgisselq
                                r_last <= r_last+{{(LGFLEN-1){1'b0}},1'b1};
110
                                // Last chases first
111
                                // Need to be prepared for a possible two
112
                                // reads in quick succession
113
                                // o_data <= fifo[r_last+1];
114
                        // else o_unfl <= 1'b1;
115
                end
116
        always @(posedge i_clk)
117
                o_data <= fifo[(i_rd)?(r_last+{{(LGFLEN-1){1'b0}},1'b1})
118
                                        :(r_last)];
119
 
120 59 dgisselq
        assign  o_err = ((i_wr)&&(will_overflow)&&(~i_rd))
121
                                ||((i_rd)&&(will_underflow)&&(~i_wr));
122 2 dgisselq
 
123
        // wire [(LGFLEN-1):0]  fill;
124
        // assign       fill = (r_first-r_last);
125
        wire    [(LGFLEN-1):0]   nxt_last;
126
        assign  nxt_last = r_last+{{(LGFLEN-1){1'b0}},1'b1};
127
        always @(posedge i_clk)
128
                if (i_rst)
129
                        o_empty_n <= 1'b0;
130
                else
131
                        o_empty_n <= (~i_rd)&&(r_first != r_last)
132
                                        ||(i_rd)&&(r_first != nxt_last);
133
endmodule

powered by: WebSVN 2.1.0

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