2024-03-21 07:46:08 +01:00
|
|
|
#include "patches.h"
|
|
|
|
#include "sys_cfb.h"
|
|
|
|
#include "buffers.h"
|
|
|
|
#include "fault.h"
|
2024-03-31 04:44:08 +02:00
|
|
|
#include "audiomgr.h"
|
2024-03-31 01:00:02 +01:00
|
|
|
#include "z64speed_meter.h"
|
2024-03-31 04:44:08 +02:00
|
|
|
#include "z64vimode.h"
|
|
|
|
#include "z64viscvg.h"
|
|
|
|
#include "z64vismono.h"
|
|
|
|
#include "z64viszbuf.h"
|
2024-05-26 15:34:26 +02:00
|
|
|
#include "input.h"
|
2024-03-21 07:46:08 +01:00
|
|
|
|
|
|
|
void recomp_set_current_frame_poll_id();
|
|
|
|
void PadMgr_HandleRetrace(void);
|
|
|
|
void PadMgr_LockPadData(void);
|
|
|
|
void PadMgr_UnlockPadData(void);
|
2024-04-15 17:15:45 +02:00
|
|
|
void PadMgr_UpdateRumble(void);
|
|
|
|
void PadMgr_UpdateConnections(void);
|
|
|
|
void PadMgr_UpdateInputs(void);
|
|
|
|
void PadMgr_InitVoice(void);
|
|
|
|
OSMesgQueue* PadMgr_AcquireSerialEventQueue(void);
|
|
|
|
void PadMgr_ReleaseSerialEventQueue(OSMesgQueue* serialEventQueue);
|
2024-03-21 07:46:08 +01:00
|
|
|
|
2024-04-15 17:15:45 +02:00
|
|
|
|
|
|
|
extern PadMgr* sPadMgrInstance;
|
|
|
|
extern s32 sPadMgrRetraceCount;
|
|
|
|
extern FaultMgr gFaultMgr;
|
|
|
|
extern s32 sVoiceInitStatus;
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
/* 0 */ VOICE_INIT_FAILED, // voice initialization failed
|
|
|
|
/* 1 */ VOICE_INIT_TRY, // try to initialize voice
|
|
|
|
/* 2 */ VOICE_INIT_SUCCESS // voice initialized
|
|
|
|
} VoiceInitStatus;
|
|
|
|
|
2024-07-26 04:17:00 +02:00
|
|
|
RECOMP_PATCH void PadMgr_HandleRetrace(void) {
|
2024-04-15 17:15:45 +02:00
|
|
|
// Execute rumble callback
|
|
|
|
if (sPadMgrInstance->rumbleRetraceCallback != NULL) {
|
|
|
|
sPadMgrInstance->rumbleRetraceCallback(sPadMgrInstance->rumbleRetraceArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try and initialize a Voice Recognition Unit if not already attempted
|
|
|
|
if (sVoiceInitStatus != VOICE_INIT_FAILED) {
|
|
|
|
PadMgr_InitVoice();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rumble Pak
|
|
|
|
if (gFaultMgr.msgId != 0) {
|
|
|
|
// If fault is active, no rumble
|
|
|
|
PadMgr_RumbleStop();
|
|
|
|
} else if (sPadMgrInstance->rumbleOffTimer > 0) {
|
|
|
|
// If the rumble off timer is active, no rumble
|
|
|
|
--sPadMgrInstance->rumbleOffTimer;
|
|
|
|
PadMgr_RumbleStop();
|
|
|
|
} else if (sPadMgrInstance->rumbleOnTimer == 0) {
|
|
|
|
// If the rumble on timer is inactive, no rumble
|
|
|
|
PadMgr_RumbleStop();
|
|
|
|
} else if (!sPadMgrInstance->isResetting) {
|
|
|
|
// If not resetting, update rumble
|
|
|
|
PadMgr_UpdateRumble();
|
|
|
|
--sPadMgrInstance->rumbleOnTimer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-26 15:34:26 +02:00
|
|
|
extern u8 sOcarinaInstrumentId;
|
|
|
|
|
2024-04-15 17:15:45 +02:00
|
|
|
void poll_inputs(void) {
|
|
|
|
OSMesgQueue* serialEventQueue = PadMgr_AcquireSerialEventQueue();
|
|
|
|
// Begin reading controller data
|
|
|
|
osContStartReadData(serialEventQueue);
|
|
|
|
|
2024-11-24 03:55:31 +01:00
|
|
|
bool needs_right_stick = recomp_analog_cam_enabled() || recomp_aiming_override_mode == RECOMP_AIMING_OVERRIDE_FORCE_RIGHT_STICK;
|
2024-05-26 15:34:26 +02:00
|
|
|
// Suppress the right analog stick if analog camera is active unless the ocarina is in use.
|
2024-11-24 03:55:31 +01:00
|
|
|
recomp_set_right_analog_suppressed(needs_right_stick && sOcarinaInstrumentId == OCARINA_INSTRUMENT_OFF);
|
|
|
|
// Resets this flag for the next frame;
|
|
|
|
recomp_aiming_override_mode = RECOMP_AIMING_OVERRIDE_OFF;
|
2024-05-26 15:34:26 +02:00
|
|
|
|
2024-04-15 17:15:45 +02:00
|
|
|
// Wait for controller data
|
|
|
|
osRecvMesg(serialEventQueue, NULL, OS_MESG_BLOCK);
|
|
|
|
osContGetReadData(sPadMgrInstance->pads);
|
|
|
|
|
|
|
|
// Clear all but controller 1
|
|
|
|
bzero(&sPadMgrInstance->pads[1], sizeof(*sPadMgrInstance->pads) * (MAXCONTROLLERS - 1));
|
|
|
|
|
|
|
|
// If in PreNMI, clear all controllers
|
|
|
|
if (sPadMgrInstance->isResetting) {
|
|
|
|
bzero(sPadMgrInstance->pads, sizeof(sPadMgrInstance->pads));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query controller statuses
|
|
|
|
osContStartQuery(serialEventQueue);
|
|
|
|
osRecvMesg(serialEventQueue, NULL, OS_MESG_BLOCK);
|
|
|
|
osContGetQuery(sPadMgrInstance->padStatus);
|
|
|
|
|
|
|
|
// Lock serial message queue
|
|
|
|
PadMgr_ReleaseSerialEventQueue(serialEventQueue);
|
|
|
|
|
|
|
|
// Update connections
|
|
|
|
PadMgr_UpdateConnections();
|
|
|
|
|
|
|
|
// Lock input data
|
|
|
|
PadMgr_LockPadData();
|
|
|
|
|
|
|
|
// Update input data
|
|
|
|
PadMgr_UpdateInputs();
|
|
|
|
|
|
|
|
// Execute input callback
|
|
|
|
if (sPadMgrInstance->inputRetraceCallback != NULL) {
|
|
|
|
sPadMgrInstance->inputRetraceCallback(sPadMgrInstance->inputRetraceArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock input data
|
|
|
|
PadMgr_UnlockPadData();
|
|
|
|
sPadMgrRetraceCount++;
|
2024-03-21 07:46:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// @recomp Patched to do the actual input polling.
|
2024-07-26 04:17:00 +02:00
|
|
|
RECOMP_PATCH void PadMgr_GetInput(Input* inputs, s32 gameRequest) {
|
2024-03-21 07:46:08 +01:00
|
|
|
// @recomp Do an actual poll if gameRequest is true.
|
|
|
|
if (gameRequest) {
|
2024-04-15 17:15:45 +02:00
|
|
|
poll_inputs();
|
2024-03-21 07:46:08 +01:00
|
|
|
// @recomp Tag the current frame's input polling id for latency tracking.
|
|
|
|
recomp_set_current_frame_poll_id();
|
|
|
|
}
|
|
|
|
PadMgr_LockPadData();
|
|
|
|
PadMgr_GetInputNoLock(inputs, gameRequest);
|
|
|
|
PadMgr_UnlockPadData();
|
|
|
|
}
|
|
|
|
|
|
|
|
// @recomp Just call PadMgr_GetInput.
|
2024-07-26 04:17:00 +02:00
|
|
|
RECOMP_PATCH void PadMgr_GetInput2(Input* inputs, s32 gameRequest) {
|
2024-03-21 07:46:08 +01:00
|
|
|
PadMgr_GetInput(inputs, gameRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern CfbInfo sGraphCfbInfos[3];
|
2024-03-31 01:00:02 +01:00
|
|
|
u32 recomp_time_us();
|
|
|
|
void recomp_measure_latency();
|
2024-03-31 04:44:08 +02:00
|
|
|
void* osViGetCurrentFramebuffer_recomp();
|
2024-03-31 01:00:02 +01:00
|
|
|
|
|
|
|
OSMesgQueue *rdp_queue_ptr = NULL;
|
2024-03-21 07:46:08 +01:00
|
|
|
|
|
|
|
// @recomp Immediately sends the graphics task instead of queueing it in the scheduler.
|
2024-07-26 04:17:00 +02:00
|
|
|
RECOMP_PATCH void Graph_TaskSet00(GraphicsContext* gfxCtx, GameState* gameState) {
|
2024-03-21 07:46:08 +01:00
|
|
|
static s32 retryCount = 10;
|
|
|
|
static s32 cfbIdx = 0;
|
|
|
|
OSTask_t* task = &gfxCtx->task.list.t;
|
|
|
|
OSScTask* scTask = &gfxCtx->task;
|
|
|
|
OSTimer timer;
|
|
|
|
OSMesg msg;
|
|
|
|
CfbInfo* cfb;
|
2024-03-31 01:00:02 +01:00
|
|
|
|
|
|
|
// @recomp Additional static members for extra scheduling purposes.
|
|
|
|
static IrqMgrClient irq_client = {0};
|
|
|
|
static OSMesgQueue vi_queue = {0};
|
|
|
|
static OSMesg vi_buf[8] = {0};
|
|
|
|
static bool created = false;
|
|
|
|
if (!created) {
|
|
|
|
created = true;
|
|
|
|
osCreateMesgQueue(&vi_queue, vi_buf, ARRAY_COUNT(vi_buf));
|
|
|
|
extern IrqMgr gIrqMgr;
|
|
|
|
IrqMgr_AddClient(&gIrqMgr, &irq_client, &vi_queue);
|
|
|
|
}
|
2024-03-21 07:46:08 +01:00
|
|
|
|
|
|
|
// @recomp Disable the wait here so that it can be moved after task submission for minimizing latency.
|
|
|
|
// retry:
|
|
|
|
// osSetTimer(&timer, OS_USEC_TO_CYCLES(3 * 1000 * 1000), 0, &gfxCtx->queue, (OSMesg)666);
|
2024-03-31 04:44:08 +02:00
|
|
|
// osRecvMesg(&gfxCtx->queue, &msg, OS_MESG_BLOCK);
|
2024-03-21 07:46:08 +01:00
|
|
|
// osStopTimer(&timer);
|
|
|
|
|
|
|
|
// if (msg == (OSMesg)666) {
|
|
|
|
// osSyncPrintf("GRAPH SP TIMEOUT\n");
|
|
|
|
// if (retryCount >= 0) {
|
|
|
|
// retryCount--;
|
|
|
|
// Sched_SendGfxCancelMsg(&gSchedContext);
|
|
|
|
// goto retry;
|
|
|
|
// } else {
|
|
|
|
// // graph.c: No more! die!
|
|
|
|
// osSyncPrintf("graph.c:もうダメ!死ぬ!\n");
|
|
|
|
// Fault_AddHungupAndCrashImpl("RCP is HUNG UP!!", "Oh! MY GOD!!");
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
gfxCtx->masterList = gGfxMasterDL;
|
|
|
|
if (gfxCtx->callback != NULL) {
|
|
|
|
gfxCtx->callback(gfxCtx, gfxCtx->callbackArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
task->type = M_GFXTASK;
|
|
|
|
task->flags = OS_SC_DRAM_DLIST;
|
|
|
|
task->ucodeBoot = SysUcode_GetUCodeBoot();
|
|
|
|
task->ucodeBootSize = SysUcode_GetUCodeBootSize();
|
|
|
|
task->ucode = SysUcode_GetUCode();
|
|
|
|
task->ucodeData = SysUcode_GetUCodeData();
|
|
|
|
task->ucodeSize = SP_UCODE_SIZE;
|
|
|
|
task->ucodeDataSize = SP_UCODE_DATA_SIZE;
|
|
|
|
task->dramStack = (u64*)gGfxSPTaskStack;
|
|
|
|
task->dramStackSize = sizeof(gGfxSPTaskStack);
|
|
|
|
task->outputBuff = gGfxSPTaskOutputBufferPtr;
|
|
|
|
task->outputBuffSize = gGfxSPTaskOutputBufferEnd;
|
|
|
|
task->dataPtr = (u64*)gGfxMasterDL;
|
|
|
|
task->dataSize = 0;
|
|
|
|
task->yieldDataPtr = (u64*)gGfxSPTaskYieldBuffer;
|
|
|
|
task->yieldDataSize = sizeof(gGfxSPTaskYieldBuffer);
|
|
|
|
|
|
|
|
scTask->next = NULL;
|
|
|
|
scTask->flags = OS_SC_RCP_MASK | OS_SC_SWAPBUFFER | OS_SC_LAST_TASK;
|
|
|
|
|
|
|
|
if (SREG(33) & 1) {
|
|
|
|
SREG(33) &= ~1;
|
|
|
|
scTask->flags &= ~OS_SC_SWAPBUFFER;
|
|
|
|
gfxCtx->framebufferIndex--;
|
|
|
|
}
|
|
|
|
|
|
|
|
scTask->msgQ = &gfxCtx->queue;
|
|
|
|
scTask->msg = NULL;
|
|
|
|
|
|
|
|
{ s32 pad; }
|
|
|
|
|
|
|
|
cfb = &sGraphCfbInfos[cfbIdx];
|
|
|
|
cfbIdx = (cfbIdx + 1) % ARRAY_COUNT(sGraphCfbInfos);
|
|
|
|
|
|
|
|
cfb->fb1 = gfxCtx->curFrameBuffer;
|
|
|
|
cfb->swapBuffer = gfxCtx->curFrameBuffer;
|
|
|
|
|
|
|
|
if (gfxCtx->updateViMode) {
|
|
|
|
gfxCtx->updateViMode = false;
|
|
|
|
cfb->viMode = gfxCtx->viMode;
|
|
|
|
cfb->features = gfxCtx->viConfigFeatures;
|
|
|
|
cfb->xScale = gfxCtx->xScale;
|
|
|
|
cfb->yScale = gfxCtx->yScale;
|
|
|
|
} else {
|
|
|
|
cfb->viMode = NULL;
|
|
|
|
}
|
|
|
|
cfb->unk_10 = 0;
|
|
|
|
cfb->updateRate = gameState->framerateDivisor;
|
|
|
|
|
|
|
|
scTask->framebuffer = cfb;
|
|
|
|
|
|
|
|
while (gfxCtx->queue.validCount != 0) {
|
|
|
|
osRecvMesg(&gfxCtx->queue, NULL, OS_MESG_NOBLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxCtx->schedMsgQ = &gSchedContext.cmdQ;
|
|
|
|
osSendMesg(&gSchedContext.cmdQ, scTask, OS_MESG_BLOCK);
|
|
|
|
Sched_SendEntryMsg(&gSchedContext);
|
|
|
|
|
2024-03-31 04:44:08 +02:00
|
|
|
// @recomp Immediately wait on the task to complete to minimize latency for the next one.
|
|
|
|
osRecvMesg(&gfxCtx->queue, &msg, OS_MESG_BLOCK);
|
|
|
|
|
|
|
|
// @recomp Wait on the VI framebuffer to change if this task has a framebuffer swap.
|
|
|
|
if (scTask->flags & OS_SC_SWAPBUFFER) {
|
2024-04-03 00:21:05 +02:00
|
|
|
int viCounter = 0;
|
|
|
|
while (osViGetCurrentFramebuffer() != cfb->fb1) {
|
|
|
|
osRecvMesg(&vi_queue, NULL, OS_MESG_BLOCK);
|
|
|
|
viCounter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't wait the full number of VIs needed between frames then wait one extra VI afterwards.
|
|
|
|
if (viCounter < gameState->framerateDivisor) {
|
2024-03-31 04:44:08 +02:00
|
|
|
osRecvMesg(&vi_queue, NULL, OS_MESG_BLOCK);
|
|
|
|
}
|
2024-03-31 01:00:02 +01:00
|
|
|
}
|
2024-03-31 04:44:08 +02:00
|
|
|
|
|
|
|
// @recomp Flush any extra messages from the VI queue.
|
2024-03-31 01:00:02 +01:00
|
|
|
while (osRecvMesg(&vi_queue, NULL, OS_MESG_NOBLOCK) == 0) {
|
|
|
|
;
|
2024-03-21 07:46:08 +01:00
|
|
|
}
|
2024-03-31 01:00:02 +01:00
|
|
|
}
|
|
|
|
|
2024-03-31 04:44:08 +02:00
|
|
|
extern SpeedMeter sGameSpeedMeter;
|
|
|
|
extern VisCvg sGameVisCvg;
|
|
|
|
extern VisZbuf sGameVisZbuf;
|
|
|
|
extern VisMono sGameVisMono;
|
|
|
|
extern ViMode sGameViMode;
|
2024-03-31 01:00:02 +01:00
|
|
|
|
2024-07-26 04:17:00 +02:00
|
|
|
RECOMP_PATCH void GameState_Destroy(GameState* gameState) {
|
2024-03-31 04:44:08 +02:00
|
|
|
AudioMgr_StopAllSfxExceptSystem();
|
|
|
|
Audio_Update();
|
2024-03-31 01:00:02 +01:00
|
|
|
|
2024-03-31 04:44:08 +02:00
|
|
|
// @recomp The wait for the gfx task was moved to directly after submission, so it's not needed here.
|
|
|
|
// osRecvMesg(&gameState->gfxCtx->queue, NULL, OS_MESG_BLOCK);
|
2024-03-31 01:00:02 +01:00
|
|
|
|
2024-03-31 04:44:08 +02:00
|
|
|
if (gameState->destroy != NULL) {
|
|
|
|
gameState->destroy(gameState);
|
2024-03-31 01:00:02 +01:00
|
|
|
}
|
|
|
|
|
2024-03-31 04:44:08 +02:00
|
|
|
Rumble_Destroy();
|
|
|
|
SpeedMeter_Destroy(&sGameSpeedMeter);
|
|
|
|
VisCvg_Destroy(&sGameVisCvg);
|
|
|
|
VisZbuf_Destroy(&sGameVisZbuf);
|
|
|
|
VisMono_Destroy(&sGameVisMono);
|
|
|
|
ViMode_Destroy(&sGameViMode);
|
|
|
|
THA_Destroy(&gameState->tha);
|
|
|
|
GameAlloc_Cleanup(&gameState->alloc);
|
2024-03-21 07:46:08 +01:00
|
|
|
}
|