blob: 0ea5ad98e3b515bce2f622fcc2677ba520aea15b [file] [log] [blame]
Peter A. G. Crosthwaitea3578d42012-08-01 20:26:12 +10001/*
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
Paolo Bonzinifd7f0d62013-02-04 10:57:50 +010015#include "qemu-common.h"
16#include "qemu/fifo8.h"
Peter A. G. Crosthwaitea3578d42012-08-01 20:26:12 +100017
18void fifo8_create(Fifo8 *fifo, uint32_t capacity)
19{
20 fifo->data = g_new(uint8_t, capacity);
21 fifo->capacity = capacity;
22 fifo->head = 0;
23 fifo->num = 0;
24}
25
26void fifo8_destroy(Fifo8 *fifo)
27{
28 g_free(fifo->data);
29}
30
31void fifo8_push(Fifo8 *fifo, uint8_t data)
32{
33 if (fifo->num == fifo->capacity) {
34 abort();
35 }
36 fifo->data[(fifo->head + fifo->num) % fifo->capacity] = data;
37 fifo->num++;
38}
39
Beniamino Galvanic4e57af2014-01-30 23:02:04 +010040void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num)
41{
42 uint32_t start, avail;
43
44 if (fifo->num + num > fifo->capacity) {
45 abort();
46 }
47
48 start = (fifo->head + fifo->num) % fifo->capacity;
49
50 if (start + num <= fifo->capacity) {
51 memcpy(&fifo->data[start], data, num);
52 } else {
53 avail = fifo->capacity - start;
54 memcpy(&fifo->data[start], data, avail);
55 memcpy(&fifo->data[0], &data[avail], num - avail);
56 }
57
58 fifo->num += num;
59}
60
Peter A. G. Crosthwaitea3578d42012-08-01 20:26:12 +100061uint8_t fifo8_pop(Fifo8 *fifo)
62{
63 uint8_t ret;
64
65 if (fifo->num == 0) {
66 abort();
67 }
68 ret = fifo->data[fifo->head++];
69 fifo->head %= fifo->capacity;
70 fifo->num--;
71 return ret;
72}
73
Beniamino Galvanic4e57af2014-01-30 23:02:04 +010074const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *num)
75{
76 uint8_t *ret;
77
78 if (max == 0 || max > fifo->num) {
79 abort();
80 }
81 *num = MIN(fifo->capacity - fifo->head, max);
82 ret = &fifo->data[fifo->head];
83 fifo->head += *num;
84 fifo->head %= fifo->capacity;
85 fifo->num -= *num;
86 return ret;
87}
88
Peter A. G. Crosthwaitea3578d42012-08-01 20:26:12 +100089void fifo8_reset(Fifo8 *fifo)
90{
91 fifo->num = 0;
Beniamino Galvani58892d42014-01-30 23:02:05 +010092 fifo->head = 0;
Peter A. G. Crosthwaitea3578d42012-08-01 20:26:12 +100093}
94
95bool fifo8_is_empty(Fifo8 *fifo)
96{
97 return (fifo->num == 0);
98}
99
100bool fifo8_is_full(Fifo8 *fifo)
101{
102 return (fifo->num == fifo->capacity);
103}
104
Beniamino Galvanic4e57af2014-01-30 23:02:04 +0100105uint32_t fifo8_num_free(Fifo8 *fifo)
106{
107 return fifo->capacity - fifo->num;
108}
109
110uint32_t fifo8_num_used(Fifo8 *fifo)
111{
112 return fifo->num;
113}
114
Peter A. G. Crosthwaitea3578d42012-08-01 20:26:12 +1000115const VMStateDescription vmstate_fifo8 = {
116 .name = "Fifo8",
117 .version_id = 1,
118 .minimum_version_id = 1,
Juan Quintela8f1e8842014-05-13 16:09:35 +0100119 .fields = (VMStateField[]) {
Peter A. G. Crosthwaitea3578d42012-08-01 20:26:12 +1000120 VMSTATE_VBUFFER_UINT32(data, Fifo8, 1, NULL, 0, capacity),
121 VMSTATE_UINT32(head, Fifo8),
122 VMSTATE_UINT32(num, Fifo8),
123 VMSTATE_END_OF_LIST()
124 }
125};