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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [wbufifo.v] - Blame information for rev 53

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

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

powered by: WebSVN 2.1.0

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