GBA ROM loading
Jeffrey Pfau jeffrey@endrift.com
Sun, 07 Apr 2013 01:46:28 -0700
M
src/gba.c
→
src/gba.c
@@ -1,6 +1,7 @@
#include "gba.h" #include <sys/mman.h> +#include <unistd.h> static const char* GBA_CANNOT_MMAP = "Could not map memory";@@ -12,6 +13,7 @@ ARMInit(&gba->cpu);
gba->memory.p = gba; GBAMemoryInit(&gba->memory); + ARMAssociateMemory(&gba->cpu, &gba->memory.d); } void GBADeinit(struct GBA* gba) {@@ -45,32 +47,37 @@ munmap(memory->wram, SIZE_WORKING_RAM);
munmap(memory->iwram, SIZE_WORKING_IRAM); } +void GBALoadROM(struct GBA* gba, int fd) { + gba->memory.rom = mmap(0, SIZE_CART0, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0); + // TODO: error check +} + int32_t GBALoad32(struct ARMMemory* memory, uint32_t address) { struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; - switch (address & OFFSET_MASK) { - case REGION_BIOS: - break; - case REGION_WORKING_RAM: + switch (address & ~OFFSET_MASK) { + case BASE_BIOS: break; - case REGION_WORKING_IRAM: + case BASE_WORKING_RAM: break; - case REGION_IO: + case BASE_WORKING_IRAM: break; - case REGION_PALETTE_RAM: + case BASE_IO: break; - case REGION_VRAM: + case BASE_PALETTE_RAM: break; - case REGION_OAM: + case BASE_VRAM: break; - case REGION_CART0: - case REGION_CART0_EX: - case REGION_CART1: - case REGION_CART1_EX: - case REGION_CART2: - case REGION_CART2_EX: + case BASE_OAM: break; - case REGION_CART_SRAM: + case BASE_CART0: + case BASE_CART0_EX: + case BASE_CART1: + case BASE_CART1_EX: + case BASE_CART2: + case BASE_CART2_EX: + return gbaMemory->rom[(address & (SIZE_CART0 - 1)) >> 2]; + case BASE_CART_SRAM: break; default: break;@@ -82,29 +89,29 @@
int16_t GBALoad16(struct ARMMemory* memory, uint32_t address) { struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; - switch (address & OFFSET_MASK) { - case REGION_BIOS: + switch (address & ~OFFSET_MASK) { + case BASE_BIOS: break; - case REGION_WORKING_RAM: + case BASE_WORKING_RAM: break; - case REGION_WORKING_IRAM: + case BASE_WORKING_IRAM: break; - case REGION_IO: + case BASE_IO: break; - case REGION_PALETTE_RAM: + case BASE_PALETTE_RAM: break; - case REGION_VRAM: + case BASE_VRAM: break; - case REGION_OAM: + case BASE_OAM: break; - case REGION_CART0: - case REGION_CART0_EX: - case REGION_CART1: - case REGION_CART1_EX: - case REGION_CART2: - case REGION_CART2_EX: + case BASE_CART0: + case BASE_CART0_EX: + case BASE_CART1: + case BASE_CART1_EX: + case BASE_CART2: + case BASE_CART2_EX: break; - case REGION_CART_SRAM: + case BASE_CART_SRAM: break; default: break;@@ -116,29 +123,29 @@
uint16_t GBALoadU16(struct ARMMemory* memory, uint32_t address) { struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; - switch (address & OFFSET_MASK) { - case REGION_BIOS: + switch (address & ~OFFSET_MASK) { + case BASE_BIOS: break; - case REGION_WORKING_RAM: + case BASE_WORKING_RAM: break; - case REGION_WORKING_IRAM: + case BASE_WORKING_IRAM: break; - case REGION_IO: + case BASE_IO: break; - case REGION_PALETTE_RAM: + case BASE_PALETTE_RAM: break; - case REGION_VRAM: + case BASE_VRAM: break; - case REGION_OAM: + case BASE_OAM: break; - case REGION_CART0: - case REGION_CART0_EX: - case REGION_CART1: - case REGION_CART1_EX: - case REGION_CART2: - case REGION_CART2_EX: + case BASE_CART0: + case BASE_CART0_EX: + case BASE_CART1: + case BASE_CART1_EX: + case BASE_CART2: + case BASE_CART2_EX: break; - case REGION_CART_SRAM: + case BASE_CART_SRAM: break; default: break;@@ -150,29 +157,29 @@
int8_t GBALoad8(struct ARMMemory* memory, uint32_t address) { struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; - switch (address & OFFSET_MASK) { - case REGION_BIOS: + switch (address & ~OFFSET_MASK) { + case BASE_BIOS: break; - case REGION_WORKING_RAM: + case BASE_WORKING_RAM: break; - case REGION_WORKING_IRAM: + case BASE_WORKING_IRAM: break; - case REGION_IO: + case BASE_IO: break; - case REGION_PALETTE_RAM: + case BASE_PALETTE_RAM: break; - case REGION_VRAM: + case BASE_VRAM: break; - case REGION_OAM: + case BASE_OAM: break; - case REGION_CART0: - case REGION_CART0_EX: - case REGION_CART1: - case REGION_CART1_EX: - case REGION_CART2: - case REGION_CART2_EX: + case BASE_CART0: + case BASE_CART0_EX: + case BASE_CART1: + case BASE_CART1_EX: + case BASE_CART2: + case BASE_CART2_EX: break; - case REGION_CART_SRAM: + case BASE_CART_SRAM: break; default: break;@@ -184,29 +191,29 @@
uint8_t GBALoadU8(struct ARMMemory* memory, uint32_t address) { struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; - switch (address & OFFSET_MASK) { - case REGION_BIOS: + switch (address & ~OFFSET_MASK) { + case BASE_BIOS: break; - case REGION_WORKING_RAM: + case BASE_WORKING_RAM: break; - case REGION_WORKING_IRAM: + case BASE_WORKING_IRAM: break; - case REGION_IO: + case BASE_IO: break; - case REGION_PALETTE_RAM: + case BASE_PALETTE_RAM: break; - case REGION_VRAM: + case BASE_VRAM: break; - case REGION_OAM: + case BASE_OAM: break; - case REGION_CART0: - case REGION_CART0_EX: - case REGION_CART1: - case REGION_CART1_EX: - case REGION_CART2: - case REGION_CART2_EX: + case BASE_CART0: + case BASE_CART0_EX: + case BASE_CART1: + case BASE_CART1_EX: + case BASE_CART2: + case BASE_CART2_EX: break; - case REGION_CART_SRAM: + case BASE_CART_SRAM: break; default: break;@@ -218,24 +225,24 @@
void GBAStore32(struct ARMMemory* memory, uint32_t address, int32_t value) { struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; - switch (address & OFFSET_MASK) { - case REGION_WORKING_RAM: + switch (address & ~OFFSET_MASK) { + case BASE_WORKING_RAM: break; - case REGION_WORKING_IRAM: + case BASE_WORKING_IRAM: break; - case REGION_IO: + case BASE_IO: break; - case REGION_PALETTE_RAM: + case BASE_PALETTE_RAM: break; - case REGION_VRAM: + case BASE_VRAM: break; - case REGION_OAM: + case BASE_OAM: break; - case REGION_CART0: + case BASE_CART0: break; - case REGION_CART2_EX: + case BASE_CART2_EX: break; - case REGION_CART_SRAM: + case BASE_CART_SRAM: break; default: break;@@ -245,24 +252,24 @@
void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value) { struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; - switch (address & OFFSET_MASK) { - case REGION_WORKING_RAM: + switch (address & ~OFFSET_MASK) { + case BASE_WORKING_RAM: break; - case REGION_WORKING_IRAM: + case BASE_WORKING_IRAM: break; - case REGION_IO: + case BASE_IO: break; - case REGION_PALETTE_RAM: + case BASE_PALETTE_RAM: break; - case REGION_VRAM: + case BASE_VRAM: break; - case REGION_OAM: + case BASE_OAM: break; - case REGION_CART0: + case BASE_CART0: break; - case REGION_CART2_EX: + case BASE_CART2_EX: break; - case REGION_CART_SRAM: + case BASE_CART_SRAM: break; default: break;@@ -272,26 +279,26 @@
void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value) { struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; - switch (address & OFFSET_MASK) { - case REGION_WORKING_RAM: + switch (address & ~OFFSET_MASK) { + case BASE_WORKING_RAM: break; - case REGION_WORKING_IRAM: + case BASE_WORKING_IRAM: break; - case REGION_IO: + case BASE_IO: break; - case REGION_PALETTE_RAM: + case BASE_PALETTE_RAM: break; - case REGION_VRAM: + case BASE_VRAM: break; - case REGION_OAM: + case BASE_OAM: break; - case REGION_CART0: + case BASE_CART0: break; - case REGION_CART2_EX: + case BASE_CART2_EX: break; - case REGION_CART_SRAM: + case BASE_CART_SRAM: break; default: break; } -}+}
M
src/gba.h
→
src/gba.h
@@ -25,6 +25,22 @@ REGION_CART2_EX = 0xD,
REGION_CART_SRAM = 0xE }; +enum GBAMemoryBase { + BASE_BIOS = 0x00000000, + BASE_WORKING_RAM = 0x02000000, + BASE_WORKING_IRAM = 0x03000000, + BASE_IO = 0x04000000, + BASE_PALETTE_RAM = 0x05000000, + BASE_VRAM = 0x06000000, + BASE_OAM = 0x07000000, + BASE_CART0 = 0x08000000, + BASE_CART0_EX = 0x09000000, + BASE_CART1 = 0x0A000000, + BASE_CART1_EX = 0x0B000000, + BASE_CART2 = 0x0C000000, + BASE_CART2_EX = 0x0D000000, + BASE_CART_SRAM = 0x0E000000 +}; enum { SIZE_BIOS = 0x00004000, SIZE_WORKING_RAM = 0x00040000,@@ -75,6 +91,8 @@
void GBAMemoryInit(struct GBAMemory* memory); void GBAMemoryDeinit(struct GBAMemory* memory); +void GBALoadROM(struct GBA* gba, int fd); + int32_t GBALoad32(struct ARMMemory* memory, uint32_t address); int16_t GBALoad16(struct ARMMemory* memory, uint32_t address); uint16_t GBALoadU16(struct ARMMemory* memory, uint32_t address);@@ -85,4 +103,4 @@ void GBAStore32(struct ARMMemory* memory, uint32_t address, int32_t value);
void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value); void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value); -#endif+#endif