src/platform/wii/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#define asm __asm__
7
8#include <fat.h>
9#include <gccore.h>
10#include <malloc.h>
11#include <wiiuse/wpad.h>
12
13#include "util/common.h"
14
15#include "gba/renderers/video-software.h"
16#include "gba/context/context.h"
17#include "gba/context/gui-runner.h"
18#include "util/gui.h"
19#include "util/gui/file-select.h"
20#include "util/gui/font.h"
21#include "util/vfs.h"
22
23#define SAMPLES 1024
24
25static void GBAWiiLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
26
27static void _audioDMA(void);
28static void _setRumble(struct GBARumble* rumble, int enable);
29static void _sampleRotation(struct GBARotationSource* source);
30static int32_t _readTiltX(struct GBARotationSource* source);
31static int32_t _readTiltY(struct GBARotationSource* source);
32static int32_t _readGyroZ(struct GBARotationSource* source);
33
34static void _drawStart(void);
35static void _drawEnd(void);
36static int _pollInput(void);
37static void _guiPrepare(void);
38
39static void _setup(struct GBAGUIRunner* runner);
40static void _gameLoaded(struct GBAGUIRunner* runner);
41static void _gameUnloaded(struct GBAGUIRunner* runner);
42static void _prepareForFrame(struct GBAGUIRunner* runner);
43static void _drawFrame(struct GBAGUIRunner* runner, bool faded);
44static uint16_t _pollGameInput(struct GBAGUIRunner* runner);
45
46static struct GBAVideoSoftwareRenderer renderer;
47static struct GBARumble rumble;
48static struct GBARotationSource rotation;
49static FILE* logfile;
50static GXRModeObj* mode;
51static Mtx model, view, modelview;
52static uint16_t* texmem;
53static GXTexObj tex;
54static int32_t tiltX;
55static int32_t tiltY;
56static int32_t gyroZ;
57
58static void* framebuffer[2];
59static int whichFb = 0;
60
61static struct GBAStereoSample audioBuffer[3][SAMPLES] __attribute__((__aligned__(32)));
62static volatile size_t audioBufferSize = 0;
63static volatile int currentAudioBuffer = 0;
64
65static struct GUIFont* font;
66
67int main() {
68 VIDEO_Init();
69 PAD_Init();
70 WPAD_Init();
71 AUDIO_Init(0);
72 AUDIO_SetDSPSampleRate(AI_SAMPLERATE_48KHZ);
73 AUDIO_RegisterDMACallback(_audioDMA);
74
75 memset(audioBuffer, 0, sizeof(audioBuffer));
76
77#if !defined(COLOR_16_BIT) && !defined(COLOR_5_6_5)
78#error This pixel format is unsupported. Please use -DCOLOR_16-BIT -DCOLOR_5_6_5
79#endif
80
81 mode = VIDEO_GetPreferredMode(0);
82 framebuffer[0] = SYS_AllocateFramebuffer(mode);
83 framebuffer[1] = SYS_AllocateFramebuffer(mode);
84
85 VIDEO_Configure(mode);
86 VIDEO_SetNextFramebuffer(framebuffer[whichFb]);
87 VIDEO_SetBlack(FALSE);
88 VIDEO_Flush();
89 VIDEO_WaitVSync();
90 if (mode->viTVMode & VI_NON_INTERLACE) {
91 VIDEO_WaitVSync();
92 }
93
94 GXColor bg = { 0, 0, 0, 0xFF };
95 void* fifo = memalign(32, 0x40000);
96 memset(fifo, 0, 0x40000);
97 GX_Init(fifo, 0x40000);
98 GX_SetCopyClear(bg, 0x00FFFFFF);
99 GX_SetViewport(0, 0, mode->fbWidth, mode->efbHeight, 0, 1);
100
101 f32 yscale = GX_GetYScaleFactor(mode->efbHeight, mode->xfbHeight);
102 u32 xfbHeight = GX_SetDispCopyYScale(yscale);
103 GX_SetScissor(0, 0, mode->viWidth, mode->viWidth);
104 GX_SetDispCopySrc(0, 0, mode->fbWidth, mode->efbHeight);
105 GX_SetDispCopyDst(mode->fbWidth, xfbHeight);
106 GX_SetCopyFilter(mode->aa, mode->sample_pattern, GX_TRUE, mode->vfilter);
107 GX_SetFieldMode(mode->field_rendering, ((mode->viHeight == 2 * mode->xfbHeight) ? GX_ENABLE : GX_DISABLE));
108
109 GX_SetCullMode(GX_CULL_NONE);
110 GX_CopyDisp(framebuffer[whichFb], GX_TRUE);
111 GX_SetDispCopyGamma(GX_GM_1_0);
112
113 GX_ClearVtxDesc();
114 GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
115 GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);
116 GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
117
118 GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XY, GX_S16, 0);
119 GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_S16, 0);
120 GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
121
122 GX_SetNumChans(1);
123 GX_SetNumTexGens(1);
124 GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
125 GX_SetTevOp(GX_TEVSTAGE0, GX_MODULATE);
126
127 GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
128 GX_InvVtxCache();
129 GX_InvalidateTexAll();
130
131 guVector cam = { 0.0f, 0.0f, 0.0f };
132 guVector up = { 0.0f, 1.0f, 0.0f };
133 guVector look = { 0.0f, 0.0f, -1.0f };
134 guLookAt(view, &cam, &up, &look);
135
136 guMtxIdentity(model);
137 guMtxTransApply(model, model, 0.0f, 0.0f, -6.0f);
138 guMtxConcat(view, model, modelview);
139 GX_LoadPosMtxImm(modelview, GX_PNMTX0);
140
141 texmem = memalign(32, 256 * 256 * BYTES_PER_PIXEL);
142 memset(texmem, 0, 256 * 256 * BYTES_PER_PIXEL);
143 GX_InitTexObj(&tex, texmem, 256, 256, GX_TF_RGB565, GX_CLAMP, GX_CLAMP, GX_FALSE);
144
145 font = GUIFontCreate();
146
147 fatInitDefault();
148
149 logfile = fopen("/mgba.log", "w");
150
151 rumble.setRumble = _setRumble;
152
153 rotation.sample = _sampleRotation;
154 rotation.readTiltX = _readTiltX;
155 rotation.readTiltY = _readTiltY;
156 rotation.readGyroZ = _readGyroZ;
157
158 struct GBAGUIRunner runner = {
159 .params = {
160 352, 230,
161 font, "/",
162 _drawStart, _drawEnd, _pollInput,
163 _guiPrepare, 0,
164
165 GUI_PARAMS_TRAIL
166 },
167 .setup = _setup,
168 .teardown = 0,
169 .gameLoaded = _gameLoaded,
170 .gameUnloaded = _gameUnloaded,
171 .prepareForFrame = _prepareForFrame,
172 .drawFrame = _drawFrame,
173 .pollGameInput = _pollGameInput
174 };
175 GBAGUIInit(&runner, 0);
176 GBAGUIRunloop(&runner);
177 GBAGUIDeinit(&runner);
178
179 fclose(logfile);
180 free(fifo);
181
182 free(renderer.outputBuffer);
183 GUIFontDestroy(font);
184
185 return 0;
186}
187
188void GBAWiiLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
189 UNUSED(thread);
190 UNUSED(level);
191 if (!logfile) {
192 return;
193 }
194 vfprintf(logfile, format, args);
195 fprintf(logfile, "\n");
196 fflush(logfile);
197}
198
199static void _audioDMA(void) {
200 if (!audioBufferSize) {
201 return;
202 }
203 DCFlushRange(audioBuffer[currentAudioBuffer], audioBufferSize * sizeof(struct GBAStereoSample));
204 AUDIO_InitDMA((u32) audioBuffer[currentAudioBuffer], audioBufferSize * sizeof(struct GBAStereoSample));
205 currentAudioBuffer = (currentAudioBuffer + 1) % 3;
206 audioBufferSize = 0;
207}
208
209static void _drawStart(void) {
210 VIDEO_WaitVSync();
211 GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
212 GX_SetColorUpdate(GX_TRUE);
213
214 GX_SetViewport(0, 0, mode->fbWidth, mode->efbHeight, 0, 1);
215}
216
217static void _drawEnd(void) {
218 GX_DrawDone();
219
220 whichFb = !whichFb;
221
222 GX_CopyDisp(framebuffer[whichFb], GX_TRUE);
223 VIDEO_SetNextFramebuffer(framebuffer[whichFb]);
224 VIDEO_Flush();
225}
226
227static int _pollInput(void) {
228 PAD_ScanPads();
229 u16 padkeys = PAD_ButtonsHeld(0);
230
231 WPAD_ScanPads();
232 u32 wiiPad = WPAD_ButtonsHeld(0);
233 u32 ext = 0;
234 WPAD_Probe(0, &ext);
235
236 int keys = 0;
237 int x = PAD_StickX(0);
238 int y = PAD_StickY(0);
239 if (x < -0x40) {
240 keys |= 1 << GUI_INPUT_LEFT;
241 }
242 if (x > 0x40) {
243 keys |= 1 << GUI_INPUT_RIGHT;
244 }
245 if (y < -0x40) {
246 keys |= 1 << GUI_INPUT_DOWN;
247 }
248 if (y > 0x40) {
249 keys |= 1 << GUI_INPUT_UP;
250 }
251 if ((padkeys & PAD_BUTTON_A) || (wiiPad & WPAD_BUTTON_2) ||
252 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_A | WPAD_CLASSIC_BUTTON_Y)))) {
253 keys |= 1 << GUI_INPUT_SELECT;
254 }
255 if ((padkeys & PAD_BUTTON_B) || (wiiPad & WPAD_BUTTON_1) ||
256 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_B | WPAD_CLASSIC_BUTTON_X)))) {
257 keys |= 1 << GUI_INPUT_BACK;
258 }
259 if ((padkeys & PAD_TRIGGER_Z) || (wiiPad & WPAD_BUTTON_HOME) ||
260 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_HOME)))) {
261 keys |= 1 << GUI_INPUT_CANCEL;
262 }
263 if ((padkeys & PAD_BUTTON_LEFT)|| (wiiPad & WPAD_BUTTON_UP) ||
264 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_LEFT))) {
265 keys |= 1 << GUI_INPUT_LEFT;
266 }
267 if ((padkeys & PAD_BUTTON_RIGHT) || (wiiPad & WPAD_BUTTON_DOWN) ||
268 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_RIGHT))) {
269 keys |= 1 << GUI_INPUT_RIGHT;
270 }
271 if ((padkeys & PAD_BUTTON_UP) || (wiiPad & WPAD_BUTTON_RIGHT) ||
272 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_UP))) {
273 keys |= 1 << GUI_INPUT_UP;
274 }
275 if ((padkeys & PAD_BUTTON_DOWN) || (wiiPad & WPAD_BUTTON_LEFT) ||
276 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_DOWN))) {
277 keys |= 1 << GUI_INPUT_DOWN;
278 }
279 return keys;
280}
281
282void _guiPrepare(void) {
283 Mtx44 proj;
284 guOrtho(proj, -20, 240, 0, 352, 0, 300);
285 GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);
286}
287
288void _setup(struct GBAGUIRunner* runner) {
289 struct GBAOptions opts = {
290 .useBios = true,
291 .logLevel = 0,
292 .idleOptimization = IDLE_LOOP_DETECT
293 };
294 GBAConfigLoadDefaults(&runner->context.config, &opts);
295 runner->context.gba->logHandler = GBAWiiLog;
296 runner->context.gba->rumble = &rumble;
297 runner->context.gba->rotationSource = &rotation;
298
299 GBAVideoSoftwareRendererCreate(&renderer);
300 renderer.outputBuffer = memalign(32, 256 * 256 * BYTES_PER_PIXEL);
301 renderer.outputBufferStride = 256;
302 runner->context.renderer = &renderer.d;
303
304 GBAAudioResizeBuffer(&runner->context.gba->audio, SAMPLES);
305
306#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
307 blip_set_rates(runner->context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 48000);
308 blip_set_rates(runner->context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000);
309#endif
310}
311
312void _gameUnloaded(struct GBAGUIRunner* runner) {
313 UNUSED(runner);
314 AUDIO_StopDMA();
315}
316
317void _gameLoaded(struct GBAGUIRunner* runner) {
318 WPAD_SetDataFormat(0, WPAD_FMT_BTNS_ACC);
319 if (runner->context.gba->memory.hw.devices & HW_GYRO) {
320 int i;
321 for (i = 0; i < 6; ++i) {
322 u32 result = WPAD_SetMotionPlus(0, 1);
323 if (result == WPAD_ERR_NONE) {
324 break;
325 }
326 sleep(1);
327 }
328 }
329
330 Mtx44 proj;
331 guOrtho(proj, -10, VIDEO_VERTICAL_PIXELS + 10, 0, VIDEO_HORIZONTAL_PIXELS, 0, 300);
332 GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);
333}
334
335void _prepareForFrame(struct GBAGUIRunner* runner) {
336#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
337 int available = blip_samples_avail(runner->context.gba->audio.left);
338 if (available + audioBufferSize > SAMPLES) {
339 available = SAMPLES - audioBufferSize;
340 }
341 available &= ~((32 / sizeof(struct GBAStereoSample)) - 1); // Force align to 32 bytes
342 if (available > 0) {
343 blip_read_samples(runner->context.gba->audio.left, &audioBuffer[currentAudioBuffer][audioBufferSize].left, available, true);
344 blip_read_samples(runner->context.gba->audio.right, &audioBuffer[currentAudioBuffer][audioBufferSize].right, available, true);
345 audioBufferSize += available;
346 }
347 if (audioBufferSize == SAMPLES && !AUDIO_GetDMAEnableFlag()) {
348 _audioDMA();
349 AUDIO_StartDMA();
350 }
351#endif
352}
353
354void _drawFrame(struct GBAGUIRunner* runner, bool faded) {
355 uint32_t color = 0xFFFFFF3F;
356 if (!faded) {
357 color |= 0xC0;
358 }
359 size_t x, y;
360 uint64_t* texdest = (uint64_t*) texmem;
361 uint64_t* texsrc = (uint64_t*) renderer.outputBuffer;
362 for (y = 0; y < VIDEO_VERTICAL_PIXELS; y += 4) {
363 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS >> 2; ++x) {
364 texdest[0 + x * 4 + y * 64] = texsrc[0 + x + y * 64];
365 texdest[1 + x * 4 + y * 64] = texsrc[64 + x + y * 64];
366 texdest[2 + x * 4 + y * 64] = texsrc[128 + x + y * 64];
367 texdest[3 + x * 4 + y * 64] = texsrc[192 + x + y * 64];
368 }
369 }
370 DCFlushRange(texdest, 256 * 256 * BYTES_PER_PIXEL);
371
372 _drawStart();
373
374 GX_SetBlendMode(GX_BM_NONE, GX_BL_ONE, GX_BL_ZERO, GX_LO_SET);
375 GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_S16, 0);
376 GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
377 GX_InvalidateTexAll();
378 GX_LoadTexObj(&tex, GX_TEXMAP0);
379
380 GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
381 GX_Position2s16(0, 256);
382 GX_Color1u32(color);
383 GX_TexCoord2s16(0, 1);
384
385 GX_Position2s16(256, 256);
386 GX_Color1u32(color);
387 GX_TexCoord2s16(1, 1);
388
389 GX_Position2s16(256, 0);
390 GX_Color1u32(color);
391 GX_TexCoord2s16(1, 0);
392
393 GX_Position2s16(0, 0);
394 GX_Color1u32(color);
395 GX_TexCoord2s16(0, 0);
396 GX_End();
397
398 _drawEnd();
399}
400
401uint16_t _pollGameInput(struct GBAGUIRunner* runner) {
402 UNUSED(runner);
403 PAD_ScanPads();
404 u16 padkeys = PAD_ButtonsHeld(0);
405 WPAD_ScanPads();
406 u32 wiiPad = WPAD_ButtonsHeld(0);
407 u32 ext = 0;
408 uint16_t keys = 0;
409 WPAD_Probe(0, &ext);
410
411 if ((padkeys & PAD_BUTTON_A) || (wiiPad & WPAD_BUTTON_2) ||
412 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_A | WPAD_CLASSIC_BUTTON_Y)))) {
413 keys |= 1 << GBA_KEY_A;
414 }
415 if ((padkeys & PAD_BUTTON_B) || (wiiPad & WPAD_BUTTON_1) ||
416 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_B | WPAD_CLASSIC_BUTTON_X)))) {
417 keys |= 1 << GBA_KEY_B;
418 }
419 if ((padkeys & PAD_TRIGGER_L) || (wiiPad & WPAD_BUTTON_B) ||
420 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_FULL_L))) {
421 keys |= 1 << GBA_KEY_L;
422 }
423 if ((padkeys & PAD_TRIGGER_R) || (wiiPad & WPAD_BUTTON_A) ||
424 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_FULL_R))) {
425 keys |= 1 << GBA_KEY_R;
426 }
427 if ((padkeys & PAD_BUTTON_START) || (wiiPad & WPAD_BUTTON_PLUS) ||
428 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_PLUS))) {
429 keys |= 1 << GBA_KEY_START;
430 }
431 if ((padkeys & (PAD_BUTTON_X | PAD_BUTTON_Y)) || (wiiPad & WPAD_BUTTON_MINUS) ||
432 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_MINUS))) {
433 keys |= 1 << GBA_KEY_SELECT;
434 }
435 if ((padkeys & PAD_BUTTON_LEFT) || (wiiPad & WPAD_BUTTON_UP) ||
436 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_LEFT))) {
437 keys |= 1 << GBA_KEY_LEFT;
438 }
439 if ((padkeys & PAD_BUTTON_RIGHT) || (wiiPad & WPAD_BUTTON_DOWN) ||
440 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_RIGHT))) {
441 keys |= 1 << GBA_KEY_RIGHT;
442 }
443 if ((padkeys & PAD_BUTTON_UP) || (wiiPad & WPAD_BUTTON_RIGHT) ||
444 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_UP))) {
445 keys |= 1 << GBA_KEY_UP;
446 }
447 if ((padkeys & PAD_BUTTON_DOWN) || (wiiPad & WPAD_BUTTON_LEFT) ||
448 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_DOWN))) {
449 keys |= 1 << GBA_KEY_DOWN;
450 }
451 int x = PAD_StickX(0);
452 int y = PAD_StickY(0);
453 if (x < -0x40) {
454 keys |= 1 << GBA_KEY_LEFT;
455 }
456 if (x > 0x40) {
457 keys |= 1 << GBA_KEY_RIGHT;
458 }
459 if (y < -0x40) {
460 keys |= 1 << GBA_KEY_DOWN;
461 }
462 if (y > 0x40) {
463 keys |= 1 << GBA_KEY_UP;
464 }
465 return keys;
466}
467
468void _setRumble(struct GBARumble* rumble, int enable) {
469 UNUSED(rumble);
470 WPAD_Rumble(0, enable);
471 if (enable) {
472 PAD_ControlMotor(0, PAD_MOTOR_RUMBLE);
473 } else {
474 PAD_ControlMotor(0, PAD_MOTOR_STOP);
475 }
476}
477
478void _sampleRotation(struct GBARotationSource* source) {
479 UNUSED(source);
480 vec3w_t accel;
481 WPAD_Accel(0, &accel);
482 // These are swapped
483 tiltX = (accel.y - 0x1EA) << 22;
484 tiltY = (accel.x - 0x1EA) << 22;
485
486 // This doesn't seem to work at all with -TR remotes
487 struct expansion_t exp;
488 WPAD_Expansion(0, &exp);
489 if (exp.type != EXP_MOTION_PLUS) {
490 return;
491 }
492 gyroZ = exp.mp.rz - 0x1FA0;
493 gyroZ <<= 18;
494}
495
496int32_t _readTiltX(struct GBARotationSource* source) {
497 UNUSED(source);
498 return tiltX;
499}
500
501int32_t _readTiltY(struct GBARotationSource* source) {
502 UNUSED(source);
503 return tiltY;
504}
505
506int32_t _readGyroZ(struct GBARotationSource* source) {
507 UNUSED(source);
508 return gyroZ;
509}