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/gui/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);
38static void _guiFinish(void);
39
40static void _setup(struct GBAGUIRunner* runner);
41static void _gameLoaded(struct GBAGUIRunner* runner);
42static void _gameUnloaded(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, _guiFinish,
164
165 GUI_PARAMS_TRAIL
166 },
167 .setup = _setup,
168 .teardown = 0,
169 .gameLoaded = _gameLoaded,
170 .gameUnloaded = _gameUnloaded,
171 .prepareForFrame = 0,
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 _guiFinish(void) {
289 Mtx44 proj;
290 guOrtho(proj, -10, VIDEO_VERTICAL_PIXELS + 10, 0, VIDEO_HORIZONTAL_PIXELS, 0, 300);
291 GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);
292}
293
294void _setup(struct GBAGUIRunner* runner) {
295 struct GBAOptions opts = {
296 .useBios = true,
297 .logLevel = 0,
298 .idleOptimization = IDLE_LOOP_DETECT
299 };
300 GBAConfigLoadDefaults(&runner->context.config, &opts);
301 runner->context.gba->logHandler = GBAWiiLog;
302 runner->context.gba->rumble = &rumble;
303 runner->context.gba->rotationSource = &rotation;
304
305 GBAVideoSoftwareRendererCreate(&renderer);
306 renderer.outputBuffer = memalign(32, 256 * 256 * BYTES_PER_PIXEL);
307 renderer.outputBufferStride = 256;
308 runner->context.renderer = &renderer.d;
309
310 GBAAudioResizeBuffer(&runner->context.gba->audio, SAMPLES);
311
312#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
313 double ratio = GBAAudioCalculateRatio(1, 60, 1);
314 blip_set_rates(runner->context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
315 blip_set_rates(runner->context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
316#endif
317}
318
319void _gameUnloaded(struct GBAGUIRunner* runner) {
320 UNUSED(runner);
321 AUDIO_StopDMA();
322}
323
324void _gameLoaded(struct GBAGUIRunner* runner) {
325 WPAD_SetDataFormat(0, WPAD_FMT_BTNS_ACC);
326 if (runner->context.gba->memory.hw.devices & HW_GYRO) {
327 int i;
328 for (i = 0; i < 6; ++i) {
329 u32 result = WPAD_SetMotionPlus(0, 1);
330 if (result == WPAD_ERR_NONE) {
331 break;
332 }
333 sleep(1);
334 }
335 }
336}
337
338void _drawFrame(struct GBAGUIRunner* runner, bool faded) {
339#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
340 int available = blip_samples_avail(runner->context.gba->audio.left);
341 if (available + audioBufferSize > SAMPLES) {
342 available = SAMPLES - audioBufferSize;
343 }
344 available &= ~((32 / sizeof(struct GBAStereoSample)) - 1); // Force align to 32 bytes
345 if (available > 0) {
346 blip_read_samples(runner->context.gba->audio.left, &audioBuffer[currentAudioBuffer][audioBufferSize].left, available, true);
347 blip_read_samples(runner->context.gba->audio.right, &audioBuffer[currentAudioBuffer][audioBufferSize].right, available, true);
348 audioBufferSize += available;
349 }
350 if (audioBufferSize == SAMPLES && !AUDIO_GetDMAEnableFlag()) {
351 _audioDMA();
352 AUDIO_StartDMA();
353 }
354#endif
355
356 uint32_t color = 0xFFFFFF3F;
357 if (!faded) {
358 color |= 0xC0;
359 }
360 size_t x, y;
361 uint64_t* texdest = (uint64_t*) texmem;
362 uint64_t* texsrc = (uint64_t*) renderer.outputBuffer;
363 for (y = 0; y < VIDEO_VERTICAL_PIXELS; y += 4) {
364 for (x = 0; x < VIDEO_HORIZONTAL_PIXELS >> 2; ++x) {
365 texdest[0 + x * 4 + y * 64] = texsrc[0 + x + y * 64];
366 texdest[1 + x * 4 + y * 64] = texsrc[64 + x + y * 64];
367 texdest[2 + x * 4 + y * 64] = texsrc[128 + x + y * 64];
368 texdest[3 + x * 4 + y * 64] = texsrc[192 + x + y * 64];
369 }
370 }
371 DCFlushRange(texdest, 256 * 256 * BYTES_PER_PIXEL);
372
373 if (faded) {
374 GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
375 } else {
376 GX_SetBlendMode(GX_BM_NONE, GX_BL_ONE, GX_BL_ZERO, GX_LO_NOOP);
377 }
378 GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_S16, 0);
379 GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
380 GX_InvalidateTexAll();
381 GX_LoadTexObj(&tex, GX_TEXMAP0);
382
383 GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
384 GX_Position2s16(0, 256);
385 GX_Color1u32(color);
386 GX_TexCoord2s16(0, 1);
387
388 GX_Position2s16(256, 256);
389 GX_Color1u32(color);
390 GX_TexCoord2s16(1, 1);
391
392 GX_Position2s16(256, 0);
393 GX_Color1u32(color);
394 GX_TexCoord2s16(1, 0);
395
396 GX_Position2s16(0, 0);
397 GX_Color1u32(color);
398 GX_TexCoord2s16(0, 0);
399 GX_End();
400}
401
402uint16_t _pollGameInput(struct GBAGUIRunner* runner) {
403 UNUSED(runner);
404 PAD_ScanPads();
405 u16 padkeys = PAD_ButtonsHeld(0);
406 WPAD_ScanPads();
407 u32 wiiPad = WPAD_ButtonsHeld(0);
408 u32 ext = 0;
409 uint16_t keys = 0;
410 WPAD_Probe(0, &ext);
411
412 if ((padkeys & PAD_BUTTON_A) || (wiiPad & WPAD_BUTTON_2) ||
413 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_A | WPAD_CLASSIC_BUTTON_Y)))) {
414 keys |= 1 << GBA_KEY_A;
415 }
416 if ((padkeys & PAD_BUTTON_B) || (wiiPad & WPAD_BUTTON_1) ||
417 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_B | WPAD_CLASSIC_BUTTON_X)))) {
418 keys |= 1 << GBA_KEY_B;
419 }
420 if ((padkeys & PAD_TRIGGER_L) || (wiiPad & WPAD_BUTTON_B) ||
421 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_FULL_L))) {
422 keys |= 1 << GBA_KEY_L;
423 }
424 if ((padkeys & PAD_TRIGGER_R) || (wiiPad & WPAD_BUTTON_A) ||
425 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_FULL_R))) {
426 keys |= 1 << GBA_KEY_R;
427 }
428 if ((padkeys & PAD_BUTTON_START) || (wiiPad & WPAD_BUTTON_PLUS) ||
429 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_PLUS))) {
430 keys |= 1 << GBA_KEY_START;
431 }
432 if ((padkeys & (PAD_BUTTON_X | PAD_BUTTON_Y)) || (wiiPad & WPAD_BUTTON_MINUS) ||
433 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_MINUS))) {
434 keys |= 1 << GBA_KEY_SELECT;
435 }
436 if ((padkeys & PAD_BUTTON_LEFT) || (wiiPad & WPAD_BUTTON_UP) ||
437 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_LEFT))) {
438 keys |= 1 << GBA_KEY_LEFT;
439 }
440 if ((padkeys & PAD_BUTTON_RIGHT) || (wiiPad & WPAD_BUTTON_DOWN) ||
441 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_RIGHT))) {
442 keys |= 1 << GBA_KEY_RIGHT;
443 }
444 if ((padkeys & PAD_BUTTON_UP) || (wiiPad & WPAD_BUTTON_RIGHT) ||
445 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_UP))) {
446 keys |= 1 << GBA_KEY_UP;
447 }
448 if ((padkeys & PAD_BUTTON_DOWN) || (wiiPad & WPAD_BUTTON_LEFT) ||
449 ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_DOWN))) {
450 keys |= 1 << GBA_KEY_DOWN;
451 }
452 int x = PAD_StickX(0);
453 int y = PAD_StickY(0);
454 if (x < -0x40) {
455 keys |= 1 << GBA_KEY_LEFT;
456 }
457 if (x > 0x40) {
458 keys |= 1 << GBA_KEY_RIGHT;
459 }
460 if (y < -0x40) {
461 keys |= 1 << GBA_KEY_DOWN;
462 }
463 if (y > 0x40) {
464 keys |= 1 << GBA_KEY_UP;
465 }
466 return keys;
467}
468
469void _setRumble(struct GBARumble* rumble, int enable) {
470 UNUSED(rumble);
471 WPAD_Rumble(0, enable);
472 if (enable) {
473 PAD_ControlMotor(0, PAD_MOTOR_RUMBLE);
474 } else {
475 PAD_ControlMotor(0, PAD_MOTOR_STOP);
476 }
477}
478
479void _sampleRotation(struct GBARotationSource* source) {
480 UNUSED(source);
481 vec3w_t accel;
482 WPAD_Accel(0, &accel);
483 // These are swapped
484 tiltX = (accel.y - 0x1EA) << 22;
485 tiltY = (accel.x - 0x1EA) << 22;
486
487 // This doesn't seem to work at all with -TR remotes
488 struct expansion_t exp;
489 WPAD_Expansion(0, &exp);
490 if (exp.type != EXP_MOTION_PLUS) {
491 return;
492 }
493 gyroZ = exp.mp.rz - 0x1FA0;
494 gyroZ <<= 18;
495}
496
497int32_t _readTiltX(struct GBARotationSource* source) {
498 UNUSED(source);
499 return tiltX;
500}
501
502int32_t _readTiltY(struct GBARotationSource* source) {
503 UNUSED(source);
504 return tiltY;
505}
506
507int32_t _readGyroZ(struct GBARotationSource* source) {
508 UNUSED(source);
509 return gyroZ;
510}