root / hw / fifo.c @ a1bc20df
History | View | Annotate | Download (1.7 kB)
1 |
/*
|
---|---|
2 |
* Generic FIFO component, implemented as a circular buffer.
|
3 |
*
|
4 |
* Copyright (c) 2012 Peter A. G. Crosthwaite
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version
|
9 |
* 2 of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* You should have received a copy of the GNU General Public License along
|
12 |
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
13 |
*/
|
14 |
|
15 |
#include "fifo.h" |
16 |
|
17 |
void fifo8_create(Fifo8 *fifo, uint32_t capacity)
|
18 |
{ |
19 |
fifo->data = g_new(uint8_t, capacity); |
20 |
fifo->capacity = capacity; |
21 |
fifo->head = 0;
|
22 |
fifo->num = 0;
|
23 |
} |
24 |
|
25 |
void fifo8_destroy(Fifo8 *fifo)
|
26 |
{ |
27 |
g_free(fifo->data); |
28 |
} |
29 |
|
30 |
void fifo8_push(Fifo8 *fifo, uint8_t data)
|
31 |
{ |
32 |
if (fifo->num == fifo->capacity) {
|
33 |
abort(); |
34 |
} |
35 |
fifo->data[(fifo->head + fifo->num) % fifo->capacity] = data; |
36 |
fifo->num++; |
37 |
} |
38 |
|
39 |
uint8_t fifo8_pop(Fifo8 *fifo) |
40 |
{ |
41 |
uint8_t ret; |
42 |
|
43 |
if (fifo->num == 0) { |
44 |
abort(); |
45 |
} |
46 |
ret = fifo->data[fifo->head++]; |
47 |
fifo->head %= fifo->capacity; |
48 |
fifo->num--; |
49 |
return ret;
|
50 |
} |
51 |
|
52 |
void fifo8_reset(Fifo8 *fifo)
|
53 |
{ |
54 |
fifo->num = 0;
|
55 |
} |
56 |
|
57 |
bool fifo8_is_empty(Fifo8 *fifo)
|
58 |
{ |
59 |
return (fifo->num == 0); |
60 |
} |
61 |
|
62 |
bool fifo8_is_full(Fifo8 *fifo)
|
63 |
{ |
64 |
return (fifo->num == fifo->capacity);
|
65 |
} |
66 |
|
67 |
const VMStateDescription vmstate_fifo8 = {
|
68 |
.name = "Fifo8",
|
69 |
.version_id = 1,
|
70 |
.minimum_version_id = 1,
|
71 |
.minimum_version_id_old = 1,
|
72 |
.fields = (VMStateField[]) { |
73 |
VMSTATE_VBUFFER_UINT32(data, Fifo8, 1, NULL, 0, capacity), |
74 |
VMSTATE_UINT32(head, Fifo8), |
75 |
VMSTATE_UINT32(num, Fifo8), |
76 |
VMSTATE_END_OF_LIST() |
77 |
} |
78 |
}; |