all repos — mgba @ 2cb04809431f0d0144c741a3a6af9b3309ab1f2c

mGBA Game Boy Advance Emulator

src/platform/3ds/main.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "gba/gba.h"
  7#include "gba/video.h"
  8
  9#include "gba/renderers/video-software.h"
 10#include "util/memory.h"
 11
 12#include "3ds-vfs.h"
 13
 14#include <3ds.h>
 15
 16int main() {
 17	srvInit();
 18	aptInit();
 19	hidInit(0);
 20	gfxInit(GSP_RGB565_OES, GSP_RGB565_OES, false);
 21	fsInit();
 22
 23	struct GBAVideoSoftwareRenderer renderer;
 24	GBAVideoSoftwareRendererCreate(&renderer);
 25
 26	size_t stride = VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL;
 27	color_t* videoBuffer = anonymousMemoryMap(stride * VIDEO_VERTICAL_PIXELS);
 28	struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
 29	struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
 30	int activeKeys = 0;
 31
 32	renderer.outputBuffer = videoBuffer;
 33	renderer.outputBufferStride = VIDEO_HORIZONTAL_PIXELS;
 34
 35	FS_archive sdmcArchive = (FS_archive) {
 36		ARCH_SDMC,
 37		(FS_path) { PATH_EMPTY, 1, (u8*)"" },
 38		0, 0
 39	};
 40	FSUSER_OpenArchive(0, &sdmcArchive);
 41
 42	struct VFile* rom = VFileOpen3DS(&sdmcArchive, "/rom.gba", FS_OPEN_READ);
 43
 44	struct VFile* save = VFileOpen3DS(&sdmcArchive, "/rom.sav", FS_OPEN_WRITE | FS_OPEN_CREATE);
 45
 46	GBACreate(gba);
 47	ARMSetComponents(cpu, &gba->d, 0, 0);
 48	ARMInit(cpu);
 49
 50	gba->keySource = &activeKeys;
 51	gba->sync = 0;
 52
 53	GBAVideoAssociateRenderer(&gba->video, &renderer.d);
 54
 55	GBALoadROM(gba, rom, save, 0);
 56
 57	ARMReset(cpu);
 58
 59	int frameCounter = 0;
 60	while (aptMainLoop()) {
 61		ARMRunLoop(cpu);
 62
 63		if (frameCounter != gba->video.frameCounter) {
 64			u16 width, height;
 65			u16* screen = (u16*) gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, &height, &width);
 66			u32 startX = (width - VIDEO_HORIZONTAL_PIXELS) / 2;
 67			u32 startY = (height + VIDEO_VERTICAL_PIXELS) / 2 - 1;
 68			u32 x, y;
 69			for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
 70				for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 71					screen[startY - y + (startX + x) * height] = videoBuffer[y * VIDEO_HORIZONTAL_PIXELS + x];
 72				}
 73			}
 74			gfxFlushBuffers();
 75			gfxSwapBuffersGpu();
 76			gspWaitForVBlank();
 77			hidScanInput();
 78			activeKeys = hidKeysHeld() & 0x3FF;
 79			if (hidKeysDown() & KEY_X) {
 80				break;
 81			}
 82			frameCounter = gba->video.frameCounter;
 83		}
 84	}
 85
 86	ARMDeinit(cpu);
 87	GBADestroy(gba);
 88
 89	rom->close(rom);
 90	save->close(save);
 91
 92	mappedMemoryFree(gba, 0);
 93	mappedMemoryFree(cpu, 0);
 94
 95	mappedMemoryFree(videoBuffer, 0);
 96
 97	fsExit();
 98	gfxExit();
 99	hidExit();
100	aptExit();
101	srvExit();
102	return 0;
103}