all repos — mgba @ dd1f1bc79e71700a279acd4c43effb235e6aa6ce

mGBA Game Boy Advance Emulator

Initial support for Windows
Jeffrey Pfau jeffrey@endrift.com
Wed, 15 Jan 2014 00:24:06 -0800
commit

dd1f1bc79e71700a279acd4c43effb235e6aa6ce

parent

003db6019c1f901060980aa7016be141b46dade6

M CMakeLists.txtCMakeLists.txt

@@ -47,5 +47,5 @@ set(MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/perf-main.c)

set(EXTRA_LIB ${EXTRA_LIB} rt) endif() -add_executable(${BINARY_NAME} ${ARM_SRC} ${GBA_SRC} ${DEBUGGER_SRC} ${RENDERER_SRC} ${UTIL_SRC} ${PLATFORM_SRC} ${MAIN_SRC}) +add_executable(${BINARY_NAME} WIN32 ${ARM_SRC} ${GBA_SRC} ${DEBUGGER_SRC} ${RENDERER_SRC} ${UTIL_SRC} ${PLATFORM_SRC} ${MAIN_SRC}) target_link_libraries(${BINARY_NAME} m pthread ${DEBUGGER_LIB} ${SDL_LIBRARY} ${OPENGL_LIBRARY} ${EXTRA_LIB})
M src/gba/gba-gpio.csrc/gba/gba-gpio.c

@@ -229,7 +229,11 @@

void _rtcUpdateClock(struct GBACartridgeGPIO* gpio) { time_t t = time(0); struct tm date; +#ifdef _WIN32 + date = *localtime(&t); +#else localtime_r(&t, &date); +#endif gpio->rtc.time[0] = _rtcBCD(date.tm_year - 100); gpio->rtc.time[1] = _rtcBCD(date.tm_mon + 1); gpio->rtc.time[2] = _rtcBCD(date.tm_mday);
M src/gba/gba-memory.csrc/gba/gba-memory.c

@@ -3,10 +3,10 @@

#include "gba-gpio.h" #include "gba-io.h" #include "hle-bios.h" +#include "memory.h" #include <limits.h> #include <string.h> -#include <sys/mman.h> static void GBASetActiveRegion(struct ARMMemory* memory, uint32_t region); static int GBAWaitMultiple(struct ARMMemory* memory, uint32_t startAddress, int count);

@@ -31,8 +31,8 @@ memory->d.store8 = GBAStore8;

memory->bios = (uint32_t*) hleBios; memory->fullBios = 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->wram = anonymousMemoryMap(SIZE_WORKING_RAM); + memory->iwram = anonymousMemoryMap(SIZE_WORKING_IRAM); memory->rom = 0; memory->gpio.p = memory->p; memset(memory->io, 0, sizeof(memory->io));

@@ -73,8 +73,8 @@ memory->d.waitMultiple = GBAWaitMultiple;

} void GBAMemoryDeinit(struct GBAMemory* memory) { - munmap(memory->wram, SIZE_WORKING_RAM); - munmap(memory->iwram, SIZE_WORKING_IRAM); + mappedMemoryFree(memory->wram, SIZE_WORKING_RAM); + mappedMemoryFree(memory->iwram, SIZE_WORKING_IRAM); GBASavedataDeinit(&memory->savedata); }
M src/gba/gba-savedata.csrc/gba/gba-savedata.c

@@ -1,11 +1,11 @@

#include "gba-savedata.h" #include "gba.h" +#include "memory.h" #include <errno.h> #include <fcntl.h> #include <string.h> -#include <sys/mman.h> #include <unistd.h> static void _flashSwitchBank(struct GBASavedata* savedata, int bank);

@@ -32,16 +32,16 @@

void GBASavedataDeinit(struct GBASavedata* savedata) { switch (savedata->type) { case SAVEDATA_SRAM: - munmap(savedata->data, SIZE_CART_SRAM); + mappedMemoryFree(savedata->data, SIZE_CART_SRAM); break; case SAVEDATA_FLASH512: - munmap(savedata->data, SIZE_CART_FLASH512); + mappedMemoryFree(savedata->data, SIZE_CART_FLASH512); break; case SAVEDATA_FLASH1M: - munmap(savedata->data, SIZE_CART_FLASH1M); + mappedMemoryFree(savedata->data, SIZE_CART_FLASH1M); break; case SAVEDATA_EEPROM: - munmap(savedata->data, SIZE_CART_EEPROM); + mappedMemoryFree(savedata->data, SIZE_CART_EEPROM); break; default: break;

@@ -62,19 +62,18 @@ return;

} savedata->fd = open(savedata->filename, O_RDWR | O_CREAT, 0666); off_t end; - int flags = MAP_SHARED; if (savedata->fd < 0) { GBALog(0, GBA_LOG_ERROR, "Cannot open savedata file %s (errno: %d)", savedata->filename, errno); end = 0; - flags |= MAP_ANON; + savedata->data = anonymousMemoryMap(SIZE_CART_FLASH1M); } else { end = lseek(savedata->fd, 0, SEEK_END); if (end < SIZE_CART_FLASH512) { ftruncate(savedata->fd, SIZE_CART_FLASH1M); } + savedata->data = fileMemoryMap(savedata->fd, SIZE_CART_FLASH1M, MEMORY_WRITE); } - // mmap enough so that we can expand the file if we need to - savedata->data = mmap(0, SIZE_CART_FLASH1M, PROT_READ | PROT_WRITE, flags, savedata->fd, 0); + savedata->currentBank = savedata->data; if (end < SIZE_CART_FLASH512) { memset(&savedata->data[end], 0xFF, SIZE_CART_FLASH512 - end);

@@ -90,18 +89,17 @@ return;

} savedata->fd = open(savedata->filename, O_RDWR | O_CREAT, 0666); off_t end; - int flags = MAP_SHARED; if (savedata->fd < 0) { GBALog(0, GBA_LOG_ERROR, "Cannot open savedata file %s (errno: %d)", savedata->filename, errno); end = 0; - flags |= MAP_ANON; + savedata->data = anonymousMemoryMap(SIZE_CART_EEPROM); } else { end = lseek(savedata->fd, 0, SEEK_END); if (end < SIZE_CART_EEPROM) { ftruncate(savedata->fd, SIZE_CART_EEPROM); } + savedata->data = fileMemoryMap(savedata->fd, SIZE_CART_EEPROM, MEMORY_WRITE); } - savedata->data = mmap(0, SIZE_CART_EEPROM, PROT_READ | PROT_WRITE, flags, savedata->fd, 0); if (end < SIZE_CART_EEPROM) { memset(&savedata->data[end], 0xFF, SIZE_CART_EEPROM - end); }

@@ -116,18 +114,18 @@ return;

} savedata->fd = open(savedata->filename, O_RDWR | O_CREAT, 0666); off_t end; - int flags = MAP_SHARED; if (savedata->fd < 0) { GBALog(0, GBA_LOG_ERROR, "Cannot open savedata file %s (errno: %d)", savedata->filename, errno); end = 0; - flags |= MAP_ANON; + savedata->data = anonymousMemoryMap(SIZE_CART_SRAM); } else { end = lseek(savedata->fd, 0, SEEK_END); if (end < SIZE_CART_SRAM) { ftruncate(savedata->fd, SIZE_CART_SRAM); } + savedata->data = fileMemoryMap(savedata->fd, SIZE_CART_SRAM, MEMORY_WRITE); } - savedata->data = mmap(0, SIZE_CART_SRAM, PROT_READ | PROT_WRITE, flags, savedata->fd, 0); + if (end < SIZE_CART_SRAM) { memset(&savedata->data[end], 0xFF, SIZE_CART_SRAM - end); }
M src/gba/gba-thread.csrc/gba/gba-thread.c

@@ -24,9 +24,11 @@ struct GBA gba;

struct GBAThread* threadContext = context; char* savedata = 0; +#ifndef _WIN32 sigset_t signals; sigfillset(&signals); pthread_sigmask(SIG_UNBLOCK, &signals, 0); +#endif GBAInit(&gba); threadContext->gba = &gba;
M src/gba/gba-video.csrc/gba/gba-video.c

@@ -3,10 +3,10 @@

#include "gba.h" #include "gba-io.h" #include "gba-thread.h" +#include "memory.h" #include <limits.h> #include <string.h> -#include <sys/mman.h> static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer); static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer);

@@ -44,12 +44,12 @@ video->nextHblankIRQ = 0;

video->nextVblankIRQ = 0; video->nextVcounterIRQ = 0; - video->vram = mmap(0, SIZE_VRAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + video->vram = anonymousMemoryMap(SIZE_VRAM); } void GBAVideoDeinit(struct GBAVideo* video) { GBAVideoAssociateRenderer(video, &dummyRenderer); - munmap(video->vram, SIZE_VRAM); + mappedMemoryFree(video->vram, SIZE_VRAM); } void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
M src/gba/gba.csrc/gba/gba.c

@@ -3,6 +3,7 @@

#include "gba-bios.h" #include "gba-io.h" #include "gba-thread.h" +#include "memory.h" #include "debugger.h"

@@ -11,7 +12,6 @@ #include <stdarg.h>

#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sys/mman.h> #include <sys/stat.h> const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;

@@ -345,7 +345,7 @@ #endif

void GBALoadROM(struct GBA* gba, int fd, const char* fname) { struct stat info; - gba->memory.rom = mmap(0, SIZE_CART0, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); + gba->memory.rom = fileMemoryMap(fd, SIZE_CART0, MEMORY_READ); gba->activeFile = fname; fstat(fd, &info); gba->memory.romSize = info.st_size;

@@ -358,7 +358,7 @@ // TODO: error check

} void GBALoadBIOS(struct GBA* gba, int fd) { - gba->memory.bios = mmap(0, SIZE_BIOS, PROT_READ, MAP_SHARED, fd, 0); + gba->memory.bios = fileMemoryMap(fd, SIZE_BIOS, MEMORY_READ); gba->memory.fullBios = 1; if ((gba->cpu.gprs[ARM_PC] >> BASE_OFFSET) == BASE_BIOS) { gba->memory.d.setActiveRegion(&gba->memory.d, gba->cpu.gprs[ARM_PC]);
M src/platform/sdl/gl-main.csrc/platform/sdl/gl-main.c

@@ -56,11 +56,13 @@ if (fd < 0) {

return 1; } +#ifndef _WIN32 sigset_t signals; sigemptyset(&signals); sigaddset(&signals, SIGINT); sigaddset(&signals, SIGTRAP); pthread_sigmask(SIG_BLOCK, &signals, 0); +#endif struct GBAThread context; struct GLSoftwareRenderer renderer;

@@ -118,8 +120,10 @@ glBindTexture(GL_TEXTURE_2D, renderer->tex);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); +#ifndef _WIN32 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); +#endif glViewport(0, 0, 240, 160);
A src/util/memory.c

@@ -0,0 +1,49 @@

+#include "memory.h" + +#ifdef _WIN32 +#include <io.h> +#include <Windows.h> + +void* anonymousMemoryMap(size_t size) { + HANDLE hMap = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size & 0xFFFFFFFF, 0); + return MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, size); +} + +void* fileMemoryMap(int fd, size_t size, int flags) { + int createFlags = PAGE_READONLY; + int mapFiles = FILE_MAP_READ; + if (flags & MEMORY_WRITE) { + createFlags = PAGE_READWRITE; + mapFiles = FILE_MAP_WRITE; + } + size_t location = lseek(fd, 0, SEEK_CUR); + size_t fileSize = lseek(fd, 0, SEEK_END); + lseek(fd, location, SEEK_SET); + if (size > fileSize) { + size = fileSize; + } + HANDLE hMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, createFlags, 0, size & 0xFFFFFFFF, 0); + return MapViewOfFile(hMap, mapFiles, 0, 0, size); +} + +void mappedMemoryFree(void* memory, size_t size) { + // TODO fill in +} +#else +#include <sys/mman.h> + +void* anonymousMemoryMap(size_t size) { + return mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); +} +void* fileMemoryMap(int fd, size_t size, int flags) { + int mmapFlags = MAP_PRIVATE; + if (flags & MEMORY_WRITE) { + mmapFlags = MAP_SHARED; + } + return mmap(0, size, PROT_READ | PROT_WRITE, mmapFlags, fd, 0); +} + +void mappedMemoryFree(void* memory, size_t size) { + munmap(memory, memory); +} +#endif
A src/util/memory.h

@@ -0,0 +1,13 @@

+#ifndef MEMORY_H +#define MEMORY_H + +#include <unistd.h> + +#define MEMORY_READ 1 +#define MEMORY_WRITE 2 + +void* anonymousMemoryMap(size_t size); +void* fileMemoryMap(int fd, size_t size, int flags); +void mappedMemoryFree(void* memory, size_t size); + +#endif