2 * Generic FIFO32 component, based on FIFO8.
4 * Copyright (c) 2016 Jean-Christophe Dubois
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.
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/>.
18 #include "qemu/fifo8.h"
26 * @fifo: struct Fifo32 to initialise with new FIFO
27 * @capacity: capacity of the newly created FIFO expressed in 32 bit words
29 * Create a FIFO of the specified size. Clients should call fifo32_destroy()
30 * when finished using the fifo. The FIFO is initially empty.
33 static inline void fifo32_create(Fifo32
*fifo
, uint32_t capacity
)
35 fifo8_create(&fifo
->fifo
, capacity
* sizeof(uint32_t));
40 * @fifo: FIFO to cleanup
42 * Cleanup a FIFO created with fifo32_create(). Frees memory created for FIFO
43 * storage. The FIFO is no longer usable after this has been called.
46 static inline void fifo32_destroy(Fifo32
*fifo
)
48 fifo8_destroy(&fifo
->fifo
);
53 * @fifo: FIFO to check
55 * Return the number of free uint32_t slots in the FIFO.
57 * Returns: Number of free 32 bit words.
60 static inline uint32_t fifo32_num_free(Fifo32
*fifo
)
62 return DIV_ROUND_UP(fifo8_num_free(&fifo
->fifo
), sizeof(uint32_t));
67 * @fifo: FIFO to check
69 * Return the number of used uint32_t slots in the FIFO.
71 * Returns: Number of used 32 bit words.
74 static inline uint32_t fifo32_num_used(Fifo32
*fifo
)
76 return DIV_ROUND_UP(fifo8_num_used(&fifo
->fifo
), sizeof(uint32_t));
81 * @fifo: FIFO to push to
82 * @data: 32 bits data word to push
84 * Push a 32 bits data word to the FIFO. Behaviour is undefined if the FIFO
85 * is full. Clients are responsible for checking for fullness using
89 static inline void fifo32_push(Fifo32
*fifo
, uint32_t data
)
93 for (i
= 0; i
< sizeof(data
); i
++) {
94 fifo8_push(&fifo
->fifo
, data
& 0xff);
101 * @fifo: FIFO to push to
102 * @data: data to push
103 * @size: number of 32 bit words to push
105 * Push a 32 bit word array to the FIFO. Behaviour is undefined if the FIFO
106 * is full. Clients are responsible for checking the space left in the FIFO
107 * using fifo32_num_free().
110 static inline void fifo32_push_all(Fifo32
*fifo
, const uint32_t *data
,
115 for (i
= 0; i
< num
; i
++) {
116 fifo32_push(fifo
, data
[i
]);
122 * @fifo: fifo to pop from
124 * Pop a 32 bits data word from the FIFO. Behaviour is undefined if the FIFO
125 * is empty. Clients are responsible for checking for emptiness using
128 * Returns: The popped 32 bits data word.
131 static inline uint32_t fifo32_pop(Fifo32
*fifo
)
136 for (i
= 0; i
< sizeof(uint32_t); i
++) {
137 ret
|= (fifo8_pop(&fifo
->fifo
) << (i
* 8));
144 * There is no fifo32_pop_buf() because the data is not stored in the buffer
145 * as a set of native-order words.
150 * @fifo: FIFO to reset
152 * Reset a FIFO. All data is discarded and the FIFO is emptied.
155 static inline void fifo32_reset(Fifo32
*fifo
)
157 fifo8_reset(&fifo
->fifo
);
162 * @fifo: FIFO to check
164 * Check if a FIFO is empty.
166 * Returns: True if the fifo is empty, false otherwise.
169 static inline bool fifo32_is_empty(Fifo32
*fifo
)
171 return fifo8_is_empty(&fifo
->fifo
);
176 * @fifo: FIFO to check
178 * Check if a FIFO is full.
180 * Returns: True if the fifo is full, false otherwise.
183 static inline bool fifo32_is_full(Fifo32
*fifo
)
185 return fifo8_num_free(&fifo
->fifo
) < sizeof(uint32_t);
188 #define VMSTATE_FIFO32(_field, _state) VMSTATE_FIFO8(_field.fifo, _state)
190 #endif /* FIFO32_H */