-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifo.cpp
374 lines (328 loc) · 8.84 KB
/
fifo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/***************************************************************************
* Copyright (c) 2013-2019, Broadcom Inc.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/
/** @file fifo.cpp
Generic bit FIFO functions */
#include <iostream>
#include "fifo.h"
/** Error logging macro */
#ifdef NDEBUG
#define ASSERT_MSG(condition, msg) 0
#else
#define ASSERT_MSG(condition, msg) \
(!(condition)) ? \
( std::cerr << "Assertion failed: (" << #condition << "), " \
<< "function " << __FUNCTION__ \
<< ", file " << __FILE__ \
<< ", line " << __LINE__ << "." \
<< std::endl << msg << std::endl, abort(), 0) : 1
#endif
/** Constructor
@param size Specifies FIFO size in bytes
*/
Fifo::Fifo(int size)
{
m_data.resize(size);
m_size = size * 8;
m_fullness = 0;
m_read_ptr = m_write_ptr = 0;
m_max_fullness = 0;
m_byte_ctr = 0;
}
/** Copy constructor
@param f FIFO object to copy
*/
Fifo::Fifo(const Fifo &f)
{
m_data = f.m_data;
m_size = f.m_size;
m_fullness = f.m_fullness;
m_read_ptr = f.m_read_ptr;
m_write_ptr = f.m_write_ptr;
m_max_fullness = f.m_max_fullness;
m_byte_ctr = f.m_byte_ctr;
}
/** Get unsigend int value from the FIFO
@param n Number of bits to retrieve
@return Value from FIFO
*/
uint32_t Fifo::GetBitsUi(int n)
{
unsigned int d = 0;
int i;
unsigned char b;
ASSERT_MSG(m_fullness >= n, "FIFO underflow has occured");
for (i = 0; i<n; ++i)
{
b = (m_data[m_read_ptr / 8] >> (7 - (m_read_ptr % 8))) & 1;
d = (d << 1) + b;
m_fullness--;
m_read_ptr++;
if (m_read_ptr >= m_size)
m_read_ptr = 0;
}
return (d);
}
/** Get signed int value from the FIFO (2's complement)
@param n Number of bits to retrieve
@return Value from FIFO
*/
int32_t Fifo::GetBitsI(int n)
{
unsigned int d = 0;
int i;
unsigned char b;
int sign = 0;
ASSERT_MSG(m_fullness >= n, "FIFO underflow has occured");
for (i = 0; i<n; ++i)
{
b = (m_data[m_read_ptr / 8] >> (7 - (m_read_ptr % 8))) & 1;
if (i == 0) sign = b;
d = (d << 1) + b;
m_fullness--;
m_read_ptr++;
if (m_read_ptr >= m_size)
m_read_ptr = 0;
}
if (sign)
{
int mask = INT32_MAX;
d |= (~mask);
}
return (d);
}
/** Put bits into a FIFO
@param d Unsigned value to add to FIFO
@param nbits Number of bits to add to FIFO
*/
void Fifo::PutBits(uint32_t d, int nbits)
{
int i;
unsigned char b;
ASSERT_MSG(m_fullness + nbits <= m_size, "FIFO has overflowed");
m_fullness += nbits;
for (i = 0; i<nbits; ++i)
{
b = (d >> (nbits - i - 1)) & 1;
if (!b)
m_data[m_write_ptr / 8] &= ~(1 << (7 - (m_write_ptr % 8)));
else
m_data[m_write_ptr / 8] |= (1 << (7 - (m_write_ptr % 8)));
m_write_ptr++;
if (m_write_ptr >= m_size)
m_write_ptr = 0;
}
if (m_fullness > m_max_fullness)
m_max_fullness = m_fullness;
}
/** Put bits into a FIFO
@param d Signed value to add to FIFO
@param nbits Number of bits to add to FIFO
*/
void Fifo::PutBits(int32_t d, int nbits)
{
int i;
unsigned char b;
ASSERT_MSG(m_fullness + nbits <= m_size, "FIFO has overflowed");
m_fullness += nbits;
for (i = 0; i<nbits; ++i)
{
b = (d >> (nbits - i - 1)) & 1;
if (!b)
m_data[m_write_ptr / 8] &= ~(1 << (7 - (m_write_ptr % 8)));
else
m_data[m_write_ptr / 8] |= (1 << (7 - (m_write_ptr % 8)));
m_write_ptr++;
if (m_write_ptr >= m_size)
m_write_ptr = 0;
}
if (m_fullness > m_max_fullness)
m_max_fullness = m_fullness;
}
/** Get (unsigend) bits from a FIFO in a 32-bit reverse order
@param n Number of bits to retrieve
@return Value from FIFO
*/
uint32_t Fifo::FlipGetBitsUi(int n)
{
unsigned int d = 0;
int i;
unsigned char b;
ASSERT_MSG(m_fullness >= n, "FIFO has underflowed");
// Note, you need to allocate 32 bits more than you plan to use so the reordering doesn't get overwritten
// Also, you need to allocate a 32-bit multiple size
for (i = 0; i<n; ++i)
{
int idx1, idx2;
idx1 = (m_read_ptr / 32) * 4;
idx2 = (31 - (m_read_ptr % 32)) / 8;
b = (m_data[idx1 + idx2] >> ((m_read_ptr % 8))) & 1;
d |= (b << i);
m_fullness--;
m_read_ptr++;
if (m_read_ptr >= m_size)
m_read_ptr = 0;
}
return (d);
}
/** Get (signed) bits from a FIFO in a 32-bit reverse order
@param n Number of bits to retrieve
@return Value from FIFO
*/
int32_t Fifo::FlipGetBitsI(int n)
{
unsigned int d = 0;
int i;
unsigned char b;
int sign = 0;
ASSERT_MSG(m_fullness >= n, "FIFO has underflowed");
// Note, you need to allocate 32 bits more than you plan to use so the reordering doesn't get overwritten
// Also, you need to allocate a 32-bit multiple size
for (i = 0; i<n; ++i)
{
int idx1, idx2;
idx1 = (m_read_ptr / 32) * 4;
idx2 = (31 - (m_read_ptr % 32)) / 8;
b = (m_data[idx1 + idx2] >> ((m_read_ptr % 8))) & 1;
sign = b; // MSbit
d |= (b << i);
m_fullness--;
m_read_ptr++;
if (m_read_ptr >= m_size)
m_read_ptr = 0;
}
if (sign)
{
int mask = INT32_MAX;
d |= (~mask);
}
return (d);
}
/** Put bits into a FIFO in 32-bit reverse order
@param d (unsigned) Value to add to FIFO
@param nbits Number of bits to add to FIFO
*/
void Fifo::FlipPutBits(uint32_t d, int nbits)
{
int i;
unsigned char b;
int word, bitpos, byte;
ASSERT_MSG(m_fullness + nbits <= m_size, "FIFO has overflowed");
m_fullness += nbits;
for (i = 0; i<nbits; ++i)
{
b = d & 1;
word = m_write_ptr / 32;
byte = (m_write_ptr / 8) % 4;
bitpos = m_write_ptr % 8;
if (!b)
m_data[word * 4 + 3 - byte] &= ~(1 << (bitpos));
else
m_data[word * 4 + 3 - byte] |= (1 << (bitpos));
m_write_ptr++;
if (m_write_ptr >= m_size)
m_write_ptr = 0;
d >>= 1; // Get next LSB
}
if (m_fullness > m_max_fullness)
m_max_fullness = m_fullness;
}
/** Put bits into a FIFO in 32-bit reverse order
@param d (signed) Value to add to FIFO
@param nbits Number of bits to add to FIFO
*/
void Fifo::FlipPutBits(int32_t d, int nbits)
{
int i;
unsigned char b;
int word, bitpos, byte;
ASSERT_MSG(m_fullness + nbits <= m_size, "FIFO has overflowed");
m_fullness += nbits;
for (i = 0; i<nbits; ++i)
{
b = d & 1;
word = m_write_ptr / 32;
byte = (m_write_ptr / 8) % 4;
bitpos = m_write_ptr % 8;
if (!b)
m_data[word * 4 + 3 - byte] &= ~(1 << (bitpos));
else
m_data[word * 4 + 3 - byte] |= (1 << (bitpos));
m_write_ptr++;
if (m_write_ptr >= m_size)
m_write_ptr = 0;
d >>= 1; // Get next LSB
}
if (m_fullness > m_max_fullness)
m_max_fullness = m_fullness;
}
/** Get a datum value from a FIFO
@param nbits Number of bits to get from FIFO
@param is_signed Flag indicating whether to do sign extension for signed value
@param direction_r2l Flag indicating to read datum values from right to left
*/
int32_t Fifo::GetDatum(int nbits, bool is_signed, bool direction_r2l)
{
if (is_signed)
{
if (direction_r2l)
return FlipGetBitsI(nbits);
else
return GetBitsI(nbits);
}
else
{
if (direction_r2l)
return static_cast<int32_t>(FlipGetBitsUi(nbits));
else
return static_cast<int32_t>(GetBitsUi(nbits));
}
}
/** Put a datum value into a FIFO
@param datum The datum value to add to FIFO
@param nbits Number of bits to add to FIFO
@param direction_r2l Flg indicating to write datum values from right to left
*/
void Fifo::PutDatum(int32_t datum, int nbits, bool direction_r2l)
{
if (direction_r2l)
FlipPutBits(datum, nbits);
else
PutBits(datum, nbits);
}
/** Clear a FIFO */
void Fifo::Clear()
{
m_fullness = 0;
m_read_ptr = m_write_ptr = 0;
m_max_fullness = 0;
m_byte_ctr = 0;
}