GBA ROM loading

This commit is contained in:
Jeffrey Pfau 2013-04-07 01:46:28 -07:00
parent 340d3ce6a7
commit b23f1ee3e3
2 changed files with 133 additions and 108 deletions

221
src/gba.c
View File

@ -1,6 +1,7 @@
#include "gba.h" #include "gba.h"
#include <sys/mman.h> #include <sys/mman.h>
#include <unistd.h>
static const char* GBA_CANNOT_MMAP = "Could not map memory"; static const char* GBA_CANNOT_MMAP = "Could not map memory";
@ -12,6 +13,7 @@ void GBAInit(struct GBA* gba) {
gba->memory.p = gba; gba->memory.p = gba;
GBAMemoryInit(&gba->memory); GBAMemoryInit(&gba->memory);
ARMAssociateMemory(&gba->cpu, &gba->memory.d);
} }
void GBADeinit(struct GBA* gba) { void GBADeinit(struct GBA* gba) {
@ -45,32 +47,37 @@ void GBAMemoryDeinit(struct GBAMemory* memory) {
munmap(memory->iwram, SIZE_WORKING_IRAM); 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) { int32_t GBALoad32(struct ARMMemory* memory, uint32_t address) {
struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
switch (address & OFFSET_MASK) { switch (address & ~OFFSET_MASK) {
case REGION_BIOS: case BASE_BIOS:
break; break;
case REGION_WORKING_RAM: case BASE_WORKING_RAM:
break; break;
case REGION_WORKING_IRAM: case BASE_WORKING_IRAM:
break; break;
case REGION_IO: case BASE_IO:
break; break;
case REGION_PALETTE_RAM: case BASE_PALETTE_RAM:
break; break;
case REGION_VRAM: case BASE_VRAM:
break; break;
case REGION_OAM: case BASE_OAM:
break; break;
case REGION_CART0: case BASE_CART0:
case REGION_CART0_EX: case BASE_CART0_EX:
case REGION_CART1: case BASE_CART1:
case REGION_CART1_EX: case BASE_CART1_EX:
case REGION_CART2: case BASE_CART2:
case REGION_CART2_EX: case BASE_CART2_EX:
break; return gbaMemory->rom[(address & (SIZE_CART0 - 1)) >> 2];
case REGION_CART_SRAM: case BASE_CART_SRAM:
break; break;
default: default:
break; break;
@ -82,29 +89,29 @@ int32_t GBALoad32(struct ARMMemory* memory, uint32_t address) {
int16_t GBALoad16(struct ARMMemory* memory, uint32_t address) { int16_t GBALoad16(struct ARMMemory* memory, uint32_t address) {
struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
switch (address & OFFSET_MASK) { switch (address & ~OFFSET_MASK) {
case REGION_BIOS: case BASE_BIOS:
break; break;
case REGION_WORKING_RAM: case BASE_WORKING_RAM:
break; break;
case REGION_WORKING_IRAM: case BASE_WORKING_IRAM:
break; break;
case REGION_IO: case BASE_IO:
break; break;
case REGION_PALETTE_RAM: case BASE_PALETTE_RAM:
break; break;
case REGION_VRAM: case BASE_VRAM:
break; break;
case REGION_OAM: case BASE_OAM:
break; break;
case REGION_CART0: case BASE_CART0:
case REGION_CART0_EX: case BASE_CART0_EX:
case REGION_CART1: case BASE_CART1:
case REGION_CART1_EX: case BASE_CART1_EX:
case REGION_CART2: case BASE_CART2:
case REGION_CART2_EX: case BASE_CART2_EX:
break; break;
case REGION_CART_SRAM: case BASE_CART_SRAM:
break; break;
default: default:
break; break;
@ -116,29 +123,29 @@ int16_t GBALoad16(struct ARMMemory* memory, uint32_t address) {
uint16_t GBALoadU16(struct ARMMemory* memory, uint32_t address) { uint16_t GBALoadU16(struct ARMMemory* memory, uint32_t address) {
struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
switch (address & OFFSET_MASK) { switch (address & ~OFFSET_MASK) {
case REGION_BIOS: case BASE_BIOS:
break; break;
case REGION_WORKING_RAM: case BASE_WORKING_RAM:
break; break;
case REGION_WORKING_IRAM: case BASE_WORKING_IRAM:
break; break;
case REGION_IO: case BASE_IO:
break; break;
case REGION_PALETTE_RAM: case BASE_PALETTE_RAM:
break; break;
case REGION_VRAM: case BASE_VRAM:
break; break;
case REGION_OAM: case BASE_OAM:
break; break;
case REGION_CART0: case BASE_CART0:
case REGION_CART0_EX: case BASE_CART0_EX:
case REGION_CART1: case BASE_CART1:
case REGION_CART1_EX: case BASE_CART1_EX:
case REGION_CART2: case BASE_CART2:
case REGION_CART2_EX: case BASE_CART2_EX:
break; break;
case REGION_CART_SRAM: case BASE_CART_SRAM:
break; break;
default: default:
break; break;
@ -150,29 +157,29 @@ uint16_t GBALoadU16(struct ARMMemory* memory, uint32_t address) {
int8_t GBALoad8(struct ARMMemory* memory, uint32_t address) { int8_t GBALoad8(struct ARMMemory* memory, uint32_t address) {
struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
switch (address & OFFSET_MASK) { switch (address & ~OFFSET_MASK) {
case REGION_BIOS: case BASE_BIOS:
break; break;
case REGION_WORKING_RAM: case BASE_WORKING_RAM:
break; break;
case REGION_WORKING_IRAM: case BASE_WORKING_IRAM:
break; break;
case REGION_IO: case BASE_IO:
break; break;
case REGION_PALETTE_RAM: case BASE_PALETTE_RAM:
break; break;
case REGION_VRAM: case BASE_VRAM:
break; break;
case REGION_OAM: case BASE_OAM:
break; break;
case REGION_CART0: case BASE_CART0:
case REGION_CART0_EX: case BASE_CART0_EX:
case REGION_CART1: case BASE_CART1:
case REGION_CART1_EX: case BASE_CART1_EX:
case REGION_CART2: case BASE_CART2:
case REGION_CART2_EX: case BASE_CART2_EX:
break; break;
case REGION_CART_SRAM: case BASE_CART_SRAM:
break; break;
default: default:
break; break;
@ -184,29 +191,29 @@ int8_t GBALoad8(struct ARMMemory* memory, uint32_t address) {
uint8_t GBALoadU8(struct ARMMemory* memory, uint32_t address) { uint8_t GBALoadU8(struct ARMMemory* memory, uint32_t address) {
struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
switch (address & OFFSET_MASK) { switch (address & ~OFFSET_MASK) {
case REGION_BIOS: case BASE_BIOS:
break; break;
case REGION_WORKING_RAM: case BASE_WORKING_RAM:
break; break;
case REGION_WORKING_IRAM: case BASE_WORKING_IRAM:
break; break;
case REGION_IO: case BASE_IO:
break; break;
case REGION_PALETTE_RAM: case BASE_PALETTE_RAM:
break; break;
case REGION_VRAM: case BASE_VRAM:
break; break;
case REGION_OAM: case BASE_OAM:
break; break;
case REGION_CART0: case BASE_CART0:
case REGION_CART0_EX: case BASE_CART0_EX:
case REGION_CART1: case BASE_CART1:
case REGION_CART1_EX: case BASE_CART1_EX:
case REGION_CART2: case BASE_CART2:
case REGION_CART2_EX: case BASE_CART2_EX:
break; break;
case REGION_CART_SRAM: case BASE_CART_SRAM:
break; break;
default: default:
break; break;
@ -218,24 +225,24 @@ uint8_t GBALoadU8(struct ARMMemory* memory, uint32_t address) {
void GBAStore32(struct ARMMemory* memory, uint32_t address, int32_t value) { void GBAStore32(struct ARMMemory* memory, uint32_t address, int32_t value) {
struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
switch (address & OFFSET_MASK) { switch (address & ~OFFSET_MASK) {
case REGION_WORKING_RAM: case BASE_WORKING_RAM:
break; break;
case REGION_WORKING_IRAM: case BASE_WORKING_IRAM:
break; break;
case REGION_IO: case BASE_IO:
break; break;
case REGION_PALETTE_RAM: case BASE_PALETTE_RAM:
break; break;
case REGION_VRAM: case BASE_VRAM:
break; break;
case REGION_OAM: case BASE_OAM:
break; break;
case REGION_CART0: case BASE_CART0:
break; break;
case REGION_CART2_EX: case BASE_CART2_EX:
break; break;
case REGION_CART_SRAM: case BASE_CART_SRAM:
break; break;
default: default:
break; break;
@ -245,24 +252,24 @@ void GBAStore32(struct ARMMemory* memory, uint32_t address, int32_t value) {
void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value) { void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value) {
struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
switch (address & OFFSET_MASK) { switch (address & ~OFFSET_MASK) {
case REGION_WORKING_RAM: case BASE_WORKING_RAM:
break; break;
case REGION_WORKING_IRAM: case BASE_WORKING_IRAM:
break; break;
case REGION_IO: case BASE_IO:
break; break;
case REGION_PALETTE_RAM: case BASE_PALETTE_RAM:
break; break;
case REGION_VRAM: case BASE_VRAM:
break; break;
case REGION_OAM: case BASE_OAM:
break; break;
case REGION_CART0: case BASE_CART0:
break; break;
case REGION_CART2_EX: case BASE_CART2_EX:
break; break;
case REGION_CART_SRAM: case BASE_CART_SRAM:
break; break;
default: default:
break; break;
@ -272,26 +279,26 @@ void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value) {
void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value) { void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value) {
struct GBAMemory* gbaMemory = (struct GBAMemory*) memory; struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
switch (address & OFFSET_MASK) { switch (address & ~OFFSET_MASK) {
case REGION_WORKING_RAM: case BASE_WORKING_RAM:
break; break;
case REGION_WORKING_IRAM: case BASE_WORKING_IRAM:
break; break;
case REGION_IO: case BASE_IO:
break; break;
case REGION_PALETTE_RAM: case BASE_PALETTE_RAM:
break; break;
case REGION_VRAM: case BASE_VRAM:
break; break;
case REGION_OAM: case BASE_OAM:
break; break;
case REGION_CART0: case BASE_CART0:
break; break;
case REGION_CART2_EX: case BASE_CART2_EX:
break; break;
case REGION_CART_SRAM: case BASE_CART_SRAM:
break; break;
default: default:
break; break;
} }
} }

View File

@ -25,6 +25,22 @@ enum GBAMemoryRegion {
REGION_CART_SRAM = 0xE 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 { enum {
SIZE_BIOS = 0x00004000, SIZE_BIOS = 0x00004000,
SIZE_WORKING_RAM = 0x00040000, SIZE_WORKING_RAM = 0x00040000,
@ -75,6 +91,8 @@ void GBADeinit(struct GBA* gba);
void GBAMemoryInit(struct GBAMemory* memory); void GBAMemoryInit(struct GBAMemory* memory);
void GBAMemoryDeinit(struct GBAMemory* memory); void GBAMemoryDeinit(struct GBAMemory* memory);
void GBALoadROM(struct GBA* gba, int fd);
int32_t GBALoad32(struct ARMMemory* memory, uint32_t address); int32_t GBALoad32(struct ARMMemory* memory, uint32_t address);
int16_t GBALoad16(struct ARMMemory* memory, uint32_t address); int16_t GBALoad16(struct ARMMemory* memory, uint32_t address);
uint16_t GBALoadU16(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 GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value);
void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value); void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value);
#endif #endif