all repos — mgba @ 2dcefe8fa5d1beb835e822ae2fc0eac216972942

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/memory.h"
 15
 16#include "3ds-vfs.h"
 17
 18#include <3ds.h>
 19#include <sf2d.h>
 20
 21static enum ScreenMode {
 22	SM_PA_BOTTOM,
 23	SM_AF_BOTTOM,
 24	SM_SF_BOTTOM,
 25	SM_PA_TOP,
 26	SM_AF_TOP,
 27	SM_SF_TOP,
 28	SM_MAX
 29} screenMode = SM_PA_BOTTOM;
 30
 31#define AUDIO_SAMPLES 0x80
 32#define AUDIO_SAMPLE_BUFFER (AUDIO_SAMPLES * 24)
 33
 34FS_archive sdmcArchive;
 35
 36static struct GBA3DSRotationSource {
 37	struct GBARotationSource d;
 38	accelVector accel;
 39	angularRate gyro;
 40} rotation;
 41
 42static struct VFile* logFile;
 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 sf2d_texture* tex;
 51
 52extern bool allocateRomBuffer(void);
 53static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
 54
 55static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio);
 56
 57static void _drawStart(void) {
 58	if (screenMode < SM_PA_TOP) {
 59		sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
 60	} else {
 61		sf2d_start_frame(GFX_TOP, GFX_LEFT);
 62	}
 63}
 64
 65static void _drawEnd(void) {
 66	sf2d_end_frame();
 67	sf2d_swapbuffers();
 68}
 69
 70static void _setup(struct GBAGUIRunner* runner) {
 71	struct GBAOptions opts = {
 72		.useBios = true,
 73		.logLevel = 0,
 74		.idleOptimization = IDLE_LOOP_DETECT
 75	};
 76	GBAConfigLoadDefaults(&runner->context.config, &opts);
 77	runner->context.gba->logHandler = GBA3DSLog;
 78	runner->context.gba->rotationSource = &rotation.d;
 79	if (hasSound) {
 80		runner->context.gba->stream = &stream;
 81	}
 82
 83	GBAVideoSoftwareRendererCreate(&renderer);
 84	renderer.outputBuffer = linearMemAlign(256 * 256 * 2, 0x100);
 85	renderer.outputBufferStride = 256;
 86	runner->context.renderer = &renderer.d;
 87
 88	GBAAudioResizeBuffer(&runner->context.gba->audio, AUDIO_SAMPLES);
 89}
 90
 91static void _gameLoaded(struct GBAGUIRunner* runner) {
 92	if (runner->context.gba->memory.hw.devices & HW_TILT) {
 93		HIDUSER_EnableAccelerometer();
 94	}
 95	if (runner->context.gba->memory.hw.devices & HW_GYRO) {
 96		HIDUSER_EnableGyroscope();
 97	}
 98
 99#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
100	double ratio = GBAAudioCalculateRatio(1, 59.8260982880808, 1);
101	blip_set_rates(runner->context.gba->audio.left,  GBA_ARM7TDMI_FREQUENCY, 32768 * ratio);
102	blip_set_rates(runner->context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 32768 * ratio);
103#endif
104	if (hasSound) {
105		memset(audioLeft, 0, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
106		memset(audioRight, 0, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
107		audioPos = 0;
108		csndPlaySound(0x8, SOUND_REPEAT | SOUND_FORMAT_16BIT, 32768, 1.0, -1.0, audioLeft, audioLeft, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
109		csndPlaySound(0x9, SOUND_REPEAT | SOUND_FORMAT_16BIT, 32768, 1.0, 1.0, audioRight, audioRight, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
110	}
111}
112
113static void _gameUnloaded(struct GBAGUIRunner* runner) {
114	if (hasSound) {
115		CSND_SetPlayState(8, 0);
116		CSND_SetPlayState(9, 0);
117		csndExecCmds(false);
118	}
119
120	if (runner->context.gba->memory.hw.devices & HW_TILT) {
121		HIDUSER_DisableAccelerometer();
122	}
123	if (runner->context.gba->memory.hw.devices & HW_GYRO) {
124		HIDUSER_DisableGyroscope();
125	}
126}
127
128static void _drawTex(bool faded) {
129	switch (screenMode) {
130	case SM_PA_TOP:
131		sf2d_draw_texture_scale_blend(tex, 80, 296, 1, -1, 0xFFFFFF3F | (faded ? 0 : 0xC0));
132		break;
133	case SM_PA_BOTTOM:
134		sf2d_draw_texture_scale_blend(tex, 40, 296, 1, -1, 0xFFFFFF3F | (faded ? 0 : 0xC0));
135		break;
136	case SM_AF_TOP:
137		sf2d_draw_texture_scale_blend(tex, 20, 384, 1.5, -1.5, 0xFFFFFF3F | (faded ? 0 : 0xC0));
138		break;
139	case SM_AF_BOTTOM:
140		sf2d_draw_texture_scale_blend(tex, 0, 368 - 40 / 3, 4 / 3.0, -4 / 3.0, 0xFFFFFF3F | (faded ? 0 : 0xC0));
141		break;
142	case SM_SF_TOP:
143		sf2d_draw_texture_scale_blend(tex, 0, 384, 5 / 3.0, -1.5, 0xFFFFFF3F | (faded ? 0 : 0xC0));
144		break;
145	case SM_SF_BOTTOM:
146		sf2d_draw_texture_scale_blend(tex, 0, 384, 4 / 3.0, -1.5, 0xFFFFFF3F | (faded ? 0 : 0xC0));
147		break;
148	}
149}
150
151static void _drawFrame(struct GBAGUIRunner* runner, bool faded) {
152	UNUSED(runner);
153	GSPGPU_FlushDataCache(0, renderer.outputBuffer, 256 * VIDEO_VERTICAL_PIXELS * 2);
154	GX_SetDisplayTransfer(0, renderer.outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
155	_drawTex(faded);
156#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
157	if (!hasSound) {
158		blip_clear(runner->context.gba->audio.left);
159		blip_clear(runner->context.gba->audio.right);
160	}
161#endif
162}
163
164static void _drawScreenshot(struct GBAGUIRunner* runner, const uint32_t* pixels, bool faded) {
165	UNUSED(runner);
166	u16* newPixels = linearMemAlign(256 * VIDEO_VERTICAL_PIXELS * 2, 0x100);
167	unsigned y, x;
168	for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
169		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
170			u16 pixel = (*pixels >> 19) & 0x1F;
171			pixel |= (*pixels >> 5) & 0x7C0;
172			pixel |= (*pixels << 8) & 0xF800;
173			newPixels[y * 256 + x] = pixel;
174			++pixels;
175		}
176		memset(&newPixels[y * 256 + VIDEO_HORIZONTAL_PIXELS], 0, 32);
177	}
178	GSPGPU_FlushDataCache(0, (void*) newPixels, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 2);
179	GX_SetDisplayTransfer(0, (void*) newPixels, GX_BUFFER_DIM(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
180	linearFree(newPixels);
181	_drawTex(faded);
182}
183
184static uint16_t _pollGameInput(struct GBAGUIRunner* runner) {
185	hidScanInput();
186	uint32_t activeKeys = hidKeysHeld() & 0xF00003FF;
187	activeKeys |= activeKeys >> 24;
188	return activeKeys;
189}
190
191static void _incrementScreenMode(struct GBAGUIRunner* runner) {
192	UNUSED(runner);
193	// Clear the buffer
194	_drawStart();
195	_drawEnd();
196	_drawStart();
197	_drawEnd();
198	screenMode = (screenMode + 1) % SM_MAX;
199}
200
201static uint32_t _pollInput(void) {
202	hidScanInput();
203	uint32_t keys = 0;
204	int activeKeys = hidKeysHeld();
205	if (activeKeys & KEY_X) {
206		keys |= 1 << GUI_INPUT_CANCEL;
207	}
208	if (activeKeys & KEY_Y) {
209		keys |= 1 << GBA_GUI_INPUT_SCREEN_MODE;
210	}
211	if (activeKeys & KEY_B) {
212		keys |= 1 << GUI_INPUT_BACK;
213	}
214	if (activeKeys & KEY_A) {
215		keys |= 1 << GUI_INPUT_SELECT;
216	}
217	if (activeKeys & KEY_LEFT) {
218		keys |= 1 << GUI_INPUT_LEFT;
219	}
220	if (activeKeys & KEY_RIGHT) {
221		keys |= 1 << GUI_INPUT_RIGHT;
222	}
223	if (activeKeys & KEY_UP) {
224		keys |= 1 << GUI_INPUT_UP;
225	}
226	if (activeKeys & KEY_DOWN) {
227		keys |= 1 << GUI_INPUT_DOWN;
228	}
229	if (activeKeys & KEY_CSTICK_UP) {
230		keys |= 1 << GBA_GUI_INPUT_INCREASE_BRIGHTNESS;
231	}
232	if (activeKeys & KEY_CSTICK_DOWN) {
233		keys |= 1 << GBA_GUI_INPUT_DECREASE_BRIGHTNESS;
234	}
235	return keys;
236}
237
238static void _sampleRotation(struct GBARotationSource* source) {
239	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
240	// Work around ctrulib getting the entries wrong
241	rotation->accel = *(accelVector*)& hidSharedMem[0x48];
242	rotation->gyro = *(angularRate*)& hidSharedMem[0x5C];
243}
244
245static int32_t _readTiltX(struct GBARotationSource* source) {
246	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
247	return rotation->accel.x << 18L;
248}
249
250static int32_t _readTiltY(struct GBARotationSource* source) {
251	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
252	return rotation->accel.y << 18L;
253}
254
255static int32_t _readGyroZ(struct GBARotationSource* source) {
256	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
257	return rotation->gyro.y << 18L; // Yes, y
258}
259
260static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio) {
261	UNUSED(stream);
262#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
263	blip_read_samples(audio->left, &audioLeft[audioPos], AUDIO_SAMPLES, false);
264	blip_read_samples(audio->right, &audioRight[audioPos], AUDIO_SAMPLES, false);
265#elif RESAMPLE_LIBRARY == RESAMPLE_NN
266	GBAAudioCopy(audio, &audioLeft[audioPos], &audioRight[audioPos], AUDIO_SAMPLES);
267#endif
268	GSPGPU_FlushDataCache(0, (void*) &audioLeft[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
269	GSPGPU_FlushDataCache(0, (void*) &audioRight[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
270	audioPos = (audioPos + AUDIO_SAMPLES) % AUDIO_SAMPLE_BUFFER;
271	if (audioPos == AUDIO_SAMPLES * 3) {
272		u8 playing = 0;
273		csndIsPlaying(0x8, &playing);
274		if (!playing) {
275			CSND_SetPlayState(0x8, 1);
276			CSND_SetPlayState(0x9, 1);
277			csndExecCmds(false);
278		}
279	}
280}
281
282int main() {
283	hasSound = !csndInit();
284
285	rotation.d.sample = _sampleRotation;
286	rotation.d.readTiltX = _readTiltX;
287	rotation.d.readTiltY = _readTiltY;
288	rotation.d.readGyroZ = _readGyroZ;
289
290	stream.postVideoFrame = 0;
291	stream.postAudioFrame = 0;
292	stream.postAudioBuffer = _postAudioBuffer;
293
294	if (!allocateRomBuffer()) {
295		return 1;
296	}
297
298	if (hasSound) {
299		audioLeft = linearAlloc(AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
300		audioRight = linearAlloc(AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
301	}
302
303	sf2d_init();
304	sf2d_set_clear_color(0);
305	tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_VRAM);
306
307	sdmcArchive = (FS_archive) {
308		ARCH_SDMC,
309		(FS_path) { PATH_EMPTY, 1, (const u8*)"" },
310		0, 0
311	};
312	FSUSER_OpenArchive(0, &sdmcArchive);
313
314	logFile = VFileOpen("/mgba.log", O_WRONLY | O_CREAT | O_TRUNC);
315	struct GUIFont* font = GUIFontCreate();
316
317	if (!font) {
318		goto cleanup;
319	}
320
321	struct GBAGUIRunner runner = {
322		.params = {
323			320, 240,
324			font, "/",
325			_drawStart, _drawEnd, _pollInput,
326			0, 0,
327
328			GUI_PARAMS_TRAIL
329		},
330		.setup = _setup,
331		.teardown = 0,
332		.gameLoaded = _gameLoaded,
333		.gameUnloaded = _gameUnloaded,
334		.prepareForFrame = 0,
335		.drawFrame = _drawFrame,
336		.drawScreenshot = _drawScreenshot,
337		.paused = _gameUnloaded,
338		.unpaused = _gameLoaded,
339		.incrementScreenMode = _incrementScreenMode,
340		.pollGameInput = _pollGameInput
341	};
342	GBAGUIInit(&runner, 0);
343	GBAGUIRunloop(&runner);
344	GBAGUIDeinit(&runner);
345
346cleanup:
347	linearFree(renderer.outputBuffer);
348
349	if (logFile) {
350		logFile->close(logFile);
351	}
352
353	sf2d_free_texture(tex);
354	sf2d_fini();
355
356	if (hasSound) {
357		linearFree(audioLeft);
358		linearFree(audioRight);
359	}
360	csndExit();
361	return 0;
362}
363
364static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
365	UNUSED(thread);
366	UNUSED(level);
367	if (!logFile) {
368		return;
369	}
370	char out[256];
371	size_t len = vsnprintf(out, sizeof(out), format, args);
372	if (len >= sizeof(out)) {
373		len = sizeof(out) - 1;
374	}
375	out[len] = '\n';
376	logFile->write(logFile, out, len + 1);
377}