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 default:
126 sf2d_draw_texture_scale_blend(tex, 40, 296, 1, -1, 0xFFFFFF3F | (faded ? 0 : 0xC0));
127 break;
128 case SM_AF_TOP:
129 sf2d_draw_texture_scale_blend(tex, 20, 384, 1.5, -1.5, 0xFFFFFF3F | (faded ? 0 : 0xC0));
130 break;
131 case SM_AF_BOTTOM:
132 sf2d_draw_texture_scale_blend(tex, 0, 368 - 40 / 3, 4 / 3.0, -4 / 3.0, 0xFFFFFF3F | (faded ? 0 : 0xC0));
133 break;
134 case SM_SF_TOP:
135 sf2d_draw_texture_scale_blend(tex, 0, 384, 5 / 3.0, -1.5, 0xFFFFFF3F | (faded ? 0 : 0xC0));
136 break;
137 case SM_SF_BOTTOM:
138 sf2d_draw_texture_scale_blend(tex, 0, 384, 4 / 3.0, -1.5, 0xFFFFFF3F | (faded ? 0 : 0xC0));
139 break;
140 }
141}
142
143static void _drawFrame(struct GBAGUIRunner* runner, bool faded) {
144 UNUSED(runner);
145 GSPGPU_FlushDataCache(0, (u8*) renderer.outputBuffer, 256 * VIDEO_VERTICAL_PIXELS * 2);
146 GX_SetDisplayTransfer(0, (u32*) renderer.outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
147 _drawTex(faded);
148#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
149 if (!hasSound) {
150 blip_clear(runner->context.gba->audio.left);
151 blip_clear(runner->context.gba->audio.right);
152 }
153#endif
154}
155
156static void _drawScreenshot(struct GBAGUIRunner* runner, const uint32_t* pixels, bool faded) {
157 UNUSED(runner);
158 u16* newPixels = linearMemAlign(256 * VIDEO_VERTICAL_PIXELS * 2, 0x80);
159 unsigned y, x;
160 for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
161 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
162 u16 pixel = (*pixels >> 19) & 0x1F;
163 pixel |= (*pixels >> 5) & 0x7C0;
164 pixel |= (*pixels << 8) & 0xF800;
165 newPixels[y * 256 + x] = pixel;
166 ++pixels;
167 }
168 memset(&newPixels[y * 256 + VIDEO_HORIZONTAL_PIXELS], 0, 32);
169 }
170 GSPGPU_FlushDataCache(0, (void*) newPixels, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 2);
171 GX_SetDisplayTransfer(0, (void*) newPixels, GX_BUFFER_DIM(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
172 linearFree(newPixels);
173 _drawTex(faded);
174}
175
176static uint16_t _pollGameInput(struct GBAGUIRunner* runner) {
177 UNUSED(runner);
178 hidScanInput();
179 uint32_t activeKeys = hidKeysHeld() & 0xF00003FF;
180 activeKeys |= activeKeys >> 24;
181 return activeKeys;
182}
183
184static void _incrementScreenMode(struct GBAGUIRunner* runner) {
185 UNUSED(runner);
186 // Clear the buffer
187 _drawStart();
188 _drawEnd();
189 _drawStart();
190 _drawEnd();
191 screenMode = (screenMode + 1) % SM_MAX;
192}
193
194static uint32_t _pollInput(void) {
195 hidScanInput();
196 uint32_t keys = 0;
197 int activeKeys = hidKeysHeld();
198 if (activeKeys & KEY_X) {
199 keys |= 1 << GUI_INPUT_CANCEL;
200 }
201 if (activeKeys & KEY_Y) {
202 keys |= 1 << GBA_GUI_INPUT_SCREEN_MODE;
203 }
204 if (activeKeys & KEY_B) {
205 keys |= 1 << GUI_INPUT_BACK;
206 }
207 if (activeKeys & KEY_A) {
208 keys |= 1 << GUI_INPUT_SELECT;
209 }
210 if (activeKeys & KEY_LEFT) {
211 keys |= 1 << GUI_INPUT_LEFT;
212 }
213 if (activeKeys & KEY_RIGHT) {
214 keys |= 1 << GUI_INPUT_RIGHT;
215 }
216 if (activeKeys & KEY_UP) {
217 keys |= 1 << GUI_INPUT_UP;
218 }
219 if (activeKeys & KEY_DOWN) {
220 keys |= 1 << GUI_INPUT_DOWN;
221 }
222 if (activeKeys & KEY_CSTICK_UP) {
223 keys |= 1 << GBA_GUI_INPUT_INCREASE_BRIGHTNESS;
224 }
225 if (activeKeys & KEY_CSTICK_DOWN) {
226 keys |= 1 << GBA_GUI_INPUT_DECREASE_BRIGHTNESS;
227 }
228 return keys;
229}
230
231static enum GUICursorState _pollCursor(int* x, int* y) {
232 hidScanInput();
233 if (!(hidKeysHeld() & KEY_TOUCH)) {
234 return GUI_CURSOR_NOT_PRESENT;
235 }
236 touchPosition pos;
237 hidTouchRead(&pos);
238 *x = pos.px;
239 *y = pos.py;
240 return GUI_CURSOR_DOWN;
241}
242
243static void _sampleRotation(struct GBARotationSource* source) {
244 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
245 // Work around ctrulib getting the entries wrong
246 rotation->accel = *(accelVector*)& hidSharedMem[0x48];
247 rotation->gyro = *(angularRate*)& hidSharedMem[0x5C];
248}
249
250static int32_t _readTiltX(struct GBARotationSource* source) {
251 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
252 return rotation->accel.x << 18L;
253}
254
255static int32_t _readTiltY(struct GBARotationSource* source) {
256 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
257 return rotation->accel.y << 18L;
258}
259
260static int32_t _readGyroZ(struct GBARotationSource* source) {
261 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
262 return rotation->gyro.y << 18L; // Yes, y
263}
264
265static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio) {
266 UNUSED(stream);
267#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
268 blip_read_samples(audio->left, &audioLeft[audioPos], AUDIO_SAMPLES, false);
269 blip_read_samples(audio->right, &audioRight[audioPos], AUDIO_SAMPLES, false);
270#elif RESAMPLE_LIBRARY == RESAMPLE_NN
271 GBAAudioCopy(audio, &audioLeft[audioPos], &audioRight[audioPos], AUDIO_SAMPLES);
272#endif
273 GSPGPU_FlushDataCache(0, (void*) &audioLeft[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
274 GSPGPU_FlushDataCache(0, (void*) &audioRight[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
275 audioPos = (audioPos + AUDIO_SAMPLES) % AUDIO_SAMPLE_BUFFER;
276 if (audioPos == AUDIO_SAMPLES * 3) {
277 u8 playing = 0;
278 csndIsPlaying(0x8, &playing);
279 if (!playing) {
280 CSND_SetPlayState(0x8, 1);
281 CSND_SetPlayState(0x9, 1);
282 csndExecCmds(false);
283 }
284 }
285}
286
287int main() {
288 hasSound = !csndInit();
289
290 rotation.d.sample = _sampleRotation;
291 rotation.d.readTiltX = _readTiltX;
292 rotation.d.readTiltY = _readTiltY;
293 rotation.d.readGyroZ = _readGyroZ;
294
295 stream.postVideoFrame = 0;
296 stream.postAudioFrame = 0;
297 stream.postAudioBuffer = _postAudioBuffer;
298
299 if (!allocateRomBuffer()) {
300 return 1;
301 }
302
303 if (hasSound) {
304 audioLeft = linearMemAlign(AUDIO_SAMPLE_BUFFER * sizeof(int16_t), 0x80);
305 audioRight = linearMemAlign(AUDIO_SAMPLE_BUFFER * sizeof(int16_t), 0x80);
306 }
307
308 sf2d_init();
309 sf2d_set_clear_color(0);
310 tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_VRAM);
311
312 sdmcArchive = (FS_archive) {
313 ARCH_SDMC,
314 (FS_path) { PATH_EMPTY, 1, (const u8*)"" },
315 0, 0
316 };
317 FSUSER_OpenArchive(0, &sdmcArchive);
318
319 struct GUIFont* font = GUIFontCreate();
320
321 if (!font) {
322 goto cleanup;
323 }
324
325 struct GBAGUIRunner runner = {
326 .params = {
327 320, 240,
328 font, "/",
329 _drawStart, _drawEnd,
330 _pollInput, _pollCursor,
331 0, 0,
332
333 GUI_PARAMS_TRAIL
334 },
335 .setup = _setup,
336 .teardown = 0,
337 .gameLoaded = _gameLoaded,
338 .gameUnloaded = _gameUnloaded,
339 .prepareForFrame = 0,
340 .drawFrame = _drawFrame,
341 .drawScreenshot = _drawScreenshot,
342 .paused = _gameUnloaded,
343 .unpaused = _gameLoaded,
344 .incrementScreenMode = _incrementScreenMode,
345 .pollGameInput = _pollGameInput
346 };
347
348 GBAGUIInit(&runner, "3ds");
349 GBAGUIRunloop(&runner);
350 GBAGUIDeinit(&runner);
351
352cleanup:
353 linearFree(renderer.outputBuffer);
354
355 sf2d_free_texture(tex);
356 sf2d_fini();
357
358 if (hasSound) {
359 linearFree(audioLeft);
360 linearFree(audioRight);
361 }
362 csndExit();
363 return 0;
364}