Initial support for Windows

This commit is contained in:
Jeffrey Pfau 2014-01-15 00:24:06 -08:00
parent 003db6019c
commit dd1f1bc79e
10 changed files with 97 additions and 27 deletions

View File

@ -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})

View File

@ -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);

View File

@ -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 @@ 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);
}

View File

@ -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 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);
}

View File

@ -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;

View File

@ -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 @@ 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) {

View File

@ -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 <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 @@ 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]);

View File

@ -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);

49
src/util/memory.c Normal file
View File

@ -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

13
src/util/memory.h Normal file
View File

@ -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