From 0d54048efd17bdd6468873eccb647802401e2db7 Mon Sep 17 00:00:00 2001 From: Florent Castelli Date: Mon, 4 Feb 2013 00:09:57 +0100 Subject: [PATCH 1/4] setCompiledSdkVersion accepts any version as in the CFW version --- Core/HLE/sceKernelMemory.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Core/HLE/sceKernelMemory.cpp b/Core/HLE/sceKernelMemory.cpp index 187021224f..07f7127c48 100644 --- a/Core/HLE/sceKernelMemory.cpp +++ b/Core/HLE/sceKernelMemory.cpp @@ -507,7 +507,7 @@ void sceKernelPrintf() void sceKernelSetCompiledSdkVersion(int sdkVersion) { - int sdkMainVersion = sdkVersion & 0xFFFF0000; +/* int sdkMainVersion = sdkVersion & 0xFFFF0000; bool valiSDK = false; switch(sdkMainVersion) { @@ -532,15 +532,15 @@ void sceKernelSetCompiledSdkVersion(int sdkVersion) } if(valiSDK) - { + {*/ sdkVersion_ = sdkVersion; flags_ |= SCE_KERNEL_HASCOMPILEDSDKVERSION; - } +/* } else { ERROR_LOG(HLE,"sceKernelSetCompiledSdkVersion unknown SDK : %x\n",sdkVersion); } - return; + return;*/ } void sceKernelSetCompiledSdkVersion370(int sdkVersion) From c37566d3bc99f7aeb3ad2d83a4a9c711c175e002 Mon Sep 17 00:00:00 2001 From: Florent Castelli Date: Mon, 4 Feb 2013 00:42:52 +0100 Subject: [PATCH 2/4] Use proper draw sequence in PPGeDraw --- Core/HLE/sceGe.h | 4 ++++ Core/Util/PPGeDraw.cpp | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Core/HLE/sceGe.h b/Core/HLE/sceGe.h index b37508a672..855f5a7a87 100644 --- a/Core/HLE/sceGe.h +++ b/Core/HLE/sceGe.h @@ -44,5 +44,9 @@ void __GeShutdown(); // Export functions for use by Util/PPGe u32 sceGeRestoreContext(u32 ctxAddr); u32 sceGeSaveContext(u32 ctxAddr); +int sceGeBreak(u32 mode); +int sceGeContinue(); +int sceGeListSync(u32 displayListID, u32 mode); u32 sceGeListEnQueue(u32 listAddress, u32 stallAddress, int callbackId, u32 optParamAddr); +u32 sceGeListEnQueueHead(u32 listAddress, u32 stallAddress, int callbackId, u32 optParamAddr); diff --git a/Core/Util/PPGeDraw.cpp b/Core/Util/PPGeDraw.cpp index d18fda45f1..980a7e014e 100644 --- a/Core/Util/PPGeDraw.cpp +++ b/Core/Util/PPGeDraw.cpp @@ -229,15 +229,15 @@ void PPGeEnd() WriteCmd(GE_CMD_END, 0); if (dataWritePtr > dataPtr) { + sceGeBreak(0); sceGeSaveContext(savedContextPtr); gpu->EnableInterrupts(false); // We actually drew something - u32 list = sceGeListEnQueue(dlPtr, dlWritePtr, 0, 0); + u32 list = sceGeListEnQueueHead(dlPtr, dlWritePtr, -1, 0); DEBUG_LOG(HLE, "PPGe enqueued display list %i", list); - // TODO: Might need to call some internal trickery function when this is actually synchronous. - // sceGeListSync(u32 displayListID, 1); //0 : wait for completion 1:check and return gpu->EnableInterrupts(true); + sceGeContinue(); sceGeRestoreContext(savedContextPtr); } } From 522983549d562160c1574eeb9f2d9774394324f4 Mon Sep 17 00:00:00 2001 From: Florent Castelli Date: Mon, 4 Feb 2013 03:09:14 +0100 Subject: [PATCH 3/4] Correctly savestate std::maps with non-uint keys. --- Common/ChunkFile.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Common/ChunkFile.h b/Common/ChunkFile.h index f758726b72..91294f789b 100644 --- a/Common/ChunkFile.h +++ b/Common/ChunkFile.h @@ -80,6 +80,12 @@ public: template void Do(std::map &x) + { + Do(x); + } + + template + void Do(std::map &x) { unsigned int number = (unsigned int)x.size(); Do(number); @@ -89,7 +95,7 @@ public: x.clear(); while (number > 0) { - unsigned int first = 0; + K first = 0; Do(first); T second; Do(second); @@ -102,7 +108,7 @@ public: case MODE_MEASURE: case MODE_VERIFY: { - typename std::map::iterator itr = x.begin(); + typename std::map::iterator itr = x.begin(); while (number > 0) { Do(itr->first); From 8004d360dd81155ef37c12d317537042d06da6de Mon Sep 17 00:00:00 2001 From: Florent Castelli Date: Mon, 4 Feb 2013 00:41:16 +0100 Subject: [PATCH 4/4] Interrupt handler reorganization for easier GE interrupt support --- Core/HLE/sceDisplay.cpp | 4 +- Core/HLE/sceGe.cpp | 90 ++++++-- Core/HLE/sceGe.h | 2 + Core/HLE/sceKernelAlarm.cpp | 69 +++--- Core/HLE/sceKernelInterrupt.cpp | 347 ++++++++++++---------------- Core/HLE/sceKernelInterrupt.h | 84 ++++--- GPU/GLES/DisplayListInterpreter.cpp | 7 +- GPU/GPUCommon.cpp | 10 + GPU/GPUCommon.h | 20 +- GPU/GPUInterface.h | 5 + GPU/Null/NullGpu.cpp | 8 +- 11 files changed, 343 insertions(+), 303 deletions(-) diff --git a/Core/HLE/sceDisplay.cpp b/Core/HLE/sceDisplay.cpp index 89fe11b3c3..d8b2fdf7a5 100644 --- a/Core/HLE/sceDisplay.cpp +++ b/Core/HLE/sceDisplay.cpp @@ -182,8 +182,8 @@ void hleEnterVblank(u64 userdata, int cyclesLate) { } vblankWaitingThreads.clear(); - // Trigger VBlank interrupt handlers - and resched even if there's none registered. - __TriggerInterrupt(PSP_INTR_IMMEDIATE | PSP_INTR_ONLY_IF_ENABLED | PSP_INTR_ALWAYS_RESCHED, PSP_VBLANK_INTR); + // Trigger VBlank interrupt handlers. + __TriggerInterrupt(PSP_INTR_IMMEDIATE | PSP_INTR_ONLY_IF_ENABLED | PSP_INTR_ALWAYS_RESCHED, PSP_VBLANK_INTR, PSP_INTR_SUB_ALL); CoreTiming::ScheduleEvent(msToCycles(vblankMs) - cyclesLate, leaveVblankEvent, vbCount+1); diff --git a/Core/HLE/sceGe.cpp b/Core/HLE/sceGe.cpp index 6fd5a83eb5..b949e484e0 100644 --- a/Core/HLE/sceGe.cpp +++ b/Core/HLE/sceGe.cpp @@ -20,6 +20,7 @@ #include "../System.h" #include "../CoreParameter.h" #include "sceGe.h" +#include "sceKernelMemory.h" #include "sceKernelThread.h" #include "sceKernelInterrupt.h" #include "../GPU/GPUState.h" @@ -28,15 +29,71 @@ static PspGeCallbackData ge_callback_data[16]; static bool ge_used_callbacks[16] = {0}; +struct GeInterruptData +{ + int listid; + u32 pc; +}; + +static std::list ge_pending_cb; + +class GeIntrHandler : public IntrHandler +{ +public: + GeIntrHandler() : IntrHandler(PSP_GE_INTR) {} + + bool run(PendingInterrupt& pend) + { + GeInterruptData intrdata = ge_pending_cb.front(); + DisplayList* dl = gpu->getList(intrdata.listid); + + gpu->InterruptStart(); + + u32 cmd = Memory::ReadUnchecked_U32(intrdata.pc) >> 24; + int subintr = dl->subIntrBase | (cmd == GE_CMD_FINISH ? PSP_GE_SUBINTR_FINISH : PSP_GE_SUBINTR_SIGNAL); + SubIntrHandler* handler = get(subintr); + + if(handler != NULL) + { + DEBUG_LOG(CPU, "Entering interrupt handler %08x", handler->handlerAddress); + currentMIPS->pc = handler->handlerAddress; + u32 data = dl->subIntrToken; + currentMIPS->r[MIPS_REG_A0] = data & 0xFFFF; + currentMIPS->r[MIPS_REG_A1] = handler->handlerArg; + currentMIPS->r[MIPS_REG_A2] = sceKernelGetCompiledSdkVersion() <= 0x02000010 ? 0 : intrdata.pc + 4; + // RA is already taken care of in __RunOnePendingInterrupt + + return true; + } + + ge_pending_cb.pop_front(); + gpu->InterruptEnd(); + + WARN_LOG(HLE, "Ignoring interrupt for display list %d, already been released.", intrdata.listid); + return false; + } + + virtual void handleResult(PendingInterrupt& pend) + { + GeInterruptData intrdata = ge_pending_cb.front(); + ge_pending_cb.pop_front(); + + gpu->InterruptEnd(); + } +}; + void __GeInit() { memset(&ge_used_callbacks, 0, sizeof(ge_used_callbacks)); + ge_pending_cb.clear(); + __RegisterIntrHandler(PSP_GE_INTR, new GeIntrHandler()); } void __GeDoState(PointerWrap &p) { p.DoArray(ge_callback_data, ARRAY_SIZE(ge_callback_data)); p.DoArray(ge_used_callbacks, ARRAY_SIZE(ge_used_callbacks)); + p.Do(ge_pending_cb); // Everything else is done in sceDisplay. p.DoMarker("sceGe"); } @@ -46,6 +103,20 @@ void __GeShutdown() } +void __GeTriggerInterrupt(int listid, u32 pc) +{ + GeInterruptData intrdata; + intrdata.listid = listid; + intrdata.pc = pc; + ge_pending_cb.push_back(intrdata); + __TriggerInterrupt(PSP_INTR_HLE, PSP_GE_INTR, PSP_INTR_SUB_NONE); +} + +bool __GeHasPendingInterrupt() +{ + return !ge_pending_cb.empty(); +} + // The GE is implemented wrong - it should be parallel to the CPU execution instead of // synchronous. @@ -63,26 +134,9 @@ u32 sceGeEdramGetSize() return retVal; } -// TODO: Probably shouldn't use an interrupt? int __GeSubIntrBase(int callbackId) { - // Negative means don't use. - if (callbackId < 0) - return 0; - - if (callbackId >= (int)(ARRAY_SIZE(ge_used_callbacks))) - { - WARN_LOG(HLE, "Unexpected (too high) GE callback id %d, ignoring", callbackId); - return 0; - } - - if (!ge_used_callbacks[callbackId]) - { - WARN_LOG(HLE, "Unregistered GE callback id %d, ignoring", callbackId); - return 0; - } - - return (callbackId + 1) << 16; + return callbackId * 2; } u32 sceGeListEnQueue(u32 listAddress, u32 stallAddress, int callbackId, diff --git a/Core/HLE/sceGe.h b/Core/HLE/sceGe.h index 855f5a7a87..ce6661fe28 100644 --- a/Core/HLE/sceGe.h +++ b/Core/HLE/sceGe.h @@ -39,6 +39,8 @@ void Register_sceGe_user(); void __GeInit(); void __GeDoState(PointerWrap &p); void __GeShutdown(); +void __GeTriggerInterrupt(int listid, u32 pc); +bool __GeHasPendingInterrupt(); // Export functions for use by Util/PPGe diff --git a/Core/HLE/sceKernelAlarm.cpp b/Core/HLE/sceKernelAlarm.cpp index b50d1056d1..eea167eff4 100644 --- a/Core/HLE/sceKernelAlarm.cpp +++ b/Core/HLE/sceKernelAlarm.cpp @@ -23,6 +23,8 @@ const int NATIVEALARM_SIZE = 20; +std::list triggeredAlarm; + struct NativeAlarm { SceSize size; @@ -49,35 +51,33 @@ struct Alarm : public KernelObject void __KernelScheduleAlarm(Alarm *alarm, u64 ticks); -class AlarmIntrHandler : public SubIntrHandler +class AlarmIntrHandler : public IntrHandler { public: - static SubIntrHandler *Create() - { - return new AlarmIntrHandler(); - } + AlarmIntrHandler() : IntrHandler(PSP_SYSTIMER0_INTR) {} - void setAlarm(Alarm *alarm) + virtual bool run(PendingInterrupt& pend) { - alarmID = alarm->GetUID(); - handlerAddress = alarm->alm.handlerPtr; - enabled = true; - } - - virtual void copyArgsToCPU(const PendingInterrupt &pend) - { - SubIntrHandler::copyArgsToCPU(pend); - u32 error; + int alarmID = triggeredAlarm.front(); + Alarm *alarm = kernelObjects.Get(alarmID, error); - if (alarm) - currentMIPS->r[MIPS_REG_A0] = alarm->alm.commonPtr; - else - ERROR_LOG(HLE, "sceKernelAlarm: Unable to send interrupt args: alarm deleted?"); + if(error) + return false; + + currentMIPS->pc = alarm->alm.handlerPtr; + currentMIPS->r[MIPS_REG_A0] = alarm->alm.commonPtr; + + return true; } - virtual void handleResult(int result) + virtual void handleResult(PendingInterrupt& pend) { + int result = currentMIPS->r[MIPS_REG_V0]; + + int alarmID = triggeredAlarm.front(); + triggeredAlarm.pop_front(); + // A non-zero result means to reschedule. if (result > 0) { @@ -92,18 +92,8 @@ public: // Delete the alarm if it's not rescheduled. kernelObjects.Destroy(alarmID); - __ReleaseSubIntrHandler(PSP_SYSTIMER0_INTR, alarmID); } } - - virtual void DoState(PointerWrap &p) - { - SubIntrHandler::DoState(p); - p.Do(alarmID); - p.DoMarker("AlarmIntrHandler"); - } - - SceUID alarmID; }; static int alarmTimer = -1; @@ -115,18 +105,23 @@ void __KernelTriggerAlarm(u64 userdata, int cyclesLate) u32 error; Alarm *alarm = kernelObjects.Get(uid, error); if (alarm) - __TriggerInterrupt(PSP_INTR_IMMEDIATE, PSP_SYSTIMER0_INTR, uid); + { + triggeredAlarm.push_back(uid); + __TriggerInterrupt(PSP_INTR_IMMEDIATE, PSP_SYSTIMER0_INTR); + } } void __KernelAlarmInit() { + triggeredAlarm.clear(); + __RegisterIntrHandler(PSP_SYSTIMER0_INTR, new AlarmIntrHandler()); alarmTimer = CoreTiming::RegisterEvent("Alarm", __KernelTriggerAlarm); - __RegisterSubIntrCreator(PSP_SYSTIMER0_INTR, AlarmIntrHandler::Create); } void __KernelAlarmDoState(PointerWrap &p) { p.Do(alarmTimer); + p.Do(triggeredAlarm); CoreTiming::RestoreRegisterEvent(alarmTimer, "Alarm", __KernelTriggerAlarm); p.DoMarker("sceKernelAlarm"); } @@ -155,15 +150,6 @@ SceUID __KernelSetAlarm(u64 ticks, u32 handlerPtr, u32 commonPtr) alarm->alm.handlerPtr = handlerPtr; alarm->alm.commonPtr = commonPtr; - u32 error; - AlarmIntrHandler *handler = (AlarmIntrHandler *) __RegisterSubIntrHandler(PSP_SYSTIMER0_INTR, uid, error); - if (error != 0) - { - kernelObjects.Destroy(uid); - return error; - } - - handler->setAlarm(alarm); __KernelScheduleAlarm(alarm, ticks); return uid; } @@ -192,7 +178,6 @@ int sceKernelCancelAlarm(SceUID uid) DEBUG_LOG(HLE, "sceKernelCancelAlarm(%08x)", uid); CoreTiming::UnscheduleEvent(alarmTimer, uid); - __ReleaseSubIntrHandler(PSP_SYSTIMER0_INTR, uid); return kernelObjects.Destroy(uid); } diff --git a/Core/HLE/sceKernelInterrupt.cpp b/Core/HLE/sceKernelInterrupt.cpp index 22cc0a69e4..21880ad98f 100644 --- a/Core/HLE/sceKernelInterrupt.cpp +++ b/Core/HLE/sceKernelInterrupt.cpp @@ -35,6 +35,34 @@ bool __InterruptsEnabled(); ////////////////////////////////////////////////////////////////////////// // INTERRUPT MANAGEMENT ////////////////////////////////////////////////////////////////////////// + +class InterruptState +{ +public: + void save(); + void restore(); + void clear(); + + void DoState(PointerWrap &p) + { + p.Do(savedCpu); + p.DoMarker("InterruptState"); + } + + ThreadContext savedCpu; +}; + +// STATE + +InterruptState intState; +IntrHandler* intrHandlers[PSP_NUMBER_INTERRUPTS]; +std::list pendingInterrupts; + +// Yeah, this bit is a bit silly. +static int interruptsEnabled = 1; +static bool inInterrupt; + + void sceKernelCpuSuspendIntr() { //LOG(HLE,"sceKernelCpuSuspendIntr"); // very spammy @@ -84,154 +112,100 @@ void sceKernelCpuResumeIntrWithSync(u32 enable) sceKernelCpuResumeIntr(enable); } -class IntrHandler { -public: - IntrHandler() - : subIntrCreator(NULL) - { - } - - SubIntrHandler *add(int subIntrNum) - { - SubIntrHandler *handler; - if (subIntrCreator != NULL) - handler = subIntrCreator(); - else - handler = new SubIntrHandler(); - - subIntrHandlers[subIntrNum] = handler; - return handler; - } - void remove(int subIntrNum) - { - if (has(subIntrNum)) - { - delete subIntrHandlers[subIntrNum]; - subIntrHandlers.erase(subIntrNum); - } - } - bool has(int subIntrNum) const - { - return subIntrHandlers.find(subIntrNum) != subIntrHandlers.end(); - } - SubIntrHandler *get(int subIntrNum) - { - if (has(subIntrNum)) - return subIntrHandlers[subIntrNum]; - else - return NULL; - } - void clear() - { - std::map::iterator it, end; - for (it = subIntrHandlers.begin(), end = subIntrHandlers.end(); it != end; ++it) - delete it->second; - subIntrHandlers.clear(); - } - - void queueUp(int subintr) - { - // Just call execute on all the subintr handlers for this interrupt. - // They will get queued up. - for (std::map::iterator iter = subIntrHandlers.begin(); iter != subIntrHandlers.end(); ++iter) - { - if (subintr == -1 || iter->first == subintr) - iter->second->queueUp(); - } - } - - void queueUpWithArg(int subintr, int arg) - { - // Just call execute on all the subintr handlers for this interrupt. - // They will get queued up. - for (std::map::iterator iter = subIntrHandlers.begin(); iter != subIntrHandlers.end(); ++iter) - { - if (subintr == -1 || iter->first == subintr) - iter->second->queueUpWithArg(arg); - } - } - - void setCreator(SubIntrCreator creator) - { - subIntrCreator = creator; - } - - void DoState(PointerWrap &p) - { - // We assume that the same creator has already been registered. - bool hasCreator = subIntrCreator != NULL; - p.Do(hasCreator); - if (hasCreator != (subIntrCreator != NULL)) - { - ERROR_LOG(HLE, "Savestate failure: incompatible sub interrupt handler."); - return; - } - - int n = (int) subIntrHandlers.size(); - p.Do(n); - - if (p.mode == p.MODE_READ) - { - clear(); - for (int i = 0; i < n; ++i) - { - int subIntrNum; - p.Do(subIntrNum); - SubIntrHandler *handler = add(subIntrNum); - handler->DoState(p); - } - } - else - { - std::map::iterator it, end; - for (it = subIntrHandlers.begin(), end = subIntrHandlers.end(); it != end; ++it) - { - p.Do(it->first); - it->second->DoState(p); - } - } - } - -private: - SubIntrCreator subIntrCreator; - std::map subIntrHandlers; -}; - -class InterruptState +bool IntrHandler::run(PendingInterrupt& pend) { -public: - void save(); - void restore(); - void clear(); - - void DoState(PointerWrap &p) + SubIntrHandler *handler = get(pend.subintr); + if (handler == NULL) { - p.Do(insideInterrupt); - p.Do(savedCpu); - p.DoMarker("InterruptState"); + WARN_LOG(HLE, "Ignoring interrupt, already been released."); + return false; } - bool insideInterrupt; - ThreadContext savedCpu; -// Action afterInterruptAction; -// Action afterHandlerAction; -}; + copyArgsToCPU(pend); -// STATE + return true; +} -InterruptState intState; -IntrHandler intrHandlers[PSP_NUMBER_INTERRUPTS]; -std::list pendingInterrupts; +void IntrHandler::copyArgsToCPU(PendingInterrupt& pend) +{ + SubIntrHandler* handler = get(pend.subintr); + DEBUG_LOG(CPU, "Entering interrupt handler %08x", handler->handlerAddress); + currentMIPS->pc = handler->handlerAddress; + currentMIPS->r[MIPS_REG_A0] = handler->subIntrNumber; + currentMIPS->r[MIPS_REG_A1] = handler->handlerArg; + // RA is already taken care of +} -// Yeah, this bit is a bit silly. -static int interruptsEnabled = 1; -static bool inInterrupt; +void IntrHandler::handleResult(PendingInterrupt& pend) +{ + //u32 result = currentMIPS->r[MIPS_REG_V0]; +} +SubIntrHandler* IntrHandler::add(int subIntrNum) +{ + return &subIntrHandlers[subIntrNum]; +} +void IntrHandler::remove(int subIntrNum) +{ + if (has(subIntrNum)) + { + subIntrHandlers.erase(subIntrNum); + } +} +bool IntrHandler::has(int subIntrNum) const +{ + return subIntrHandlers.find(subIntrNum) != subIntrHandlers.end(); +} +void IntrHandler::enable(int subIntrNum) +{ + subIntrHandlers[subIntrNum].enabled = true; +} +void IntrHandler::disable(int subIntrNum) +{ + subIntrHandlers[subIntrNum].enabled = true; +} +SubIntrHandler* IntrHandler::get(int subIntrNum) +{ + if (has(subIntrNum)) + return &subIntrHandlers[subIntrNum]; + else + return NULL; +} +void IntrHandler::clear() +{ + subIntrHandlers.clear(); +} + +void IntrHandler::queueUp(int subintr) +{ + if(subintr == PSP_INTR_SUB_NONE) + { + pendingInterrupts.push_back(PendingInterrupt(intrNumber, subintr)); + } + else + { + // Just call execute on all the subintr handlers for this interrupt. + // They will get queued up. + for (std::map::iterator iter = subIntrHandlers.begin(); iter != subIntrHandlers.end(); ++iter) + { + if ((subintr == PSP_INTR_SUB_ALL || iter->first == subintr) && iter->second.enabled) + pendingInterrupts.push_back(PendingInterrupt(intrNumber, iter->first)); + } + } +} + +void IntrHandler::DoState(PointerWrap &p) +{ + p.Do(intrNumber); + p.Do(subIntrHandlers); +} void __InterruptsInit() { interruptsEnabled = 1; inInterrupt = false; + for(int i = 0; i < ARRAY_SIZE(intrHandlers); ++i) + intrHandlers[i] = new IntrHandler(i); intState.clear(); } @@ -257,14 +231,22 @@ void __InterruptsDoStateLate(PointerWrap &p) { // We do these later to ensure the handlers have been registered. for (int i = 0; i < PSP_NUMBER_INTERRUPTS; ++i) - intrHandlers[i].DoState(p); + intrHandlers[i]->DoState(p); p.DoMarker("sceKernelInterrupt Late"); } void __InterruptsShutdown() { - for (int i = 0; i < PSP_NUMBER_INTERRUPTS; ++i) - intrHandlers[i].clear(); + for (int i = 0; i < ARRAY_SIZE(intrHandlers); ++i) + intrHandlers[i]->clear(); + for(int i = 0; i < ARRAY_SIZE(intrHandlers); ++i) + { + if(intrHandlers[i]) + { + delete intrHandlers[i]; + intrHandlers[i] = 0; + } + } pendingInterrupts.clear(); } @@ -295,50 +277,22 @@ bool __CanExecuteInterrupt() void InterruptState::save() { - insideInterrupt = __IsInInterrupt(); __KernelSaveContext(&savedCpu); } void InterruptState::restore() { - ::inInterrupt = insideInterrupt; __KernelLoadContext(&savedCpu); } void InterruptState::clear() { - insideInterrupt = false; } // http://forums.ps2dev.org/viewtopic.php?t=5687 // http://www.google.se/url?sa=t&rct=j&q=&esrc=s&source=web&cd=7&ved=0CFYQFjAG&url=http%3A%2F%2Fdev.psnpt.com%2Fredmine%2Fprojects%2Fuofw%2Frepository%2Frevisions%2F65%2Fraw%2Ftrunk%2Finclude%2Finterruptman.h&ei=J4pCUKvyK4nl4QSu-YC4Cg&usg=AFQjCNFxJcgzQnv6dK7aiQlht_BM9grfQQ&sig2=GGk5QUEWI6qouYDoyE07YQ -void SubIntrHandler::queueUp() -{ - if (!enabled) - return; - - pendingInterrupts.push_back(PendingInterrupt(intrNumber, number)); -}; - -void SubIntrHandler::queueUpWithArg(int arg) -{ - if (!enabled) - return; - - pendingInterrupts.push_back(PendingInterrupt(intrNumber, number, arg)); -} - -void SubIntrHandler::copyArgsToCPU(const PendingInterrupt &pend) -{ - DEBUG_LOG(CPU, "Entering interrupt handler %08x", handlerAddress); - currentMIPS->pc = handlerAddress; - currentMIPS->r[MIPS_REG_A0] = pend.hasArg ? pend.arg : number; - currentMIPS->r[MIPS_REG_A1] = handlerArg; - // RA is already taken care of -} - // Returns true if anything was executed. bool __RunOnePendingInterrupt() @@ -350,26 +304,32 @@ bool __RunOnePendingInterrupt() } // Can easily prioritize between different kinds of interrupts if necessary. retry: - if (pendingInterrupts.size()) + if (!pendingInterrupts.empty()) { // If we came from CoreTiming::Advance(), we might've come from a waiting thread's callback. // To avoid "injecting" return values into our saved state, we context switch here. __KernelSwitchOffThread("interrupt"); PendingInterrupt pend = pendingInterrupts.front(); - SubIntrHandler *handler = intrHandlers[pend.intr].get(pend.subintr); - if (handler == NULL) + + IntrHandler* handler = intrHandlers[pend.intr]; + if(handler == NULL) { - WARN_LOG(HLE, "Ignoring interrupt, already been released."); + WARN_LOG(HLE, "Ignoring interrupt"); pendingInterrupts.pop_front(); goto retry; } intState.save(); - handler->copyArgsToCPU(pend); + inInterrupt = true; + + if(!handler->run(pend)) { + pendingInterrupts.pop_front(); + inInterrupt = false; + goto retry; + } currentMIPS->r[MIPS_REG_RA] = __KernelInterruptReturnAddress(); - inInterrupt = true; return true; } else @@ -400,37 +360,22 @@ void __TriggerInterrupt(int type, PSPInterrupt intno, int subintr) { if (interruptsEnabled || (type & PSP_INTR_ONLY_IF_ENABLED) == 0) { - intrHandlers[intno].queueUp(subintr); + intrHandlers[intno]->queueUp(subintr); DEBUG_LOG(HLE, "Triggering subinterrupts for interrupt %i sub %i (%i in queue)", intno, subintr, (u32)pendingInterrupts.size()); __TriggerRunInterrupts(type); } } -void __TriggerInterruptWithArg(int type, PSPInterrupt intno, int subintr, int arg) -{ - if (interruptsEnabled || (type & PSP_INTR_ONLY_IF_ENABLED) == 0) - { - intrHandlers[intno].queueUpWithArg(subintr, arg); - DEBUG_LOG(HLE, "Triggering subinterrupts for interrupt %i sub %i with arg %i (%i in queue)", intno, subintr, arg, - (u32)pendingInterrupts.size()); - __TriggerRunInterrupts(type); - } -} - void __KernelReturnFromInterrupt() { DEBUG_LOG(CPU, "Left interrupt handler at %08x", currentMIPS->pc); - inInterrupt = false; // This is what we just ran. PendingInterrupt pend = pendingInterrupts.front(); pendingInterrupts.pop_front(); - SubIntrHandler *handler = intrHandlers[pend.intr].get(pend.subintr); - if (handler != NULL) - handler->handleResult(currentMIPS->r[MIPS_REG_V0]); - else - ERROR_LOG(HLE, "Interrupt released itself? Should not happen."); + intrHandlers[pend.intr]->handleResult(pend); + inInterrupt = false; // Restore context after running the interrupt. intState.restore(); @@ -441,15 +386,17 @@ void __KernelReturnFromInterrupt() __KernelReSchedule("return from interrupt"); } -void __RegisterSubIntrCreator(u32 intrNumber, SubIntrCreator creator) +void __RegisterIntrHandler(u32 intrNumber, IntrHandler* handler) { - intrHandlers[intrNumber].setCreator(creator); + if(intrHandlers[intrNumber]) + delete intrHandlers[intrNumber]; + intrHandlers[intrNumber] = handler; } SubIntrHandler *__RegisterSubIntrHandler(u32 intrNumber, u32 subIntrNumber, u32 &error) { - SubIntrHandler *subIntrHandler = intrHandlers[intrNumber].add(subIntrNumber); - subIntrHandler->number = subIntrNumber; + SubIntrHandler *subIntrHandler = intrHandlers[intrNumber]->add(subIntrNumber); + subIntrHandler->subIntrNumber = subIntrNumber; subIntrHandler->intrNumber = intrNumber; error = 0; return subIntrHandler; @@ -457,7 +404,7 @@ SubIntrHandler *__RegisterSubIntrHandler(u32 intrNumber, u32 subIntrNumber, u32 u32 __ReleaseSubIntrHandler(u32 intrNumber, u32 subIntrNumber) { - if (!intrHandlers[intrNumber].has(subIntrNumber)) + if (!intrHandlers[intrNumber]->has(subIntrNumber)) return -1; for (std::list::iterator it = pendingInterrupts.begin(); it != pendingInterrupts.end(); ) @@ -468,7 +415,7 @@ u32 __ReleaseSubIntrHandler(u32 intrNumber, u32 subIntrNumber) ++it; } - intrHandlers[intrNumber].remove(subIntrNumber); + intrHandlers[intrNumber]->remove(subIntrNumber); return 0; } @@ -506,10 +453,10 @@ u32 sceKernelEnableSubIntr(u32 intrNumber, u32 subIntrNumber) if (intrNumber >= PSP_NUMBER_INTERRUPTS) return -1; - if (!intrHandlers[intrNumber].has(subIntrNumber)) + if (!intrHandlers[intrNumber]->has(subIntrNumber)) return -1; - intrHandlers[intrNumber].get(subIntrNumber)->enabled = true; + intrHandlers[intrNumber]->enable(subIntrNumber); return 0; } @@ -519,10 +466,10 @@ u32 sceKernelDisableSubIntr(u32 intrNumber, u32 subIntrNumber) if (intrNumber >= PSP_NUMBER_INTERRUPTS) return -1; - if (!intrHandlers[intrNumber].has(subIntrNumber)) + if (!intrHandlers[intrNumber]->has(subIntrNumber)) return -1; - intrHandlers[intrNumber].get(subIntrNumber)->enabled = false; + intrHandlers[intrNumber]->disable(subIntrNumber); return 0; } diff --git a/Core/HLE/sceKernelInterrupt.h b/Core/HLE/sceKernelInterrupt.h index cd3b6f6b8f..3327dd6162 100644 --- a/Core/HLE/sceKernelInterrupt.h +++ b/Core/HLE/sceKernelInterrupt.h @@ -52,8 +52,8 @@ enum PSPInterrupt { // These are invented for PPSSPP: enum PSPGeSubInterrupts { - PSP_GE_SUBINTR_FINISH = 14, - PSP_GE_SUBINTR_SIGNAL = 15 + PSP_GE_SUBINTR_SIGNAL = 0, + PSP_GE_SUBINTR_FINISH = 1, }; enum PSPInterruptTriggerType { @@ -68,46 +68,61 @@ enum PSPInterruptTriggerType { PSP_INTR_ALWAYS_RESCHED = 0x4, }; -struct PendingInterrupt { - PendingInterrupt(int intr_, int subintr_) - : intr(intr_), subintr(subintr_), hasArg(false) {} - PendingInterrupt(int intr_, int subintr_, int arg_) - : intr(intr_), subintr(subintr_), hasArg(true), arg(arg_) {} - - u32 intr; - u32 subintr; - bool hasArg; - int arg; +enum PSPSubInterruptTriggerType { + // Trigger all sub intr + PSP_INTR_SUB_ALL = -2, + // Trigger code at the interrupt handler level + PSP_INTR_SUB_NONE = -1, + // Trigger specific sub interrupt + PSP_INTR_SUB_NUMBER = 0, }; -class SubIntrHandler +struct PendingInterrupt { + PendingInterrupt(int intr_, int subintr_) + : intr(intr_), subintr(subintr_) {} + + int intr; + int subintr; +}; + +struct SubIntrHandler { -public: - SubIntrHandler() {} - virtual ~SubIntrHandler() {} - virtual void queueUp(); - virtual void queueUpWithArg(int arg); - virtual void copyArgsToCPU(const PendingInterrupt &pend); - virtual void handleResult(int result) {} - - virtual void DoState(PointerWrap &p) - { - p.Do(enabled); - p.Do(intrNumber); - p.Do(number); - p.Do(handlerAddress); - p.Do(handlerArg); - p.DoMarker("SubIntrHandler"); - } - bool enabled; int intrNumber; - int number; + int subIntrNumber; u32 handlerAddress; u32 handlerArg; }; -typedef SubIntrHandler *(*SubIntrCreator)(); +class IntrHandler +{ +public: + IntrHandler(int intrNumber_) + : intrNumber(intrNumber_) + { + } + virtual ~IntrHandler() {} + + virtual bool run(PendingInterrupt& pend); + virtual void copyArgsToCPU(PendingInterrupt& pend); + virtual void handleResult(PendingInterrupt& pend); + void queueUp(int subintr); + + SubIntrHandler* add(int subIntrNum); + void remove(int subIntrNum); + bool has(int subIntrNum) const; + void enable(int subIntrNum); + void disable(int subIntrNum); + SubIntrHandler *get(int subIntrNum); + void clear(); + + + void DoState(PointerWrap &p); + +private: + int intrNumber; + std::map subIntrHandlers; +}; bool __IsInInterrupt(); void __InterruptsInit(); @@ -115,11 +130,10 @@ void __InterruptsDoState(PointerWrap &p); void __InterruptsDoStateLate(PointerWrap &p); void __InterruptsShutdown(); void __TriggerInterrupt(int type, PSPInterrupt intno, int subInterrupts = -1); -void __TriggerInterruptWithArg(int type, PSPInterrupt intno, int subintr, int arg); // For GE "callbacks" bool __RunOnePendingInterrupt(); void __KernelReturnFromInterrupt(); -void __RegisterSubIntrCreator(u32 intrNumber, SubIntrCreator creator); +void __RegisterIntrHandler(u32 intrNumber, IntrHandler* handler); SubIntrHandler *__RegisterSubIntrHandler(u32 intrNumber, u32 subIntrNumber, u32 &error); u32 __ReleaseSubIntrHandler(u32 intrNumber, u32 subIntrNumber); diff --git a/GPU/GLES/DisplayListInterpreter.cpp b/GPU/GLES/DisplayListInterpreter.cpp index 1194a5afbe..67d9776633 100644 --- a/GPU/GLES/DisplayListInterpreter.cpp +++ b/GPU/GLES/DisplayListInterpreter.cpp @@ -33,6 +33,7 @@ #include "../../Core/HLE/sceKernelThread.h" #include "../../Core/HLE/sceKernelInterrupt.h" +#include "../../Core/HLE/sceGe.h" extern u32 curTextureWidth; extern u32 curTextureHeight; @@ -439,13 +440,15 @@ void GLES_GPU::ExecuteOp(u32 op, u32 diff) { case GE_CMD_SIGNAL: { // Processed in GE_END. Has data. + currentList->subIntrToken = data & 0xFFFF; } break; case GE_CMD_FINISH: + currentList->subIntrToken = data & 0xFFFF; // TODO: Should this run while interrupts are suspended? if (interruptsEnabled_) - __TriggerInterruptWithArg(PSP_INTR_HLE, PSP_GE_INTR, currentList->subIntrBase | PSP_GE_SUBINTR_FINISH, 0); + __GeTriggerInterrupt(currentList->id, currentList->pc); break; case GE_CMD_END: @@ -483,7 +486,7 @@ void GLES_GPU::ExecuteOp(u32 op, u32 diff) { } // TODO: Should this run while interrupts are suspended? if (interruptsEnabled_) - __TriggerInterruptWithArg(PSP_INTR_HLE, PSP_GE_INTR, currentList->subIntrBase | PSP_GE_SUBINTR_SIGNAL, signal); + __GeTriggerInterrupt(currentList->id, currentList->pc); } break; case GE_CMD_FINISH: diff --git a/GPU/GPUCommon.cpp b/GPU/GPUCommon.cpp index 9a0512e25a..f82ff8bcc1 100644 --- a/GPU/GPUCommon.cpp +++ b/GPU/GPUCommon.cpp @@ -135,3 +135,13 @@ void GPUCommon::DoState(PointerWrap &p) { p.Do(dlQueue); p.DoMarker("GPUCommon"); } + +void GPUCommon::InterruptStart() +{ + interruptRunning = true; +} +void GPUCommon::InterruptEnd() +{ + interruptRunning = false; + ProcessDLQueue(); +} diff --git a/GPU/GPUCommon.h b/GPU/GPUCommon.h index 803f63efd5..c5b5f16e59 100644 --- a/GPU/GPUCommon.h +++ b/GPU/GPUCommon.h @@ -13,6 +13,9 @@ public: dumpThisFrame_(false) {} + virtual void InterruptStart(); + virtual void InterruptEnd(); + virtual void PreExecuteOp(u32 op, u32 diff); virtual bool InterpretList(DisplayList &list); virtual bool ProcessDLQueue(); @@ -28,6 +31,7 @@ protected: DisplayList *currentList; DisplayListQueue dlQueue; + bool interruptRunning; u32 prev; u32 stack[2]; u32 stackptr; @@ -35,4 +39,18 @@ protected: bool dumpNextFrame_; bool dumpThisFrame_; -}; \ No newline at end of file + + +public: + virtual DisplayList* getList(int listid) + { + if(currentList->id == listid) + return currentList; + for(auto it = dlQueue.begin(); it != dlQueue.end(); ++it) + { + if(it->id == listid) + return &*it; + } + return NULL; + } +}; diff --git a/GPU/GPUInterface.h b/GPU/GPUInterface.h index ee5f4bdb6d..42ca2be528 100644 --- a/GPU/GPUInterface.h +++ b/GPU/GPUInterface.h @@ -38,6 +38,7 @@ struct DisplayList u32 stall; DisplayListStatus status; int subIntrBase; + u16 subIntrToken; }; class GPUInterface @@ -49,12 +50,16 @@ public: virtual void InitClear() = 0; // Draw queue management + virtual DisplayList* getList(int listid) = 0; // TODO: Much of this should probably be shared between the different GPU implementations. virtual u32 EnqueueList(u32 listpc, u32 stall, int subIntrBase, bool head) = 0; virtual void UpdateStall(int listid, u32 newstall) = 0; virtual void DrawSync(int mode) = 0; virtual void Continue() = 0; + virtual void InterruptStart() = 0; + virtual void InterruptEnd() = 0; + virtual void PreExecuteOp(u32 op, u32 diff) = 0; virtual void ExecuteOp(u32 op, u32 diff) = 0; virtual bool InterpretList(DisplayList& list) = 0; diff --git a/GPU/Null/NullGpu.cpp b/GPU/Null/NullGpu.cpp index 7276fe6093..73517092ac 100644 --- a/GPU/Null/NullGpu.cpp +++ b/GPU/Null/NullGpu.cpp @@ -21,6 +21,7 @@ #include "../ge_constants.h" #include "../../Core/MemMap.h" #include "../../Core/HLE/sceKernelInterrupt.h" +#include "../../Core/HLE/sceGe.h" NullGPU::NullGPU() { @@ -149,11 +150,11 @@ void NullGPU::ExecuteOp(u32 op, u32 diff) { ERROR_LOG(G3D, "DL GE_CMD_SIGNAL %08x", data & 0xFFFFFF); int behaviour = (data >> 16) & 0xFF; - int signal = data & 0xFFFF; + currentList->subIntrToken = data & 0xFFFF; // TODO: Should this run while interrupts are suspended? if (interruptsEnabled_) - __TriggerInterruptWithArg(PSP_INTR_HLE, PSP_GE_INTR, currentList->subIntrBase | PSP_GE_SUBINTR_SIGNAL, signal); + __GeTriggerInterrupt(currentList->id, currentList->pc); } break; @@ -184,9 +185,10 @@ void NullGPU::ExecuteOp(u32 op, u32 diff) case GE_CMD_FINISH: DEBUG_LOG(G3D,"DL CMD FINISH"); + currentList->subIntrToken = data & 0xFFFF; // TODO: Should this run while interrupts are suspended? if (interruptsEnabled_) - __TriggerInterruptWithArg(PSP_INTR_HLE, PSP_GE_INTR, currentList->subIntrBase | PSP_GE_SUBINTR_FINISH, 0); + __GeTriggerInterrupt(currentList->id, currentList->pc); break; case GE_CMD_END: