#ifndef UTIL_MMIO_H_
#define UTIL_MMIO_H_

#include <stdint.h>

static inline void mmiowrite8(void *p, uint8_t data) {
  *(volatile uint8_t *)(p) = data;
}

static inline uint8_t mmioread8(void *p) {
  return *(volatile uint8_t *)(p);
}

static inline void mmiowrite16(void *p, uint16_t data) {
  *(volatile uint16_t *)(p) = data;
}

static inline uint16_t mmioread16(void *p) {
  return *(volatile uint16_t *)(p);
}

static inline void mmiowrite32(void *p, uint32_t data) {
  *(volatile uint32_t *)(p) = data;
}

static inline uint32_t mmioread32(void *p) {
  return *(volatile uint32_t *)(p);
}

static inline void mmiowrite64(void *p, uint64_t data) {
  *(volatile uint64_t *)(p) = data;
}

static inline uint64_t mmioread64(void *p) {
  return *(volatile uint64_t *)(p);
}

static inline void mmioreadn(void *dst, const volatile void *src, size_t bytes) {
  volatile uint8_t *s = (volatile uint8_t *)src;
  uint8_t *d = (uint8_t *)dst;
  while (bytes > 0) {
      *d =  *s;
      ++s;
      ++d;
      --bytes;
  }
}

#endif // UTIL_MMIO_H_
