all repos — mgba @ 631b287b3a6ecc66eec765a9cf7987a2e7cfb364

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/gui/gui-runner.h"
 10#include "gba/video.h"
 11#include "util/gui.h"
 12#include "util/gui/file-select.h"
 13#include "util/gui/font.h"
 14#include "util/gui/menu.h"
 15#include "util/memory.h"
 16
 17#include "3ds-vfs.h"
 18#include "ctr-gpu.h"
 19
 20#include <3ds.h>
 21
 22static enum ScreenMode {
 23	SM_PA_BOTTOM,
 24	SM_AF_BOTTOM,
 25	SM_SF_BOTTOM,
 26	SM_PA_TOP,
 27	SM_AF_TOP,
 28	SM_SF_TOP,
 29	SM_MAX
 30} screenMode = SM_PA_TOP;
 31
 32#define AUDIO_SAMPLES 0x80
 33#define AUDIO_SAMPLE_BUFFER (AUDIO_SAMPLES * 24)
 34
 35FS_archive sdmcArchive;
 36
 37static struct GBA3DSRotationSource {
 38	struct GBARotationSource d;
 39	accelVector accel;
 40	angularRate gyro;
 41} rotation;
 42
 43static bool hasSound;
 44// TODO: Move into context
 45static struct GBAVideoSoftwareRenderer renderer;
 46static struct GBAAVStream stream;
 47static int16_t* audioLeft = 0;
 48static int16_t* audioRight = 0;
 49static size_t audioPos = 0;
 50static struct ctrTexture gbaOutputTexture;
 51static int guiDrawn;
 52static int screenCleanup;
 53
 54enum {
 55	GUI_ACTIVE = 1,
 56	GUI_THIS_FRAME = 2,
 57};
 58
 59enum {
 60	SCREEN_CLEANUP_TOP_1 = 1,
 61	SCREEN_CLEANUP_TOP_2 = 2,
 62	SCREEN_CLEANUP_TOP = SCREEN_CLEANUP_TOP_1 | SCREEN_CLEANUP_TOP_2,
 63	SCREEN_CLEANUP_BOTTOM_1 = 4,
 64	SCREEN_CLEANUP_BOTTOM_2 = 8,
 65	SCREEN_CLEANUP_BOTTOM = SCREEN_CLEANUP_BOTTOM_1 | SCREEN_CLEANUP_BOTTOM_2,
 66};
 67
 68extern bool allocateRomBuffer(void);
 69
 70static void _csndPlaySound(u32 flags, u32 sampleRate, float vol, void* left, void* right, u32 size)
 71{
 72	u32 pleft = 0, pright = 0;
 73
 74	int loopMode = (flags >> 10) & 3;
 75	if (!loopMode) {
 76		flags |= SOUND_ONE_SHOT;
 77	}
 78
 79	pleft = osConvertVirtToPhys((u32) left);
 80	pright = osConvertVirtToPhys((u32) right);
 81
 82	u32 timer = CSND_TIMER(sampleRate);
 83	if (timer < 0x0042) {
 84		timer = 0x0042;
 85	}
 86	else if (timer > 0xFFFF) {
 87		timer = 0xFFFF;
 88	}
 89	flags &= ~0xFFFF001F;
 90	flags |= SOUND_ENABLE | (timer << 16);
 91
 92	u32 volumes = CSND_VOL(vol, -1.0);
 93	CSND_SetChnRegs(flags | SOUND_CHANNEL(8), pleft, pleft, size, volumes, volumes);
 94	volumes = CSND_VOL(vol, 1.0);
 95	CSND_SetChnRegs(flags | SOUND_CHANNEL(9), pright, pright, size, volumes, volumes);
 96}
 97
 98static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio);
 99
100static void _drawStart(void) {
101	ctrGpuBeginDrawing();
102	if (screenMode < SM_PA_TOP || (guiDrawn & GUI_ACTIVE)) {
103		ctrGpuBeginFrame(GFX_BOTTOM);
104		ctrSetViewportSize(320, 240);
105	} else {
106		ctrGpuBeginFrame(GFX_TOP);
107		ctrSetViewportSize(400, 240);
108	}
109	guiDrawn &= ~GUI_THIS_FRAME;
110}
111
112static void _drawEnd(void) {
113	int screen = screenMode < SM_PA_TOP ? GFX_BOTTOM : GFX_TOP;
114	u16 width = 0, height = 0;
115
116	void* outputFramebuffer = gfxGetFramebuffer(screen, GFX_LEFT, &height, &width);
117	ctrGpuEndFrame(screen, outputFramebuffer, width, height);
118
119	if (screen != GFX_BOTTOM) {
120		if (guiDrawn & (GUI_THIS_FRAME | GUI_ACTIVE)) {
121			void* outputFramebuffer = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, &height, &width);
122			ctrGpuEndFrame(GFX_BOTTOM, outputFramebuffer, width, height);
123		} else if (screenCleanup & SCREEN_CLEANUP_BOTTOM) {
124			ctrGpuBeginFrame(GFX_BOTTOM);
125			if (screenCleanup & SCREEN_CLEANUP_BOTTOM_1) {
126				screenCleanup &= ~SCREEN_CLEANUP_BOTTOM_1;
127			} else if (screenCleanup & SCREEN_CLEANUP_BOTTOM_2) {
128				screenCleanup &= ~SCREEN_CLEANUP_BOTTOM_2;
129			}
130			void* outputFramebuffer = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, &height, &width);
131			ctrGpuEndFrame(GFX_BOTTOM, outputFramebuffer, width, height);
132		}
133	}
134
135	if ((screenCleanup & SCREEN_CLEANUP_TOP) && screen != GFX_TOP) {
136		ctrGpuBeginFrame(GFX_TOP);
137		if (screenCleanup & SCREEN_CLEANUP_TOP_1) {
138			screenCleanup &= ~SCREEN_CLEANUP_TOP_1;
139		} else if (screenCleanup & SCREEN_CLEANUP_TOP_2) {
140			screenCleanup &= ~SCREEN_CLEANUP_TOP_2;
141		}
142		void* outputFramebuffer = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &height, &width);
143		ctrGpuEndFrame(GFX_TOP, outputFramebuffer, width, height);
144	}
145
146	ctrGpuEndDrawing();
147}
148
149static int _batteryState(void) {
150	u8 charge;
151	u8 adapter;
152	PTMU_GetBatteryLevel(0, &charge);
153	PTMU_GetBatteryChargeState(0, &adapter);
154	int state = 0;
155	if (adapter) {
156		state |= BATTERY_CHARGING;
157	}
158	if (charge > 0) {
159		--charge;
160	}
161	return state | charge;
162}
163
164static void _guiPrepare(void) {
165	guiDrawn = GUI_ACTIVE | GUI_THIS_FRAME;
166	int screen = screenMode < SM_PA_TOP ? GFX_BOTTOM : GFX_TOP;
167	if (screen == GFX_BOTTOM) {
168		return;
169	}
170
171	ctrFlushBatch();
172	ctrGpuBeginFrame(GFX_BOTTOM);
173	ctrSetViewportSize(320, 240);
174}
175
176static void _guiFinish(void) {
177	guiDrawn &= ~GUI_ACTIVE;
178	screenCleanup |= SCREEN_CLEANUP_BOTTOM;
179}
180
181static void _setup(struct GBAGUIRunner* runner) {
182	runner->context.gba->rotationSource = &rotation.d;
183	if (hasSound) {
184		runner->context.gba->stream = &stream;
185	}
186
187	GBAVideoSoftwareRendererCreate(&renderer);
188	renderer.outputBuffer = linearMemAlign(256 * VIDEO_VERTICAL_PIXELS * 2, 0x80);
189	renderer.outputBufferStride = 256;
190	runner->context.renderer = &renderer.d;
191
192	unsigned mode;
193	if (GBAConfigGetUIntValue(&runner->context.config, "screenMode", &mode) && mode < SM_MAX) {
194		screenMode = mode;
195	}
196
197	GBAAudioResizeBuffer(&runner->context.gba->audio, AUDIO_SAMPLES);
198}
199
200static void _gameLoaded(struct GBAGUIRunner* runner) {
201	if (runner->context.gba->memory.hw.devices & HW_TILT) {
202		HIDUSER_EnableAccelerometer();
203	}
204	if (runner->context.gba->memory.hw.devices & HW_GYRO) {
205		HIDUSER_EnableGyroscope();
206	}
207	osSetSpeedupEnable(true);
208
209#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
210	double ratio = GBAAudioCalculateRatio(1, 59.8260982880808, 1);
211	blip_set_rates(runner->context.gba->audio.left,  GBA_ARM7TDMI_FREQUENCY, 32768 * ratio);
212	blip_set_rates(runner->context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 32768 * ratio);
213#endif
214	if (hasSound) {
215		memset(audioLeft, 0, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
216		memset(audioRight, 0, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
217		audioPos = 0;
218		_csndPlaySound(SOUND_REPEAT | SOUND_FORMAT_16BIT, 32768, 1.0, audioLeft, audioRight, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
219		csndExecCmds(false);
220	}
221	unsigned mode;
222	if (GBAConfigGetUIntValue(&runner->context.config, "screenMode", &mode) && mode != screenMode) {
223		screenMode = mode;
224		screenCleanup |= SCREEN_CLEANUP_BOTTOM | SCREEN_CLEANUP_TOP;
225	}
226}
227
228static void _gameUnloaded(struct GBAGUIRunner* runner) {
229	if (hasSound) {
230		CSND_SetPlayState(8, 0);
231		CSND_SetPlayState(9, 0);
232		csndExecCmds(false);
233	}
234	osSetSpeedupEnable(false);
235
236	if (runner->context.gba->memory.hw.devices & HW_TILT) {
237		HIDUSER_DisableAccelerometer();
238	}
239	if (runner->context.gba->memory.hw.devices & HW_GYRO) {
240		HIDUSER_DisableGyroscope();
241	}
242}
243
244static void _drawTex(bool faded) {
245	u32 color = faded ? 0x3FFFFFFF : 0xFFFFFFFF;
246
247	int screen_w = screenMode < SM_PA_TOP ? 320 : 400;
248	int screen_h = 240;
249
250	int w, h;
251
252	switch (screenMode) {
253	case SM_PA_TOP:
254	case SM_PA_BOTTOM:
255	default:
256		w = VIDEO_HORIZONTAL_PIXELS;
257		h = VIDEO_VERTICAL_PIXELS;
258		break;
259	case SM_AF_TOP:
260		w = 360;
261		h = 240;
262		break;
263	case SM_AF_BOTTOM:
264		// Largest possible size with 3:2 aspect ratio and integer dimensions
265		w = 318;
266		h = 212;
267		break;
268	case SM_SF_TOP:
269	case SM_SF_BOTTOM:
270		w = screen_w;
271		h = screen_h;
272		break;
273	}
274
275	int x = (screen_w - w) / 2;
276	int y = (screen_h - h) / 2;
277
278	ctrAddRectScaled(color, x, y, w, h, 0, 0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
279}
280
281static void _drawFrame(struct GBAGUIRunner* runner, bool faded) {
282	UNUSED(runner);
283
284	void* outputBuffer = renderer.outputBuffer;
285	struct ctrTexture* tex = &gbaOutputTexture;
286
287	GSPGPU_FlushDataCache(NULL, outputBuffer, 256 * VIDEO_VERTICAL_PIXELS * 2);
288	GX_SetDisplayTransfer(NULL,
289			outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS),
290			tex->data, GX_BUFFER_DIM(256, 256),
291			GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGB565) |
292				GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB565) |
293				GX_TRANSFER_OUT_TILED(1) | GX_TRANSFER_FLIP_VERT(1));
294
295#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
296	if (!hasSound) {
297		blip_clear(runner->context.gba->audio.left);
298		blip_clear(runner->context.gba->audio.right);
299	}
300#endif
301
302	gspWaitForPPF();
303	ctrActivateTexture(tex);
304	_drawTex(faded);
305}
306
307static void _drawScreenshot(struct GBAGUIRunner* runner, const uint32_t* pixels, bool faded) {
308	UNUSED(runner);
309
310	struct ctrTexture* tex = &gbaOutputTexture;
311
312	u16* newPixels = linearMemAlign(256 * VIDEO_VERTICAL_PIXELS * sizeof(u32), 0x100);
313
314	// Convert image from RGBX8 to BGR565
315	for (unsigned y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
316		for (unsigned x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
317			// 0xXXBBGGRR -> 0bRRRRRGGGGGGBBBBB
318			u32 p = *pixels++;
319			newPixels[y * 256 + x] =
320				(p << 24 >> (24 + 3) << 11) | // R
321				(p << 16 >> (24 + 2) <<  5) | // G
322				(p <<  8 >> (24 + 3) <<  0);  // B
323		}
324		memset(&newPixels[y * 256 + VIDEO_HORIZONTAL_PIXELS], 0, (256 - VIDEO_HORIZONTAL_PIXELS) * sizeof(u32));
325	}
326
327	GSPGPU_FlushDataCache(NULL, (void*)newPixels, 256 * VIDEO_VERTICAL_PIXELS * sizeof(u32));
328	GX_SetDisplayTransfer(NULL,
329			(void*)newPixels, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS),
330			tex->data, GX_BUFFER_DIM(256, 256),
331			GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGB565) |
332				GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB565) |
333				GX_TRANSFER_OUT_TILED(1) | GX_TRANSFER_FLIP_VERT(1));
334	gspWaitForPPF();
335	linearFree(newPixels);
336
337	ctrActivateTexture(tex);
338	_drawTex(faded);
339}
340
341static uint16_t _pollGameInput(struct GBAGUIRunner* runner) {
342	UNUSED(runner);
343
344	hidScanInput();
345	uint32_t activeKeys = hidKeysHeld() & 0xF00003FF;
346	activeKeys |= activeKeys >> 24;
347	return activeKeys;
348}
349
350static void _incrementScreenMode(struct GBAGUIRunner* runner) {
351	UNUSED(runner);
352	screenCleanup |= SCREEN_CLEANUP_TOP | SCREEN_CLEANUP_BOTTOM;
353	screenMode = (screenMode + 1) % SM_MAX;
354	GBAConfigSetUIntValue(&runner->context.config, "screenMode", screenMode);
355}
356
357static uint32_t _pollInput(void) {
358	hidScanInput();
359	uint32_t keys = 0;
360	int activeKeys = hidKeysHeld();
361	if (activeKeys & KEY_X) {
362		keys |= 1 << GUI_INPUT_CANCEL;
363	}
364	if (activeKeys & KEY_Y) {
365		keys |= 1 << GBA_GUI_INPUT_SCREEN_MODE;
366	}
367	if (activeKeys & KEY_B) {
368		keys |= 1 << GUI_INPUT_BACK;
369	}
370	if (activeKeys & KEY_A) {
371		keys |= 1 << GUI_INPUT_SELECT;
372	}
373	if (activeKeys & KEY_LEFT) {
374		keys |= 1 << GUI_INPUT_LEFT;
375	}
376	if (activeKeys & KEY_RIGHT) {
377		keys |= 1 << GUI_INPUT_RIGHT;
378	}
379	if (activeKeys & KEY_UP) {
380		keys |= 1 << GUI_INPUT_UP;
381	}
382	if (activeKeys & KEY_DOWN) {
383		keys |= 1 << GUI_INPUT_DOWN;
384	}
385	if (activeKeys & KEY_CSTICK_UP) {
386		keys |= 1 << GBA_GUI_INPUT_INCREASE_BRIGHTNESS;
387	}
388	if (activeKeys & KEY_CSTICK_DOWN) {
389		keys |= 1 << GBA_GUI_INPUT_DECREASE_BRIGHTNESS;
390	}
391	return keys;
392}
393
394static enum GUICursorState _pollCursor(int* x, int* y) {
395	hidScanInput();
396	if (!(hidKeysHeld() & KEY_TOUCH)) {
397		return GUI_CURSOR_NOT_PRESENT;
398	}
399	touchPosition pos;
400	hidTouchRead(&pos);
401	*x = pos.px;
402	*y = pos.py;
403	return GUI_CURSOR_DOWN;
404}
405
406static void _sampleRotation(struct GBARotationSource* source) {
407	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
408	// Work around ctrulib getting the entries wrong
409	rotation->accel = *(accelVector*)& hidSharedMem[0x48];
410	rotation->gyro = *(angularRate*)& hidSharedMem[0x5C];
411}
412
413static int32_t _readTiltX(struct GBARotationSource* source) {
414	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
415	return rotation->accel.x << 18L;
416}
417
418static int32_t _readTiltY(struct GBARotationSource* source) {
419	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
420	return rotation->accel.y << 18L;
421}
422
423static int32_t _readGyroZ(struct GBARotationSource* source) {
424	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
425	return rotation->gyro.y << 18L; // Yes, y
426}
427
428static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio) {
429	UNUSED(stream);
430#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
431	blip_read_samples(audio->left, &audioLeft[audioPos], AUDIO_SAMPLES, false);
432	blip_read_samples(audio->right, &audioRight[audioPos], AUDIO_SAMPLES, false);
433#elif RESAMPLE_LIBRARY == RESAMPLE_NN
434	GBAAudioCopy(audio, &audioLeft[audioPos], &audioRight[audioPos], AUDIO_SAMPLES);
435#endif
436	GSPGPU_FlushDataCache(0, (void*) &audioLeft[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
437	GSPGPU_FlushDataCache(0, (void*) &audioRight[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
438	audioPos = (audioPos + AUDIO_SAMPLES) % AUDIO_SAMPLE_BUFFER;
439	if (audioPos == AUDIO_SAMPLES * 3) {
440		u8 playing = 0;
441		csndIsPlaying(0x8, &playing);
442		if (!playing) {
443			CSND_SetPlayState(0x8, 1);
444			CSND_SetPlayState(0x9, 1);
445			csndExecCmds(false);
446		}
447	}
448}
449
450int main() {
451	ptmInit();
452	hasSound = !csndInit();
453
454	rotation.d.sample = _sampleRotation;
455	rotation.d.readTiltX = _readTiltX;
456	rotation.d.readTiltY = _readTiltY;
457	rotation.d.readGyroZ = _readGyroZ;
458
459	stream.postVideoFrame = 0;
460	stream.postAudioFrame = 0;
461	stream.postAudioBuffer = _postAudioBuffer;
462
463	if (!allocateRomBuffer()) {
464		return 1;
465	}
466
467	if (hasSound) {
468		audioLeft = linearMemAlign(AUDIO_SAMPLE_BUFFER * sizeof(int16_t), 0x80);
469		audioRight = linearMemAlign(AUDIO_SAMPLE_BUFFER * sizeof(int16_t), 0x80);
470	}
471
472	gfxInit(GSP_BGR8_OES, GSP_BGR8_OES, false);
473
474	if (ctrInitGpu() < 0) {
475		goto cleanup;
476	}
477
478	ctrTexture_Init(&gbaOutputTexture);
479	gbaOutputTexture.format = GPU_RGB565;
480	gbaOutputTexture.filter = GPU_LINEAR;
481	gbaOutputTexture.width = 256;
482	gbaOutputTexture.height = 256;
483	gbaOutputTexture.data = vramAlloc(256 * 256 * 2);
484	void* outputTextureEnd = (u8*)gbaOutputTexture.data + 256 * 256 * 2;
485
486	// Zero texture data to make sure no garbage around the border interferes with filtering
487	GX_SetMemoryFill(NULL,
488			gbaOutputTexture.data, 0x0000, outputTextureEnd, GX_FILL_16BIT_DEPTH | GX_FILL_TRIGGER,
489			NULL, 0, NULL, 0);
490	gspWaitForPSC0();
491
492	sdmcArchive = (FS_archive) {
493		ARCH_SDMC,
494		(FS_path) { PATH_EMPTY, 1, (const u8*)"" },
495		0, 0
496	};
497	FSUSER_OpenArchive(0, &sdmcArchive);
498
499	struct GUIFont* font = GUIFontCreate();
500
501	if (!font) {
502		goto cleanup;
503	}
504
505	struct GBAGUIRunner runner = {
506		.params = {
507			320, 240,
508			font, "/",
509			_drawStart, _drawEnd,
510			_pollInput, _pollCursor,
511			_batteryState,
512			_guiPrepare, _guiFinish,
513
514			GUI_PARAMS_TRAIL
515		},
516		.configExtra = (struct GUIMenuItem[]) {
517			{
518				.title = "Screen mode",
519				.data = "screenMode",
520				.submenu = 0,
521				.state = SM_PA_TOP,
522				.validStates = (const char*[]) {
523					"Pixel-Accurate/Bottom",
524					"Aspect-Ratio Fit/Bottom",
525					"Stretched/Bottom",
526					"Pixel-Accurate/Top",
527					"Aspect-Ratio Fit/Top",
528					"Stretched/Top",
529					0
530				}
531			}
532		},
533		.nConfigExtra = 1,
534		.setup = _setup,
535		.teardown = 0,
536		.gameLoaded = _gameLoaded,
537		.gameUnloaded = _gameUnloaded,
538		.prepareForFrame = 0,
539		.drawFrame = _drawFrame,
540		.drawScreenshot = _drawScreenshot,
541		.paused = _gameUnloaded,
542		.unpaused = _gameLoaded,
543		.incrementScreenMode = _incrementScreenMode,
544		.pollGameInput = _pollGameInput
545	};
546
547	GBAGUIInit(&runner, "3ds");
548	GBAGUIRunloop(&runner);
549	GBAGUIDeinit(&runner);
550
551cleanup:
552	linearFree(renderer.outputBuffer);
553
554	ctrDeinitGpu();
555	vramFree(gbaOutputTexture.data);
556
557	gfxExit();
558
559	if (hasSound) {
560		linearFree(audioLeft);
561		linearFree(audioRight);
562	}
563	csndExit();
564	ptmExit();
565	return 0;
566}