all repos — mgba @ 7e74cba49a7ed6408897f3a946319899a43a3f12

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
  7#include "gba/renderers/video-software.h"
  8#include "gba/context/context.h"
  9#include "gba/video.h"
 10#include "util/gui.h"
 11#include "util/gui/file-select.h"
 12#include "util/gui/font.h"
 13#include "util/memory.h"
 14
 15#include "3ds-vfs.h"
 16
 17#include <3ds.h>
 18#include <sf2d.h>
 19
 20FS_archive sdmcArchive;
 21
 22struct GBA3DSRotationSource {
 23	struct GBARotationSource d;
 24	accelVector accel;
 25	angularRate gyro;
 26};
 27
 28extern bool allocateRomBuffer(void);
 29static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
 30static struct VFile* logFile;
 31
 32static void _drawStart(void) {
 33	sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
 34}
 35static void _drawEnd(void) {
 36	sf2d_end_frame();
 37	sf2d_swapbuffers();
 38}
 39
 40static int _pollInput(void) {
 41	hidScanInput();
 42	int keys = 0;
 43	int activeKeys = hidKeysHeld();
 44	if (activeKeys & KEY_X) {
 45		keys |= 1 << GUI_INPUT_CANCEL;
 46	}
 47	if (activeKeys & KEY_B) {
 48		keys |= 1 << GUI_INPUT_BACK;
 49	}
 50	if (activeKeys & KEY_A) {
 51		keys |= 1 << GUI_INPUT_SELECT;
 52	}
 53	if (activeKeys & KEY_LEFT) {
 54		keys |= 1 << GUI_INPUT_LEFT;
 55	}
 56	if (activeKeys & KEY_RIGHT) {
 57		keys |= 1 << GUI_INPUT_RIGHT;
 58	}
 59	if (activeKeys & KEY_UP) {
 60		keys |= 1 << GUI_INPUT_UP;
 61	}
 62	if (activeKeys & KEY_DOWN) {
 63		keys |= 1 << GUI_INPUT_DOWN;
 64	}
 65	return keys;
 66}
 67
 68static void _sampleRotation(struct GBARotationSource* source) {
 69	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
 70	// Work around ctrulib getting the entries wrong
 71	rotation->accel = *(accelVector*)& hidSharedMem[0x48];
 72	rotation->gyro = *(angularRate*)& hidSharedMem[0x5C];
 73}
 74
 75static int32_t _readTiltX(struct GBARotationSource* source) {
 76	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
 77	return rotation->accel.x << 18L;
 78}
 79
 80static int32_t _readTiltY(struct GBARotationSource* source) {
 81	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
 82	return rotation->accel.y << 18L;
 83}
 84
 85static int32_t _readGyroZ(struct GBARotationSource* source) {
 86	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
 87	return rotation->gyro.y << 18L; // Yes, y
 88}
 89
 90int main() {
 91	struct GBAContext context;
 92	struct GBA3DSRotationSource rotation;
 93	rotation.d.sample = _sampleRotation;
 94	rotation.d.readTiltX = _readTiltX;
 95	rotation.d.readTiltY = _readTiltY;
 96	rotation.d.readGyroZ = _readGyroZ;
 97
 98	if (!allocateRomBuffer()) {
 99		return 1;
100	}
101
102	sf2d_init();
103	sf2d_set_clear_color(0);
104	sf2d_texture* tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_RAM);
105	memset(tex->data, 0, 256 * 256 * 2);
106
107	sdmcArchive = (FS_archive) {
108		ARCH_SDMC,
109		(FS_path) { PATH_EMPTY, 1, (const u8*)"" },
110		0, 0
111	};
112	FSUSER_OpenArchive(0, &sdmcArchive);
113
114	logFile = VFileOpen("/mgba.log", O_WRONLY | O_CREAT | O_TRUNC);
115	struct GUIFont* font = GUIFontCreate();
116
117	GBAContextInit(&context, 0);
118	struct GBAOptions opts = {
119		.useBios = true,
120		.logLevel = 0,
121		.idleOptimization = IDLE_LOOP_DETECT
122	};
123	GBAConfigLoadDefaults(&context.config, &opts);
124	context.gba->logHandler = GBA3DSLog;
125	context.gba->rotationSource = &rotation.d;
126
127	struct GBAVideoSoftwareRenderer renderer;
128	GBAVideoSoftwareRendererCreate(&renderer);
129	renderer.outputBuffer = anonymousMemoryMap(256 * VIDEO_VERTICAL_PIXELS * 2);
130	renderer.outputBufferStride = 256;
131	context.renderer = &renderer.d;
132
133	if (!font) {
134		goto cleanup;
135	}
136
137	struct GUIParams params = {
138		320, 240,
139		font, _drawStart, _drawEnd, _pollInput
140	};
141
142	char currentPath[256] = "";
143	while (aptMainLoop()) {
144		char path[256];
145		if (!selectFile(&params, "/", path, currentPath, sizeof(path), GBAIsROM)) {
146			break;
147		}
148		_drawStart();
149		GUIFontPrintf(font, 160, (GUIFontHeight(font) + 240) / 2, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
150		_drawEnd();
151		if (!GBAContextLoadROM(&context, path, true)) {
152			continue;
153		}
154		if (!GBAContextStart(&context)) {
155			continue;
156		}
157
158		if (context.gba->memory.hw.devices & HW_TILT) {
159			HIDUSER_EnableAccelerometer();
160		}
161		if (context.gba->memory.hw.devices & HW_GYRO) {
162			HIDUSER_EnableGyroscope();
163		}
164
165#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
166		blip_set_rates(context.gba->audio.left,  GBA_ARM7TDMI_FREQUENCY, 48000);
167		blip_set_rates(context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000);
168#endif
169
170		while (aptMainLoop()) {
171			hidScanInput();
172			int activeKeys = hidKeysHeld() & 0x3FF;
173			if (hidKeysDown() & KEY_X) {
174				break;
175			}
176			GBAContextFrame(&context, activeKeys);
177			GX_SetDisplayTransfer(0, renderer.outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
178			GSPGPU_FlushDataCache(0, tex->data, 256 * VIDEO_VERTICAL_PIXELS * 2);
179#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
180			blip_clear(context.gba->audio.left);
181			blip_clear(context.gba->audio.right);
182#endif
183			gspWaitForPPF();
184			_drawStart();
185			sf2d_draw_texture_scale(tex, 40, 296, 1, -1);
186			_drawEnd();
187		}
188		GBAContextStop(&context);
189
190		if (context.gba->memory.hw.devices & HW_TILT) {
191			HIDUSER_DisableAccelerometer();
192		}
193		if (context.gba->memory.hw.devices & HW_GYRO) {
194			HIDUSER_DisableGyroscope();
195		}
196	}
197	GBAContextDeinit(&context);
198
199cleanup:
200	mappedMemoryFree(renderer.outputBuffer, 0);
201
202	if (logFile) {
203		logFile->close(logFile);
204	}
205
206	sf2d_free_texture(tex);
207	sf2d_fini();
208	return 0;
209}
210
211static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
212	UNUSED(thread);
213	UNUSED(level);
214	if (!logFile) {
215		return;
216	}
217	char out[256];
218	size_t len = vsnprintf(out, sizeof(out), format, args);
219	if (len >= sizeof(out)) {
220		len = sizeof(out) - 1;
221	}
222	out[len] = '\n';
223	logFile->write(logFile, out, len + 1);
224}