all repos — mgba @ 672512c144bd14c012e6c64d81b16cc4283ec121

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