all repos — mgba @ 4b5822a8ecf3b770288f1449e004de8e90bf3542

mGBA Game Boy Advance Emulator

3DS: Add 3DS main file
Jeffrey Pfau jeffrey@endrift.com
Mon, 08 Dec 2014 19:39:07 -0800
commit

4b5822a8ecf3b770288f1449e004de8e90bf3542

parent

39c88da6508b85d121918de88dfa4c20b392832a

3 files changed, 100 insertions(+), 2 deletions(-)

jump to
M src/platform/3ds/3ds-vfs.csrc/platform/3ds/3ds-vfs.c

@@ -10,8 +10,6 @@ * 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 "3ds-vfs.h" -#include "util/vfs.h" - #include "util/memory.h" struct VFile3DS {
M src/platform/3ds/3ds-vfs.hsrc/platform/3ds/3ds-vfs.h

@@ -8,6 +8,8 @@ *

* 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 "util/vfs.h" + #include <3ds.h> struct VFile* VFileOpen3DS(FS_archive archive, const char* path, int flags);
A src/platform/3ds/main.c

@@ -0,0 +1,98 @@

+/* Copyright (c) 2013-2014 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 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "gba.h" +#include "gba-video.h" + +#include "renderers/video-software.h" + +#include "3ds-vfs.h" + +#include <3ds.h> + +int main() { + srvInit(); + aptInit(); + hidInit(0); + gfxInit(); + fsInit(); + + gfxSetScreenFormat(GFX_BOTTOM, GSP_RGBA8_OES); + + struct GBAVideoSoftwareRenderer renderer; + GBAVideoSoftwareRendererCreate(&renderer); + + size_t stride = sizeof(color_t) * VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL; + color_t* videoBuffer = malloc(stride * VIDEO_VERTICAL_PIXELS); + struct GBA* gba = malloc(sizeof(struct GBA)); + struct ARMCore* cpu = malloc(sizeof(struct ARMCore)); + int activeKeys = 0; + + renderer.outputBuffer = videoBuffer; + renderer.outputBufferStride = stride; + + gba->keySource = &activeKeys; + gba->sync = 0; + + FS_archive sdmcArchive = (FS_archive) { + ARCH_SDMC, + (FS_path) { PATH_EMPTY, 1, (u8*)"" }, + 0, 0 + }; + + struct VFile* rom = VFileOpen3DS(sdmcArchive, "/rom.gba", O_RDONLY); + struct VFile* save = VFileOpen3DS(sdmcArchive, "/rom.sav", O_RDWR | O_CREAT); + + GBACreate(gba); + ARMSetComponents(cpu, &gba->d, 0, 0); + ARMInit(cpu); + + GBAVideoAssociateRenderer(&gba->video, &renderer.d); + + GBALoadROM(gba, rom, save, "rom.gba"); + + ARMReset(cpu); + + bool seenVblank = false; + while (aptMainLoop()) { + hidScanInput(); + + ARMRunLoop(cpu); + + if (!seenVblank) { + if (GBARegisterDISPSTATIsInVblank(gba->video.dispstat)) { + u16 width, height; + u8* screen = gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, &width, &height); + int y; + for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) { + u8* row = &screen[(width - VIDEO_HORIZONTAL_PIXELS) * BYTES_PER_PIXEL / 2]; + row = &row[width * BYTES_PER_PIXEL * (((height - VIDEO_VERTICAL_PIXELS) / 2) + y)]; + memcpy(row, &videoBuffer[stride * y], stride); + } + + gfxSwapBuffersGpu(); + gspWaitForEvent(GSPEVENT_VBlank0, false); + } + } + + seenVblank = GBARegisterDISPSTATIsInVblank(gba->video.dispstat); + } + + ARMDeinit(cpu); + GBADestroy(gba); + + free(gba); + free(cpu); + + rom->close(rom); + save->close(save); + + fsExit(); + gfxExit(); + hidExit(); + aptExit(); + srvExit(); + return 0; +}