Skip to content

Commit

Permalink
Update to mGBA 0.8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
clobber committed Apr 7, 2020
1 parent 6593ecc commit 4fbc43b
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>0.8.0</string>
<string>0.8.1</string>
<key>NSPrincipalClass</key>
<string>OEGameCoreController</string>
<key>OEGameCoreClass</key>
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ bool mCoreLoadELF(struct mCore* core, struct ELF* elf) {
Elf32_Phdr* phdr = ELFProgramHeadersGetPointer(&ph, i);
void* block = mCoreGetMemoryBlock(core, phdr->p_paddr, &bsize);
char* bytes = ELFBytes(elf, &esize);
if (block && bsize >= phdr->p_filesz && esize >= phdr->p_filesz + phdr->p_offset) {
if (block && bsize >= phdr->p_filesz && bsize > phdr->p_offset && esize >= phdr->p_filesz + phdr->p_offset) {
memcpy(block, &bytes[phdr->p_offset], phdr->p_filesz);
} else {
return false;
Expand Down
8 changes: 6 additions & 2 deletions src/gb/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1078,9 +1078,13 @@ void GBAudioPSGDeserialize(struct GBAudio* audio, const struct GBSerializedPSGSt
LOAD_32LE(audio->ch4.lastEvent, 0, &state->ch4.lastEvent);
LOAD_32LE(when, 0, &state->ch4.nextEvent);
if (audio->ch4.envelope.dead < 2 && audio->playingCh4) {
if (when - audio->ch4.lastEvent > (uint32_t) audio->sampleInterval) {
if (!audio->ch4.lastEvent) {
// Back-compat: fake this value
audio->ch4.lastEvent = when - audio->sampleInterval;
uint32_t currentTime = mTimingCurrentTime(audio->timing);
int32_t cycles = audio->ch4.ratio ? 2 * audio->ch4.ratio : 1;
cycles <<= audio->ch4.frequency;
cycles *= 8 * audio->timingFactor;
audio->ch4.lastEvent = currentTime + (when & (cycles - 1)) - cycles;
}
mTimingSchedule(audio->timing, &audio->ch4Event, when);
}
Expand Down
6 changes: 6 additions & 0 deletions src/gb/debugger/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ static void _GBCLIDebuggerInit(struct CLIDebuggerSystem*);
static bool _GBCLIDebuggerCustom(struct CLIDebuggerSystem*);

static void _frame(struct CLIDebugger*, struct CLIDebugVector*);
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
static void _load(struct CLIDebugger*, struct CLIDebugVector*);
static void _save(struct CLIDebugger*, struct CLIDebugVector*);
#endif

struct CLIDebuggerCommandSummary _GBCLIDebuggerCommands[] = {
{ "frame", _frame, "", "Frame advance" },
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
{ "load", _load, "*", "Load a savestate" },
{ "save", _save, "*", "Save a savestate" },
#endif
{ 0, 0, 0, 0 }
};

Expand Down Expand Up @@ -73,6 +77,7 @@ static void _frame(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
gbDebugger->inVblank = GBRegisterSTATGetMode(((struct GB*) gbDebugger->core->board)->memory.io[REG_STAT]) == 1;
}

#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
static void _load(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
struct CLIDebuggerBackend* be = debugger->backend;
if (!dv || dv->type != CLIDV_INT_TYPE) {
Expand Down Expand Up @@ -106,3 +111,4 @@ static void _save(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {

mCoreSaveState(gbDebugger->core, dv->intValue, SAVESTATE_SCREENSHOT | SAVESTATE_RTC | SAVESTATE_METADATA);
}
#endif
4 changes: 2 additions & 2 deletions src/gba/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ void GBAAudioResizeBuffer(struct GBAAudio* audio, size_t samples) {
}

void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info) {
info->reg = GBADMARegisterSetDestControl(info->reg, GBA_DMA_FIXED);
info->reg = GBADMARegisterSetWidth(info->reg, 1);
switch (info->dest) {
case BASE_IO | REG_FIFO_A_LO:
audio->chA.dmaSource = number;
Expand All @@ -129,8 +131,6 @@ void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA*
audio->externalMixing = false;
}
}
info->reg = GBADMARegisterSetDestControl(info->reg, GBA_DMA_FIXED);
info->reg = GBADMARegisterSetWidth(info->reg, 1);
}

void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value) {
Expand Down
2 changes: 1 addition & 1 deletion src/gba/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ static void _GBACoreReset(struct mCore* core) {
#endif

ARMReset(core->cpu);
if (core->opts.skipBios && (gba->romVf || gba->memory.rom)) {
if ((core->opts.skipBios && (gba->romVf || gba->memory.rom)) || (gba->romVf && GBAIsMB(gba->romVf))) {
GBASkipBIOS(core->board);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/gba/debugger/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ static void _GBACLIDebuggerInit(struct CLIDebuggerSystem*);
static bool _GBACLIDebuggerCustom(struct CLIDebuggerSystem*);

static void _frame(struct CLIDebugger*, struct CLIDebugVector*);
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
static void _load(struct CLIDebugger*, struct CLIDebugVector*);
static void _save(struct CLIDebugger*, struct CLIDebugVector*);
#endif

struct CLIDebuggerCommandSummary _GBACLIDebuggerCommands[] = {
{ "frame", _frame, "", "Frame advance" },
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
{ "load", _load, "*", "Load a savestate" },
{ "save", _save, "*", "Save a savestate" },
#endif
{ 0, 0, 0, 0 }
};

Expand Down Expand Up @@ -72,6 +76,7 @@ static void _frame(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
gbaDebugger->inVblank = GBARegisterDISPSTATGetInVblank(((struct GBA*) gbaDebugger->core->board)->memory.io[REG_DISPSTAT >> 1]);
}

#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
static void _load(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
struct CLIDebuggerBackend* be = debugger->backend;
if (!dv || dv->type != CLIDV_INT_TYPE) {
Expand Down Expand Up @@ -107,3 +112,4 @@ static void _save(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {

mCoreSaveState(gbaDebugger->core, dv->intValue, SAVESTATE_SCREENSHOT | SAVESTATE_RTC | SAVESTATE_METADATA);
}
#endif
2 changes: 1 addition & 1 deletion src/gba/gba.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void GBASkipBIOS(struct GBA* gba) {
if (gba->memory.rom) {
cpu->gprs[ARM_PC] = BASE_CART0;
} else {
cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
cpu->gprs[ARM_PC] = BASE_WORKING_RAM + 0xC0;
}
gba->video.vcount = 0x7D;
gba->memory.io[REG_VCOUNT >> 1] = 0x7D;
Expand Down
7 changes: 4 additions & 3 deletions src/gba/hle-bios.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <mgba/internal/gba/memory.h>

const uint8_t hleBios[SIZE_BIOS] = {
0x06, 0x00, 0x00, 0xea, 0xfe, 0xff, 0xff, 0xea, 0x0b, 0x00, 0x00, 0xea,
0x06, 0x00, 0x00, 0xea, 0x88, 0x00, 0x00, 0xea, 0x0b, 0x00, 0x00, 0xea,
0xfe, 0xff, 0xff, 0xea, 0xfe, 0xff, 0xff, 0xea, 0x00, 0x00, 0xa0, 0xe1,
0x2c, 0x00, 0x00, 0xea, 0xfe, 0xff, 0xff, 0xea, 0x02, 0x03, 0xa0, 0xe3,
0x03, 0x10, 0xd0, 0xe5, 0xea, 0x00, 0x51, 0xe3, 0xf8, 0x01, 0x9f, 0x15,
0x03, 0x10, 0xd0, 0xe5, 0xea, 0x00, 0x51, 0xe3, 0x04, 0x02, 0x9f, 0x15,
0x10, 0xff, 0x2f, 0xe1, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x29, 0xe1,
0x00, 0x00, 0x5d, 0xe3, 0x01, 0xd3, 0xa0, 0x03, 0x20, 0xd0, 0x4d, 0x02,
0x00, 0x58, 0x2d, 0xe9, 0x02, 0xb0, 0x5e, 0xe5, 0x9c, 0xc0, 0xa0, 0xe3,
Expand Down Expand Up @@ -49,5 +49,6 @@ const uint8_t hleBios[SIZE_BIOS] = {
0x03, 0xa0, 0xa0, 0xe1, 0x02, 0x00, 0x51, 0xe1, 0xf8, 0x07, 0xa1, 0xb8,
0xfc, 0xff, 0xff, 0xba, 0x03, 0x00, 0x00, 0xea, 0x02, 0x00, 0x51, 0xe1,
0xf8, 0x07, 0xb0, 0xb8, 0xf8, 0x07, 0xa1, 0xb8, 0xfb, 0xff, 0xff, 0xba,
0xf0, 0x87, 0xbd, 0xe8, 0xc0, 0x00, 0x00, 0x02
0xf0, 0x87, 0xbd, 0xe8, 0x04, 0xf0, 0x5e, 0xe2, 0x00, 0x00, 0x00, 0x00,
0x04, 0xe0, 0xa0, 0x03, 0xc0, 0x00, 0x00, 0x02
};
5 changes: 5 additions & 0 deletions src/gba/hle-bios.s
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,9 @@ blt 0b
2:
ldmfd sp!, {r4-r10, pc}

undefBase:
subs pc, lr, #4
.word 0
.word 0x03A0E004

.ltorg
9 changes: 4 additions & 5 deletions src/gba/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ void GBAIOSerialize(struct GBA* gba, struct GBASerializedState* state) {
STORE_32(gba->memory.dma[i].when, 0, &state->dma[i].when);
}

state->dmaTransferRegister = gba->memory.dmaTransferRegister;
STORE_32(gba->memory.dmaTransferRegister, 0, &state->dmaTransferRegister);

GBAHardwareSerialize(&gba->memory.hw, state);
}
Expand Down Expand Up @@ -988,12 +988,11 @@ void GBAIODeserialize(struct GBA* gba, const struct GBASerializedState* state) {
LOAD_32(gba->memory.dma[i].nextDest, 0, &state->dma[i].nextDest);
LOAD_32(gba->memory.dma[i].nextCount, 0, &state->dma[i].nextCount);
LOAD_32(gba->memory.dma[i].when, 0, &state->dma[i].when);
if (GBADMARegisterGetTiming(gba->memory.dma[i].reg) != GBA_DMA_TIMING_NOW) {
GBADMASchedule(gba, i, &gba->memory.dma[i]);
}
}
GBAAudioWriteSOUNDCNT_X(&gba->audio, gba->memory.io[REG_SOUNDCNT_X >> 1]);
gba->memory.dmaTransferRegister = state->dmaTransferRegister;

LOAD_32(gba->memory.dmaTransferRegister, 0, &state->dmaTransferRegister);

GBADMAUpdate(gba);
GBAHardwareDeserialize(&gba->memory.hw, state);
}
20 changes: 12 additions & 8 deletions src/gba/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,12 +772,12 @@ uint32_t GBALoad8(struct ARMCore* cpu, uint32_t address, int* cycleCounter) {
#define STORE_SRAM \
if (address & 0x3) { \
mLOG(GBA_MEM, GAME_ERROR, "Unaligned SRAM Store32: 0x%08X", address); \
value = 0; \
} \
GBAStore8(cpu, address & ~0x3, value, cycleCounter); \
GBAStore8(cpu, (address & ~0x3) | 1, value, cycleCounter); \
GBAStore8(cpu, (address & ~0x3) | 2, value, cycleCounter); \
GBAStore8(cpu, (address & ~0x3) | 3, value, cycleCounter);
} else { \
GBAStore8(cpu, address, value, cycleCounter); \
GBAStore8(cpu, address | 1, value, cycleCounter); \
GBAStore8(cpu, address | 2, value, cycleCounter); \
GBAStore8(cpu, address | 3, value, cycleCounter); \
}

#define STORE_BAD \
mLOG(GBA_MEM, GAME_ERROR, "Bad memory Store32: 0x%08X", address);
Expand Down Expand Up @@ -923,8 +923,12 @@ void GBAStore16(struct ARMCore* cpu, uint32_t address, int16_t value, int* cycle
break;
case REGION_CART_SRAM:
case REGION_CART_SRAM_MIRROR:
GBAStore8(cpu, (address & ~0x1), value, cycleCounter);
GBAStore8(cpu, (address & ~0x1) | 1, value, cycleCounter);
if (address & 1) {
mLOG(GBA_MEM, GAME_ERROR, "Unaligned SRAM Store16: 0x%08X", address);
break;
}
GBAStore8(cpu, address, value, cycleCounter);
GBAStore8(cpu, address | 1, value, cycleCounter);
break;
default:
mLOG(GBA_MEM, GAME_ERROR, "Bad memory Store16: 0x%08X", address);
Expand Down
7 changes: 4 additions & 3 deletions src/gba/renderers/gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ void GBAVideoGLRendererDeinit(struct GBAVideoRenderer* renderer) {
void GBAVideoGLRendererReset(struct GBAVideoRenderer* renderer) {
struct GBAVideoGLRenderer* glRenderer = (struct GBAVideoGLRenderer*) renderer;

glRenderer->oamDirty = true;
glRenderer->paletteDirty = true;
glRenderer->vramDirty = 0xFFFFFF;
glRenderer->firstAffine = -1;
Expand Down Expand Up @@ -1727,13 +1728,13 @@ void GBAVideoGLRendererDrawSprite(struct GBAVideoGLRenderer* renderer, struct GB
glStencilFunc(GL_EQUAL, 1, 1);
glUseProgram(shader->program);
glDrawBuffers(2, (GLenum[]) { GL_NONE, GL_COLOR_ATTACHMENT1 });
glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
glBindVertexArray(shader->vao);
glUniform2i(uniforms[GBA_GL_VS_LOC], totalHeight, 0);
glUniform2i(uniforms[GBA_GL_VS_MAXPOS], totalWidth, totalHeight);
glUniform4i(uniforms[GBA_GL_OBJ_INFLAGS], GBAObjAttributesCGetPriority(sprite->c), 0, 0, 0);
glUniform4i(uniforms[GBA_GL_OBJ_INFLAGS], GBAObjAttributesCGetPriority(sprite->c),
(renderer->target1Obj || GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT) | (renderer->target2Obj * 2) | (renderer->blendEffect * 4),
renderer->blda, GBAObjAttributesAGetMode(sprite->a) == OBJ_MODE_SEMITRANSPARENT);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

glDrawBuffers(1, (GLenum[]) { GL_COLOR_ATTACHMENT0 });
}
Expand Down
2 changes: 1 addition & 1 deletion src/gba/renderers/video-software.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* render

_drawScanline(softwareRenderer, y);

if (softwareRenderer->forceTarget1 && softwareRenderer->target2Bd) {
if ((softwareRenderer->forceTarget1 || softwareRenderer->bg[0].target1 || softwareRenderer->bg[1].target1 || softwareRenderer->bg[2].target1 || softwareRenderer->bg[3].target1) && softwareRenderer->target2Bd) {
x = 0;
for (w = 0; w < softwareRenderer->nWindows; ++w) {
uint32_t backdrop = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/util/elf-read.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ void ELFGetProgramHeaders(struct ELF* elf, struct ELFProgramHeaders* ph) {
ELFProgramHeadersClear(ph);
Elf32_Ehdr* hdr = elf32_getehdr(elf->e);
Elf32_Phdr* phdr = elf32_getphdr(elf->e);
if (!hdr || !phdr) {
return;
}
ELFProgramHeadersResize(ph, hdr->e_phnum);
memcpy(ELFProgramHeadersGetPointer(ph, 0), phdr, sizeof(*phdr) * hdr->e_phnum);
}
Expand Down
46 changes: 39 additions & 7 deletions src/util/vfs/vfs-fd.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2013-2015 Jeffrey Pfau
/* Copyright (c) 2013-2020 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -14,11 +14,23 @@
#include <windows.h>
#endif

#include <mgba-util/vector.h>

#ifdef _WIN32
struct HandleMappingTuple {
HANDLE handle;
void* mapping;
};

DECLARE_VECTOR(HandleMappingList, struct HandleMappingTuple);
DEFINE_VECTOR(HandleMappingList, struct HandleMappingTuple);
#endif

struct VFileFD {
struct VFile d;
int fd;
#ifdef _WIN32
HANDLE hMap;
struct HandleMappingList handles;
#endif
};

Expand Down Expand Up @@ -74,12 +86,23 @@ struct VFile* VFileFromFD(int fd) {
vfd->d.truncate = _vfdTruncate;
vfd->d.size = _vfdSize;
vfd->d.sync = _vfdSync;
#ifdef _WIN32
HandleMappingListInit(&vfd->handles, 4);
#endif

return &vfd->d;
}

bool _vfdClose(struct VFile* vf) {
struct VFileFD* vfd = (struct VFileFD*) vf;
#ifdef _WIN32
size_t i;
for (i = 0; i < HandleMappingListSize(&vfd->handles); ++i) {
UnmapViewOfFile(HandleMappingListGetPointer(&vfd->handles, i)->mapping);
CloseHandle(HandleMappingListGetPointer(&vfd->handles, i)->handle);
}
HandleMappingListDeinit(&vfd->handles);
#endif
if (close(vfd->fd) < 0) {
return false;
}
Expand Down Expand Up @@ -134,16 +157,25 @@ static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
if (size > fileSize) {
size = fileSize;
}
vfd->hMap = CreateFileMapping((HANDLE) _get_osfhandle(vfd->fd), 0, createFlags, 0, size & 0xFFFFFFFF, 0);
return MapViewOfFile(vfd->hMap, mapFiles, 0, 0, size);
struct HandleMappingTuple tuple = {0};
tuple.handle = CreateFileMapping((HANDLE) _get_osfhandle(vfd->fd), 0, createFlags, 0, size & 0xFFFFFFFF, 0);
tuple.mapping = MapViewOfFile(tuple.handle, mapFiles, 0, 0, size);
*HandleMappingListAppend(&vfd->handles) = tuple;
return tuple.mapping;
}

static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
UNUSED(size);
struct VFileFD* vfd = (struct VFileFD*) vf;
UnmapViewOfFile(memory);
CloseHandle(vfd->hMap);
vfd->hMap = 0;
size_t i;
for (i = 0; i < HandleMappingListSize(&vfd->handles); ++i) {
if (HandleMappingListGetPointer(&vfd->handles, i)->mapping == memory) {
UnmapViewOfFile(memory);
CloseHandle(HandleMappingListGetPointer(&vfd->handles, i)->handle);
HandleMappingListShift(&vfd->handles, i, 1);
break;
}
}
}
#endif

Expand Down

0 comments on commit 4fbc43b

Please sign in to comment.