all repos — mgba @ 5d3b6d5fd84dee29cb706075c08011c83ed10621

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