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 0x800
22
23FS_archive sdmcArchive;
24
25static struct GBA3DSRotationSource {
26 struct GBARotationSource d;
27 accelVector accel;
28 angularRate gyro;
29} rotation;
30
31static struct VFile* logFile;
32static bool hasSound;
33// TODO: Move into context
34static struct GBAVideoSoftwareRenderer renderer;
35static struct GBAAVStream stream;
36static int16_t* audioLeft = 0;
37static int16_t* audioRight = 0;
38static sf2d_texture* tex;
39
40extern bool allocateRomBuffer(void);
41static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
42
43static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio);
44
45static void _drawStart(void) {
46 sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
47}
48
49static void _drawEnd(void) {
50 sf2d_end_frame();
51 sf2d_swapbuffers();
52}
53
54static void _setup(struct GBAGUIRunner* runner) {
55 struct GBAOptions opts = {
56 .useBios = true,
57 .logLevel = 0,
58 .idleOptimization = IDLE_LOOP_DETECT
59 };
60 GBAConfigLoadDefaults(&runner->context.config, &opts);
61 runner->context.gba->logHandler = GBA3DSLog;
62 runner->context.gba->rotationSource = &rotation.d;
63 if (hasSound) {
64 runner->context.gba->stream = &stream;
65 }
66
67 GBAVideoSoftwareRendererCreate(&renderer);
68 renderer.outputBuffer = linearAlloc(256 * VIDEO_VERTICAL_PIXELS * 2);
69 renderer.outputBufferStride = 256;
70 runner->context.renderer = &renderer.d;
71
72 GBAAudioResizeBuffer(&runner->context.gba->audio, AUDIO_SAMPLES);
73}
74
75static void _gameLoaded(struct GBAGUIRunner* runner) {
76 if (runner->context.gba->memory.hw.devices & HW_TILT) {
77 HIDUSER_EnableAccelerometer();
78 }
79 if (runner->context.gba->memory.hw.devices & HW_GYRO) {
80 HIDUSER_EnableGyroscope();
81 }
82
83#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
84 double ratio = GBAAudioCalculateRatio(1, 60, 1);
85 blip_set_rates(runner->context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 0x8000 * ratio);
86 blip_set_rates(runner->context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 0x8000 * ratio);
87#endif
88 if (hasSound) {
89 memset(audioLeft, 0, AUDIO_SAMPLES * sizeof(int16_t));
90 memset(audioRight, 0, AUDIO_SAMPLES * sizeof(int16_t));
91 }
92}
93
94static void _gameUnloaded(struct GBAGUIRunner* runner) {
95 if (hasSound) {
96 CSND_SetPlayState(8, 0);
97 CSND_SetPlayState(9, 0);
98 csndExecCmds(false);
99 }
100
101 if (runner->context.gba->memory.hw.devices & HW_TILT) {
102 HIDUSER_DisableAccelerometer();
103 }
104 if (runner->context.gba->memory.hw.devices & HW_GYRO) {
105 HIDUSER_DisableGyroscope();
106 }
107}
108
109static void _drawFrame(struct GBAGUIRunner* runner, bool faded) {
110 GX_SetDisplayTransfer(0, renderer.outputBuffer, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), tex->data, GX_BUFFER_DIM(256, VIDEO_VERTICAL_PIXELS), 0x000002202);
111 GSPGPU_FlushDataCache(0, tex->data, 256 * VIDEO_VERTICAL_PIXELS * 2);
112#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
113 if (!hasSound) {
114 blip_clear(runner->context.gba->audio.left);
115 blip_clear(runner->context.gba->audio.right);
116 }
117#endif
118 gspWaitForPPF();
119 sf2d_draw_texture_scale_blend(tex, 40, 296, 1, -1, 0xFFFFFF3F | (faded ? 0 : 0xC0));
120}
121
122static uint16_t _pollGameInput(struct GBAGUIRunner* runner) {
123 hidScanInput();
124 uint32_t activeKeys = hidKeysHeld() & 0xF00003FF;
125 activeKeys |= activeKeys >> 24;
126 return activeKeys;
127}
128
129static uint32_t _pollInput(void) {
130 hidScanInput();
131 uint32_t keys = 0;
132 int activeKeys = hidKeysHeld();
133 if (activeKeys & KEY_X) {
134 keys |= 1 << GUI_INPUT_CANCEL;
135 }
136 if (activeKeys & KEY_B) {
137 keys |= 1 << GUI_INPUT_BACK;
138 }
139 if (activeKeys & KEY_A) {
140 keys |= 1 << GUI_INPUT_SELECT;
141 }
142 if (activeKeys & KEY_LEFT) {
143 keys |= 1 << GUI_INPUT_LEFT;
144 }
145 if (activeKeys & KEY_RIGHT) {
146 keys |= 1 << GUI_INPUT_RIGHT;
147 }
148 if (activeKeys & KEY_UP) {
149 keys |= 1 << GUI_INPUT_UP;
150 }
151 if (activeKeys & KEY_DOWN) {
152 keys |= 1 << GUI_INPUT_DOWN;
153 }
154 if (activeKeys & KEY_CSTICK_UP) {
155 keys |= 1 << GBA_GUI_INPUT_INCREASE_BRIGHTNESS;
156 }
157 if (activeKeys & KEY_CSTICK_DOWN) {
158 keys |= 1 << GBA_GUI_INPUT_DECREASE_BRIGHTNESS;
159 }
160 return keys;
161}
162
163static void _sampleRotation(struct GBARotationSource* source) {
164 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
165 // Work around ctrulib getting the entries wrong
166 rotation->accel = *(accelVector*)& hidSharedMem[0x48];
167 rotation->gyro = *(angularRate*)& hidSharedMem[0x5C];
168}
169
170static int32_t _readTiltX(struct GBARotationSource* source) {
171 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
172 return rotation->accel.x << 18L;
173}
174
175static int32_t _readTiltY(struct GBARotationSource* source) {
176 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
177 return rotation->accel.y << 18L;
178}
179
180static int32_t _readGyroZ(struct GBARotationSource* source) {
181 struct GBA3DSRotationSource* rotation = (struct GBA3DSRotationSource*) source;
182 return rotation->gyro.y << 18L; // Yes, y
183}
184
185static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio) {
186 UNUSED(stream);
187 memset(audioLeft, 0, AUDIO_SAMPLES * sizeof(int16_t));
188 memset(audioRight, 0, AUDIO_SAMPLES * sizeof(int16_t));
189#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
190 blip_read_samples(audio->left, audioLeft, AUDIO_SAMPLES, false);
191 blip_read_samples(audio->right, audioRight, AUDIO_SAMPLES, false);
192#elif RESAMPLE_LIBRARY == RESAMPLE_NN
193 GBAAudioCopy(audio, audioLeft, audioRight, AUDIO_SAMPLES);
194#endif
195 GSPGPU_FlushDataCache(0, (void*) audioLeft, AUDIO_SAMPLES * sizeof(int16_t));
196 GSPGPU_FlushDataCache(0, (void*) audioRight, AUDIO_SAMPLES * sizeof(int16_t));
197 csndPlaySound(0x8, SOUND_ONE_SHOT | SOUND_FORMAT_16BIT, 0x8000, 1.0, -1.0, audioLeft, audioLeft, AUDIO_SAMPLES * sizeof(int16_t));
198 csndPlaySound(0x9, SOUND_ONE_SHOT | SOUND_FORMAT_16BIT, 0x8000, 1.0, 1.0, audioRight, audioRight, AUDIO_SAMPLES * sizeof(int16_t));
199 CSND_SetPlayState(0x8, 1);
200 CSND_SetPlayState(0x9, 1);
201 csndExecCmds(false);
202}
203
204int main() {
205 hasSound = !csndInit();
206
207 rotation.d.sample = _sampleRotation;
208 rotation.d.readTiltX = _readTiltX;
209 rotation.d.readTiltY = _readTiltY;
210 rotation.d.readGyroZ = _readGyroZ;
211
212 stream.postVideoFrame = 0;
213 stream.postAudioFrame = 0;
214 stream.postAudioBuffer = _postAudioBuffer;
215
216 if (!allocateRomBuffer()) {
217 return 1;
218 }
219
220 if (hasSound) {
221 audioLeft = linearAlloc(AUDIO_SAMPLES * sizeof(int16_t));
222 audioRight = linearAlloc(AUDIO_SAMPLES * sizeof(int16_t));
223 }
224
225 sf2d_init();
226 sf2d_set_clear_color(0);
227 tex = sf2d_create_texture(256, 256, TEXFMT_RGB565, SF2D_PLACE_RAM);
228 memset(tex->data, 0, 256 * 256 * 2);
229
230 sdmcArchive = (FS_archive) {
231 ARCH_SDMC,
232 (FS_path) { PATH_EMPTY, 1, (const u8*)"" },
233 0, 0
234 };
235 FSUSER_OpenArchive(0, &sdmcArchive);
236
237 logFile = VFileOpen("/mgba.log", O_WRONLY | O_CREAT | O_TRUNC);
238 struct GUIFont* font = GUIFontCreate();
239
240 if (!font) {
241 goto cleanup;
242 }
243
244 struct GBAGUIRunner runner = {
245 .params = {
246 320, 240,
247 font, "/",
248 _drawStart, _drawEnd, _pollInput,
249 0, 0,
250
251 GUI_PARAMS_TRAIL
252 },
253 .setup = _setup,
254 .teardown = 0,
255 .gameLoaded = _gameLoaded,
256 .gameUnloaded = _gameUnloaded,
257 .prepareForFrame = 0,
258 .drawFrame = _drawFrame,
259 .pollGameInput = _pollGameInput
260 };
261 GBAGUIInit(&runner, 0);
262 GBAGUIRunloop(&runner);
263 GBAGUIDeinit(&runner);
264
265cleanup:
266 linearFree(renderer.outputBuffer);
267
268 if (logFile) {
269 logFile->close(logFile);
270 }
271
272 sf2d_free_texture(tex);
273 sf2d_fini();
274
275 if (hasSound) {
276 linearFree(audioLeft);
277 linearFree(audioRight);
278 }
279 csndExit();
280 return 0;
281}
282
283static void GBA3DSLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
284 UNUSED(thread);
285 UNUSED(level);
286 if (!logFile) {
287 return;
288 }
289 char out[256];
290 size_t len = vsnprintf(out, sizeof(out), format, args);
291 if (len >= sizeof(out)) {
292 len = sizeof(out) - 1;
293 }
294 out[len] = '\n';
295 logFile->write(logFile, out, len + 1);
296}