all repos — mgba @ 74e3826b13c76a1cc4e8088a65968bd177050760

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
 21#define AUDIO_SAMPLES 0x800
 22
 23FS_archive sdmcArchive;
 24
 25static struct GBA3DSRotationSource {
 26	struct GBARotationSource d;
 27	accelVector accel;
 28	angularRate gyro;
 29} rotation;
 30
 31static struct VFile* logFile;
 32static bool hasSound;
 33// TODO: Move into context
 34static struct GBAVideoSoftwareRenderer renderer;
 35static int16_t* audioLeft = 0;
 36static int16_t* audioRight = 0;
 37static sf2d_texture* tex;
 38static size_t audioPos = 0;
 39
 40extern bool allocateRomBuffer(void);
 41static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
 42
 43static void _drawStart(void) {
 44	sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
 45}
 46
 47static void _drawEnd(void) {
 48	sf2d_end_frame();
 49	sf2d_swapbuffers();
 50}
 51
 52static void _setup(struct GBAGUIRunner* runner) {
 53	struct GBAOptions opts = {
 54		.useBios = true,
 55		.logLevel = 0,
 56		.idleOptimization = IDLE_LOOP_DETECT
 57	};
 58	GBAConfigLoadDefaults(&runner->context.config, &opts);
 59	runner->context.gba->logHandler = GBA3DSLog;
 60	runner->context.gba->rotationSource = &rotation.d;
 61
 62	GBAVideoSoftwareRendererCreate(&renderer);
 63	renderer.outputBuffer = anonymousMemoryMap(256 * VIDEO_VERTICAL_PIXELS * 2);
 64	renderer.outputBufferStride = 256;
 65	runner->context.renderer = &renderer.d;
 66}
 67
 68static void _gameLoaded(struct GBAGUIRunner* runner) {
 69	if (runner->context.gba->memory.hw.devices & HW_TILT) {
 70		HIDUSER_EnableAccelerometer();
 71	}
 72	if (runner->context.gba->memory.hw.devices & HW_GYRO) {
 73		HIDUSER_EnableGyroscope();
 74	}
 75
 76#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
 77	blip_set_rates(runner->context.gba->audio.left,  GBA_ARM7TDMI_FREQUENCY, 0x8000);
 78	blip_set_rates(runner->context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 0x8000);
 79#endif
 80	memset(audioLeft, 0, AUDIO_SAMPLES * 2 * sizeof(int16_t));
 81	memset(audioRight, 0, AUDIO_SAMPLES * 2 * sizeof(int16_t));
 82}
 83
 84static void _gameUnloaded(struct GBAGUIRunner* runner) {
 85	CSND_SetPlayState(8, 0);
 86	CSND_SetPlayState(9, 0);
 87	csndExecCmds(0);
 88
 89	if (runner->context.gba->memory.hw.devices & HW_TILT) {
 90		HIDUSER_DisableAccelerometer();
 91	}
 92	if (runner->context.gba->memory.hw.devices & HW_GYRO) {
 93		HIDUSER_DisableGyroscope();
 94	}
 95}
 96
 97static void _drawFrame(struct GBAGUIRunner* runner, bool faded) {
 98	GX_SetDisplayTransfer(0, renderer.outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
 99	GSPGPU_FlushDataCache(0, tex->data, 256 * VIDEO_VERTICAL_PIXELS * 2);
100#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
101	if (hasSound) {
102		memset(&audioLeft[audioPos], 0, AUDIO_SAMPLES);
103		memset(&audioRight[audioPos], 0, AUDIO_SAMPLES);
104		size_t samples = blip_read_samples(runner->context.gba->audio.left, &audioLeft[audioPos], AUDIO_SAMPLES, false);
105		blip_read_samples(runner->context.gba->audio.right, &audioRight[audioPos], AUDIO_SAMPLES, false);
106		size_t audioPosNew = (audioPos + AUDIO_SAMPLES) % (AUDIO_SAMPLES * 2);
107		GSPGPU_FlushDataCache(0, (void*) audioLeft, AUDIO_SAMPLES * 2 * sizeof(int16_t));
108		GSPGPU_FlushDataCache(0, (void*) audioRight, AUDIO_SAMPLES * 2 * sizeof(int16_t));
109		csndPlaySound(0x8, SOUND_ONE_SHOT | SOUND_FORMAT_16BIT, 0x8000, 1.0, -1.0, &audioLeft[audioPos], &audioLeft[audioPosNew], samples * 2);
110		csndPlaySound(0x9, SOUND_ONE_SHOT | SOUND_FORMAT_16BIT, 0x8000, 1.0, 1.0, &audioRight[audioPos], &audioLeft[audioPosNew], samples * 2);
111		audioPos = audioPosNew;
112	} else {
113		blip_clear(runner->context.gba->audio.left);
114		blip_clear(runner->context.gba->audio.right);
115	}
116#endif
117	gspWaitForPPF();
118	_drawStart();
119	sf2d_draw_texture_scale_blend(tex, 40, 296, 1, -1, 0xFFFFFF3F | (faded ? 0 : 0xC0));
120	_drawEnd();
121}
122
123static uint16_t _pollGameInput(struct GBAGUIRunner* runner) {
124	hidScanInput();
125	uint32_t activeKeys = hidKeysHeld() & 0xF00003FF;
126	activeKeys |= activeKeys >> 24;
127	return activeKeys;
128}
129
130static int _pollInput(void) {
131	hidScanInput();
132	int keys = 0;
133	int activeKeys = hidKeysHeld();
134	if (activeKeys & KEY_X) {
135		keys |= 1 << GUI_INPUT_CANCEL;
136	}
137	if (activeKeys & KEY_B) {
138		keys |= 1 << GUI_INPUT_BACK;
139	}
140	if (activeKeys & KEY_A) {
141		keys |= 1 << GUI_INPUT_SELECT;
142	}
143	if (activeKeys & KEY_LEFT) {
144		keys |= 1 << GUI_INPUT_LEFT;
145	}
146	if (activeKeys & KEY_RIGHT) {
147		keys |= 1 << GUI_INPUT_RIGHT;
148	}
149	if (activeKeys & KEY_UP) {
150		keys |= 1 << GUI_INPUT_UP;
151	}
152	if (activeKeys & KEY_DOWN) {
153		keys |= 1 << GUI_INPUT_DOWN;
154	}
155	return keys;
156}
157
158static void _sampleRotation(struct GBARotationSource* source) {
159	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
160	// Work around ctrulib getting the entries wrong
161	rotation->accel = *(accelVector*)& hidSharedMem[0x48];
162	rotation->gyro = *(angularRate*)& hidSharedMem[0x5C];
163}
164
165static int32_t _readTiltX(struct GBARotationSource* source) {
166	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
167	return rotation->accel.x << 18L;
168}
169
170static int32_t _readTiltY(struct GBARotationSource* source) {
171	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
172	return rotation->accel.y << 18L;
173}
174
175static int32_t _readGyroZ(struct GBARotationSource* source) {
176	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
177	return rotation->gyro.y << 18L; // Yes, y
178}
179
180int main() {
181	hasSound = !csndInit();
182
183	rotation.d.sample = _sampleRotation;
184	rotation.d.readTiltX = _readTiltX;
185	rotation.d.readTiltY = _readTiltY;
186	rotation.d.readGyroZ = _readGyroZ;
187
188	if (!allocateRomBuffer()) {
189		return 1;
190	}
191
192	if (hasSound) {
193		audioLeft = linearAlloc(AUDIO_SAMPLES * 2 * sizeof(int16_t));
194		audioRight = linearAlloc(AUDIO_SAMPLES * 2 * sizeof(int16_t));
195	}
196
197	sf2d_init();
198	sf2d_set_clear_color(0);
199	tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_RAM);
200	memset(tex->data, 0, 256 * 256 * 2);
201
202	sdmcArchive = (FS_archive) {
203		ARCH_SDMC,
204		(FS_path) { PATH_EMPTY, 1, (const u8*)"" },
205		0, 0
206	};
207	FSUSER_OpenArchive(0, &sdmcArchive);
208
209	logFile = VFileOpen("/mgba.log", O_WRONLY | O_CREAT | O_TRUNC);
210	struct GUIFont* font = GUIFontCreate();
211
212	if (!font) {
213		goto cleanup;
214	}
215
216	struct GBAGUIRunner runner = {
217		.params = {
218			320, 240,
219			font, "/",
220			_drawStart, _drawEnd, _pollInput,
221			0, 0,
222
223			GUI_PARAMS_TRAIL
224		},
225		.setup = _setup,
226		.teardown = 0,
227		.gameLoaded = _gameLoaded,
228		.gameUnloaded = _gameUnloaded,
229		.prepareForFrame = 0,
230		.drawFrame = _drawFrame,
231		.pollGameInput = _pollGameInput
232	};
233	GBAGUIInit(&runner, 0);
234	GBAGUIRunloop(&runner);
235	GBAGUIDeinit(&runner);
236
237cleanup:
238	mappedMemoryFree(renderer.outputBuffer, 0);
239
240	if (logFile) {
241		logFile->close(logFile);
242	}
243
244	sf2d_free_texture(tex);
245	sf2d_fini();
246
247	if (hasSound) {
248		linearFree(audioLeft);
249		linearFree(audioRight);
250	}
251	csndExit();
252	return 0;
253}
254
255static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
256	UNUSED(thread);
257	UNUSED(level);
258	if (!logFile) {
259		return;
260	}
261	char out[256];
262	size_t len = vsnprintf(out, sizeof(out), format, args);
263	if (len >= sizeof(out)) {
264		len = sizeof(out) - 1;
265	}
266	out[len] = '\n';
267	logFile->write(logFile, out, len + 1);
268}