#include "al/cpu.h"

void init_cpu()
{

}

inline void cpu_mem_write32(uintptr_t addr, uint32_t data)
{
  volatile uint32_t *ptr = (uint32_t*)addr;
  *ptr = data;
}

inline uint32_t cpu_mem_read32(uintptr_t addr)
{
  volatile uint32_t *ptr = (uint32_t*)addr;

  return *ptr;
}

void cpu_dcache_invalidate(const void *buf, size_t size)
{
  size_t i;
  size_t dcache_line_size;

  // retrieve dcache line size from config register (bits 12:10)
  asm volatile("mfc0 %0, $16, 1" : "=r" (dcache_line_size));

  dcache_line_size = 2 << ((dcache_line_size>>10) & 0x7);

  // iterate on lines to invalidate each one of them
  for ( i=0; i<size; i+=dcache_line_size )
    asm volatile(" cache %0, %1"
        :
        :"i" (0x11), "R" (*((unsigned char*)buf+i))
        : "memory");
}
