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