all repos — mgba @ 44736f89be783d41571bb62347f42eeeb43b74a5

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	sf2d_draw_texture_scale_blend(tex, 40, 296, 1, -1, 0xFFFFFF3F | (faded ? 0 : 0xC0));
120}
121
122static uint16_t _pollGameInput(struct GBAGUIRunner* runner) {
123	hidScanInput();
124	uint32_t activeKeys = hidKeysHeld() & 0xF00003FF;
125	activeKeys |= activeKeys >> 24;
126	return activeKeys;
127}
128
129static int _pollInput(void) {
130	hidScanInput();
131	int keys = 0;
132	int activeKeys = hidKeysHeld();
133	if (activeKeys & KEY_X) {
134		keys |= 1 << GUI_INPUT_CANCEL;
135	}
136	if (activeKeys & KEY_B) {
137		keys |= 1 << GUI_INPUT_BACK;
138	}
139	if (activeKeys & KEY_A) {
140		keys |= 1 << GUI_INPUT_SELECT;
141	}
142	if (activeKeys & KEY_LEFT) {
143		keys |= 1 << GUI_INPUT_LEFT;
144	}
145	if (activeKeys & KEY_RIGHT) {
146		keys |= 1 << GUI_INPUT_RIGHT;
147	}
148	if (activeKeys & KEY_UP) {
149		keys |= 1 << GUI_INPUT_UP;
150	}
151	if (activeKeys & KEY_DOWN) {
152		keys |= 1 << GUI_INPUT_DOWN;
153	}
154	return keys;
155}
156
157static void _sampleRotation(struct GBARotationSource* source) {
158	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
159	// Work around ctrulib getting the entries wrong
160	rotation->accel = *(accelVector*)& hidSharedMem[0x48];
161	rotation->gyro = *(angularRate*)& hidSharedMem[0x5C];
162}
163
164static int32_t _readTiltX(struct GBARotationSource* source) {
165	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
166	return rotation->accel.x << 18L;
167}
168
169static int32_t _readTiltY(struct GBARotationSource* source) {
170	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
171	return rotation->accel.y << 18L;
172}
173
174static int32_t _readGyroZ(struct GBARotationSource* source) {
175	struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
176	return rotation->gyro.y << 18L; // Yes, y
177}
178
179static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio) {
180	UNUSED(stream);
181	memset(audioLeft, 0, AUDIO_SAMPLES * sizeof(int16_t));
182	memset(audioRight, 0, AUDIO_SAMPLES * sizeof(int16_t));
183#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
184	blip_read_samples(audio->left, audioLeft, AUDIO_SAMPLES, false);
185	blip_read_samples(audio->right, audioRight, AUDIO_SAMPLES, false);
186#elif RESAMPLE_LIBRARY == RESAMPLE_NN
187	GBAAudioCopy(audio, audioLeft, audioRight, AUDIO_SAMPLES);
188#endif
189	GSPGPU_FlushDataCache(0, (void*) audioLeft, AUDIO_SAMPLES * sizeof(int16_t));
190	GSPGPU_FlushDataCache(0, (void*) audioRight, AUDIO_SAMPLES * sizeof(int16_t));
191	csndPlaySound(0x8, SOUND_ONE_SHOT | SOUND_FORMAT_16BIT, 0x8000, 1.0, -1.0, audioLeft, audioLeft, AUDIO_SAMPLES * sizeof(int16_t));
192	csndPlaySound(0x9, SOUND_ONE_SHOT | SOUND_FORMAT_16BIT, 0x8000, 1.0, 1.0, audioRight, audioRight, AUDIO_SAMPLES * sizeof(int16_t));
193	CSND_SetPlayState(0x8, 1);
194	CSND_SetPlayState(0x9, 1);
195	csndExecCmds(false);
196}
197
198int main() {
199	hasSound = !csndInit();
200
201	rotation.d.sample = _sampleRotation;
202	rotation.d.readTiltX = _readTiltX;
203	rotation.d.readTiltY = _readTiltY;
204	rotation.d.readGyroZ = _readGyroZ;
205
206	stream.postVideoFrame = 0;
207	stream.postAudioFrame = 0;
208	stream.postAudioBuffer = _postAudioBuffer;
209
210	if (!allocateRomBuffer()) {
211		return 1;
212	}
213
214	if (hasSound) {
215		audioLeft = linearAlloc(AUDIO_SAMPLES * sizeof(int16_t));
216		audioRight = linearAlloc(AUDIO_SAMPLES * sizeof(int16_t));
217	}
218
219	sf2d_init();
220	sf2d_set_clear_color(0);
221	tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_RAM);
222	memset(tex->data, 0, 256 * 256 * 2);
223
224	sdmcArchive = (FS_archive) {
225		ARCH_SDMC,
226		(FS_path) { PATH_EMPTY, 1, (const u8*)"" },
227		0, 0
228	};
229	FSUSER_OpenArchive(0, &sdmcArchive);
230
231	logFile = VFileOpen("/mgba.log", O_WRONLY | O_CREAT | O_TRUNC);
232	struct GUIFont* font = GUIFontCreate();
233
234	if (!font) {
235		goto cleanup;
236	}
237
238	struct GBAGUIRunner runner = {
239		.params = {
240			320, 240,
241			font, "/",
242			_drawStart, _drawEnd, _pollInput,
243			0, 0,
244
245			GUI_PARAMS_TRAIL
246		},
247		.setup = _setup,
248		.teardown = 0,
249		.gameLoaded = _gameLoaded,
250		.gameUnloaded = _gameUnloaded,
251		.prepareForFrame = 0,
252		.drawFrame = _drawFrame,
253		.pollGameInput = _pollGameInput
254	};
255	GBAGUIInit(&runner, 0);
256	GBAGUIRunloop(&runner);
257	GBAGUIDeinit(&runner);
258
259cleanup:
260	linearFree(renderer.outputBuffer);
261
262	if (logFile) {
263		logFile->close(logFile);
264	}
265
266	sf2d_free_texture(tex);
267	sf2d_fini();
268
269	if (hasSound) {
270		linearFree(audioLeft);
271		linearFree(audioRight);
272	}
273	csndExit();
274	return 0;
275}
276
277static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
278	UNUSED(thread);
279	UNUSED(level);
280	if (!logFile) {
281		return;
282	}
283	char out[256];
284	size_t len = vsnprintf(out, sizeof(out), format, args);
285	if (len >= sizeof(out)) {
286		len = sizeof(out) - 1;
287	}
288	out[len] = '\n';
289	logFile->write(logFile, out, len + 1);
290}