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

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [trunk/] [Hardware/] [adv_dbg_if/] [rtl/] [verilog/] [bytefifo.v] - Blame information for rev 42

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 42 nyawn
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  bytefifo.v                                                  ////
4
////                                                              ////
5
////                                                              ////
6
////  A simple byte-wide FIFO with byte and free space counts     ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////       Nathan Yawn (nathan.yawn@opencores.org)                ////
10
////                                                              ////
11
////                                                              ////
12
////                                                              ////
13
//////////////////////////////////////////////////////////////////////
14
////                                                              ////
15
//// Copyright (C) 2010 Authors                                   ////
16
////                                                              ////
17
//// This source file may be used and distributed without         ////
18
//// restriction provided that this copyright statement is not    ////
19
//// removed from the file and that any derivative work contains  ////
20
//// the original copyright notice and the associated disclaimer. ////
21
////                                                              ////
22
//// This source file is free software; you can redistribute it   ////
23
//// and/or modify it under the terms of the GNU Lesser General   ////
24
//// Public License as published by the Free Software Foundation; ////
25
//// either version 2.1 of the License, or (at your option) any   ////
26
//// later version.                                               ////
27
////                                                              ////
28
//// This source is distributed in the hope that it will be       ////
29
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
30
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
31
//// PURPOSE.  See the GNU Lesser General Public License for more ////
32
//// details.                                                     ////
33
////                                                              ////
34
//// You should have received a copy of the GNU Lesser General    ////
35
//// Public License along with this source; if not, download it   ////
36
//// from http://www.opencores.org/lgpl.shtml                     ////
37
////                                                              ////
38
//////////////////////////////////////////////////////////////////////
39
//
40
// This is an 8-entry, byte-wide, single-port FIFO.  It can either
41
// push or pop a byte each clock cycle (but not both).  It includes
42
// outputs indicating the number of bytes in the FIFO, and the number
43
// of bytes free - if you don't connect BYTES_FREE, the synthesis
44
// tool should eliminate the hardware to generate it.
45
//
46
// This attempts to use few resources.  There is only 1 counter,
47
// and only 1 decoder.  The FIFO works like a big shift register:
48
// bytes are always written to entry '0' of the FIFO, and older
49
// bytes are shifted toward entry '7' as newer bytes are added.
50
// The counter determines which entry the output reads.
51
//
52
// One caveat is that the DATA_OUT will glitch during a 'push'
53
// operation.  If the output is being sent to another clock
54
// domain, you should register it first.
55
//
56
// Ports:
57
// CLK:  Clock for all synchronous elements
58
// RST:  Zeros the counter and all registers asynchronously
59
// DATA_IN: Data to be pushed into the FIFO
60
// DATA_OUT: Always shows the data at the head of the FIFO, 'XX' if empty
61
// PUSH_POPn: When high (and EN is high), DATA_IN will be pushed onto the
62
//            FIFO and the count will be incremented at the next posedge
63
//            of CLK (assuming the FIFO is not full).  When low (and EN
64
//            is high), the count will be decremented and the output changed
65
//            to the next value in the FIFO (assuming FIFO not empty).
66
// EN: When high at posedege CLK, a push or pop operation will be performed,
67
//     based on the value of PUSH_POPn, assuming sufficient data or space.
68
// BYTES_AVAIL: Number of bytes in the FIFO.  May be in the range 0 to 8.
69
// BYTES_FREE: Free space in the FIFO.  May be in the range 0 to 8.          
70
 
71
 
72
// Top module
73
module bytefifo (
74
                 CLK,
75
                 RST,
76
                 DATA_IN,
77
                 DATA_OUT,
78
                 PUSH_POPn,
79
                 EN,
80
                 BYTES_AVAIL,
81
                 BYTES_FREE
82
                );
83
 
84
 
85
   input        CLK;
86
   input        RST;
87
   input  [7:0] DATA_IN;
88
   output [7:0] DATA_OUT;
89
   input        PUSH_POPn;
90
   input        EN;
91
   output [3:0] BYTES_AVAIL;
92
   output [3:0] BYTES_FREE;
93
 
94
   reg [7:0]     reg0, reg1, reg2, reg3, reg4, reg5, reg6, reg7;
95
   reg [3:0]     counter;
96
 
97
   reg [7:0]  DATA_OUT;
98
   wire [3:0]  BYTES_AVAIL;
99
   wire [3:0]    BYTES_FREE;
100
   wire         push_ok;
101
   wire    pop_ok;
102
 
103
   ///////////////////////////////////
104
   // Combinatorial assignments
105
 
106
   assign BYTES_AVAIL = counter;
107
   assign  BYTES_FREE = 4'h8 - BYTES_AVAIL;
108
   assign  push_ok = !(counter == 4'h8);
109
   assign  pop_ok = !(counter == 4'h0);
110
 
111
   ///////////////////////////////////
112
   // FIFO memory / shift registers
113
 
114
   // Reg 0 - takes input from DATA_IN
115
   always @ (posedge CLK or posedge RST)
116
     begin
117
        if(RST)
118
          reg0 <= 8'h0;
119
        else if(EN & PUSH_POPn & push_ok)
120
          reg0 <= DATA_IN;
121
     end
122
 
123
 
124
   // Reg 1 - takes input from reg0
125
   always @ (posedge CLK or posedge RST)
126
     begin
127
        if(RST)
128
          reg1 <= 8'h0;
129
        else if(EN & PUSH_POPn & push_ok)
130
          reg1 <= reg0;
131
     end
132
 
133
 
134
   // Reg 2 - takes input from reg1
135
   always @ (posedge CLK or posedge RST)
136
     begin
137
        if(RST)
138
          reg2 <= 8'h0;
139
        else if(EN & PUSH_POPn & push_ok)
140
          reg2 <= reg1;
141
     end
142
 
143
 
144
   // Reg 3 - takes input from reg2
145
   always @ (posedge CLK or posedge RST)
146
     begin
147
        if(RST)
148
          reg3 <= 8'h0;
149
        else if(EN & PUSH_POPn & push_ok)
150
          reg3 <= reg2;
151
     end
152
 
153
 
154
   // Reg 4 - takes input from reg3
155
   always @ (posedge CLK or posedge RST)
156
     begin
157
        if(RST)
158
          reg4 <= 8'h0;
159
        else if(EN & PUSH_POPn & push_ok)
160
          reg4 <= reg3;
161
     end
162
 
163
 
164
   // Reg 5 - takes input from reg4
165
   always @ (posedge CLK or posedge RST)
166
     begin
167
        if(RST)
168
          reg5 <= 8'h0;
169
        else if(EN & PUSH_POPn & push_ok)
170
          reg5 <= reg4;
171
     end
172
 
173
 
174
   // Reg 6 - takes input from reg5
175
   always @ (posedge CLK or posedge RST)
176
     begin
177
        if(RST)
178
          reg6 <= 8'h0;
179
        else if(EN & PUSH_POPn & push_ok)
180
          reg6 <= reg5;
181
     end
182
 
183
 
184
   // Reg 7 - takes input from reg6
185
   always @ (posedge CLK or posedge RST)
186
     begin
187
        if(RST)
188
          reg7 <= 8'h0;
189
        else if(EN & PUSH_POPn & push_ok)
190
          reg7 <= reg6;
191
     end
192
 
193
   ///////////////////////////////////////////////////
194
   // Read counter
195
   // This is a 4-bit saturating up/down counter
196
  // The 'saturating' is done via push_ok and pop_ok
197
 
198
   always @ (posedge CLK or posedge RST)
199
     begin
200
        if(RST)             counter <= 4'h0;
201
        else if(EN & PUSH_POPn & push_ok)  counter <= counter + 4'h1;
202
        else if(EN & (~PUSH_POPn) & pop_ok)    counter <= counter - 4'h1;
203
     end
204
 
205
   /////////////////////////////////////////////////
206
   // Output decoder
207
 
208
   always @ (counter or reg0 or reg1 or reg2 or reg3 or reg4 or reg5
209
             or reg6 or reg7)
210
     begin
211
        case (counter)
212
          4'h1:     DATA_OUT <= reg0;
213
          4'h2:     DATA_OUT <= reg1;
214
          4'h3:     DATA_OUT <= reg2;
215
          4'h4:     DATA_OUT <= reg3;
216
          4'h5:     DATA_OUT <= reg4;
217
          4'h6:     DATA_OUT <= reg5;
218
          4'h7:     DATA_OUT <= reg6;
219
          4'h8:     DATA_OUT <= reg7;
220
          default:  DATA_OUT <= 8'hXX;
221
        endcase
222
     end
223
 
224
 
225
endmodule

powered by: WebSVN 2.1.0

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