all repos — mgba @ 33e3fb9a454e5377515024af19111e510b5afad8

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