MatchLib
Loading...
Searching...
No Matches
fifo.h
1/*
2 * Copyright (c) 2017-2022, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef FIFO_H
17#define FIFO_H
18
19#include <nvhls_types.h>
20#include <mem_array.h>
21#include <nvhls_assert.h>
64template <typename DataType, unsigned int FifoLen, unsigned int NumBanks = 1>
65class FIFO {
66
67 public:
68 static const int BankSelWidth =
69 (NumBanks == 1) ? 1 : nvhls::nbits<NumBanks - 1>::val;
70 static const int AddrWidth =
71 (FifoLen == 1) ? 1 : nvhls::nbits<FifoLen - 1>::val;
72 typedef NVUINTW(BankSelWidth) BankIdx;
73 typedef NVUINTW(AddrWidth) FifoIdx;
74 typedef NVUINTW(AddrWidth+1) FifoIdxPlusOne;
75
76 FifoIdx head[NumBanks]; // where to read from
77 FifoIdx tail[NumBanks]; // where to write to
78 // FifoLen is number of entries in each bank
80 bool last_action_was_push[NumBanks];
81 static const int width = mem_array_sep<DataType, FifoLen * NumBanks, NumBanks>::width + 2 * NumBanks * AddrWidth + NumBanks;
82
83 // Function to do modulo increment of pointer
84 FifoIdx ModIncr(FifoIdx curr_idx) {
85 if (curr_idx == FifoLen - 1) {
86 return 0;
87 }
88 return curr_idx + 1;
89 }
90
91 // Constructor
92 FIFO() {
93#pragma hls_unroll yes
94 for (unsigned i = 0; i < NumBanks; i++) {
95 head[i] = 0;
96 tail[i] = 0;
97 last_action_was_push[i] = false;
98 }
99 }
100
101 // Function to push data to FIFO
102 void push(DataType wr_data, BankIdx bidx = 0) {
103 NVHLS_ASSERT_MSG(!isFull(bidx), "Pushing data to full FIFO");
104 FifoIdx tail_local = tail[bidx];
105 fifo_body.write(tail_local, bidx, wr_data);
106 tail[bidx] = ModIncr(tail_local);
107 last_action_was_push[bidx] = true;
108 }
109
110 // Function to pop data from FIFO
111 DataType pop(BankIdx bidx = 0) {
112 NVHLS_ASSERT_MSG(!isEmpty(bidx), "Popping data from empty FIFO");
113 FifoIdx head_local = head[bidx];
114 DataType rd_data = fifo_body.read(head_local, bidx);
115 head[bidx] = ModIncr(head_local);
116 last_action_was_push[bidx] = false;
117 return rd_data;
118 }
119
120 // Function to increment the head pointer (emulate a pop)
121 void incrHead(BankIdx bidx = 0) {
122 NVHLS_ASSERT_MSG(!isEmpty(bidx), "Incrementing Head of empty FIFO");
123 FifoIdx head_local = head[bidx];
124 head[bidx] = ModIncr(head_local);
125 last_action_was_push[bidx] = false;
126 }
127
128 // Function to peek from FIFO
129 DataType peek(BankIdx bidx = 0) {
130 NVHLS_ASSERT_MSG(!isEmpty(bidx), "Peeking data from empty FIFO");
131 FifoIdx head_local = head[bidx];
132 return fifo_body.read(head_local, bidx);
133 }
134
135 #pragma map_to_operator [CCORE]
136 #pragma ccore_type combinational
137 bool is_equal_core(FifoIdx a1, FifoIdx a2) {
138 return a1 == a2;
139 }
140
141 // Checks if FIFO is empty
142 bool isEmpty(BankIdx bidx = 0) {
143 FifoIdx head_local = head[bidx];
144 FifoIdx tail_local = tail[bidx];
145 return is_equal_core(tail_local, head_local) && (!last_action_was_push[bidx]);
146 }
147
148 // Checks if FIFO is full
149 bool isFull(BankIdx bidx = 0) {
150 FifoIdx head_local = head[bidx];
151 FifoIdx tail_local = tail[bidx];
152 return is_equal_core(tail_local, head_local) && (last_action_was_push[bidx]);
153 }
154
155 // Returns number of entries filled in FIFO
156 FifoIdxPlusOne NumFilled(BankIdx bidx = 0) {
157 FifoIdx head_local = head[bidx];
158 FifoIdx tail_local = tail[bidx];
159 if (isEmpty(bidx)) {
160 return 0;
161 }
162 if (isFull(bidx)) {
163 return FifoLen;
164 }
165 if (head_local < tail_local) {
166 return (tail_local - head_local);
167 } else {
168 return (FifoLen - head_local + tail_local);
169 }
170 }
171
172 // Returns number of empty entries in FIFO
173 FifoIdxPlusOne NumAvailable(BankIdx bidx = 0) {
174 return (FifoLen - NumFilled(bidx));
175 }
176
177 // Reset head and tail pointers
178 void reset() {
179#pragma hls_unroll yes
180 for (unsigned i = 0; i < NumBanks; i++) {
181 head[i] = 0;
182 tail[i] = 0;
183 last_action_was_push[i] = false;
184 }
185 }
186
187 FifoIdx get_head(BankIdx bidx = 0) { return head[bidx]; }
188 FifoIdx get_tail(BankIdx bidx = 0) { return tail[bidx]; }
189
190 template<unsigned int Size>
191 void Marshall(Marshaller<Size>& m) {
192 for (unsigned i = 0; i < NumBanks; i++) {
193 m & head[i];
194 }
195 for (unsigned i = 0; i < NumBanks; i++) {
196 m & tail[i];
197 }
198 m & fifo_body;
199 for (unsigned i = 0; i < NumBanks; i++) {
200 m & last_action_was_push[i];
201 }
202 }
203
204}; // end FIFO class
205
206template <typename DataType, unsigned int NumBanks>
207class FIFO<DataType, 0, NumBanks> { // 0 entries, NumBanks
208 // make sure no one is accessing a fifo with 0 entries ever
209 // it will still be fine to create it
210 static const int BankSelWidth =
211 (NumBanks == 1) ? 1 : nvhls::nbits<NumBanks - 1>::val;
212
213 public:
214 typedef NVUINTW(BankSelWidth) BankIdx;
215 typedef NVUINTW(1) FifoIdx;
216 FIFO() {}
217
218 void push(DataType wr_data, BankIdx bidx = 0) {NVHLS_ASSERT_MSG(0, "FIFO size is zero");}
219
220 DataType pop(BankIdx bidx = 0) { NVHLS_ASSERT_MSG(0, "FIFO size is zero"); return DataType(); }
221
222 void incrHead(BankIdx bidx = 0) {NVHLS_ASSERT_MSG(0, "FIFO size is zero"); }
223
224 DataType peek(BankIdx bidx = 0) { NVHLS_ASSERT_MSG(0, "FIFO size is zero"); return DataType(); }
225
226 bool isEmpty(BankIdx bidx = 0) {NVHLS_ASSERT_MSG(0, "FIFO size is zero"); return true; }
227
228 bool isFull(BankIdx bidx = 0) {NVHLS_ASSERT_MSG(0, "FIFO size is zero"); return true; }
229
230 FifoIdx NumFilled(BankIdx bidx = 0) {NVHLS_ASSERT_MSG(0, "FIFO size is zero"); return 0; }
231
232 FifoIdx NumAvailable(BankIdx bidx = 0) {NVHLS_ASSERT_MSG(0, "FIFO size is zero"); return 0; }
233
234 void reset() {}
235};
236
244template <typename DataType>
245class FIFO<DataType, 1, 1> {
246 DataType data;
247 bool valid;
248
249 public:
250 static const int width = Wrapped<DataType>::width + 1;
251 typedef NVUINTW(1) T; //redundant
252
253 FIFO() {reset();}
254
255 inline void push(DataType wr_data, T bidx = 0)
256 {
257 NVHLS_ASSERT_MSG(!isFull(), "Pushing data to full FIFO");
258 data = wr_data;
259 valid = true;
260 }
261
262 inline DataType pop(T bidx = 0)
263 {
264 incrHead();
265 return data;
266 }
267
268 inline void incrHead(T bidx = 0)
269 {
270 NVHLS_ASSERT_MSG(!isEmpty(), "Incrementing head of empty FIFO");
271 valid = false;
272 }
273
274 inline DataType peek(T bidx = 0)
275 {
276 NVHLS_ASSERT_MSG(!isEmpty(), "Peeking data from empty FIFO");
277 return data;
278 }
279
280 inline bool isEmpty(T bidx = 0)
281 {
282 return !valid;
283 }
284
285 inline bool isFull(T bidx = 0)
286 {
287 return valid;
288 }
289
290 inline T NumFilled(T bidx = 0) {
291 return valid;
292 }
293
294 inline T NumAvailable(T bidx = 0) {
295 return !valid;
296 }
297
298 inline void reset()
299 {
300 valid = false;
301 }
302 template<unsigned int Size>
303 void Marshall(Marshaller<Size>& m) {
304 m & valid;
305 m & data;
306 }
307};
316template <typename DataType, unsigned int NumBanks>
317class FIFO<DataType, 1, NumBanks> {
318 DataType data[NumBanks];
319 bool valid[NumBanks];
320 static const int width = NumBanks * Wrapped<DataType>::width + NumBanks;
321
322 public:
323 static const int BankSelWidth =
324 (NumBanks == 1) ? 1 : nvhls::nbits<NumBanks - 1>::val;
325 typedef NVUINTW(BankSelWidth) BankIdx;
326 typedef NVUINTW(1) T;
327
328 FIFO() {
329 reset();
330 }
331
332 inline void push(DataType wr_data, BankIdx bidx = 0) {
333 NVHLS_ASSERT_MSG(!isFull(bidx), "Pushing data to full FIFO");
334 data[bidx] = wr_data;
335 valid[bidx] = true;
336 }
337
338 inline DataType pop(BankIdx bidx = 0) {
339 incrHead(bidx);
340 return data[bidx];
341 }
342
343 inline void incrHead(BankIdx bidx = 0) {
344 NVHLS_ASSERT_MSG(!isEmpty(bidx), "Incrementing Head of empty FIFO");
345 valid[bidx] = false;
346 }
347
348 inline DataType peek(BankIdx bidx = 0) {
349 NVHLS_ASSERT_MSG(!isEmpty(bidx), "Peeking data from empty FIFO");
350 return data[bidx];
351 }
352
353 inline bool isEmpty(BankIdx bidx = 0) {
354 return !valid[bidx];
355 }
356
357 inline bool isFull(BankIdx bidx = 0) {
358 return valid[bidx];
359 }
360
361 inline T NumFilled(T bidx = 0) {
362 return valid;
363 }
364
365 inline T NumAvailable(T bidx = 0) {
366 return !valid;
367 }
368
369 inline void reset() {
370 #pragma hls_unroll yes
371 for(unsigned i=0; i<NumBanks; i++) {
372 valid[i] = false;
373 }
374 }
375 template<unsigned int Size>
376 void Marshall(Marshaller<Size>& m) {
377 for (unsigned i = 0; i < NumBanks; i++) {
378 m & valid[i];
379 }
380 for (unsigned i = 0; i < NumBanks; i++) {
381 m & data[i];
382 }
383 }
384};
385
386#endif // end #define FIFO_H macro
Configurable FIFO class.
Definition fifo.h:65
Abstract Memory Class.
Definition mem_array.h:83
#define NVHLS_ASSERT_MSG(X, MSG)
#define NVUINTW(width)
Definition nvhls_types.h:35
Compute number of bits to represent a constant.
Definition nvhls_int.h:118