End of day: working on vueRead()/VUE.read()

This commit is contained in:
2020-08-03 20:34:02 -05:00
parent 030fc96fc7
commit cc25345960
8 changed files with 307 additions and 40 deletions
+47
View File
@@ -0,0 +1,47 @@
/* This file is included through vue.c and cannot be built directly. */
#ifdef VUEAPI
/* Read a value directly from host memory */
static int32_t busReadMemory(uint8_t *data, int type) {
/* Host memory is little-endian */
#ifndef VUE_BIGENDIAN
switch (type) {
case VUE_S8 : return *(int8_t *)data;
case VUE_U8 : return *(uint8_t *)data;
case VUE_S16: return *(int16_t *)data;
case VUE_U16: return *(uint16_t *)data;
case VUE_S32: return *(int32_t *)data;
}
/* Host memory is big-endian */
#else
switch (type) {
case VUE_S8 : return (int8_t ) data[0];
case VUE_U8 : return (uint8_t ) data[0];
case VUE_S16: return (int16_t ) data[0] << 8 | data[1];
case VUE_U16: return (uint16_t) data[0] << 8 | data[1];
case VUE_S32: return (int32_t )
data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
}
#endif
return 0; /* Unreachable, but the compiler doesn't know that */
}
/* Perform a read operation */
static int32_t busReadValue(VUE *vue, uint32_t address, int type, vbool debug){
address &= ~TYPE_SIZES[type] + 1;
switch (address >> 24 & 7) {
case 3: /* Unmapped */
case 4: return 0; /* Cartridge expansion */
case 5: return busReadMemory(&vue->wram[address & 8191], type);
case 6: return vue->sram == NULL ? 0 :
busReadMemory(&vue->sram[address & (vue->sram_size - 1)], type);
case 7: return vue->rom == NULL ? 0 :
busReadMemory(&vue->rom [address & (vue->rom_size - 1)], type);
}
return debug * 0; /* Unreachable */
}
#endif /* VUEAPI */
+45
View File
@@ -15,6 +15,51 @@ extern "C" {
/*****************************************************************************
* Constants *
*****************************************************************************/
/* Boolean values */
#define VUE_FALSE 0
#define VUE_TRUE 1
/* Memory access types */
#define VUE_S8 0
#define VUE_U8 1
#define VUE_S16 2
#define VUE_U16 3
#define VUE_S32 4
/*****************************************************************************
* Types *
*****************************************************************************/
/* Boolean */
typedef int vbool;
/* Emulation state */
typedef struct {
uint8_t *rom; /* Cartridge ROM */
uint32_t rom_size; /* Number of bytes in cartridge ROM */
uint8_t *sram; /* Cartridge RAM */
uint32_t sram_size; /* Number of bytes in cartridge RAM */
uint8_t wram[0x10000]; /* System memory */
} VUE;
/*****************************************************************************
* Function Prototypes *
*****************************************************************************/
VUEAPI void vueInitialize(VUE *vue);
VUEAPI vbool vueRead (VUE *vue, uint32_t address, uint8_t *data, uint32_t length);
VUEAPI vbool vueSetROM (VUE *vue, uint8_t *rom, uint32_t size);
#ifdef __cplusplus
}
#endif
+74
View File
@@ -1,2 +1,76 @@
#define VUEAPI
#include <vue.h>
/*****************************************************************************
* Constants *
*****************************************************************************/
/* Memory access type sizes */
static const int TYPE_SIZES[] = { 1, 1, 2, 2, 4 };
/*****************************************************************************
* Component Includes *
*****************************************************************************/
#include "bus.c"
/*****************************************************************************
* Library Functions *
*****************************************************************************/
/* Prepare an emulation state context for use */
void vueInitialize(VUE *vue) {
vue->rom = NULL;
vue->sram = NULL;
}
/* Perform a read operation */
vbool vueRead(VUE *vue, uint32_t address, uint8_t *data, uint32_t length) {
uint32_t count; /* Bytes to read in one iteration */
/* Error checking */
if (
vue == NULL ||
data == NULL ||
length > 0x01000000
) return VUE_FALSE;
/* Perform the operation */
for (; length > 0; address += count, length -= count, data += count) {
/* Determine the maximum number of bytes to process at once */
count = 0x01000000 - (address & 0x00FFFFFF);
if (count > length)
count = length;
/* Process by component */
switch (address >> 24 & 7) {
/* case 7: ROM, etc */
}
};
return VUE_TRUE;
}
/* Specify a new ROM buffer */
vbool vueSetROM(VUE *vue, uint8_t *rom, uint32_t size) {
/* Error checking */
if (
vue == NULL ||
rom == NULL ||
size < 1024 || size > 0x01000000 ||
(size & (size - 1)) != 0
) return VUE_FALSE;
/* Accept the new ROM buffer */
vue->rom = rom;
vue->rom_size = size;
return VUE_TRUE;
}