Simple error checking
Jeffrey Pfau jeffrey@endrift.com
Sat, 06 Apr 2013 04:34:19 -0700
M
src/gba.c
→
src/gba.c
@@ -2,8 +2,14 @@ #include "gba.h"
#include <sys/mman.h> +static const char* GBA_CANNOT_MMAP = "Could not map memory"; + void GBAInit(struct GBA* gba) { + gba->errno = GBA_NO_ERROR; + gba->errstr = 0; + ARMInit(&gba->cpu); + gba->memory.p = gba; GBAMemoryInit(&gba->memory); }@@ -26,6 +32,12 @@ memory->bios = 0;
memory->wram = mmap(0, SIZE_WORKING_RAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); memory->iwram = mmap(0, SIZE_WORKING_IRAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); memory->rom = 0; + + if (!memory->wram || !memory->iwram) { + GBAMemoryDeinit(memory); + memory->p->errno = GBA_OUT_OF_MEMORY; + memory->p->errstr = GBA_CANNOT_MMAP; + } } void GBAMemoryDeinit(struct GBAMemory* memory) {
M
src/gba.h
→
src/gba.h
@@ -3,6 +3,11 @@ #define GBA_MEMORY_H
#include "arm.h" +enum GBAError { + GBA_NO_ERROR = 0, + GBA_OUT_OF_MEMORY = -1 +}; + enum GBAMemoryRegion { REGION_BIOS = 0x0, REGION_WORKING_RAM = 0x2,@@ -59,6 +64,9 @@ struct GBA {
struct ARMCore cpu; struct GBABoard board; struct GBAMemory memory; + + enum GBAError errno; + const char* errstr; }; void GBAInit(struct GBA* gba);