#include <string.h>
#include <stdio.h>
#include "cache.h"
#include "serial.h"
#include "sys_defs.h"

unsigned int cacheValues[CACHE_VALLEN] = { 0 };
unsigned char cacheIOTypes[CACHE_IOLEN] = { 0 };

void cacheSetEntry(unsigned char index, unsigned int value,
    unsigned char iotype)
{    
    unsigned int ioIndex = index / 8;
    unsigned int ioShift = 7 - (index - 8 * ioIndex);
    
    cacheValues[index] = value;
    
    switch (iotype)
    {
        case CACHE_IO_IN: case CACHE_IO_OUT:
            cacheIOTypes[ioIndex] ^= iotype << ioShift;
        break;
    }
}

void cacheGetEntry(unsigned char index, unsigned int* value,
    unsigned char* iotype)
{
    unsigned int ioIndex = index / 8;
    unsigned int ioShift = 7 - (index - 8 * ioIndex);
    
    if (value != NULL)
    {
        *value = cacheValues[index];
    }
    
    if (iotype != NULL)
    {
        *iotype = (cacheIOTypes[ioIndex] >> ioShift) & 1;
    }
}
