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/gui/menu.h"
15#include "util/memory.h"
16
17#include "3ds-vfs.h"
18#include "ctr-gpu.h"
19
20#include <3ds.h>
21
22static enum ScreenMode {
23 SM_PA_BOTTOM,
24 SM_AF_BOTTOM,
25 SM_SF_BOTTOM,
26 SM_PA_TOP,
27 SM_AF_TOP,
28 SM_SF_TOP,
29 SM_MAX
30} screenMode = SM_PA_TOP;
31
32#define AUDIO_SAMPLES 0x80
33#define AUDIO_SAMPLE_BUFFER (AUDIO_SAMPLES * 24)
34
35FS_archive sdmcArchive;
36
37static struct GBA3DSRotationSource {
38 struct GBARotationSource d;
39 accelVector accel;
40 angularRate gyro;
41} rotation;
42
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 struct ctrTexture gbaOutputTexture;
51
52extern bool allocateRomBuffer(void);
53
54static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio);
55
56static void _drawStart(void) {
57 ctrGpuBeginFrame();
58 if (screenMode < SM_PA_TOP) {
59 ctrSetViewportSize(320, 240);
60 } else {
61 ctrSetViewportSize(400, 240);
62 }
63}
64
65static void _drawEnd(void) {
66 int screen = screenMode < SM_PA_TOP ? GFX_BOTTOM : GFX_TOP;
67 u16 width = 0, height = 0;
68
69 void* outputFramebuffer = gfxGetFramebuffer(screen, GFX_LEFT, &height, &width);
70 ctrGpuEndFrame(outputFramebuffer, width, height);
71 gfxSwapBuffersGpu();
72 gspWaitForEvent(GSPEVENT_VBlank0, false);
73}
74
75static void _setup(struct GBAGUIRunner* runner) {
76 runner->context.gba->rotationSource = &rotation.d;
77 if (hasSound) {
78 runner->context.gba->stream = &stream;
79 }
80
81 GBAVideoSoftwareRendererCreate(&renderer);
82 renderer.outputBuffer = linearMemAlign(256 * VIDEO_VERTICAL_PIXELS * 2, 0x80);
83 renderer.outputBufferStride = 256;
84 runner->context.renderer = &renderer.d;
85
86 unsigned mode;
87 if (GBAConfigGetUIntValue(&runner->context.config, "screenMode", &mode) && mode < SM_MAX) {
88 screenMode = mode;
89 }
90
91 GBAAudioResizeBuffer(&runner->context.gba->audio, AUDIO_SAMPLES);
92}
93
94static void _gameLoaded(struct GBAGUIRunner* runner) {
95 if (runner->context.gba->memory.hw.devices & HW_TILT) {
96 HIDUSER_EnableAccelerometer();
97 }
98 if (runner->context.gba->memory.hw.devices & HW_GYRO) {
99 HIDUSER_EnableGyroscope();
100 }
101
102#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
103 double ratio = GBAAudioCalculateRatio(1, 59.8260982880808, 1);
104 blip_set_rates(runner->context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 32768 * ratio);
105 blip_set_rates(runner->context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 32768 * ratio);
106#endif
107 if (hasSound) {
108 memset(audioLeft, 0, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
109 memset(audioRight, 0, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
110 audioPos = 0;
111 csndPlaySound(0x8, SOUND_REPEAT | SOUND_FORMAT_16BIT, 32768, 1.0, -1.0, audioLeft, audioLeft, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
112 csndPlaySound(0x9, SOUND_REPEAT | SOUND_FORMAT_16BIT, 32768, 1.0, 1.0, audioRight, audioRight, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
113 }
114}
115
116static void _gameUnloaded(struct GBAGUIRunner* runner) {
117 if (hasSound) {
118 CSND_SetPlayState(8, 0);
119 CSND_SetPlayState(9, 0);
120 csndExecCmds(false);
121 }
122
123 if (runner->context.gba->memory.hw.devices & HW_TILT) {
124 HIDUSER_DisableAccelerometer();
125 }
126 if (runner->context.gba->memory.hw.devices & HW_GYRO) {
127 HIDUSER_DisableGyroscope();
128 }
129}
130
131static void _drawTex(bool faded) {
132 u32 color = faded ? 0x3FFFFFFF : 0xFFFFFFFF;
133
134 int screen_w = screenMode < SM_PA_TOP ? 320 : 400;
135 int screen_h = 240;
136
137 int w, h;
138
139 switch (screenMode) {
140 case SM_PA_TOP:
141 case SM_PA_BOTTOM:
142 default:
143 w = VIDEO_HORIZONTAL_PIXELS;
144 h = VIDEO_VERTICAL_PIXELS;
145 break;
146 case SM_AF_TOP:
147 w = 360;
148 h = 240;
149 break;
150 case SM_AF_BOTTOM:
151 // Largest possible size with 3:2 aspect ratio and integer dimensions
152 w = 318;
153 h = 212;
154 break;
155 case SM_SF_TOP:
156 case SM_SF_BOTTOM:
157 w = screen_w;
158 h = screen_h;
159 break;
160 }
161
162 int x = (screen_w - w) / 2;
163 int y = (screen_h - h) / 2;
164
165 ctrAddRectScaled(color, x, y, w, h, 0, 0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
166}
167
168static void _drawFrame(struct GBAGUIRunner* runner, bool faded) {
169 UNUSED(runner);
170
171 void* outputBuffer = renderer.outputBuffer;
172 struct ctrTexture* tex = &gbaOutputTexture;
173
174 GSPGPU_FlushDataCache(NULL, outputBuffer, 256 * VIDEO_VERTICAL_PIXELS * 2);
175 GX_SetDisplayTransfer(NULL,
176 outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS),
177 tex->data, GX_BUFFER_DIM(256, 256),
178 GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGB565) |
179 GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB565) |
180 GX_TRANSFER_OUT_TILED(1) | GX_TRANSFER_FLIP_VERT(1));
181
182#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
183 if (!hasSound) {
184 blip_clear(runner->context.gba->audio.left);
185 blip_clear(runner->context.gba->audio.right);
186 }
187#endif
188
189 gspWaitForPPF();
190 ctrActivateTexture(tex);
191 _drawTex(faded);
192}
193
194static void _drawScreenshot(struct GBAGUIRunner* runner, const uint32_t* pixels, bool faded) {
195 UNUSED(runner);
196
197 struct ctrTexture* tex = &gbaOutputTexture;
198
199 u16* newPixels = linearMemAlign(256 * VIDEO_VERTICAL_PIXELS * sizeof(u32), 0x100);
200
201 // Convert image from RGBX8 to BGR565
202 for (unsigned y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
203 for (unsigned x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
204 // 0xXXBBGGRR -> 0bRRRRRGGGGGGBBBBB
205 u32 p = *pixels++;
206 newPixels[y * 256 + x] =
207 (p << 24 >> (24 + 3) << 11) | // R
208 (p << 16 >> (24 + 2) << 5) | // G
209 (p << 8 >> (24 + 3) << 0); // B
210 }
211 memset(&newPixels[y * 256 + VIDEO_HORIZONTAL_PIXELS], 0, (256 - VIDEO_HORIZONTAL_PIXELS) * sizeof(u32));
212 }
213
214 GSPGPU_FlushDataCache(NULL, (void*)newPixels, 256 * VIDEO_VERTICAL_PIXELS * sizeof(u32));
215 GX_SetDisplayTransfer(NULL,
216 (void*)newPixels, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS),
217 tex->data, GX_BUFFER_DIM(256, 256),
218 GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGB565) |
219 GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB565) |
220 GX_TRANSFER_OUT_TILED(1) | GX_TRANSFER_FLIP_VERT(1));
221 gspWaitForPPF();
222 linearFree(newPixels);
223
224 ctrActivateTexture(tex);
225 _drawTex(faded);
226}
227
228static uint16_t _pollGameInput(struct GBAGUIRunner* runner) {
229 UNUSED(runner);
230
231 hidScanInput();
232 uint32_t activeKeys = hidKeysHeld() & 0xF00003FF;
233 activeKeys |= activeKeys >> 24;
234 return activeKeys;
235}
236
237static void _incrementScreenMode(struct GBAGUIRunner* runner) {
238 UNUSED(runner);
239 // Clear the buffer
240 _drawStart();
241 _drawEnd();
242 _drawStart();
243 _drawEnd();
244 unsigned mode;
245 if (GBAConfigGetUIntValue(&runner->context.config, "screenMode", &mode) && mode != screenMode) {
246 screenMode = mode;
247 } else {
248 screenMode = (screenMode + 1) % SM_MAX;
249 GBAConfigSetUIntValue(&runner->context.config, "screenMode", screenMode);
250 }
251}
252
253static uint32_t _pollInput(void) {
254 hidScanInput();
255 uint32_t keys = 0;
256 int activeKeys = hidKeysHeld();
257 if (activeKeys & KEY_X) {
258 keys |= 1 << GUI_INPUT_CANCEL;
259 }
260 if (activeKeys & KEY_Y) {
261 keys |= 1 << GBA_GUI_INPUT_SCREEN_MODE;
262 }
263 if (activeKeys & KEY_B) {
264 keys |= 1 << GUI_INPUT_BACK;
265 }
266 if (activeKeys & KEY_A) {
267 keys |= 1 << GUI_INPUT_SELECT;
268 }
269 if (activeKeys & KEY_LEFT) {
270 keys |= 1 << GUI_INPUT_LEFT;
271 }
272 if (activeKeys & KEY_RIGHT) {
273 keys |= 1 << GUI_INPUT_RIGHT;
274 }
275 if (activeKeys & KEY_UP) {
276 keys |= 1 << GUI_INPUT_UP;
277 }
278 if (activeKeys & KEY_DOWN) {
279 keys |= 1 << GUI_INPUT_DOWN;
280 }
281 if (activeKeys & KEY_CSTICK_UP) {
282 keys |= 1 << GBA_GUI_INPUT_INCREASE_BRIGHTNESS;
283 }
284 if (activeKeys & KEY_CSTICK_DOWN) {
285 keys |= 1 << GBA_GUI_INPUT_DECREASE_BRIGHTNESS;
286 }
287 return keys;
288}
289
290static enum GUICursorState _pollCursor(int* x, int* y) {
291 hidScanInput();
292 if (!(hidKeysHeld() & KEY_TOUCH)) {
293 return GUI_CURSOR_NOT_PRESENT;
294 }
295 touchPosition pos;
296 hidTouchRead(&pos);
297 *x = pos.px;
298 *y = pos.py;
299 return GUI_CURSOR_DOWN;
300}
301
302static void _sampleRotation(struct GBARotationSource* source) {
303 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
304 // Work around ctrulib getting the entries wrong
305 rotation->accel = *(accelVector*)& hidSharedMem[0x48];
306 rotation->gyro = *(angularRate*)& hidSharedMem[0x5C];
307}
308
309static int32_t _readTiltX(struct GBARotationSource* source) {
310 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
311 return rotation->accel.x << 18L;
312}
313
314static int32_t _readTiltY(struct GBARotationSource* source) {
315 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
316 return rotation->accel.y << 18L;
317}
318
319static int32_t _readGyroZ(struct GBARotationSource* source) {
320 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
321 return rotation->gyro.y << 18L; // Yes, y
322}
323
324static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio) {
325 UNUSED(stream);
326#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
327 blip_read_samples(audio->left, &audioLeft[audioPos], AUDIO_SAMPLES, false);
328 blip_read_samples(audio->right, &audioRight[audioPos], AUDIO_SAMPLES, false);
329#elif RESAMPLE_LIBRARY == RESAMPLE_NN
330 GBAAudioCopy(audio, &audioLeft[audioPos], &audioRight[audioPos], AUDIO_SAMPLES);
331#endif
332 GSPGPU_FlushDataCache(0, (void*) &audioLeft[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
333 GSPGPU_FlushDataCache(0, (void*) &audioRight[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
334 audioPos = (audioPos + AUDIO_SAMPLES) % AUDIO_SAMPLE_BUFFER;
335 if (audioPos == AUDIO_SAMPLES * 3) {
336 u8 playing = 0;
337 csndIsPlaying(0x8, &playing);
338 if (!playing) {
339 CSND_SetPlayState(0x8, 1);
340 CSND_SetPlayState(0x9, 1);
341 csndExecCmds(false);
342 }
343 }
344}
345
346int main() {
347 hasSound = !csndInit();
348
349 rotation.d.sample = _sampleRotation;
350 rotation.d.readTiltX = _readTiltX;
351 rotation.d.readTiltY = _readTiltY;
352 rotation.d.readGyroZ = _readGyroZ;
353
354 stream.postVideoFrame = 0;
355 stream.postAudioFrame = 0;
356 stream.postAudioBuffer = _postAudioBuffer;
357
358 if (!allocateRomBuffer()) {
359 return 1;
360 }
361
362 if (hasSound) {
363 audioLeft = linearMemAlign(AUDIO_SAMPLE_BUFFER * sizeof(int16_t), 0x80);
364 audioRight = linearMemAlign(AUDIO_SAMPLE_BUFFER * sizeof(int16_t), 0x80);
365 }
366
367 gfxInit(GSP_BGR8_OES, GSP_BGR8_OES, false);
368
369 if (ctrInitGpu() < 0) {
370 goto cleanup;
371 }
372
373 ctrTexture_Init(&gbaOutputTexture);
374 gbaOutputTexture.format = GPU_RGB565;
375 gbaOutputTexture.filter = GPU_LINEAR;
376 gbaOutputTexture.width = 256;
377 gbaOutputTexture.height = 256;
378 gbaOutputTexture.data = vramAlloc(256 * 256 * 2);
379 void* outputTextureEnd = (u8*)gbaOutputTexture.data + 256 * 256 * 2;
380
381 // Zero texture data to make sure no garbage around the border interferes with filtering
382 GX_SetMemoryFill(NULL,
383 gbaOutputTexture.data, 0x0000, outputTextureEnd, GX_FILL_16BIT_DEPTH | GX_FILL_TRIGGER,
384 NULL, 0, NULL, 0);
385 gspWaitForPSC0();
386
387 sdmcArchive = (FS_archive) {
388 ARCH_SDMC,
389 (FS_path) { PATH_EMPTY, 1, (const u8*)"" },
390 0, 0
391 };
392 FSUSER_OpenArchive(0, &sdmcArchive);
393
394 struct GUIFont* font = GUIFontCreate();
395
396 if (!font) {
397 goto cleanup;
398 }
399
400 struct GBAGUIRunner runner = {
401 .params = {
402 320, 240,
403 font, "/",
404 _drawStart, _drawEnd,
405 _pollInput, _pollCursor,
406 0, 0,
407
408 GUI_PARAMS_TRAIL
409 },
410 .configExtra = (struct GUIMenuItem[]) {
411 {
412 .title = "Screen mode",
413 .data = "screenMode",
414 .submenu = 0,
415 .state = SM_PA_TOP,
416 .validStates = (const char*[]) {
417 "Pixel-Accurate/Bottom",
418 "Aspect-Ratio Fit/Bottom",
419 "Stretched/Bottom",
420 "Pixel-Accurate/Top",
421 "Aspect-Ratio Fit/Top",
422 "Stretched/Top",
423 0
424 }
425 }
426 },
427 .nConfigExtra = 1,
428 .setup = _setup,
429 .teardown = 0,
430 .gameLoaded = _gameLoaded,
431 .gameUnloaded = _gameUnloaded,
432 .prepareForFrame = 0,
433 .drawFrame = _drawFrame,
434 .drawScreenshot = _drawScreenshot,
435 .paused = _gameUnloaded,
436 .unpaused = _gameLoaded,
437 .incrementScreenMode = _incrementScreenMode,
438 .pollGameInput = _pollGameInput
439 };
440
441 GBAGUIInit(&runner, "3ds");
442 GBAGUIRunloop(&runner);
443 GBAGUIDeinit(&runner);
444
445cleanup:
446 linearFree(renderer.outputBuffer);
447
448 ctrDeinitGpu();
449 vramFree(gbaOutputTexture.data);
450
451 gfxExit();
452
453 if (hasSound) {
454 linearFree(audioLeft);
455 linearFree(audioRight);
456 }
457 csndExit();
458 return 0;
459}