all repos — mgba @ e65d12d35e406d6d62aa859611c2feb560d7801f

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