1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2024-12-02 15:55:59 +00:00
oot/src/code/padmgr.c

416 lines
15 KiB
C
Raw Normal View History

#include "global.h"
#include "vt.h"
2020-03-17 04:31:30 +00:00
s32 D_8012D280 = 1;
OSMesgQueue* PadMgr_LockSerialMesgQueue(PadMgr* padMgr) {
OSMesgQueue* serialEventQueue = NULL;
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
if (D_8012D280 > 2) {
// "serialMsgQ Waiting for lock"
2020-03-22 21:19:43 +00:00
osSyncPrintf("%2d %d serialMsgQロック待ち %08x %08x %08x\n", osGetThreadId(NULL),
MQ_GET_COUNT(&padMgr->serialLockQueue), padMgr, &padMgr->serialLockQueue, &serialEventQueue);
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
osRecvMesg(&padMgr->serialLockQueue, (OSMesg*)&serialEventQueue, OS_MESG_BLOCK);
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
if (D_8012D280 > 2) {
// "serialMsgQ Locked"
2020-03-22 21:19:43 +00:00
osSyncPrintf("%2d %d serialMsgQをロックしました %08x\n", osGetThreadId(NULL),
MQ_GET_COUNT(&padMgr->serialLockQueue), serialEventQueue);
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
return serialEventQueue;
2020-03-17 04:31:30 +00:00
}
void PadMgr_UnlockSerialMesgQueue(PadMgr* padMgr, OSMesgQueue* serialEventQueue) {
2020-03-22 21:19:43 +00:00
if (D_8012D280 > 2) {
// "serialMsgQ Unlock"
2020-03-22 21:19:43 +00:00
osSyncPrintf("%2d %d serialMsgQロック解除します %08x %08x %08x\n", osGetThreadId(NULL),
MQ_GET_COUNT(&padMgr->serialLockQueue), padMgr, &padMgr->serialLockQueue, serialEventQueue);
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
osSendMesg(&padMgr->serialLockQueue, (OSMesg)serialEventQueue, OS_MESG_BLOCK);
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
if (D_8012D280 > 2) {
// "serialMsgQ Unlocked"
2020-03-22 21:19:43 +00:00
osSyncPrintf("%2d %d serialMsgQロック解除しました %08x %08x %08x\n", osGetThreadId(NULL),
MQ_GET_COUNT(&padMgr->serialLockQueue), padMgr, &padMgr->serialLockQueue, serialEventQueue);
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
}
void PadMgr_LockPadData(PadMgr* padMgr) {
osRecvMesg(&padMgr->lockQueue, NULL, OS_MESG_BLOCK);
2020-03-17 04:31:30 +00:00
}
void PadMgr_UnlockPadData(PadMgr* padMgr) {
osSendMesg(&padMgr->lockQueue, NULL, OS_MESG_BLOCK);
2020-03-17 04:31:30 +00:00
}
void PadMgr_RumbleControl(PadMgr* padMgr) {
static u32 errcnt = 0;
static u32 frame;
s32 temp = 1;
s32 triedRumbleComm;
OSMesgQueue* serialEventQueue = PadMgr_LockSerialMesgQueue(padMgr);
2020-03-17 04:31:30 +00:00
s32 var4;
s32 i;
triedRumbleComm = 0;
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
for (i = 0; i < 4; i++) {
if (padMgr->ctrlrIsConnected[i]) {
if (padMgr->padStatus[i].status & 1) {
if (padMgr->pakType[i] == temp) {
if (padMgr->rumbleEnable[i] != 0) {
if (padMgr->rumbleCounter[i] < 3) {
// clang-format off
if (1) {} osSyncPrintf(VT_FGCOL(YELLOW));
// clang-format on
// "Vibration pack jumble jumble"?
osSyncPrintf("padmgr: %dコン: %s\n", i + 1, "振動パック ぶるぶるぶるぶる");
osSyncPrintf(VT_RST);
if (__osMotorAccess(&padMgr->pfs[i], temp) != 0) {
padMgr->pakType[i] = 0;
osSyncPrintf(VT_FGCOL(YELLOW));
// "A communication error has occurred with the vibration pack"
osSyncPrintf("padmgr: %dコン: %s\n", i + 1, "振動パックで通信エラーが発生しました");
osSyncPrintf(VT_RST);
2020-03-22 21:19:43 +00:00
} else {
padMgr->rumbleCounter[i] = 3;
2020-03-17 04:31:30 +00:00
}
triedRumbleComm = 1;
2020-03-17 04:31:30 +00:00
}
2020-03-22 21:19:43 +00:00
} else {
if (padMgr->rumbleCounter[i] != 0) {
// clang-format off
if (1) {} osSyncPrintf(VT_FGCOL(YELLOW));
// clang-format on
// "Stop vibration pack"
osSyncPrintf("padmgr: %dコン: %s\n", i + 1, "振動パック 停止");
osSyncPrintf(VT_RST);
if (osMotorStop(&padMgr->pfs[i]) != 0) {
padMgr->pakType[i] = 0;
osSyncPrintf(VT_FGCOL(YELLOW));
// "A communication error has occurred with the vibration pack"
osSyncPrintf("padmgr: %dコン: %s\n", i + 1, "振動パックで通信エラーが発生しました");
osSyncPrintf(VT_RST);
2020-03-22 21:19:43 +00:00
} else {
padMgr->rumbleCounter[i]--;
2020-03-17 04:31:30 +00:00
}
triedRumbleComm = 1;
2020-03-17 04:31:30 +00:00
}
}
}
2020-03-22 21:19:43 +00:00
} else {
if (padMgr->pakType[i] != 0) {
if (padMgr->pakType[i] == 1) {
osSyncPrintf(VT_FGCOL(YELLOW));
// "It seems that a vibration pack was pulled out"
osSyncPrintf("padmgr: %dコン: %s\n", i + 1, "振動パックが抜かれたようです");
osSyncPrintf(VT_RST);
padMgr->pakType[i] = 0;
2020-03-22 21:19:43 +00:00
} else {
osSyncPrintf(VT_FGCOL(YELLOW));
// "It seems that a controller pack that is not a vibration pack was pulled out"
2020-03-22 21:19:43 +00:00
osSyncPrintf("padmgr: %dコン: %s\n", i + 1,
"振動パックではないコントローラパックが抜かれたようです");
osSyncPrintf(VT_RST);
padMgr->pakType[i] = 0;
2020-03-17 04:31:30 +00:00
}
}
}
}
}
if (!triedRumbleComm) {
i = frame % 4;
2020-03-17 04:31:30 +00:00
if (padMgr->ctrlrIsConnected[i] && (padMgr->padStatus[i].status & 1) && (padMgr->pakType[i] != 1)) {
var4 = osMotorInit(serialEventQueue, &padMgr->pfs[i], i);
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
if (var4 == 0) {
padMgr->pakType[i] = 1;
osMotorStart(&padMgr->pfs[i]);
osMotorStop(&padMgr->pfs[i]);
osSyncPrintf(VT_FGCOL(YELLOW));
// "Recognized vibration pack"
osSyncPrintf("padmgr: %dコン: %s\n", i + 1, "振動パックを認識しました");
osSyncPrintf(VT_RST);
2020-03-22 21:19:43 +00:00
} else if (var4 == 11) {
padMgr->pakType[i] = 2;
2020-03-22 21:19:43 +00:00
} else if (var4 == 4) {
LOG_NUM("++errcnt", ++errcnt, "../padmgr.c", 282);
osSyncPrintf(VT_FGCOL(YELLOW));
// "Controller pack communication error"
osSyncPrintf("padmgr: %dコン: %s\n", i + 1, "コントローラパックの通信エラー");
osSyncPrintf(VT_RST);
2020-03-17 04:31:30 +00:00
}
}
}
frame++;
PadMgr_UnlockSerialMesgQueue(padMgr, serialEventQueue);
2020-03-17 04:31:30 +00:00
}
void PadMgr_RumbleStop(PadMgr* padMgr) {
2020-03-17 04:31:30 +00:00
s32 i;
OSMesgQueue* serialEventQueue = PadMgr_LockSerialMesgQueue(padMgr);
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
for (i = 0; i < 4; i++) {
if (osMotorInit(serialEventQueue, &padMgr->pfs[i], i) == 0) {
if ((gFaultMgr.msgId == 0) && (padMgr->rumbleOnFrames != 0)) {
osSyncPrintf(VT_FGCOL(YELLOW));
// "Stop vibration pack"
osSyncPrintf("padmgr: %dコン: %s\n", i + 1, "振動パック 停止");
osSyncPrintf(VT_RST);
2020-03-17 04:31:30 +00:00
}
osMotorStop(&padMgr->pfs[i]);
2020-03-17 04:31:30 +00:00
}
}
PadMgr_UnlockSerialMesgQueue(padMgr, serialEventQueue);
2020-03-17 04:31:30 +00:00
}
void PadMgr_RumbleReset(PadMgr* padMgr) {
padMgr->rumbleOffFrames = 3;
2020-03-17 04:31:30 +00:00
}
void PadMgr_RumbleSetSingle(PadMgr* padMgr, u32 ctrlr, u32 rumble) {
padMgr->rumbleEnable[ctrlr] = rumble;
padMgr->rumbleOnFrames = 240;
2020-03-17 04:31:30 +00:00
}
void PadMgr_RumbleSet(PadMgr* padMgr, u8* ctrlrRumbles) {
s32 i;
for (i = 0; i < 4; i++) {
padMgr->rumbleEnable[i] = ctrlrRumbles[i];
}
padMgr->rumbleOnFrames = 240;
}
void PadMgr_ProcessInputs(PadMgr* padMgr) {
s32 i;
Input* input;
OSContPad* padnow1; // original name
s32 buttonDiff;
2020-03-17 04:31:30 +00:00
PadMgr_LockPadData(padMgr);
input = &padMgr->inputs[0];
padnow1 = &padMgr->pads[0];
for (i = 0; i < padMgr->nControllers; i++, input++, padnow1++) {
input->prev = input->cur;
if (1) {} // Necessary to match
switch (padnow1->errno) {
case 0:
input->cur = *padnow1;
if (!padMgr->ctrlrIsConnected[i]) {
padMgr->ctrlrIsConnected[i] = true;
osSyncPrintf(VT_FGCOL(YELLOW));
osSyncPrintf("padmgr: %dコン: %s\n", i + 1, "認識しました"); // "Recognized"
osSyncPrintf(VT_RST);
}
break;
case 4:
input->cur = input->prev;
LOG_NUM("this->Key_switch[i]", padMgr->ctrlrIsConnected[i], "../padmgr.c", 380);
osSyncPrintf(VT_FGCOL(YELLOW));
// "Overrun error occurred"
osSyncPrintf("padmgr: %dコン: %s\n", i + 1, "オーバーランエラーが発生");
osSyncPrintf(VT_RST);
break;
case 8:
input->cur.button = 0;
input->cur.stick_x = 0;
input->cur.stick_y = 0;
input->cur.errno = padnow1->errno;
if (padMgr->ctrlrIsConnected[i]) {
padMgr->ctrlrIsConnected[i] = false;
padMgr->pakType[i] = 0;
padMgr->rumbleCounter[i] = 0xFF;
osSyncPrintf(VT_FGCOL(YELLOW));
// "Do not respond"?
osSyncPrintf("padmgr: %dコン: %s\n", i + 1, "応答しません");
osSyncPrintf(VT_RST);
}
break;
default:
LOG_HEX("padnow1->errno", padnow1->errno, "../padmgr.c", 396);
Fault_AddHungupAndCrash("../padmgr.c", 397);
}
buttonDiff = input->prev.button ^ input->cur.button;
input->press.button |= (u16)(buttonDiff & input->cur.button);
input->rel.button |= (u16)(buttonDiff & input->prev.button);
PadUtils_UpdateRelXY(input);
input->press.stick_x += (s8)(input->cur.stick_x - input->prev.stick_x);
input->press.stick_y += (s8)(input->cur.stick_y - input->prev.stick_y);
}
PadMgr_UnlockPadData(padMgr);
2020-03-17 04:31:30 +00:00
}
void PadMgr_HandleRetraceMsg(PadMgr* padMgr) {
s32 i;
OSMesgQueue* serialEventQueue = PadMgr_LockSerialMesgQueue(padMgr);
u32 mask;
2020-03-17 04:31:30 +00:00
osContStartReadData(serialEventQueue);
if (padMgr->retraceCallback) {
padMgr->retraceCallback(padMgr, padMgr->retraceCallbackValue);
}
osRecvMesg(serialEventQueue, NULL, OS_MESG_BLOCK);
osContGetReadData(padMgr->pads);
if (padMgr->preNMIShutdown) {
bzero(padMgr->pads, sizeof(padMgr->pads));
}
PadMgr_ProcessInputs(padMgr);
osContStartQuery(serialEventQueue);
osRecvMesg(serialEventQueue, NULL, OS_MESG_BLOCK);
osContGetQuery(padMgr->padStatus);
PadMgr_UnlockSerialMesgQueue(padMgr, serialEventQueue);
mask = 0;
for (i = 0; i < 4; i++) {
if (padMgr->padStatus[i].errno == 0) {
if (padMgr->padStatus[i].type == CONT_TYPE_NORMAL) {
mask |= 1 << i;
} else {
LOG_HEX("this->pad_status[i].type", padMgr->padStatus[i].type, "../padmgr.c", 458);
// "An unknown type of controller is connected"
osSyncPrintf("知らない種類のコントローラが接続されています\n");
}
}
}
padMgr->validCtrlrsMask = mask;
if (gFaultMgr.msgId != 0) {
PadMgr_RumbleStop(padMgr);
} else if (padMgr->rumbleOffFrames > 0) {
--padMgr->rumbleOffFrames;
PadMgr_RumbleStop(padMgr);
} else if (padMgr->rumbleOnFrames == 0) {
PadMgr_RumbleStop(padMgr);
} else if (!padMgr->preNMIShutdown) {
PadMgr_RumbleControl(padMgr);
--padMgr->rumbleOnFrames;
}
}
2020-03-17 04:31:30 +00:00
void PadMgr_HandlePreNMI(PadMgr* padMgr) {
2020-03-17 04:31:30 +00:00
osSyncPrintf("padmgr_HandlePreNMI()\n");
padMgr->preNMIShutdown = true;
PadMgr_RumbleReset(padMgr);
2020-03-17 04:31:30 +00:00
}
void PadMgr_RequestPadData(PadMgr* padMgr, Input* inputs, s32 mode) {
s32 i;
Input* ogInput;
Input* newInput;
s32 buttonDiff;
PadMgr_LockPadData(padMgr);
ogInput = &padMgr->inputs[0];
newInput = &inputs[0];
for (i = 0; i < 4; i++) {
if (mode != 0) {
*newInput = *ogInput;
ogInput->press.button = 0;
ogInput->press.stick_x = 0;
ogInput->press.stick_y = 0;
ogInput->rel.button = 0;
} else {
newInput->prev = newInput->cur;
newInput->cur = ogInput->cur;
buttonDiff = newInput->prev.button ^ newInput->cur.button;
newInput->press.button = newInput->cur.button & buttonDiff;
newInput->rel.button = newInput->prev.button & buttonDiff;
PadUtils_UpdateRelXY(newInput);
newInput->press.stick_x += (s8)(newInput->cur.stick_x - newInput->prev.stick_x);
newInput->press.stick_y += (s8)(newInput->cur.stick_y - newInput->prev.stick_y);
}
ogInput++;
newInput++;
}
PadMgr_UnlockPadData(padMgr);
}
2020-03-17 04:31:30 +00:00
void PadMgr_ThreadEntry(PadMgr* padMgr) {
s16* msg = NULL;
s32 exit;
2020-03-17 04:31:30 +00:00
osSyncPrintf("コントローラスレッド実行開始\n"); // "Controller thread execution start"
2020-03-17 04:31:30 +00:00
exit = false;
while (!exit) {
if ((D_8012D280 > 2) && MQ_IS_EMPTY(&padMgr->interruptQueue)) {
// "Waiting for controller thread event"
osSyncPrintf("コントローラスレッドイベント待ち %lld\n", OS_CYCLES_TO_USEC(osGetTime()));
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
osRecvMesg(&padMgr->interruptQueue, (OSMesg*)&msg, OS_MESG_BLOCK);
LogUtils_CheckNullPointer("msg", msg, "../padmgr.c", 563);
2020-03-17 04:31:30 +00:00
switch (*msg) {
2020-03-17 04:31:30 +00:00
case OS_SC_RETRACE_MSG:
2020-03-22 21:19:43 +00:00
if (D_8012D280 > 2) {
osSyncPrintf("padmgr_HandleRetraceMsg START %lld\n", OS_CYCLES_TO_USEC(osGetTime()));
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
PadMgr_HandleRetraceMsg(padMgr);
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
if (D_8012D280 > 2) {
osSyncPrintf("padmgr_HandleRetraceMsg END %lld\n", OS_CYCLES_TO_USEC(osGetTime()));
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
break;
case OS_SC_PRE_NMI_MSG:
PadMgr_HandlePreNMI(padMgr);
2020-03-17 04:31:30 +00:00
break;
case OS_SC_NMI_MSG:
exit = true;
2020-03-17 04:31:30 +00:00
break;
}
}
IrqMgr_RemoveClient(padMgr->irqMgr, &padMgr->irqClient);
osSyncPrintf("コントローラスレッド実行終了\n"); // "Controller thread execution end"
2020-03-17 04:31:30 +00:00
}
void PadMgr_Init(PadMgr* padMgr, OSMesgQueue* serialEventQueue, IrqMgr* irqMgr, OSId id, OSPri priority, void* stack) {
osSyncPrintf("パッドマネージャ作成 padmgr_Create()\n"); // "Pad Manager creation"
bzero(padMgr, sizeof(PadMgr));
padMgr->irqMgr = irqMgr;
2020-03-17 04:31:30 +00:00
osCreateMesgQueue(&padMgr->interruptQueue, padMgr->interruptMsgBuf, ARRAY_COUNT(padMgr->interruptMsgBuf));
IrqMgr_AddClient(padMgr->irqMgr, &padMgr->irqClient, &padMgr->interruptQueue);
osCreateMesgQueue(&padMgr->serialLockQueue, padMgr->serialLockMsgBuf, ARRAY_COUNT(padMgr->serialLockMsgBuf));
PadMgr_UnlockSerialMesgQueue(padMgr, serialEventQueue);
osCreateMesgQueue(&padMgr->lockQueue, padMgr->lockMsgBuf, ARRAY_COUNT(padMgr->lockMsgBuf));
PadMgr_UnlockPadData(padMgr);
PadSetup_Init(serialEventQueue, (u8*)&padMgr->validCtrlrsMask, padMgr->padStatus);
2020-03-17 04:31:30 +00:00
padMgr->nControllers = 4;
osContSetCh(padMgr->nControllers);
2020-03-17 04:31:30 +00:00
osCreateThread(&padMgr->thread, id, (void (*)(void*))PadMgr_ThreadEntry, padMgr, stack, priority);
osStartThread(&padMgr->thread);
2020-03-17 04:31:30 +00:00
}