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

Subversion Repositories xulalx25soc

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

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

Line No. Rev Author Line
1 2 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    wbufifo.v
4
//
5
// Project:     XuLA2 board
6
//
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 59 dgisselq
        reg     will_overflow;
49
        initial will_overflow = 1'b0;
50
        always @(posedge i_clk)
51
                if (i_rst)
52
                        will_overflow <= 1'b0;
53
                else if (i_rd)
54
                        will_overflow <= (will_overflow)&&(i_wr);
55
                else if (i_wr)
56
                        will_overflow <= (r_first+2 == r_last);
57
                else if (r_first+1 == r_last)
58
                        will_overflow <= 1'b1;
59
 
60 2 dgisselq
        // Write
61
        initial r_first = 0;
62
        always @(posedge i_clk)
63
                if (i_rst)
64
                        r_first <= { (LGFLEN){1'b0} };
65
                else if (i_wr)
66
                begin // Cowardly refuse to overflow
67 59 dgisselq
                        if ((i_rd)||(~will_overflow)) // (r_first+1 != r_last)
68 2 dgisselq
                                r_first <= r_first+{{(LGFLEN-1){1'b0}},1'b1};
69
                        // else o_ovfl <= 1'b1;
70
                end
71
        always @(posedge i_clk)
72
                if (i_wr) // Write our new value regardless--on overflow or not
73
                        fifo[r_first] <= i_data;
74
 
75
        // Reads
76
        //      Following a read, the next sample will be available on the
77
        //      next clock
78
        //      Clock   ReadCMD ReadAddr        Output
79
        //      0        0        0                fifo[0]
80
        //      1       1       0                fifo[0]
81
        //      2       0        1               fifo[1]
82
        //      3       0        1               fifo[1]
83
        //      4       1       1               fifo[1]
84
        //      5       1       2               fifo[2]
85
        //      6       0        3               fifo[3]
86
        //      7       0        3               fifo[3]
87 59 dgisselq
        reg     will_underflow;
88
        initial will_underflow = 1'b0;
89 2 dgisselq
        always @(posedge i_clk)
90
                if (i_rst)
91 59 dgisselq
                        will_underflow <= 1'b0;
92
                else if (i_wr)
93
                        will_underflow <= (will_underflow)&&(i_rd);
94
                else if (i_rd)
95
                        will_underflow <= (r_last+1==r_first);
96
                else
97
                        will_underflow <= (r_last == r_first);
98
 
99
        initial r_last = 0;
100
        always @(posedge i_clk)
101
                if (i_rst)
102 2 dgisselq
                        r_last <= { (LGFLEN){1'b0} };
103
                else if (i_rd)
104
                begin
105 59 dgisselq
                        if ((i_wr)||(~will_underflow)) // (r_first != r_last)
106 2 dgisselq
                                r_last <= r_last+{{(LGFLEN-1){1'b0}},1'b1};
107
                                // Last chases first
108
                                // Need to be prepared for a possible two
109
                                // reads in quick succession
110
                                // o_data <= fifo[r_last+1];
111
                        // else o_unfl <= 1'b1;
112
                end
113
        always @(posedge i_clk)
114
                o_data <= fifo[(i_rd)?(r_last+{{(LGFLEN-1){1'b0}},1'b1})
115
                                        :(r_last)];
116
 
117
        wire    [(LGFLEN-1):0]   nxt_first;
118
        assign  nxt_first = r_first+{{(LGFLEN-1){1'b0}},1'b1};
119 59 dgisselq
        assign  o_err = ((i_wr)&&(will_overflow)&&(~i_rd))
120
                                ||((i_rd)&&(will_underflow)&&(~i_wr));
121 2 dgisselq
 
122
        // wire [(LGFLEN-1):0]  fill;
123
        // assign       fill = (r_first-r_last);
124
        wire    [(LGFLEN-1):0]   nxt_last;
125
        assign  nxt_last = r_last+{{(LGFLEN-1){1'b0}},1'b1};
126
        always @(posedge i_clk)
127
                if (i_rst)
128
                        o_empty_n <= 1'b0;
129
                else
130
                        o_empty_n <= (~i_rd)&&(r_first != r_last)
131
                                        ||(i_rd)&&(r_first != nxt_last);
132
endmodule

powered by: WebSVN 2.1.0

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