all repos — mgba @ 7b090aa0d129514f9af6cf6f0e3c3d385e35060f

mGBA Game Boy Advance Emulator

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 <ogc/machine/processor.h>
 11#include <malloc.h>
 12#include <unistd.h>
 13#include <wiiuse/wpad.h>
 14
 15#include "util/common.h"
 16
 17#include "gba/renderers/video-software.h"
 18#include "gba/context/context.h"
 19#include "gba/gui/gui-runner.h"
 20#include "util/gui.h"
 21#include "util/gui/file-select.h"
 22#include "util/gui/font.h"
 23#include "util/gui/menu.h"
 24#include "util/vfs.h"
 25
 26static enum ScreenMode {
 27	SM_PA,
 28	SM_SF,
 29	SM_MAX
 30} screenMode = SM_PA;
 31
 32enum FilterMode {
 33	FM_NEAREST,
 34	FM_LINEAR,
 35	FM_MAX
 36};
 37
 38#define SAMPLES 1024
 39
 40static void _retraceCallback(u32 count);
 41
 42static void _audioDMA(void);
 43static void _setRumble(struct GBARumble* rumble, int enable);
 44static void _sampleRotation(struct GBARotationSource* source);
 45static int32_t _readTiltX(struct GBARotationSource* source);
 46static int32_t _readTiltY(struct GBARotationSource* source);
 47static int32_t _readGyroZ(struct GBARotationSource* source);
 48
 49static void _drawStart(void);
 50static void _drawEnd(void);
 51static uint32_t _pollInput(void);
 52static enum GUICursorState _pollCursor(int* x, int* y);
 53static void _guiPrepare(void);
 54static void _guiFinish(void);
 55
 56static void _setup(struct GBAGUIRunner* runner);
 57static void _gameLoaded(struct GBAGUIRunner* runner);
 58static void _gameUnloaded(struct GBAGUIRunner* runner);
 59static void _unpaused(struct GBAGUIRunner* runner);
 60static void _drawFrame(struct GBAGUIRunner* runner, bool faded);
 61static uint16_t _pollGameInput(struct GBAGUIRunner* runner);
 62
 63static s8 WPAD_StickX(u8 chan, u8 right);
 64static s8 WPAD_StickY(u8 chan, u8 right);
 65
 66static struct GBAVideoSoftwareRenderer renderer;
 67static struct GBARumble rumble;
 68static struct GBARotationSource rotation;
 69static GXRModeObj* vmode;
 70static Mtx model, view, modelview;
 71static uint16_t* texmem;
 72static GXTexObj tex;
 73static int32_t tiltX;
 74static int32_t tiltY;
 75static int32_t gyroZ;
 76static uint32_t retraceCount;
 77static uint32_t referenceRetraceCount;
 78static int scaleFactor;
 79
 80static void* framebuffer[2] = { 0, 0 };
 81static int whichFb = 0;
 82
 83static struct GBAStereoSample audioBuffer[3][SAMPLES] __attribute__((__aligned__(32)));
 84static volatile size_t audioBufferSize = 0;
 85static volatile int currentAudioBuffer = 0;
 86
 87static struct GUIFont* font;
 88
 89static void reconfigureScreen(GXRModeObj* vmode) {
 90	free(framebuffer[0]);
 91	free(framebuffer[1]);
 92
 93	framebuffer[0] = SYS_AllocateFramebuffer(vmode);
 94	framebuffer[1] = SYS_AllocateFramebuffer(vmode);
 95
 96	VIDEO_SetBlack(true);
 97	VIDEO_Configure(vmode);
 98	VIDEO_SetNextFramebuffer(framebuffer[whichFb]);
 99	VIDEO_SetBlack(false);
100	VIDEO_Flush();
101	VIDEO_WaitVSync();
102	if (vmode->viTVMode & VI_NON_INTERLACE) {
103		VIDEO_WaitVSync();
104	}
105	GX_SetViewport(0, 0, vmode->fbWidth, vmode->efbHeight, 0, 1);
106
107	f32 yscale = GX_GetYScaleFactor(vmode->efbHeight, vmode->xfbHeight);
108	u32 xfbHeight = GX_SetDispCopyYScale(yscale);
109	GX_SetScissor(0, 0, vmode->viWidth, vmode->viWidth);
110	GX_SetDispCopySrc(0, 0, vmode->fbWidth, vmode->efbHeight);
111	GX_SetDispCopyDst(vmode->fbWidth, xfbHeight);
112	GX_SetCopyFilter(vmode->aa, vmode->sample_pattern, GX_TRUE, vmode->vfilter);
113	GX_SetFieldMode(vmode->field_rendering, ((vmode->viHeight == 2 * vmode->xfbHeight) ? GX_ENABLE : GX_DISABLE));
114
115	int hfactor = vmode->fbWidth / VIDEO_HORIZONTAL_PIXELS;
116	int vfactor = vmode->efbHeight / VIDEO_VERTICAL_PIXELS;
117	if (hfactor > vfactor) {
118		scaleFactor = vfactor;
119	} else {
120		scaleFactor = hfactor;
121	}
122};
123
124int main() {
125	VIDEO_Init();
126	PAD_Init();
127	WPAD_Init();
128	WPAD_SetDataFormat(0, WPAD_FMT_BTNS_ACC_IR);
129	AUDIO_Init(0);
130	AUDIO_SetDSPSampleRate(AI_SAMPLERATE_48KHZ);
131	AUDIO_RegisterDMACallback(_audioDMA);
132
133	memset(audioBuffer, 0, sizeof(audioBuffer));
134
135#if !defined(COLOR_16_BIT) && !defined(COLOR_5_6_5)
136#error This pixel format is unsupported. Please use -DCOLOR_16-BIT -DCOLOR_5_6_5
137#endif
138
139	vmode = VIDEO_GetPreferredMode(0);
140
141	GXColor bg = { 0, 0, 0, 0xFF };
142	void* fifo = memalign(32, 0x40000);
143	memset(fifo, 0, 0x40000);
144	GX_Init(fifo, 0x40000);
145	GX_SetCopyClear(bg, 0x00FFFFFF);
146
147	reconfigureScreen(vmode);
148
149	GX_SetCullMode(GX_CULL_NONE);
150	GX_SetDispCopyGamma(GX_GM_1_0);
151
152	GX_ClearVtxDesc();
153	GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
154	GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);
155	GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
156
157	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XY, GX_S16, 0);
158	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_S16, 0);
159	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
160
161	GX_SetNumChans(1);
162	GX_SetNumTexGens(1);
163	GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
164	GX_SetTevOp(GX_TEVSTAGE0, GX_MODULATE);
165
166	GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
167	GX_InvVtxCache();
168	GX_InvalidateTexAll();
169
170	guVector cam = { 0.0f, 0.0f, 0.0f };
171	guVector up = { 0.0f, 1.0f, 0.0f };
172	guVector look = { 0.0f, 0.0f, -1.0f };
173	guLookAt(view, &cam, &up, &look);
174
175	guMtxIdentity(model);
176	guMtxTransApply(model, model, 0.0f, 0.0f, -6.0f);
177	guMtxConcat(view, model, modelview);
178	GX_LoadPosMtxImm(modelview, GX_PNMTX0);
179
180	texmem = memalign(32, 256 * 256 * BYTES_PER_PIXEL);
181	memset(texmem, 0, 256 * 256 * BYTES_PER_PIXEL);
182	GX_InitTexObj(&tex, texmem, 256, 256, GX_TF_RGB565, GX_CLAMP, GX_CLAMP, GX_FALSE);
183
184	VIDEO_SetPostRetraceCallback(_retraceCallback);
185
186	font = GUIFontCreate();
187
188	fatInitDefault();
189
190	rumble.setRumble = _setRumble;
191
192	rotation.sample = _sampleRotation;
193	rotation.readTiltX = _readTiltX;
194	rotation.readTiltY = _readTiltY;
195	rotation.readGyroZ = _readGyroZ;
196
197	struct GBAGUIRunner runner = {
198		.params = {
199			vmode->fbWidth * 0.9, vmode->efbHeight * 0.9,
200			font, "",
201			_drawStart, _drawEnd,
202			_pollInput, _pollCursor,
203			0,
204			_guiPrepare, _guiFinish,
205
206			GUI_PARAMS_TRAIL
207		},
208		.configExtra = (struct GUIMenuItem[]) {
209			{
210				.title = "Screen mode",
211				.data = "screenMode",
212				.submenu = 0,
213				.state = 0,
214				.validStates = (const char*[]) {
215					"Pixel-Accurate",
216					"Stretched",
217					0
218				}
219			},
220			{
221				.title = "Filtering",
222				.data = "filter",
223				.submenu = 0,
224				.state = 0,
225				.validStates = (const char*[]) {
226					"Pixelated",
227					"Resampled",
228					0
229				}
230			}
231		},
232		.nConfigExtra = 2,
233		.setup = _setup,
234		.teardown = 0,
235		.gameLoaded = _gameLoaded,
236		.gameUnloaded = _gameUnloaded,
237		.prepareForFrame = 0,
238		.drawFrame = _drawFrame,
239		.paused = _gameUnloaded,
240		.unpaused = _unpaused,
241		.pollGameInput = _pollGameInput
242	};
243	GBAGUIInit(&runner, "wii");
244	GBAGUIRunloop(&runner);
245	GBAGUIDeinit(&runner);
246
247	free(fifo);
248
249	free(renderer.outputBuffer);
250	GUIFontDestroy(font);
251
252	free(framebuffer[0]);
253	free(framebuffer[1]);
254
255	return 0;
256}
257
258static void _audioDMA(void) {
259	if (!audioBufferSize) {
260		return;
261	}
262	DCFlushRange(audioBuffer[currentAudioBuffer], audioBufferSize * sizeof(struct GBAStereoSample));
263	AUDIO_InitDMA((u32) audioBuffer[currentAudioBuffer], audioBufferSize * sizeof(struct GBAStereoSample));
264	currentAudioBuffer = (currentAudioBuffer + 1) % 3;
265	audioBufferSize = 0;
266}
267
268static void _drawStart(void) {
269	u32 level = 0;
270	_CPU_ISR_Disable(level);
271	if (referenceRetraceCount >= retraceCount) {
272		VIDEO_WaitVSync();
273	}
274	_CPU_ISR_Restore(level);
275
276	GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
277	GX_SetColorUpdate(GX_TRUE);
278
279	GX_SetViewport(0, 0, vmode->fbWidth, vmode->efbHeight, 0, 1);
280}
281
282static void _drawEnd(void) {
283	whichFb = !whichFb;
284
285	GX_CopyDisp(framebuffer[whichFb], GX_TRUE);
286	GX_DrawDone();
287	VIDEO_SetNextFramebuffer(framebuffer[whichFb]);
288	VIDEO_Flush();
289
290	u32 level = 0;
291	_CPU_ISR_Disable(level);
292	++referenceRetraceCount;
293	_CPU_ISR_Restore(level);
294}
295
296static uint32_t _pollInput(void) {
297	PAD_ScanPads();
298	u16 padkeys = PAD_ButtonsHeld(0);
299
300	WPAD_ScanPads();
301	u32 wiiPad = WPAD_ButtonsHeld(0);
302	u32 ext = 0;
303	WPAD_Probe(0, &ext);
304
305	int keys = 0;
306	int x = PAD_StickX(0);
307	int y = PAD_StickY(0);
308	int w_x = WPAD_StickX(0,0);
309	int w_y = WPAD_StickY(0,0);
310	if (x < -0x40 || w_x < -0x40) {
311		keys |= 1 << GUI_INPUT_LEFT;
312	}
313	if (x > 0x40 || w_x > 0x40) {
314		keys |= 1 << GUI_INPUT_RIGHT;
315	}
316	if (y < -0x40 || w_y <- 0x40) {
317		keys |= 1 << GUI_INPUT_DOWN;
318	}
319	if (y > 0x40 || w_y > 0x40) {
320		keys |= 1 << GUI_INPUT_UP;
321	}
322	if ((padkeys & PAD_BUTTON_A) || (wiiPad & WPAD_BUTTON_2) || 
323	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_A | WPAD_CLASSIC_BUTTON_Y)))) {
324		keys |= 1 << GUI_INPUT_SELECT;
325	}
326	if ((padkeys & PAD_BUTTON_B) || (wiiPad & WPAD_BUTTON_1) || (wiiPad & WPAD_BUTTON_B) ||
327	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_B | WPAD_CLASSIC_BUTTON_X)))) {
328		keys |= 1 << GUI_INPUT_BACK;
329	}
330	if ((padkeys & PAD_TRIGGER_Z) || (wiiPad & WPAD_BUTTON_HOME) ||
331	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_HOME)))) {
332		keys |= 1 << GUI_INPUT_CANCEL;
333	}
334	if ((padkeys & PAD_BUTTON_LEFT)|| (wiiPad & WPAD_BUTTON_UP) ||
335	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_LEFT))) {
336		keys |= 1 << GUI_INPUT_LEFT;
337	}
338	if ((padkeys & PAD_BUTTON_RIGHT) || (wiiPad & WPAD_BUTTON_DOWN) ||
339	   ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_RIGHT))) {
340		keys |= 1 << GUI_INPUT_RIGHT;
341	}
342	if ((padkeys & PAD_BUTTON_UP) || (wiiPad & WPAD_BUTTON_RIGHT) ||
343	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_UP))) {
344		keys |= 1 << GUI_INPUT_UP;
345	}
346	if ((padkeys & PAD_BUTTON_DOWN) || (wiiPad & WPAD_BUTTON_LEFT) ||
347	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_DOWN))) {
348		keys |= 1 << GUI_INPUT_DOWN;
349	}
350	return keys;
351}
352
353static enum GUICursorState _pollCursor(int* x, int* y) {
354	ir_t ir;
355	WPAD_IR(0, &ir);
356	if (!ir.smooth_valid) {
357		return GUI_CURSOR_NOT_PRESENT;
358	}
359	*x = ir.sx;
360	*y = ir.sy;
361	WPAD_ScanPads();
362	u32 wiiPad = WPAD_ButtonsHeld(0);
363	if (wiiPad & WPAD_BUTTON_A) {
364		return GUI_CURSOR_DOWN;
365	}
366	return GUI_CURSOR_UP;
367}
368
369void _reproj(int w, int h) {
370	Mtx44 proj;
371	int top = (vmode->efbHeight - h) / 2;
372	int left = (vmode->fbWidth - w) / 2;
373	guOrtho(proj, -top, top + h, -left, left + w, 0, 300);
374	GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);
375}
376
377void _guiPrepare(void) {
378	int w = vmode->fbWidth * 0.9;
379	int h = vmode->efbHeight * 0.9;
380	_reproj(w, h);
381}
382
383void _guiFinish(void) {
384	if (screenMode == SM_PA) {
385		_reproj(VIDEO_HORIZONTAL_PIXELS * scaleFactor, VIDEO_VERTICAL_PIXELS * scaleFactor);
386	} else {
387		Mtx44 proj;
388		short top = (CONF_GetAspectRatio() == CONF_ASPECT_16_9) ? 10 : 20;
389		short bottom = VIDEO_VERTICAL_PIXELS + top;
390		guOrtho(proj, -top, bottom, 0, VIDEO_HORIZONTAL_PIXELS, 0, 300);
391		GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);
392	}
393}
394
395void _setup(struct GBAGUIRunner* runner) {
396	runner->context.gba->rumble = &rumble;
397	runner->context.gba->rotationSource = &rotation;
398
399	GBAVideoSoftwareRendererCreate(&renderer);
400	renderer.outputBuffer = memalign(32, 256 * 256 * BYTES_PER_PIXEL);
401	renderer.outputBufferStride = 256;
402	runner->context.renderer = &renderer.d;
403
404	GBAAudioResizeBuffer(&runner->context.gba->audio, SAMPLES);
405
406#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
407	double ratio = GBAAudioCalculateRatio(1, 60 / 1.001, 1);
408	blip_set_rates(runner->context.gba->audio.left,  GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
409	blip_set_rates(runner->context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
410#endif
411}
412
413void _gameUnloaded(struct GBAGUIRunner* runner) {
414	UNUSED(runner);
415	AUDIO_StopDMA();
416}
417
418void _gameLoaded(struct GBAGUIRunner* runner) {
419	if (runner->context.gba->memory.hw.devices & HW_GYRO) {
420		int i;
421		for (i = 0; i < 6; ++i) {
422			u32 result = WPAD_SetMotionPlus(0, 1);
423			if (result == WPAD_ERR_NONE) {
424				break;
425			}
426			sleep(1);
427		}
428	}
429	_unpaused(runner);
430}
431
432void _unpaused(struct GBAGUIRunner* runner) {
433	u32 level = 0;
434	_CPU_ISR_Disable(level);
435	referenceRetraceCount = retraceCount;
436	_CPU_ISR_Restore(level);
437
438	unsigned mode;
439	if (GBAConfigGetUIntValue(&runner->context.config, "screenMode", &mode) && mode < SM_MAX) {
440		screenMode = mode;
441	}
442	if (GBAConfigGetUIntValue(&runner->context.config, "filter", &mode) && mode < FM_MAX) {
443		switch (mode) {
444		case FM_NEAREST:
445		default:
446			GX_InitTexObjFilterMode(&tex, GX_NEAR, GX_NEAR);
447			break;
448		case FM_LINEAR:
449			GX_InitTexObjFilterMode(&tex, GX_LINEAR, GX_LINEAR);
450			break;
451		}
452	}
453	_guiFinish();
454}
455
456void _drawFrame(struct GBAGUIRunner* runner, bool faded) {
457#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
458	int available = blip_samples_avail(runner->context.gba->audio.left);
459	if (available + audioBufferSize > SAMPLES) {
460		available = SAMPLES - audioBufferSize;
461	}
462	available &= ~((32 / sizeof(struct GBAStereoSample)) - 1); // Force align to 32 bytes
463	if (available > 0) {
464		// These appear to be reversed for AUDIO_InitDMA
465		blip_read_samples(runner->context.gba->audio.left, &audioBuffer[currentAudioBuffer][audioBufferSize].right, available, true);
466		blip_read_samples(runner->context.gba->audio.right, &audioBuffer[currentAudioBuffer][audioBufferSize].left, available, true);
467		audioBufferSize += available;
468	}
469	if (audioBufferSize == SAMPLES && !AUDIO_GetDMAEnableFlag()) {
470		_audioDMA();
471		AUDIO_StartDMA();
472	}
473#endif
474
475	uint32_t color = 0xFFFFFF3F;
476	if (!faded) {
477		color |= 0xC0;
478	}
479	size_t x, y;
480	uint64_t* texdest = (uint64_t*) texmem;
481	uint64_t* texsrc = (uint64_t*) renderer.outputBuffer;
482	for (y = 0; y < VIDEO_VERTICAL_PIXELS; y += 4) {
483		for (x = 0; x < VIDEO_HORIZONTAL_PIXELS >> 2; ++x) {
484			texdest[0 + x * 4 + y * 64] = texsrc[0   + x + y * 64];
485			texdest[1 + x * 4 + y * 64] = texsrc[64  + x + y * 64];
486			texdest[2 + x * 4 + y * 64] = texsrc[128 + x + y * 64];
487			texdest[3 + x * 4 + y * 64] = texsrc[192 + x + y * 64];
488		}
489	}
490	DCFlushRange(texdest, 256 * 256 * BYTES_PER_PIXEL);
491
492	if (faded) {
493		GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_NOOP);
494	} else {
495		GX_SetBlendMode(GX_BM_NONE, GX_BL_ONE, GX_BL_ZERO, GX_LO_NOOP);
496	}
497	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_S16, 0);
498	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
499	GX_InvalidateTexAll();
500	GX_LoadTexObj(&tex, GX_TEXMAP0);
501
502	s16 vertSize = 256;
503	if (screenMode == SM_PA) {
504		vertSize *= scaleFactor;
505	}
506
507	GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
508	GX_Position2s16(0, vertSize);
509	GX_Color1u32(color);
510	GX_TexCoord2s16(0, 1);
511
512	GX_Position2s16(vertSize, vertSize);
513	GX_Color1u32(color);
514	GX_TexCoord2s16(1, 1);
515
516	GX_Position2s16(vertSize, 0);
517	GX_Color1u32(color);
518	GX_TexCoord2s16(1, 0);
519
520	GX_Position2s16(0, 0);
521	GX_Color1u32(color);
522	GX_TexCoord2s16(0, 0);
523	GX_End();
524}
525
526uint16_t _pollGameInput(struct GBAGUIRunner* runner) {
527	UNUSED(runner);
528	PAD_ScanPads();
529	u16 padkeys = PAD_ButtonsHeld(0);
530	WPAD_ScanPads();
531	u32 wiiPad = WPAD_ButtonsHeld(0);
532	u32 ext = 0;
533	uint16_t keys = 0;
534	WPAD_Probe(0, &ext);
535
536	if ((padkeys & PAD_BUTTON_A) || (wiiPad & WPAD_BUTTON_2) || 
537	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_A | WPAD_CLASSIC_BUTTON_Y)))) {
538		keys |= 1 << GBA_KEY_A;
539	}
540	if ((padkeys & PAD_BUTTON_B) || (wiiPad & WPAD_BUTTON_1) ||
541	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & (WPAD_CLASSIC_BUTTON_B | WPAD_CLASSIC_BUTTON_X)))) {
542		keys |= 1 << GBA_KEY_B;
543	}
544	if ((padkeys & PAD_TRIGGER_L) || (wiiPad & WPAD_BUTTON_B) ||
545	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_FULL_L))) {
546		keys |= 1 << GBA_KEY_L;
547	}
548	if ((padkeys & PAD_TRIGGER_R) || (wiiPad & WPAD_BUTTON_A) ||
549	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_FULL_R))) {
550		keys |= 1 << GBA_KEY_R;
551	}
552	if ((padkeys & PAD_BUTTON_START) || (wiiPad & WPAD_BUTTON_PLUS) ||
553	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_PLUS))) {
554		keys |= 1 << GBA_KEY_START;
555	}
556	if ((padkeys & (PAD_BUTTON_X | PAD_BUTTON_Y)) || (wiiPad & WPAD_BUTTON_MINUS) ||
557	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_MINUS))) {
558		keys |= 1 << GBA_KEY_SELECT;
559	}
560	if ((padkeys & PAD_BUTTON_LEFT) || (wiiPad & WPAD_BUTTON_UP) ||
561	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_LEFT))) {
562		keys |= 1 << GBA_KEY_LEFT;
563	}
564	if ((padkeys & PAD_BUTTON_RIGHT) || (wiiPad & WPAD_BUTTON_DOWN) ||
565	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_RIGHT))) {
566		keys |= 1 << GBA_KEY_RIGHT;
567	}
568	if ((padkeys & PAD_BUTTON_UP) || (wiiPad & WPAD_BUTTON_RIGHT) ||
569	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_UP))) {
570		keys |= 1 << GBA_KEY_UP;
571	}
572	if ((padkeys & PAD_BUTTON_DOWN) || (wiiPad & WPAD_BUTTON_LEFT) ||
573	    ((ext == WPAD_EXP_CLASSIC) && (wiiPad & WPAD_CLASSIC_BUTTON_DOWN))) {
574		keys |= 1 << GBA_KEY_DOWN;
575	}
576	int x = PAD_StickX(0);
577	int y = PAD_StickY(0);
578	int w_x = WPAD_StickX(0,0);
579	int w_y = WPAD_StickY(0,0);
580	if (x < -0x40 || w_x < -0x40) {
581		keys |= 1 << GBA_KEY_LEFT;
582	}
583	if (x > 0x40 || w_x > 0x40) {
584		keys |= 1 << GBA_KEY_RIGHT;
585	}
586	if (y < -0x40 || w_y < -0x40) {
587		keys |= 1 << GBA_KEY_DOWN;
588	}
589	if (y > 0x40 || w_y > 0x40) {
590		keys |= 1 << GBA_KEY_UP;
591	}
592	return keys;
593}
594
595void _setRumble(struct GBARumble* rumble, int enable) {
596	UNUSED(rumble);
597	WPAD_Rumble(0, enable);
598	if (enable) {
599		PAD_ControlMotor(0, PAD_MOTOR_RUMBLE);
600	} else {
601		PAD_ControlMotor(0, PAD_MOTOR_STOP);
602	}
603}
604
605void _sampleRotation(struct GBARotationSource* source) {
606	UNUSED(source);
607	vec3w_t accel;
608	WPAD_Accel(0, &accel);
609	// These are swapped
610	tiltX = (accel.y - 0x1EA) << 22;
611	tiltY = (accel.x - 0x1EA) << 22;
612
613	// This doesn't seem to work at all with -TR remotes
614	struct expansion_t exp;
615	WPAD_Expansion(0, &exp);
616	if (exp.type != EXP_MOTION_PLUS) {
617		return;
618	}
619	gyroZ = exp.mp.rz - 0x1FA0;
620	gyroZ <<= 18;
621}
622
623int32_t _readTiltX(struct GBARotationSource* source) {
624	UNUSED(source);
625	return tiltX;
626}
627
628int32_t _readTiltY(struct GBARotationSource* source) {
629	UNUSED(source);
630	return tiltY;
631}
632
633int32_t _readGyroZ(struct GBARotationSource* source) {
634	UNUSED(source);
635	return gyroZ;
636}
637
638static s8 WPAD_StickX(u8 chan, u8 right) {
639	float mag = 0.0;
640	float ang = 0.0;
641	WPADData *data = WPAD_Data(chan);
642
643	switch (data->exp.type)	{
644	case WPAD_EXP_NUNCHUK:
645	case WPAD_EXP_GUITARHERO3:
646		if (right == 0) {
647			mag = data->exp.nunchuk.js.mag;
648			ang = data->exp.nunchuk.js.ang;
649		}
650		break;
651	case WPAD_EXP_CLASSIC:
652		if (right == 0) {
653			mag = data->exp.classic.ljs.mag;
654			ang = data->exp.classic.ljs.ang;
655		} else {
656			mag = data->exp.classic.rjs.mag;
657			ang = data->exp.classic.rjs.ang;
658		}
659		break;
660	default:
661		break;
662	}
663
664	/* calculate X value (angle need to be converted into radian) */
665	if (mag > 1.0) {
666		mag = 1.0;
667	} else if (mag < -1.0) {
668		mag = -1.0;
669	}
670	double val = mag * sinf(M_PI * ang / 180.0f);
671 
672	return (s8)(val * 128.0f);
673}
674
675static s8 WPAD_StickY(u8 chan, u8 right) {
676	float mag = 0.0;
677	float ang = 0.0;
678	WPADData *data = WPAD_Data(chan);
679
680	switch (data->exp.type) {
681	case WPAD_EXP_NUNCHUK:
682	case WPAD_EXP_GUITARHERO3:
683		if (right == 0) {
684			mag = data->exp.nunchuk.js.mag;
685			ang = data->exp.nunchuk.js.ang;
686		}
687		break;
688	case WPAD_EXP_CLASSIC:
689		if (right == 0) {
690			mag = data->exp.classic.ljs.mag;
691			ang = data->exp.classic.ljs.ang;
692		} else {
693			mag = data->exp.classic.rjs.mag;
694			ang = data->exp.classic.rjs.ang;
695		}
696		break;
697	default:
698		break;
699	}
700
701	/* calculate X value (angle need to be converted into radian) */
702	if (mag > 1.0) { 
703		mag = 1.0;
704	} else if (mag < -1.0) {
705		mag = -1.0;
706	}
707	double val = mag * cosf(M_PI * ang / 180.0f);
708 
709	return (s8)(val * 128.0f);
710}
711
712void _retraceCallback(u32 count) {
713	u32 level = 0;
714	_CPU_ISR_Disable(level);
715	retraceCount = count;
716	_CPU_ISR_Restore(level);
717}