all repos — mgba @ f453dbd7f1ba7f7adcd29d8bc10c3c7545a59965

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
 16static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
 17static Handle logFile;
 18
 19int main() {
 20	srvInit();
 21	aptInit();
 22	hidInit(0);
 23	gfxInit(GSP_RGB565_OES, GSP_RGB565_OES, false);
 24	fsInit();
 25
 26	FS_archive sdmcArchive = (FS_archive) {
 27		ARCH_SDMC,
 28		(FS_path) { PATH_EMPTY, 1, (u8*)"" },
 29		0, 0
 30	};
 31	FSUSER_OpenArchive(0, &sdmcArchive);
 32	FSUSER_OpenFile(0, &logFile, sdmcArchive, FS_makePath(PATH_CHAR, "/mgba.log"), FS_OPEN_WRITE | FS_OPEN_CREATE, FS_ATTRIBUTE_NONE);
 33
 34	struct GBAVideoSoftwareRenderer renderer;
 35	GBAVideoSoftwareRendererCreate(&renderer);
 36
 37	size_t stride = VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL;
 38	color_t* videoBuffer = anonymousMemoryMap(stride * VIDEO_VERTICAL_PIXELS);
 39	struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
 40	struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
 41	int activeKeys = 0;
 42
 43	renderer.outputBuffer = videoBuffer;
 44	renderer.outputBufferStride = VIDEO_HORIZONTAL_PIXELS;
 45
 46	struct VFile* rom = VFileOpen3DS(&sdmcArchive, "/rom.gba", FS_OPEN_READ);
 47	struct VFile* save = VFileOpen3DS(&sdmcArchive, "/rom.sav", FS_OPEN_READ | FS_OPEN_WRITE);
 48
 49	GBACreate(gba);
 50	ARMSetComponents(cpu, &gba->d, 0, 0);
 51	ARMInit(cpu);
 52
 53	gba->keySource = &activeKeys;
 54	gba->sync = 0;
 55
 56	GBAVideoAssociateRenderer(&gba->video, &renderer.d);
 57
 58	GBALoadROM(gba, rom, save, 0);
 59
 60	gba->logHandler = GBA3DSLog;
 61
 62	ARMReset(cpu);
 63
 64	int frameCounter = 0;
 65	while (aptMainLoop()) {
 66		ARMRunLoop(cpu);
 67
 68		if (frameCounter != gba->video.frameCounter) {
 69			u16 width, height;
 70			u16* screen = (u16*) gfxGetFramebuffer(GFX_BOTTOM, GFX_BOTTOM, &height, &width);
 71			u32 startX = (width - VIDEO_HORIZONTAL_PIXELS) / 2;
 72			u32 startY = (height + VIDEO_VERTICAL_PIXELS) / 2 - 1;
 73			u32 x, y;
 74			for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
 75				for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
 76					screen[startY - y + (startX + x) * height] = videoBuffer[y * VIDEO_HORIZONTAL_PIXELS + x];
 77				}
 78			}
 79			gfxFlushBuffers();
 80			gfxSwapBuffersGpu();
 81			gspWaitForVBlank();
 82			hidScanInput();
 83			activeKeys = hidKeysHeld() & 0x3FF;
 84			if (hidKeysDown() & KEY_X) {
 85				break;
 86			}
 87			frameCounter = gba->video.frameCounter;
 88		}
 89	}
 90
 91	ARMDeinit(cpu);
 92	GBADestroy(gba);
 93
 94	rom->close(rom);
 95	save->close(save);
 96
 97	mappedMemoryFree(gba, 0);
 98	mappedMemoryFree(cpu, 0);
 99
100	mappedMemoryFree(videoBuffer, 0);
101
102	FSFILE_Close(logFile);
103
104	fsExit();
105	gfxExit();
106	hidExit();
107	aptExit();
108	srvExit();
109	return 0;
110}
111
112static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
113	UNUSED(thread);
114	UNUSED(level);
115	char out[256];
116	u64 size;
117	u32 written;
118	size_t len = vsnprintf(out, sizeof(out), format, args);
119	if (len >= 256) {
120		len = 255;
121	}
122	out[len] = '\n';
123	FSFILE_GetSize(logFile, &size);
124	FSFILE_Write(logFile, &written, size, out, len + 1, FS_WRITE_FLUSH);
125}