From ca42faae3c397fb480966637ba99bdda85e7a644 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 22 Aug 2015 17:22:25 -0700 Subject: [PATCH] 3DS: Use GBAContext --- src/platform/3ds/main.c | 91 ++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 55 deletions(-) diff --git a/src/platform/3ds/main.c b/src/platform/3ds/main.c index 9d6305978..5f1449f56 100644 --- a/src/platform/3ds/main.c +++ b/src/platform/3ds/main.c @@ -3,10 +3,10 @@ * 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 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "gba/gba.h" -#include "gba/video.h" #include "gba/renderers/video-software.h" +#include "gba/supervisor/context.h" +#include "gba/video.h" #include "util/memory.h" #include "3ds-vfs.h" @@ -19,6 +19,7 @@ static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const ch static Handle logFile; int main() { + struct GBAContext context; srvInit(); aptInit(); hidInit(0); @@ -32,71 +33,51 @@ int main() { FSUSER_OpenArchive(0, &sdmcArchive); FSUSER_OpenFile(0, &logFile, sdmcArchive, FS_makePath(PATH_CHAR, "/mgba.log"), FS_OPEN_WRITE | FS_OPEN_CREATE, FS_ATTRIBUTE_NONE); + GBAContextInit(&context, 0); + struct GBAOptions opts = { + .useBios = true, + .logLevel = 0, + .idleOptimization = IDLE_LOOP_REMOVE + }; + GBAConfigLoadDefaults(&context.config, &opts); + context.gba->logHandler = GBA3DSLog; + struct GBAVideoSoftwareRenderer renderer; GBAVideoSoftwareRendererCreate(&renderer); - size_t stride = VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL; color_t* videoBuffer = anonymousMemoryMap(stride * VIDEO_VERTICAL_PIXELS); - struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA)); - struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore)); - int activeKeys = 0; - renderer.outputBuffer = videoBuffer; renderer.outputBufferStride = VIDEO_HORIZONTAL_PIXELS; + GBAVideoAssociateRenderer(&context.gba->video, &renderer.d); - struct VFile* rom = VFileOpen3DS(&sdmcArchive, "/rom.gba", FS_OPEN_READ); - struct VFile* save = VFileOpen3DS(&sdmcArchive, "/rom.sav", FS_OPEN_READ | FS_OPEN_WRITE); + GBAContextLoadROM(&context, "/rom.gba", true); + GBAContextStart(&context); - GBACreate(gba); - ARMSetComponents(cpu, &gba->d, 0, 0); - ARMInit(cpu); - - gba->keySource = &activeKeys; - gba->sync = 0; - - GBAVideoAssociateRenderer(&gba->video, &renderer.d); - - GBALoadROM(gba, rom, save, 0); - - gba->logHandler = GBA3DSLog; - - ARMReset(cpu); - - int frameCounter = 0; while (aptMainLoop()) { - ARMRunLoop(cpu); - - if (frameCounter != gba->video.frameCounter) { - u16 width, height; - u16* screen = (u16*) gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, &height, &width); - u32 startX = (width - VIDEO_HORIZONTAL_PIXELS) / 2; - u32 startY = (height + VIDEO_VERTICAL_PIXELS) / 2 - 1; - u32 x, y; - for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) { - for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) { - screen[startY - y + (startX + x) * height] = videoBuffer[y * VIDEO_HORIZONTAL_PIXELS + x]; - } - } - gfxFlushBuffers(); - gfxSwapBuffers(); - gspWaitForVBlank1(); - hidScanInput(); - activeKeys = hidKeysHeld() & 0x3FF; - if (hidKeysDown() & KEY_X) { - break; - } - frameCounter = gba->video.frameCounter; + hidScanInput(); + int activeKeys = hidKeysHeld() & 0x3FF; + if (hidKeysDown() & KEY_X) { + break; } + GBAContextFrame(&context, activeKeys); + + u16 width, height; + u16* screen = (u16*) gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, &height, &width); + u32 startX = (width - VIDEO_HORIZONTAL_PIXELS) / 2; + u32 startY = (height + VIDEO_VERTICAL_PIXELS) / 2 - 1; + u32 x, y; + for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) { + for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) { + screen[startY - y + (startX + x) * height] = videoBuffer[y * VIDEO_HORIZONTAL_PIXELS + x]; + } + } + gfxFlushBuffers(); + gfxSwapBuffers(); + gspWaitForVBlank1(); } - ARMDeinit(cpu); - GBADestroy(gba); - - rom->close(rom); - save->close(save); - - mappedMemoryFree(gba, 0); - mappedMemoryFree(cpu, 0); + GBAContextStop(&context); + GBAContextDeinit(&context); mappedMemoryFree(videoBuffer, 0);