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 * VIDEO_VERTICAL_PIXELS * 2, 0x80);
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, 0x80);
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 enum GUICursorState _pollCursor(int* x, int* y) {
239 hidScanInput();
240 if (!(hidKeysHeld() & KEY_TOUCH)) {
241 return GUI_CURSOR_NOT_PRESENT;
242 }
243 touchPosition pos;
244 hidTouchRead(&pos);
245 *x = pos.px;
246 *y = pos.py;
247 return GUI_CURSOR_DOWN;
248}
249
250static void _sampleRotation(struct GBARotationSource* source) {
251 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
252 // Work around ctrulib getting the entries wrong
253 rotation->accel = *(accelVector*)& hidSharedMem[0x48];
254 rotation->gyro = *(angularRate*)& hidSharedMem[0x5C];
255}
256
257static int32_t _readTiltX(struct GBARotationSource* source) {
258 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
259 return rotation->accel.x << 18L;
260}
261
262static int32_t _readTiltY(struct GBARotationSource* source) {
263 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
264 return rotation->accel.y << 18L;
265}
266
267static int32_t _readGyroZ(struct GBARotationSource* source) {
268 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
269 return rotation->gyro.y << 18L; // Yes, y
270}
271
272static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio) {
273 UNUSED(stream);
274#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
275 blip_read_samples(audio->left, &audioLeft[audioPos], AUDIO_SAMPLES, false);
276 blip_read_samples(audio->right, &audioRight[audioPos], AUDIO_SAMPLES, false);
277#elif RESAMPLE_LIBRARY == RESAMPLE_NN
278 GBAAudioCopy(audio, &audioLeft[audioPos], &audioRight[audioPos], AUDIO_SAMPLES);
279#endif
280 GSPGPU_FlushDataCache(0, (void*) &audioLeft[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
281 GSPGPU_FlushDataCache(0, (void*) &audioRight[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
282 audioPos = (audioPos + AUDIO_SAMPLES) % AUDIO_SAMPLE_BUFFER;
283 if (audioPos == AUDIO_SAMPLES * 3) {
284 u8 playing = 0;
285 csndIsPlaying(0x8, &playing);
286 if (!playing) {
287 CSND_SetPlayState(0x8, 1);
288 CSND_SetPlayState(0x9, 1);
289 csndExecCmds(false);
290 }
291 }
292}
293
294int main() {
295 hasSound = !csndInit();
296
297 rotation.d.sample = _sampleRotation;
298 rotation.d.readTiltX = _readTiltX;
299 rotation.d.readTiltY = _readTiltY;
300 rotation.d.readGyroZ = _readGyroZ;
301
302 stream.postVideoFrame = 0;
303 stream.postAudioFrame = 0;
304 stream.postAudioBuffer = _postAudioBuffer;
305
306 if (!allocateRomBuffer()) {
307 return 1;
308 }
309
310 if (hasSound) {
311 audioLeft = linearMemAlign(AUDIO_SAMPLE_BUFFER * sizeof(int16_t), 0x80);
312 audioRight = linearMemAlign(AUDIO_SAMPLE_BUFFER * sizeof(int16_t), 0x80);
313 }
314
315 sf2d_init();
316 sf2d_set_clear_color(0);
317 tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_VRAM);
318
319 sdmcArchive = (FS_archive) {
320 ARCH_SDMC,
321 (FS_path) { PATH_EMPTY, 1, (const u8*)"" },
322 0, 0
323 };
324 FSUSER_OpenArchive(0, &sdmcArchive);
325
326 logFile = VFileOpen("/mgba.log", O_WRONLY | O_CREAT | O_TRUNC);
327 struct GUIFont* font = GUIFontCreate();
328
329 if (!font) {
330 goto cleanup;
331 }
332
333 struct GBAGUIRunner runner = {
334 .params = {
335 320, 240,
336 font, "/",
337 _drawStart, _drawEnd,
338 _pollInput, _pollCursor,
339 0, 0,
340
341 GUI_PARAMS_TRAIL
342 },
343 .setup = _setup,
344 .teardown = 0,
345 .gameLoaded = _gameLoaded,
346 .gameUnloaded = _gameUnloaded,
347 .prepareForFrame = 0,
348 .drawFrame = _drawFrame,
349 .drawScreenshot = _drawScreenshot,
350 .paused = _gameUnloaded,
351 .unpaused = _gameLoaded,
352 .incrementScreenMode = _incrementScreenMode,
353 .pollGameInput = _pollGameInput
354 };
355 GBAGUIInit(&runner, 0);
356 GBAGUIRunloop(&runner);
357 GBAGUIDeinit(&runner);
358
359cleanup:
360 linearFree(renderer.outputBuffer);
361
362 if (logFile) {
363 logFile->close(logFile);
364 }
365
366 sf2d_free_texture(tex);
367 sf2d_fini();
368
369 if (hasSound) {
370 linearFree(audioLeft);
371 linearFree(audioRight);
372 }
373 csndExit();
374 return 0;
375}
376
377static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
378 UNUSED(thread);
379 UNUSED(level);
380 if (!logFile) {
381 return;
382 }
383 char out[256];
384 size_t len = vsnprintf(out, sizeof(out), format, args);
385 if (len >= sizeof(out)) {
386 len = sizeof(out) - 1;
387 }
388 out[len] = '\n';
389 logFile->write(logFile, out, len + 1);
390}