From dd1f1bc79e71700a279acd4c43effb235e6aa6ce Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Wed, 15 Jan 2014 00:24:06 -0800 Subject: [PATCH] Initial support for Windows --- CMakeLists.txt | 2 +- src/gba/gba-gpio.c | 4 ++++ src/gba/gba-memory.c | 10 ++++---- src/gba/gba-savedata.c | 28 ++++++++++------------ src/gba/gba-thread.c | 2 ++ src/gba/gba-video.c | 6 ++--- src/gba/gba.c | 6 ++--- src/platform/sdl/gl-main.c | 4 ++++ src/util/memory.c | 49 ++++++++++++++++++++++++++++++++++++++ src/util/memory.h | 13 ++++++++++ 10 files changed, 97 insertions(+), 27 deletions(-) create mode 100644 src/util/memory.c create mode 100644 src/util/memory.h diff --git a/CMakeLists.txt b/CMakeLists.txt index aa558a47f..5a0cbe77a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,5 +47,5 @@ if(BUILD_PERF) 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}) diff --git a/src/gba/gba-gpio.c b/src/gba/gba-gpio.c index 68f28019b..1831020fc 100644 --- a/src/gba/gba-gpio.c +++ b/src/gba/gba-gpio.c @@ -229,7 +229,11 @@ unsigned _rtcOutput(struct GBACartridgeGPIO* gpio) { 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); diff --git a/src/gba/gba-memory.c b/src/gba/gba-memory.c index 8ea448607..8be4c2e0d 100644 --- a/src/gba/gba-memory.c +++ b/src/gba/gba-memory.c @@ -3,10 +3,10 @@ #include "gba-gpio.h" #include "gba-io.h" #include "hle-bios.h" +#include "memory.h" #include #include -#include static void GBASetActiveRegion(struct ARMMemory* memory, uint32_t region); static int GBAWaitMultiple(struct ARMMemory* memory, uint32_t startAddress, int count); @@ -31,8 +31,8 @@ void GBAMemoryInit(struct GBAMemory* memory) { 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 @@ void GBAMemoryInit(struct GBAMemory* memory) { } 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); } diff --git a/src/gba/gba-savedata.c b/src/gba/gba-savedata.c index b6d9036fc..495c67bb2 100644 --- a/src/gba/gba-savedata.c +++ b/src/gba/gba-savedata.c @@ -1,11 +1,11 @@ #include "gba-savedata.h" #include "gba.h" +#include "memory.h" #include #include #include -#include #include static void _flashSwitchBank(struct GBASavedata* savedata, int bank); @@ -32,16 +32,16 @@ void GBASavedataForceType(struct GBASavedata* savedata, enum SavedataType type) 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 @@ void GBASavedataInitFlash(struct GBASavedata* savedata) { } 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 @@ void GBASavedataInitEEPROM(struct GBASavedata* savedata) { } 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 @@ void GBASavedataInitSRAM(struct GBASavedata* savedata) { } 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); } diff --git a/src/gba/gba-thread.c b/src/gba/gba-thread.c index 0600283e0..752697d64 100644 --- a/src/gba/gba-thread.c +++ b/src/gba/gba-thread.c @@ -24,9 +24,11 @@ static void* _GBAThreadRun(void* context) { 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; diff --git a/src/gba/gba-video.c b/src/gba/gba-video.c index 172ec8ccc..b4036e0e3 100644 --- a/src/gba/gba-video.c +++ b/src/gba/gba-video.c @@ -3,10 +3,10 @@ #include "gba.h" #include "gba-io.h" #include "gba-thread.h" +#include "memory.h" #include #include -#include static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer); static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer); @@ -44,12 +44,12 @@ void GBAVideoInit(struct GBAVideo* video) { 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) { diff --git a/src/gba/gba.c b/src/gba/gba.c index df156cd16..b6f931d2d 100644 --- a/src/gba/gba.c +++ b/src/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 #include #include -#include #include const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000; @@ -345,7 +345,7 @@ void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) { 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 @@ void GBALoadROM(struct GBA* gba, int fd, const char* fname) { } 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]); diff --git a/src/platform/sdl/gl-main.c b/src/platform/sdl/gl-main.c index 217e0b611..3b0a1563b 100644 --- a/src/platform/sdl/gl-main.c +++ b/src/platform/sdl/gl-main.c @@ -56,11 +56,13 @@ int main(int argc, char** argv) { 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 @@ static int _GBASDLInit(struct GLSoftwareRenderer* renderer) { 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); diff --git a/src/util/memory.c b/src/util/memory.c new file mode 100644 index 000000000..23d4b42b8 --- /dev/null +++ b/src/util/memory.c @@ -0,0 +1,49 @@ +#include "memory.h" + +#ifdef _WIN32 +#include +#include + +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 + +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 \ No newline at end of file diff --git a/src/util/memory.h b/src/util/memory.h new file mode 100644 index 000000000..93394259e --- /dev/null +++ b/src/util/memory.h @@ -0,0 +1,13 @@ +#ifndef MEMORY_H +#define MEMORY_H + +#include + +#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 \ No newline at end of file