Skip to content

Instantly share code, notes, and snippets.

@CreeperMario
Created April 20, 2016 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CreeperMario/7fe77a2b67edd3d854b76739e8e6b873 to your computer and use it in GitHub Desktop.
Save CreeperMario/7fe77a2b67edd3d854b76739e8e6b873 to your computer and use it in GitHub Desktop.
Wii U Homebrew: Wii Remote Button Tester (buggy when remotes are not connected)
#ifndef COREINIT_H
#define COREINIT_H
#include "types.h"
#if (VER==200)
#define OSDynLoad_Acquire ((void (*)(char* rpl, unsigned int *handle))0x010220AC)
#define OSDynLoad_FindExport ((void (*)(unsigned int handle, int isdata, char *symbol, void *address))0x01022D98)
#define OSFatal ((void (*)(char* msg))0x01027688)
#define __os_snprintf ((int(*)(char* s, int n, const char * format, ... ))0x01025FB4)
#elif (VER==210)
#define OSDynLoad_Acquire ((void (*)(char* rpl, unsigned int *handle))0x0102232C)
#define OSDynLoad_FindExport ((void (*)(unsigned int handle, int isdata, char *symbol, void *address))0x01023018)
#define OSFatal ((void (*)(char* msg))0x01027908)
#define __os_snprintf ((int(*)(char* s, int n, const char * format, ... ))0x01026014)
#elif (VER==300) | (VER==310)
#define OSDynLoad_Acquire ((void (*)(char* rpl, unsigned int *handle))0x01022CBC)
#define OSDynLoad_FindExport ((void (*)(unsigned int handle, int isdata, char *symbol, void *address))0x01023D88)
#define OSFatal ((void (*)(char* msg))0x01028A68)
#define __os_snprintf ((int(*)(char* s, int n, const char * format, ... ))0x01027390)
#elif (VER==400) | (VER==410)
#define OSDynLoad_Acquire ((void (*)(char* rpl, unsigned int *handle))0x01026e60)
#define OSDynLoad_FindExport ((void (*)(unsigned int handle, int isdata, char *symbol, void *address))0x01028460)
#define OSFatal ((void (*)(char* msg))0x0102D01C)
#define __os_snprintf ((int(*)(char* s, int n, const char * format, ... ))0x0102b9ac)
#elif VER==500
#define OSDynLoad_Acquire ((void (*)(char* rpl, unsigned int *handle))0x01029F70)
#define OSDynLoad_FindExport ((void (*)(unsigned int handle, int isdata, char *symbol, void *address))0x0102B3E4)
#define OSFatal ((void (*)(char* msg))0x01030ECC)
#define __os_snprintf ((int(*)(char* s, int n, const char * format, ... ))0x0102ECE0)
#elif VER==532
#define OSDynLoad_Acquire ((void (*)(char* rpl, unsigned int *handle))0x102a31c)
#define OSDynLoad_FindExport ((void (*)(unsigned int handle, int isdata, char *symbol, void *address))0x102b790)
#define OSFatal ((void (*)(char* msg))0x1031368)
#define __os_snprintf ((int(*)(char* s, int n, const char * format, ... ))0x102f09c)
#elif VER==550
#define OSDynLoad_Acquire ((void (*)(char* rpl, unsigned int *handle))0x0102A3B4)
#define OSDynLoad_FindExport ((void (*)(unsigned int handle, int isdata, char *symbol, void *address))0x0102B828)
#define OSFatal ((void (*)(char* msg))0x01031618)
#define __os_snprintf ((int(*)(char* s, int n, const char * format, ... ))0x0102F160)
#else
#error "Unsupported Wii U software version"
#endif
/* ioctlv() I/O vector */
struct iovec
{
void *buffer;
int len;
char unknown8[0xc-0x8];
};
typedef struct OSContext
{
/* OSContext identifier */
uint32_t tag1;
uint32_t tag2;
/* GPRs */
uint32_t gpr[32];
/* Special registers */
uint32_t cr;
uint32_t lr;
uint32_t ctr;
uint32_t xer;
/* Initial PC and MSR */
uint32_t srr0;
uint32_t srr1;
} OSContext;
#endif /* COREINIT_H */
#include "loader.h"
void _start() {
asm(
"lis %r1, 0x1ab5 ;"
"ori %r1, %r1, 0xd138 ;"
);
uint32_t coreinit_handle, padscore_handle, vpad_handle;
OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle);
OSDynLoad_Acquire("padscore.rpl", &padscore_handle);
OSDynLoad_Acquire("vpad.rpl", &vpad_handle);
int(*_Exit)();
int(*IM_Close)(int fd);
int(*IM_Open)();
int(*IM_SetDeviceState)(int fd, void *mem, int state, int a, int b);
void*(*memset)(void * dest, uint32_t value, uint32_t bytes);
void*(*OSAllocFromSystem)(uint32_t size, int align);
void(*OSFreeToSystem)(void *ptr);
void(*OSScreenClearBufferEx)(int bufferNum, uint32_t colour);
uint32_t(*OSScreenGetBufferSizeEx)(uint32_t buffer);
void(*OSScreenFlipBuffersEx)(int bufferNum);
void(*OSScreenInit)();
void(*OSScreenPutFontEx)(int bufferNum, uint32_t posX, uint32_t posY, const char *str);
uint32_t(*OSScreenSetBufferEx)(uint32_t buffer, void * address);
void(*WPADInit)(void);
int(*WPADRead)(int chan, void *buffer);
bool(*WPADSetAcceptConnection)(bool acceptConnections);
void(*WPADShutdown)(void);
int(*VPADRead)(int padnum, VPADData *buffer, int num_datasets, int *err);
OSDynLoad_FindExport(coreinit_handle, 0, "_Exit", &_Exit);
OSDynLoad_FindExport(coreinit_handle, 0, "IM_Close", &IM_Close);
OSDynLoad_FindExport(coreinit_handle, 0, "IM_Open", &IM_Open);
OSDynLoad_FindExport(coreinit_handle, 0, "IM_SetDeviceState", &IM_SetDeviceState);
OSDynLoad_FindExport(coreinit_handle, 0, "memset", &memset);
OSDynLoad_FindExport(coreinit_handle, 0, "OSAllocFromSystem", &OSAllocFromSystem);
OSDynLoad_FindExport(coreinit_handle, 0, "OSFreeToSystem", &OSFreeToSystem);
OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenClearBufferEx", &OSScreenClearBufferEx);
OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenGetBufferSizeEx", &OSScreenGetBufferSizeEx);
OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenFlipBuffersEx", &OSScreenFlipBuffersEx);
OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenInit", &OSScreenInit);
OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenPutFontEx", &OSScreenPutFontEx);
OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenSetBufferEx", &OSScreenSetBufferEx);
OSDynLoad_FindExport(padscore_handle, 0, "WPADInit", &WPADInit);
OSDynLoad_FindExport(padscore_handle, 0, "WPADRead", &WPADRead);
OSDynLoad_FindExport(padscore_handle, 0, "WPADSetAcceptConnection", &WPADSetAcceptConnection);
OSDynLoad_FindExport(padscore_handle, 0, "WPADShutdown", &WPADShutdown);
OSDynLoad_FindExport(vpad_handle, 0, "VPADRead", &VPADRead);
int fd = IM_Open();
void *mem = OSAllocFromSystem(0x100, 64);
memset(mem, 0, 0x100);
IM_SetDeviceState(fd, mem, 3, 0, 0);
IM_Close(fd);
OSFreeToSystem(mem);
unsigned int i = 0x1FFFFFFF;
while(i--);
OSScreenInit();
uint32_t buf0_size = OSScreenGetBufferSizeEx(0);
uint32_t buf1_size = OSScreenGetBufferSizeEx(1);
OSScreenSetBufferEx(0, (void *)0xF4000000);
OSScreenSetBufferEx(1, (void *)0xF4000000 + buf0_size);
OSScreenClearBufferEx(0, 0x000000FF);
OSScreenFlipBuffersEx(0);
OSScreenClearBufferEx(1, 0x000000FF);
OSScreenPutFontEx(1, 0, 0, "Loading...");
OSScreenFlipBuffersEx(1);
VPADData vpad_data;
int32_t vpad_error;
VPADRead(0, &vpad_data, 1, &vpad_error);
WPADStatus pad1;
WPADStatus pad2;
WPADStatus pad3;
WPADStatus pad4;
WPADRead(0, &pad1);
WPADRead(1, &pad2);
WPADRead(2, &pad3);
WPADRead(3, &pad4);
uint8_t errBuffer[4];
while(1) {
VPADRead(0, &vpad_data, 1, &vpad_error);
if(vpad_data.btn_trigger & BUTTON_HOME) {
break;
}
WPADRead(0, &pad1);
WPADRead(1, &pad2);
WPADRead(2, &pad3);
WPADRead(3, &pad4);
OSScreenClearBufferEx(1, 0x000000FF);
OSScreenPutFontEx(1, 19, 0, "Wii Remote Button Tester");
OSScreenPutFontEx(1, 0, 2, " 1 2 3 4\n Left\nRight\n Down\n Up\n +\n 2\n 1\n B\n A\n -\n Z\n C\n HOME\nError");
OSScreenPutFontEx(1, 29, 2, " 1 2 3 4\n X Rotation\n Y Rotation\n Z Rotation\n IR Pointer 1\n IR Pointer 2\n IR Pointer 3\n IR Pointer 4");
if(pad1.buttons & WPAD_BUTTON_LEFT) OSScreenPutFontEx(1, 8, 3, "@");
if(pad1.buttons & WPAD_BUTTON_RIGHT) OSScreenPutFontEx(1, 8, 4, "@");
if(pad1.buttons & WPAD_BUTTON_DOWN) OSScreenPutFontEx(1, 8, 5, "@");
if(pad1.buttons & WPAD_BUTTON_UP) OSScreenPutFontEx(1, 8, 6, "@");
if(pad1.buttons & WPAD_BUTTON_PLUS) OSScreenPutFontEx(1, 8, 7, "@");
if(pad1.buttons & WPAD_BUTTON_2) OSScreenPutFontEx(1, 8, 8, "@");
if(pad1.buttons & WPAD_BUTTON_1) OSScreenPutFontEx(1, 8, 9, "@");
if(pad1.buttons & WPAD_BUTTON_B) OSScreenPutFontEx(1, 8, 10, "@");
if(pad1.buttons & WPAD_BUTTON_A) OSScreenPutFontEx(1, 8, 11, "@");
if(pad1.buttons & WPAD_BUTTON_MINUS) OSScreenPutFontEx(1, 8, 12, "@");
if(pad1.buttons & WPAD_BUTTON_Z) OSScreenPutFontEx(1, 8, 13, "@");
if(pad1.buttons & WPAD_BUTTON_C) OSScreenPutFontEx(1, 8, 14, "@");
if(pad1.buttons & WPAD_BUTTON_HOME) OSScreenPutFontEx(1, 8, 15, "@");
if(pad2.buttons & WPAD_BUTTON_LEFT) OSScreenPutFontEx(1, 12, 3, "@");
if(pad2.buttons & WPAD_BUTTON_RIGHT) OSScreenPutFontEx(1, 12, 4, "@");
if(pad2.buttons & WPAD_BUTTON_DOWN) OSScreenPutFontEx(1, 12, 5, "@");
if(pad2.buttons & WPAD_BUTTON_UP) OSScreenPutFontEx(1, 12, 6, "@");
if(pad2.buttons & WPAD_BUTTON_PLUS) OSScreenPutFontEx(1, 12, 7, "@");
if(pad2.buttons & WPAD_BUTTON_2) OSScreenPutFontEx(1, 12, 8, "@");
if(pad2.buttons & WPAD_BUTTON_1) OSScreenPutFontEx(1, 12, 9, "@");
if(pad2.buttons & WPAD_BUTTON_B) OSScreenPutFontEx(1, 12, 10, "@");
if(pad2.buttons & WPAD_BUTTON_A) OSScreenPutFontEx(1, 12, 11, "@");
if(pad2.buttons & WPAD_BUTTON_MINUS) OSScreenPutFontEx(1, 12, 12, "@");
if(pad2.buttons & WPAD_BUTTON_Z) OSScreenPutFontEx(1, 12, 13, "@");
if(pad2.buttons & WPAD_BUTTON_C) OSScreenPutFontEx(1, 12, 14, "@");
if(pad2.buttons & WPAD_BUTTON_HOME) OSScreenPutFontEx(1, 12, 15, "@");
if(pad3.buttons & WPAD_BUTTON_LEFT) OSScreenPutFontEx(1, 16, 3, "@");
if(pad3.buttons & WPAD_BUTTON_RIGHT) OSScreenPutFontEx(1, 16, 4, "@");
if(pad3.buttons & WPAD_BUTTON_DOWN) OSScreenPutFontEx(1, 16, 5, "@");
if(pad3.buttons & WPAD_BUTTON_UP) OSScreenPutFontEx(1, 16, 6, "@");
if(pad3.buttons & WPAD_BUTTON_PLUS) OSScreenPutFontEx(1, 16, 7, "@");
if(pad3.buttons & WPAD_BUTTON_2) OSScreenPutFontEx(1, 16, 8, "@");
if(pad3.buttons & WPAD_BUTTON_1) OSScreenPutFontEx(1, 16, 9, "@");
if(pad3.buttons & WPAD_BUTTON_B) OSScreenPutFontEx(1, 16, 10, "@");
if(pad3.buttons & WPAD_BUTTON_A) OSScreenPutFontEx(1, 16, 11, "@");
if(pad3.buttons & WPAD_BUTTON_MINUS) OSScreenPutFontEx(1, 16, 12, "@");
if(pad3.buttons & WPAD_BUTTON_Z) OSScreenPutFontEx(1, 16, 13, "@");
if(pad3.buttons & WPAD_BUTTON_C) OSScreenPutFontEx(1, 16, 14, "@");
if(pad3.buttons & WPAD_BUTTON_HOME) OSScreenPutFontEx(1, 16, 15, "@");
if(pad4.buttons & WPAD_BUTTON_LEFT) OSScreenPutFontEx(1, 20, 3, "@");
if(pad4.buttons & WPAD_BUTTON_RIGHT) OSScreenPutFontEx(1, 20, 4, "@");
if(pad4.buttons & WPAD_BUTTON_DOWN) OSScreenPutFontEx(1, 20, 5, "@");
if(pad4.buttons & WPAD_BUTTON_UP) OSScreenPutFontEx(1, 20, 6, "@");
if(pad4.buttons & WPAD_BUTTON_PLUS) OSScreenPutFontEx(1, 20, 7, "@");
if(pad4.buttons & WPAD_BUTTON_2) OSScreenPutFontEx(1, 20, 8, "@");
if(pad4.buttons & WPAD_BUTTON_1) OSScreenPutFontEx(1, 20, 9, "@");
if(pad4.buttons & WPAD_BUTTON_B) OSScreenPutFontEx(1, 20, 10, "@");
if(pad4.buttons & WPAD_BUTTON_A) OSScreenPutFontEx(1, 20, 11, "@");
if(pad4.buttons & WPAD_BUTTON_MINUS) OSScreenPutFontEx(1, 20, 12, "@");
if(pad4.buttons & WPAD_BUTTON_Z) OSScreenPutFontEx(1, 20, 13, "@");
if(pad4.buttons & WPAD_BUTTON_C) OSScreenPutFontEx(1, 20, 14, "@");
if(pad4.buttons & WPAD_BUTTON_HOME) OSScreenPutFontEx(1, 20, 15, "@");
__os_snprintf(errBuffer, 4, "%d", pad1.err);
OSScreenPutFontEx(1, 8, 16, errBuffer);
__os_snprintf(errBuffer, 4, "%d", pad2.err);
OSScreenPutFontEx(1, 12, 16, errBuffer);
__os_snprintf(errBuffer, 4, "%d", pad3.err);
OSScreenPutFontEx(1, 16, 16, errBuffer);
__os_snprintf(errBuffer, 4, "%d", pad4.err);
OSScreenPutFontEx(1, 20, 16, errBuffer);
__os_snprintf(errBuffer, 4, "%d", pad1.accelX);
OSScreenPutFontEx(1, 45, 3, errBuffer);
__os_snprintf(errBuffer, 4, "%d", pad1.accelY);
OSScreenPutFontEx(1, 45, 4, errBuffer);
__os_snprintf(errBuffer, 4, "%d", pad1.accelZ);
OSScreenPutFontEx(1, 45, 5, errBuffer);
__os_snprintf(errBuffer, 4, "%d", pad1.IR[0]);
OSScreenPutFontEx(1, 45, 6, errBuffer);
__os_snprintf(errBuffer, 4, "%d", pad1.IR[1]);
OSScreenPutFontEx(1, 45, 7, errBuffer);
__os_snprintf(errBuffer, 4, "%d", pad1.IR[2]);
OSScreenPutFontEx(1, 45, 8, errBuffer);
__os_snprintf(errBuffer, 4, "%d", pad1.IR[3]);
OSScreenPutFontEx(1, 45, 9, errBuffer);
OSScreenFlipBuffersEx(1);
}
for(i = 0; i < 2; i++) {
OSScreenClearBufferEx(0, 0x00000000);
OSScreenFlipBuffersEx(0);
OSScreenClearBufferEx(1, 0x00000000);
OSScreenFlipBuffersEx(1);
}
_Exit();
}
#ifndef LOADER_H
#define LOADER_H
#include "../../../libwiiu/src/coreinit.h"
#include "../../../libwiiu/src/padscore.h"
#include "../../../libwiiu/src/vpad.h"
void _start();
#endif
#define WPAD_BUTTON_LEFT 0x0001
#define WPAD_BUTTON_RIGHT 0x0002
#define WPAD_BUTTON_DOWN 0x0004
#define WPAD_BUTTON_UP 0x0008
#define WPAD_BUTTON_PLUS 0x0010
#define WPAD_BUTTON_2 0x0100
#define WPAD_BUTTON_1 0x0200
#define WPAD_BUTTON_B 0x0400
#define WPAD_BUTTON_A 0x0800
#define WPAD_BUTTON_MINUS 0x1000
#define WPAD_BUTTON_Z 0x2000
#define WPAD_BUTTON_C 0x4000
#define WPAD_BUTTON_HOME 0x8000
#define WPAD_EXT_CORE 0
#define WPAD_EXT_NUNCHUK 1
#define WPAD_EXT_CLASSIC 2
#define WPAD_EXT_MPLUS 5
#define WPAD_EXT_MPLUS_NUNCHUK 6
#define WPAD_EXT_MPLUS_CLASSIC 7
#define WPAD_BATTERYLVL_CRIT 0
#define WPAD_BATTERYLVL_LOW 1
#define WPAD_BATTERYLVL_MEDIUM 2
#define WPAD_BATTERYLVL_HIGH 3
#define WPAD_BATTERYLVL_FULL 4
typedef struct {
uint16_t x; /* Resolution is 1024 */
uint16_t y; /* Resolution is 768 */
uint16_t size;
uint8_t ID;
uint8_t pad;
} IRObject;
typedef struct {
uint16_t buttons; /* See WPAD_BUTTON */
uint16_t accelX; /* Resolution is 1024 */
uint16_t accelY; /* Resolution is 1024 */
uint16_t accelZ; /* Resolution is 1024 */
IRObject IR[4]; /* Max number of objects */
uint8_t ext; /* Extension, see WPAD_EXT */
uint8_t err; /* Error codes */
} WPADStatus; /* Normal Wiimote */
typedef struct {
uint16_t buttons; /* See WPAD_BUTTON */
uint16_t accelX; /* Resolution is 1024 */
uint16_t accelY; /* Resolution is 1024 */
uint16_t accelZ; /* Resolution is 1024 */
IRObject IR[4]; /* Max number of objects */
uint8_t ext; /* Extension, see WPAD_EXT */
uint8_t err; /* Error codes */
uint16_t ncAccX; /* Resolution is 1024 */
uint16_t ncAccY; /* Resolution is 1024 */
uint16_t ncAccZ; /* Resolution is 1024 */
uint16_t ncStickX; /* Resolution is 256 */
uint16_t ncStickY; /* Resolution is 256 */
} WPADNCStatus; /* Nunchuk extension */
typedef struct {
uint16_t buttons; /* See WPAD_BUTTON */
uint16_t accelX; /* Resolution is 1024 */
uint16_t accelY; /* Resolution is 1024 */
uint16_t accelZ; /* Resolution is 1024 */
IRObject IR[4]; /* Max number of objects */
uint8_t ext; /* Extension, see WPAD_EXT */
uint8_t err; /* Error codes */
uint16_t pad[6]; /* Padding, Classic/Nunchuk? */
uint8_t status; /* WiiMotionPlus */
uint8_t reserved;
uint16_t pitch; /* WiiMotionPlus */
uint16_t yaw; /* WiiMotionPlus */
uint16_t roll; /* WiiMotionPlus */
} WPADMPStatus; /* WiiMotionPlus */
typedef struct {
uint16_t WPADData[20]; /* Default WPAD data isn't used */
uint8_t ext; /* Extension, see WPAD_EXT */
uint8_t err; /* Error codes */
uint16_t press[4]; /* 4 Pads for sensing balance */
uint8_t temp; /* Temperature */
uint8_t battery; /* Battery */
} WPADBLStatus; /* Balance Board */
#ifndef TYPES_H
#define TYPES_H
typedef unsigned long long uint64_t;
typedef long long int64_t;
typedef unsigned int uint32_t;
typedef int int32_t;
typedef unsigned short uint16_t;
typedef short int16_t;
typedef unsigned char uint8_t;
typedef char int8_t;
typedef uint32_t size_t;
typedef _Bool bool;
#define true 1
#define false 0
#define null 0
#define NULL (void*)0
#endif /* TYPES_H */
#ifndef VPAD_H
#define VPAD_H
#include "types.h"
#define BUTTON_A 0x8000
#define BUTTON_B 0x4000
#define BUTTON_X 0x2000
#define BUTTON_Y 0x1000
#define BUTTON_LEFT 0x0800
#define BUTTON_RIGHT 0x0400
#define BUTTON_UP 0x0200
#define BUTTON_DOWN 0x0100
#define BUTTON_ZL 0x0080
#define BUTTON_ZR 0x0040
#define BUTTON_L 0x0020
#define BUTTON_R 0x0010
#define BUTTON_PLUS 0x0008
#define BUTTON_MINUS 0x0004
#define BUTTON_HOME 0x0002
#define BUTTON_SYNC 0x0001
#define BUTTON_STICK_R 0x00020000
#define BUTTON_STICK_L 0x00040000
#define BUTTON_TV 0x00010000
typedef struct
{
float x,y;
} Vec2D;
typedef struct
{
uint16_t x, y; /* Touch coordinates */
uint16_t touched; /* 1 = Touched, 0 = Not touched */
uint16_t validity; /* 0 = All valid, 1 = X invalid, 2 = Y invalid, 3 = Both invalid? */
} VPADTPData;
typedef struct
{
uint32_t btn_hold; /* Held buttons */
uint32_t btn_trigger; /* Buttons that are pressed at that instant */
uint32_t btn_release; /* Released buttons */
Vec2D lstick, rstick; /* Each contains 4-byte X and Y components */
char unknown1c[0x52 - 0x1c]; /* Contains accelerometer and gyroscope data somewhere */
VPADTPData tpdata; /* Normal touchscreen data */
VPADTPData tpdata1; /* Modified touchscreen data 1 */
VPADTPData tpdata2; /* Modified touchscreen data 2 */
char unknown6a[0xa0 - 0x6a];
uint8_t volume;
uint8_t battery; /* 0 to 6 */
uint8_t unk_volume; /* One less than volume */
char unknowna4[0xac - 0xa4];
} VPADData;
#endif /* VPAD_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment