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