all repos — mgba @ 70231031630875a52521706deeb701c0605d4c37

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		GUI_PARAMS_TRAIL
142	};
143	GUIInit(&params);
144
145	while (aptMainLoop()) {
146		char path[256];
147		if (!GUISelectFile(&params, path, sizeof(path), GBAIsROM)) {
148			break;
149		}
150		_drawStart();
151		GUIFontPrintf(font, 160, (GUIFontHeight(font) + 240) / 2, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
152		_drawEnd();
153		if (!GBAContextLoadROM(&context, path, true)) {
154			continue;
155		}
156		if (!GBAContextStart(&context)) {
157			continue;
158		}
159
160		if (context.gba->memory.hw.devices & HW_TILT) {
161			HIDUSER_EnableAccelerometer();
162		}
163		if (context.gba->memory.hw.devices & HW_GYRO) {
164			HIDUSER_EnableGyroscope();
165		}
166
167#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
168		blip_set_rates(context.gba->audio.left,  GBA_ARM7TDMI_FREQUENCY, 48000);
169		blip_set_rates(context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000);
170#endif
171
172		while (aptMainLoop()) {
173			hidScanInput();
174			int activeKeys = hidKeysHeld() & 0x3FF;
175			if (hidKeysDown() & KEY_X) {
176				break;
177			}
178			GBAContextFrame(&context, activeKeys);
179			GX_SetDisplayTransfer(0, renderer.outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
180			GSPGPU_FlushDataCache(0, tex->data, 256 * VIDEO_VERTICAL_PIXELS * 2);
181#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
182			blip_clear(context.gba->audio.left);
183			blip_clear(context.gba->audio.right);
184#endif
185			gspWaitForPPF();
186			_drawStart();
187			sf2d_draw_texture_scale(tex, 40, 296, 1, -1);
188			_drawEnd();
189		}
190		GBAContextStop(&context);
191
192		if (context.gba->memory.hw.devices & HW_TILT) {
193			HIDUSER_DisableAccelerometer();
194		}
195		if (context.gba->memory.hw.devices & HW_GYRO) {
196			HIDUSER_DisableGyroscope();
197		}
198	}
199	GBAContextDeinit(&context);
200
201cleanup:
202	mappedMemoryFree(renderer.outputBuffer, 0);
203
204	if (logFile) {
205		logFile->close(logFile);
206	}
207
208	sf2d_free_texture(tex);
209	sf2d_fini();
210	return 0;
211}
212
213static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
214	UNUSED(thread);
215	UNUSED(level);
216	if (!logFile) {
217		return;
218	}
219	char out[256];
220	size_t len = vsnprintf(out, sizeof(out), format, args);
221	if (len >= sizeof(out)) {
222		len = sizeof(out) - 1;
223	}
224	out[len] = '\n';
225	logFile->write(logFile, out, len + 1);
226}