19 #include <nvhls_types.h>
20 #include <mem_array.h>
21 #include <nvhls_assert.h>
64 template <
typename DataType,
unsigned int FifoLen,
unsigned int NumBanks = 1>
68 static const int BankSelWidth =
70 static const int AddrWidth =
72 typedef NVUINTW(BankSelWidth) BankIdx;
73 typedef NVUINTW(AddrWidth) FifoIdx;
74 typedef NVUINTW(AddrWidth+1) FifoIdxPlusOne;
76 FifoIdx head[NumBanks];
77 FifoIdx tail[NumBanks];
80 bool last_action_was_push[NumBanks];
84 FifoIdx ModIncr(FifoIdx curr_idx) {
85 if (curr_idx == FifoLen - 1) {
93 #pragma hls_unroll yes
94 for (
unsigned i = 0; i < NumBanks; i++) {
97 last_action_was_push[i] =
false;
102 void push(DataType wr_data, BankIdx bidx = 0) {
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;
111 DataType pop(BankIdx bidx = 0) {
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;
121 void incrHead(BankIdx bidx = 0) {
123 FifoIdx head_local = head[bidx];
124 head[bidx] = ModIncr(head_local);
125 last_action_was_push[bidx] =
false;
129 DataType peek(BankIdx bidx = 0) {
131 FifoIdx head_local = head[bidx];
132 return fifo_body.read(head_local, bidx);
136 bool isEmpty(BankIdx bidx = 0) {
137 FifoIdx head_local = head[bidx];
138 FifoIdx tail_local = tail[bidx];
139 return (tail_local == head_local) && (!last_action_was_push[bidx]);
143 bool isFull(BankIdx bidx = 0) {
144 FifoIdx head_local = head[bidx];
145 FifoIdx tail_local = tail[bidx];
146 return (tail_local == head_local) && (last_action_was_push[bidx]);
150 FifoIdxPlusOne NumFilled(BankIdx bidx = 0) {
151 FifoIdx head_local = head[bidx];
152 FifoIdx tail_local = tail[bidx];
159 if (head_local < tail_local) {
160 return (tail_local - head_local);
162 return (FifoLen - head_local + tail_local);
167 FifoIdxPlusOne NumAvailable(BankIdx bidx = 0) {
168 return (FifoLen - NumFilled(bidx));
173 #pragma hls_unroll yes
174 for (
unsigned i = 0; i < NumBanks; i++) {
177 last_action_was_push[i] =
false;
181 FifoIdx get_head(BankIdx bidx = 0) {
return head[bidx]; }
182 FifoIdx get_tail(BankIdx bidx = 0) {
return tail[bidx]; }
184 template<
unsigned int Size>
185 void Marshall(Marshaller<Size>& m) {
186 for (
unsigned i = 0; i < NumBanks; i++) {
189 for (
unsigned i = 0; i < NumBanks; i++) {
193 for (
unsigned i = 0; i < NumBanks; i++) {
194 m & last_action_was_push[i];
200 template <
typename DataType,
unsigned int NumBanks>
201 class FIFO<DataType, 0, NumBanks> {
204 static const int BankSelWidth =
208 typedef NVUINTW(BankSelWidth) BankIdx;
209 typedef NVUINTW(1) FifoIdx;
212 void push(DataType wr_data, BankIdx bidx = 0) {
NVHLS_ASSERT_MSG(0,
"FIFO size is zero");}
214 DataType pop(BankIdx bidx = 0) {
NVHLS_ASSERT_MSG(0,
"FIFO size is zero");
return DataType(); }
216 void incrHead(BankIdx bidx = 0) {
NVHLS_ASSERT_MSG(0,
"FIFO size is zero"); }
218 DataType peek(BankIdx bidx = 0) {
NVHLS_ASSERT_MSG(0,
"FIFO size is zero");
return DataType(); }
220 bool isEmpty(BankIdx bidx = 0) {
NVHLS_ASSERT_MSG(0,
"FIFO size is zero");
return true; }
222 bool isFull(BankIdx bidx = 0) {
NVHLS_ASSERT_MSG(0,
"FIFO size is zero");
return true; }
224 FifoIdx NumFilled(BankIdx bidx = 0) {
NVHLS_ASSERT_MSG(0,
"FIFO size is zero");
return 0; }
226 FifoIdx NumAvailable(BankIdx bidx = 0) {
NVHLS_ASSERT_MSG(0,
"FIFO size is zero");
return 0; }
238 template <
typename DataType>
244 static const int width = Wrapped<DataType>::width + 1;
245 typedef NVUINTW(1) T;
249 inline void push(DataType wr_data, T bidx = 0)
256 inline DataType pop(T bidx = 0)
262 inline void incrHead(T bidx = 0)
268 inline DataType peek(T bidx = 0)
274 inline bool isEmpty(T bidx = 0)
279 inline bool isFull(T bidx = 0)
284 inline T NumFilled(T bidx = 0) {
288 inline T NumAvailable(T bidx = 0) {
296 template<
unsigned int Size>
297 void Marshall(Marshaller<Size>& m) {
310 template <
typename DataType,
unsigned int NumBanks>
311 class FIFO<DataType, 1, NumBanks> {
312 DataType data[NumBanks];
313 bool valid[NumBanks];
314 static const int width = NumBanks * Wrapped<DataType>::width + NumBanks;
317 static const int BankSelWidth =
319 typedef NVUINTW(BankSelWidth) BankIdx;
320 typedef NVUINTW(1) T;
326 inline void push(DataType wr_data, BankIdx bidx = 0) {
328 data[bidx] = wr_data;
332 inline DataType pop(BankIdx bidx = 0) {
337 inline void incrHead(BankIdx bidx = 0) {
342 inline DataType peek(BankIdx bidx = 0) {
347 inline bool isEmpty(BankIdx bidx = 0) {
351 inline bool isFull(BankIdx bidx = 0) {
355 inline T NumFilled(T bidx = 0) {
359 inline T NumAvailable(T bidx = 0) {
363 inline void reset() {
364 #pragma hls_unroll yes
365 for(
unsigned i=0; i<NumBanks; i++) {
369 template<
unsigned int Size>
370 void Marshall(Marshaller<Size>& m) {
371 for (
unsigned i = 0; i < NumBanks; i++) {
374 for (
unsigned i = 0; i < NumBanks; i++) {
#define NVHLS_ASSERT_MSG(X, MSG)
Compute number of bits to represent a constant.