Skip to content

Instantly share code, notes, and snippets.

@ITotalJustice
Last active September 30, 2024 14:44
Show Gist options
  • Save ITotalJustice/56704bef37a7bf7626dceb014c0423ec to your computer and use it in GitHub Desktop.
Save ITotalJustice/56704bef37a7bf7626dceb014c0423ec to your computer and use it in GitHub Desktop.
ring buffer from gba fifo
struct RingBuf
{
uint32_t buf[8];
uint32_t r_index;
uint32_t w_index;
};
static void ringbuf_reset(struct RingBuf* ring)
{
ring->r_index = ring->w_index;
}
static uint32_t ringbuf_capacity(const struct RingBuf* ring)
{
return sizeof(ring->buf) / sizeof(ring->buf[0]);
}
static unsigned ringbuf_size(const struct RingBuf* ring)
{
return (ring->w_index - ring->r_index) % (ringbuf_capacity(ring) * 2U);
}
static unsigned ringbuf_free(const struct RingBuf* ring)
{
return ringbuf_capacity(ring) - ringbuf_size(ring);
}
static uint32_t ringbuf_read(const struct RingBuf* ring)
{
return ring->buf[ring->r_index % ringbuf_capacity(ring)];
}
static void ringbuf_push(struct RingBuf* ring, uint32_t value)
{
ring->buf[ring->w_index % ringbuf_capacity(ring)] = value;
ring->w_index = (ring->w_index + 1U) % (ringbuf_capacity(ring) * 2U);
}
static uint32_t ringbuf_pop(struct RingBuf* ring)
{
const uint32_t value = ring->buf[ring->r_index % ringbuf_capacity(ring)];
ring->r_index = (ring->r_index + 1U) % (ringbuf_capacity(ring) * 2U);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment