all repos — mgba @ 91fb4407b9a3bc64c84d74eaffe9673d89059a10

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