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 = linearAlloc(256 * VIDEO_VERTICAL_PIXELS * 2);
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.826, 1);
87 blip_set_rates(runner->context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 44100 * ratio);
88 blip_set_rates(runner->context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 44100 * 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, 44100, 1.0, -1.0, audioLeft, audioLeft, AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
95 csndPlaySound(0x9, SOUND_REPEAT | SOUND_FORMAT_16BIT, 44100, 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 gspWaitForPPF();
119 GSPGPU_FlushDataCache(0, tex->data, 256 * VIDEO_VERTICAL_PIXELS * 2);
120 sf2d_draw_texture_scale_blend(tex, 40, 296, 1, -1, 0xFFFFFF3F | (faded ? 0 : 0xC0));
121#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
122 if (!hasSound) {
123 blip_clear(runner->context.gba->audio.left);
124 blip_clear(runner->context.gba->audio.right);
125 }
126#endif
127}
128
129static void _drawScreenshot(struct GBAGUIRunner* runner, const uint32_t* pixels, bool faded) {
130 UNUSED(runner);
131 u16* newPixels = linearMemAlign(256 * VIDEO_VERTICAL_PIXELS * 2, 0x80);
132 unsigned y, x;
133 for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
134 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS; ++x) {
135 u16 pixel = (*pixels >> 19) & 0x1F;
136 pixel |= (*pixels >> 5) & 0x7C0;
137 pixel |= (*pixels << 8) & 0xF800;
138 newPixels[y * 256 + x] = pixel;
139 ++pixels;
140 }
141 memset(&newPixels[y * 256 + VIDEO_HORIZONTAL_PIXELS], 0, 32);
142 }
143 GSPGPU_FlushDataCache(0, (void*) newPixels, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 2);
144 GX_SetDisplayTransfer(0, (void*) newPixels, GX_BUFFER_DIM(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
145 gspWaitForPPF();
146 linearFree(newPixels);
147 GSPGPU_FlushDataCache(0, (void*) tex->data, 256 * VIDEO_VERTICAL_PIXELS * 2);
148 sf2d_draw_texture_scale_blend(tex, 40, 296, 1, -1, 0xFFFFFF3F | (faded ? 0 : 0xC0));
149}
150
151static uint16_t _pollGameInput(struct GBAGUIRunner* runner) {
152 hidScanInput();
153 uint32_t activeKeys = hidKeysHeld() & 0xF00003FF;
154 activeKeys |= activeKeys >> 24;
155 return activeKeys;
156}
157
158static uint32_t _pollInput(void) {
159 hidScanInput();
160 uint32_t keys = 0;
161 int activeKeys = hidKeysHeld();
162 if (activeKeys & KEY_X) {
163 keys |= 1 << GUI_INPUT_CANCEL;
164 }
165 if (activeKeys & KEY_B) {
166 keys |= 1 << GUI_INPUT_BACK;
167 }
168 if (activeKeys & KEY_A) {
169 keys |= 1 << GUI_INPUT_SELECT;
170 }
171 if (activeKeys & KEY_LEFT) {
172 keys |= 1 << GUI_INPUT_LEFT;
173 }
174 if (activeKeys & KEY_RIGHT) {
175 keys |= 1 << GUI_INPUT_RIGHT;
176 }
177 if (activeKeys & KEY_UP) {
178 keys |= 1 << GUI_INPUT_UP;
179 }
180 if (activeKeys & KEY_DOWN) {
181 keys |= 1 << GUI_INPUT_DOWN;
182 }
183 if (activeKeys & KEY_CSTICK_UP) {
184 keys |= 1 << GBA_GUI_INPUT_INCREASE_BRIGHTNESS;
185 }
186 if (activeKeys & KEY_CSTICK_DOWN) {
187 keys |= 1 << GBA_GUI_INPUT_DECREASE_BRIGHTNESS;
188 }
189 return keys;
190}
191
192static void _sampleRotation(struct GBARotationSource* source) {
193 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
194 // Work around ctrulib getting the entries wrong
195 rotation->accel = *(accelVector*)& hidSharedMem[0x48];
196 rotation->gyro = *(angularRate*)& hidSharedMem[0x5C];
197}
198
199static int32_t _readTiltX(struct GBARotationSource* source) {
200 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
201 return rotation->accel.x << 18L;
202}
203
204static int32_t _readTiltY(struct GBARotationSource* source) {
205 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
206 return rotation->accel.y << 18L;
207}
208
209static int32_t _readGyroZ(struct GBARotationSource* source) {
210 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
211 return rotation->gyro.y << 18L; // Yes, y
212}
213
214static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio) {
215 UNUSED(stream);
216#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
217 blip_read_samples(audio->left, &audioLeft[audioPos], AUDIO_SAMPLES, false);
218 blip_read_samples(audio->right, &audioRight[audioPos], AUDIO_SAMPLES, false);
219#elif RESAMPLE_LIBRARY == RESAMPLE_NN
220 GBAAudioCopy(audio, &audioLeft[audioPos], &audioRight[audioPos], AUDIO_SAMPLES);
221#endif
222 GSPGPU_FlushDataCache(0, (void*) &audioLeft[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
223 GSPGPU_FlushDataCache(0, (void*) &audioRight[audioPos], AUDIO_SAMPLES * sizeof(int16_t));
224 audioPos = (audioPos + AUDIO_SAMPLES) % AUDIO_SAMPLE_BUFFER;
225 if (audioPos == AUDIO_SAMPLE_BUFFER / 8) {
226 u8 playing = 0;
227 csndIsPlaying(0x8, &playing);
228 if (!playing) {
229 CSND_SetPlayState(0x8, 1);
230 CSND_SetPlayState(0x9, 1);
231 csndExecCmds(false);
232 }
233 }
234}
235
236int main() {
237 hasSound = !csndInit();
238
239 rotation.d.sample = _sampleRotation;
240 rotation.d.readTiltX = _readTiltX;
241 rotation.d.readTiltY = _readTiltY;
242 rotation.d.readGyroZ = _readGyroZ;
243
244 stream.postVideoFrame = 0;
245 stream.postAudioFrame = 0;
246 stream.postAudioBuffer = _postAudioBuffer;
247
248 if (!allocateRomBuffer()) {
249 return 1;
250 }
251
252 if (hasSound) {
253 audioLeft = linearAlloc(AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
254 audioRight = linearAlloc(AUDIO_SAMPLE_BUFFER * sizeof(int16_t));
255 }
256
257 sf2d_init();
258 sf2d_set_clear_color(0);
259 tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_RAM);
260 memset(tex->data, 0, 256 * 256 * 2);
261
262 sdmcArchive = (FS_archive) {
263 ARCH_SDMC,
264 (FS_path) { PATH_EMPTY, 1, (const u8*)"" },
265 0, 0
266 };
267 FSUSER_OpenArchive(0, &sdmcArchive);
268
269 logFile = VFileOpen("/mgba.log", O_WRONLY | O_CREAT | O_TRUNC);
270 struct GUIFont* font = GUIFontCreate();
271
272 if (!font) {
273 goto cleanup;
274 }
275
276 struct GBAGUIRunner runner = {
277 .params = {
278 320, 240,
279 font, "/",
280 _drawStart, _drawEnd, _pollInput,
281 0, 0,
282
283 GUI_PARAMS_TRAIL
284 },
285 .setup = _setup,
286 .teardown = 0,
287 .gameLoaded = _gameLoaded,
288 .gameUnloaded = _gameUnloaded,
289 .prepareForFrame = 0,
290 .drawFrame = _drawFrame,
291 .drawScreenshot = _drawScreenshot,
292 .paused = _gameUnloaded,
293 .unpaused = _gameLoaded,
294 .pollGameInput = _pollGameInput
295 };
296 GBAGUIInit(&runner, 0);
297 GBAGUIRunloop(&runner);
298 GBAGUIDeinit(&runner);
299
300cleanup:
301 linearFree(renderer.outputBuffer);
302
303 if (logFile) {
304 logFile->close(logFile);
305 }
306
307 sf2d_free_texture(tex);
308 sf2d_fini();
309
310 if (hasSound) {
311 linearFree(audioLeft);
312 linearFree(audioRight);
313 }
314 csndExit();
315 return 0;
316}
317
318static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
319 UNUSED(thread);
320 UNUSED(level);
321 if (!logFile) {
322 return;
323 }
324 char out[256];
325 size_t len = vsnprintf(out, sizeof(out), format, args);
326 if (len >= sizeof(out)) {
327 len = sizeof(out) - 1;
328 }
329 out[len] = '\n';
330 logFile->write(logFile, out, len + 1);
331}