From bf8abaebe5f81be11fcb34c539b64b059da27551 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 25 Nov 2012 12:05:43 -0800 Subject: [PATCH 01/14] Don't include analog when not enabled in ctrl. Fixes menu, movement in Tales of Eternia. --- Core/HLE/sceCtrl.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index 0e255bfdd7..77769a0b5d 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -157,7 +157,13 @@ u32 sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) //if (ctrlInited) //{ SampleControls(); - Memory::WriteStruct(ctrlDataPtr, &ctrl); + _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); + memcpy(ctrlData, &ctrl, sizeof(_ctrl_data)); + if (!analogEnabled) + { + ctrlData->analog[0] = 128; + ctrlData->analog[1] = 128; + } //} return 1; } From 843d495c5bb7113e0e383841880e0e47ff56f170 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 25 Nov 2012 20:59:52 -0800 Subject: [PATCH 02/14] Don't expect sceCtrlInit ever, minor ctrl funcs. I don't see any indication that you need to call init first. --- Core/HLE/sceCtrl.cpp | 180 ++++++++++++++++++++++++++++--------------- 1 file changed, 120 insertions(+), 60 deletions(-) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index 77769a0b5d..65872a2672 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -27,6 +27,7 @@ #define CTRL_MODE_DIGITAL 0 #define CTRL_MODE_ANALOG 1 +const int PSP_CTRL_ERROR_INVALID_MODE = 0x80000107; // Returned control data struct _ctrl_data @@ -60,15 +61,13 @@ static std::recursive_mutex ctrlMutex; ////////////////////////////////////////////////////////////////////////// -void SampleControls() { - static int frame = 0; - _ctrl_data &data = ctrl; - data.frame=1;//frame; - frame++; -} +void sceCtrlInit(); +void UpdateLatch() +{ + if (!ctrlInited) + sceCtrlInit(); -void UpdateLatch() { u32 changed = ctrl.buttons ^ oldButtons; latch.btnMake = ctrl.buttons & changed; latch.btnBreak = oldButtons & changed; @@ -81,6 +80,9 @@ void UpdateLatch() { u32 __CtrlPeekButtons() { + if (!ctrlInited) + sceCtrlInit(); + return ctrl.buttons; } @@ -89,61 +91,91 @@ u32 __CtrlPeekButtons() void __CtrlButtonDown(u32 buttonBit) { - std::lock_guard guard(ctrlMutex); - ctrl.buttons |= buttonBit; + if (!ctrlInited) + sceCtrlInit(); + + std::lock_guard guard(ctrlMutex); + ctrl.buttons |= buttonBit; } void __CtrlButtonUp(u32 buttonBit) { - std::lock_guard guard(ctrlMutex); - ctrl.buttons &= ~buttonBit; + if (!ctrlInited) + sceCtrlInit(); + + std::lock_guard guard(ctrlMutex); + ctrl.buttons &= ~buttonBit; } void __CtrlSetAnalog(float x, float y) { - std::lock_guard guard(ctrlMutex); - // TODO: Circle! - if (x > 1.0f) x = 1.0f; - if (y > 1.0f) y = 1.0f; - if (x < -1.0f) x = -1.0f; - if (y < -1.0f) y = -1.0f; - ctrl.analog[0] = (u8)(x * 127.f + 128.f); - ctrl.analog[1] = (u8)(y * 127.f + 128.f); + if (!ctrlInited) + sceCtrlInit(); + + std::lock_guard guard(ctrlMutex); + // TODO: Circle! + if (x > 1.0f) x = 1.0f; + if (y > 1.0f) y = 1.0f; + if (x < -1.0f) x = -1.0f; + if (y < -1.0f) y = -1.0f; + ctrl.analog[0] = (u8)(x * 127.f + 128.f); + ctrl.analog[1] = (u8)(y * 127.f + 128.f); } void sceCtrlInit() { ctrlInited = true; - memset(&ctrl, 0, sizeof(ctrl)); + + std::lock_guard guard(ctrlMutex); + + memset(&ctrl, 0, sizeof(ctrl)); ctrl.analog[0] = 128; ctrl.analog[1] = 128; + // TODO: Make this increment in the correct way. + ctrl.frame = 1; + DEBUG_LOG(HLE,"sceCtrlInit"); RETURN(0); } -void sceCtrlSetSamplingCycle() { - ERROR_LOG(HLE,"UNIMPL sceCtrlSetSamplingCycle"); - RETURN(0); +u32 sceCtrlSetSamplingCycle(u32 cycle) +{ + ERROR_LOG(HLE, "UNIMPL sceCtrlSetSamplingCycle(%u)", cycle); + return 0; +} + +int sceCtrlGetSamplingCycle(u32 cyclePtr) +{ + ERROR_LOG(HLE, "UNIMPL sceCtrlSetSamplingCycle(%08x)", cyclePtr); + return 0; } u32 sceCtrlSetSamplingMode(u32 mode) { u32 retVal = 0; - DEBUG_LOG(HLE,"sceCtrlSetSamplingMode(%i)", mode); - _assert_msg_(HLE, mode >= 0 && mode <= 1, "sceCtrlSetSamplingMode: mode outside expected range."); + DEBUG_LOG(HLE, "sceCtrlSetSamplingMode(%i)", mode); + if (mode > 1) + return PSP_CTRL_ERROR_INVALID_MODE; - if (ctrlInited) - { - retVal = analogEnabled == true ? CTRL_MODE_ANALOG : CTRL_MODE_DIGITAL; - analogEnabled = mode == CTRL_MODE_ANALOG ? true : false; - } + retVal = analogEnabled == true ? CTRL_MODE_ANALOG : CTRL_MODE_DIGITAL; + analogEnabled = mode == CTRL_MODE_ANALOG ? true : false; return retVal; } +int sceCtrlGetSamplingMode(u32 modePtr) +{ + u32 retVal = analogEnabled == true ? CTRL_MODE_ANALOG : CTRL_MODE_DIGITAL; + + if (Memory::IsValidAddress(modePtr)) + Memory::Write_U32(retVal, modePtr); + + return 0; +} + void sceCtrlSetIdleCancelThreshold() { - DEBUG_LOG(HLE,"UNIMPL sceCtrlSetIdleCancelThreshold"); + DEBUG_LOG(HLE,"UNIMPL sceCtrlSetIdleCancelThreshold"); RETURN(0); } @@ -152,11 +184,13 @@ u32 sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) DEBUG_LOG(HLE,"sceCtrlReadBufferPositive(%08x, %i)", ctrlDataPtr, nBufs); _assert_msg_(HLE, nBufs > 0, "sceCtrlReadBufferPositive: trying to read nothing?"); + if (!ctrlInited) + sceCtrlInit(); + std::lock_guard guard(ctrlMutex); - // Let's just ignore if ctrl is inited or not; some games don't init it (Super Fruit Fall) - //if (ctrlInited) - //{ - SampleControls(); + + if (Memory::IsValidAddress(ctrlDataPtr)) + { _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); memcpy(ctrlData, &ctrl, sizeof(_ctrl_data)); if (!analogEnabled) @@ -164,11 +198,38 @@ u32 sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) ctrlData->analog[0] = 128; ctrlData->analog[1] = 128; } - //} + } + return 1; } -u32 sceCtrlPeekLatch(u32 latchDataPtr) { +u32 sceCtrlReadBufferNegative(u32 ctrlDataPtr, u32 nBufs) +{ + DEBUG_LOG(HLE,"sceCtrlReadBufferNegative(%08x, %i)", ctrlDataPtr, nBufs); + _assert_msg_(HLE, nBufs > 0, "sceCtrlReadBufferNegative: trying to read nothing?"); + + if (!ctrlInited) + sceCtrlInit(); + + std::lock_guard guard(ctrlMutex); + + if (Memory::IsValidAddress(ctrlDataPtr)) + { + _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); + memcpy(ctrlData, &ctrl, sizeof(_ctrl_data)); + ctrlData->buttons = ~ctrlData->buttons; + if (!analogEnabled) + { + ctrlData->analog[0] = 128; + ctrlData->analog[1] = 128; + } + } + + return 1; +} + +u32 sceCtrlPeekLatch(u32 latchDataPtr) +{ ERROR_LOG(HLE,"FAKE sceCtrlPeekLatch(%08x)", latchDataPtr); if (Memory::IsValidAddress(latchDataPtr)) @@ -176,14 +237,13 @@ u32 sceCtrlPeekLatch(u32 latchDataPtr) { return 1; } -u32 sceCtrlReadLatch(u32 latchDataPtr) { +u32 sceCtrlReadLatch(u32 latchDataPtr) +{ ERROR_LOG(HLE,"FAKE sceCtrlReadLatch(%08x)", latchDataPtr); // Hackery to do it here. - SampleControls(); UpdateLatch(); - if (Memory::IsValidAddress(latchDataPtr)) Memory::WriteStruct(latchDataPtr, &latch); @@ -192,26 +252,26 @@ u32 sceCtrlReadLatch(u32 latchDataPtr) { static const HLEFunction sceCtrl[] = { - {0x6a2774f3, sceCtrlInit, "sceCtrlInit"}, //(int unknown), init with 0 - {0x1f4011e6, &WrapU_U, "sceCtrlSetSamplingMode"}, //(int on); - {0x1f803938, &WrapU_UU, "sceCtrlReadBufferPositive"}, //(ctrl_data_t* paddata, int unknown) // unknown should be 1 - {0x6A2774F3, 0, "sceCtrlSetSamplingCycle"}, //? - {0x6A2774F3,sceCtrlSetSamplingCycle,"sceCtrlSetSamplingCycle"}, - {0x02BAAD91,0,"sceCtrlGetSamplingCycle"}, - {0xDA6B76A1,0,"sceCtrlGetSamplingMode"}, - {0x3A622550,&WrapU_UU, "sceCtrlPeekBufferPositive"}, - {0xC152080A,0,"sceCtrlPeekBufferNegative"}, - {0x60B81F86,0,"sceCtrlReadBufferNegative"}, - {0xB1D0E5CD,&WrapU_U,"sceCtrlPeekLatch"}, - {0x0B588501,&WrapU_U,"sceCtrlReadLatch"}, - {0x348D99D4,0,"sceCtrl_348D99D4"}, - {0xAF5960F3,0,"sceCtrl_AF5960F3"}, - {0xA68FD260,0,"sceCtrlClearRapidFire"}, - {0x6841BE1A,0,"sceCtrlSetRapidFire"}, - {0xa7144800,sceCtrlSetIdleCancelThreshold,"sceCtrlSetIdleCancelThreshold"}, - {0x687660fa,0,"sceCtrlGetIdleCancelThreshold"}, + {0x3E65A0EA, WrapV_V, "sceCtrlInit"}, //(int unknown), init with 0 + {0x1f4011e6, WrapU_U, "sceCtrlSetSamplingMode"}, //(int on); + {0x6A2774F3, WrapU_U, "sceCtrlSetSamplingCycle"}, + {0x02BAAD91, WrapI_U,"sceCtrlGetSamplingCycle"}, + {0xDA6B76A1, WrapI_U, "sceCtrlGetSamplingMode"}, + {0x1f803938, WrapU_UU, "sceCtrlReadBufferPositive"}, //(ctrl_data_t* paddata, int unknown) // unknown should be 1 + {0x3A622550, WrapU_UU, "sceCtrlPeekBufferPositive"}, + {0xC152080A, WrapU_UU, "sceCtrlPeekBufferNegative"}, + {0x60B81F86, WrapU_UU, "sceCtrlReadBufferNegative"}, + {0xB1D0E5CD, WrapU_U, "sceCtrlPeekLatch"}, + {0x0B588501, WrapU_U, "sceCtrlReadLatch"}, + {0x348D99D4, 0, "sceCtrl_348D99D4"}, + {0xAF5960F3, 0, "sceCtrl_AF5960F3"}, + {0xA68FD260, 0, "sceCtrlClearRapidFire"}, + {0x6841BE1A, 0, "sceCtrlSetRapidFire"}, + {0xa7144800, WrapV_V, "sceCtrlSetIdleCancelThreshold"}, + {0x687660fa, 0, "sceCtrlGetIdleCancelThreshold"}, }; -void Register_sceCtrl() { - RegisterModule("sceCtrl",ARRAY_SIZE(sceCtrl),sceCtrl); +void Register_sceCtrl() +{ + RegisterModule("sceCtrl", ARRAY_SIZE(sceCtrl), sceCtrl); } From 0dc8e2a420eb1fc65f81a9e72a865316263166fa Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 25 Nov 2012 21:44:01 -0800 Subject: [PATCH 03/14] Set the ctrl frame value to something useful. --- Core/HLE/sceCtrl.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index 65872a2672..56ba019a7d 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -17,6 +17,7 @@ #include "HLE.h" #include "../MIPS/MIPS.h" +#include "../CoreTiming.h" #include "StdMutex.h" #include "sceCtrl.h" @@ -28,6 +29,7 @@ #define CTRL_MODE_ANALOG 1 const int PSP_CTRL_ERROR_INVALID_MODE = 0x80000107; +const int PSP_CTRL_ERROR_INVALID_NUM_BUFFERS = 0x80000104; // Returned control data struct _ctrl_data @@ -131,7 +133,6 @@ void sceCtrlInit() memset(&ctrl, 0, sizeof(ctrl)); ctrl.analog[0] = 128; ctrl.analog[1] = 128; - // TODO: Make this increment in the correct way. ctrl.frame = 1; DEBUG_LOG(HLE,"sceCtrlInit"); @@ -181,9 +182,14 @@ void sceCtrlSetIdleCancelThreshold() u32 sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) { + // TODO: Test rescheduling. DEBUG_LOG(HLE,"sceCtrlReadBufferPositive(%08x, %i)", ctrlDataPtr, nBufs); _assert_msg_(HLE, nBufs > 0, "sceCtrlReadBufferPositive: trying to read nothing?"); + // Pretend we have only 64 of them. + if (nBufs > 64) + return PSP_CTRL_ERROR_INVALID_NUM_BUFFERS; + if (!ctrlInited) sceCtrlInit(); @@ -193,6 +199,8 @@ u32 sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) { _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); memcpy(ctrlData, &ctrl, sizeof(_ctrl_data)); + + ctrlData->frame = CoreTiming::GetTicks() / CoreTiming::GetClockFrequencyMHz(); if (!analogEnabled) { ctrlData->analog[0] = 128; @@ -205,6 +213,7 @@ u32 sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) u32 sceCtrlReadBufferNegative(u32 ctrlDataPtr, u32 nBufs) { + // TODO: Test rescheduling. DEBUG_LOG(HLE,"sceCtrlReadBufferNegative(%08x, %i)", ctrlDataPtr, nBufs); _assert_msg_(HLE, nBufs > 0, "sceCtrlReadBufferNegative: trying to read nothing?"); @@ -217,7 +226,9 @@ u32 sceCtrlReadBufferNegative(u32 ctrlDataPtr, u32 nBufs) { _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); memcpy(ctrlData, &ctrl, sizeof(_ctrl_data)); + ctrlData->buttons = ~ctrlData->buttons; + ctrlData->frame = CoreTiming::GetTicks() / CoreTiming::GetClockFrequencyMHz(); if (!analogEnabled) { ctrlData->analog[0] = 128; From 754a49dfd7f1af5ed4f27d2a73fe86515f6ca800 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 25 Nov 2012 22:10:25 -0800 Subject: [PATCH 04/14] Make sceCtrlReadLatch work more correctly. It gets changes since last call, makes sense. --- Core/HLE/sceCtrl.cpp | 50 +++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index 56ba019a7d..f847eb973a 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -40,7 +40,6 @@ struct _ctrl_data u8 unused[6]; }; -static u32 oldButtons; struct CtrlLatch { u32 btnMake; u32 btnBreak; @@ -53,6 +52,8 @@ struct CtrlLatch { // STATE BEGIN static bool ctrlInited = false; static bool analogEnabled = false; +static int ctrlLatchBufs = 0; +static u32 ctrlOldButtons = 0; static _ctrl_data ctrl; static CtrlLatch latch; @@ -65,20 +66,33 @@ static std::recursive_mutex ctrlMutex; void sceCtrlInit(); -void UpdateLatch() +void __CtrlUpdateLatch() { if (!ctrlInited) sceCtrlInit(); - u32 changed = ctrl.buttons ^ oldButtons; - latch.btnMake = ctrl.buttons & changed; - latch.btnBreak = oldButtons & changed; - latch.btnPress = ctrl.buttons; - latch.btnRelease = (oldButtons & ~ctrl.buttons) & changed; + std::lock_guard guard(ctrlMutex); + + u32 changed = ctrl.buttons ^ ctrlOldButtons; + latch.btnMake |= ctrl.buttons & changed; + latch.btnBreak |= ctrlOldButtons & changed; + latch.btnPress |= ctrl.buttons; + latch.btnRelease |= (ctrlOldButtons & ~ctrl.buttons) & changed; + // TODO: This should really be happening based on the "sampling cycle"... + ctrlLatchBufs++; - oldButtons = ctrl.buttons; + ctrlOldButtons = ctrl.buttons; } +int __CtrlResetLatch() +{ + std::lock_guard guard(ctrlMutex); + + int oldBufs = ctrlLatchBufs; + memset(&latch, 0, sizeof(CtrlLatch)); + ctrlLatchBufs = 0; + return oldBufs; +} u32 __CtrlPeekButtons() { @@ -98,6 +112,7 @@ void __CtrlButtonDown(u32 buttonBit) std::lock_guard guard(ctrlMutex); ctrl.buttons |= buttonBit; + __CtrlUpdateLatch(); } void __CtrlButtonUp(u32 buttonBit) @@ -107,6 +122,7 @@ void __CtrlButtonUp(u32 buttonBit) std::lock_guard guard(ctrlMutex); ctrl.buttons &= ~buttonBit; + __CtrlUpdateLatch(); } void __CtrlSetAnalog(float x, float y) @@ -122,6 +138,8 @@ void __CtrlSetAnalog(float x, float y) if (y < -1.0f) y = -1.0f; ctrl.analog[0] = (u8)(x * 127.f + 128.f); ctrl.analog[1] = (u8)(y * 127.f + 128.f); + + __CtrlUpdateLatch(); } void sceCtrlInit() @@ -130,6 +148,7 @@ void sceCtrlInit() std::lock_guard guard(ctrlMutex); + memset(&latch, 0, sizeof(latch)); memset(&ctrl, 0, sizeof(ctrl)); ctrl.analog[0] = 128; ctrl.analog[1] = 128; @@ -200,7 +219,7 @@ u32 sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); memcpy(ctrlData, &ctrl, sizeof(_ctrl_data)); - ctrlData->frame = CoreTiming::GetTicks() / CoreTiming::GetClockFrequencyMHz(); + ctrlData->frame = (u32) (CoreTiming::GetTicks() / CoreTiming::GetClockFrequencyMHz()); if (!analogEnabled) { ctrlData->analog[0] = 128; @@ -228,7 +247,7 @@ u32 sceCtrlReadBufferNegative(u32 ctrlDataPtr, u32 nBufs) memcpy(ctrlData, &ctrl, sizeof(_ctrl_data)); ctrlData->buttons = ~ctrlData->buttons; - ctrlData->frame = CoreTiming::GetTicks() / CoreTiming::GetClockFrequencyMHz(); + ctrlData->frame = (u32) (CoreTiming::GetTicks() / CoreTiming::GetClockFrequencyMHz()); if (!analogEnabled) { ctrlData->analog[0] = 128; @@ -243,22 +262,25 @@ u32 sceCtrlPeekLatch(u32 latchDataPtr) { ERROR_LOG(HLE,"FAKE sceCtrlPeekLatch(%08x)", latchDataPtr); + // TODO: We don't really want to do this here, it should be on an interval. + __CtrlUpdateLatch(); + if (Memory::IsValidAddress(latchDataPtr)) Memory::WriteStruct(latchDataPtr, &latch); - return 1; + return ctrlLatchBufs; } u32 sceCtrlReadLatch(u32 latchDataPtr) { ERROR_LOG(HLE,"FAKE sceCtrlReadLatch(%08x)", latchDataPtr); - // Hackery to do it here. - UpdateLatch(); + // TODO: We don't really want to do this here, it should be on an interval. + __CtrlUpdateLatch(); if (Memory::IsValidAddress(latchDataPtr)) Memory::WriteStruct(latchDataPtr, &latch); - return 1; + return __CtrlResetLatch(); } static const HLEFunction sceCtrl[] = From b93c4f636cf424d34da3f19f490232c56fa2c497 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 1 Dec 2012 02:08:43 -0800 Subject: [PATCH 05/14] Oops, prevent a possible crash in ctrl. --- Core/HLE/sceCtrl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index f847eb973a..003408a31c 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -214,9 +214,9 @@ u32 sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) std::lock_guard guard(ctrlMutex); - if (Memory::IsValidAddress(ctrlDataPtr)) + _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); + if (Memory::IsValidAddress(ctrlDataPtr) && ctrlData) { - _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); memcpy(ctrlData, &ctrl, sizeof(_ctrl_data)); ctrlData->frame = (u32) (CoreTiming::GetTicks() / CoreTiming::GetClockFrequencyMHz()); @@ -241,9 +241,9 @@ u32 sceCtrlReadBufferNegative(u32 ctrlDataPtr, u32 nBufs) std::lock_guard guard(ctrlMutex); - if (Memory::IsValidAddress(ctrlDataPtr)) + _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); + if (Memory::IsValidAddress(ctrlDataPtr) && ctrlData) { - _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); memcpy(ctrlData, &ctrl, sizeof(_ctrl_data)); ctrlData->buttons = ~ctrlData->buttons; From 7edb34057720808fab638643b93a7d6ff5fc33a0 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 2 Dec 2012 12:44:33 -0800 Subject: [PATCH 06/14] Start the ctrl latch off all just released. --- Core/HLE/sceCtrl.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index 003408a31c..3dd877a02e 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -149,6 +149,9 @@ void sceCtrlInit() std::lock_guard guard(ctrlMutex); memset(&latch, 0, sizeof(latch)); + // Start with everything released. + latch.btnRelease = 0xffffffff; + memset(&ctrl, 0, sizeof(ctrl)); ctrl.analog[0] = 128; ctrl.analog[1] = 128; From fd2d6661917791653664e38156be1760e3d0d6d2 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 2 Dec 2012 15:44:23 -0800 Subject: [PATCH 07/14] Add ctrl sampling (vblank). This makes it work like the PSP does with number of buffers, etc. Still doesn't wait yet. --- Core/HLE/sceCtrl.cpp | 208 ++++++++++++++++++++++------------------ Core/HLE/sceCtrl.h | 2 + Core/HLE/sceDisplay.cpp | 19 ++++ Core/HLE/sceDisplay.h | 3 + Core/HLE/sceKernel.cpp | 2 + 5 files changed, 143 insertions(+), 91 deletions(-) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index 3dd877a02e..7eb63da4aa 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -20,6 +20,7 @@ #include "../CoreTiming.h" #include "StdMutex.h" #include "sceCtrl.h" +#include "sceDisplay.h" /* Index for the two analog directions */ #define CTRL_ANALOG_X 0 @@ -55,7 +56,10 @@ static bool analogEnabled = false; static int ctrlLatchBufs = 0; static u32 ctrlOldButtons = 0; -static _ctrl_data ctrl; +static _ctrl_data ctrlBufs[64]; +static _ctrl_data ctrlCurrent; +static int ctrlBuf = 0; +static int ctrlBufRead = 0; static CtrlLatch latch; static std::recursive_mutex ctrlMutex; @@ -64,30 +68,39 @@ static std::recursive_mutex ctrlMutex; ////////////////////////////////////////////////////////////////////////// -void sceCtrlInit(); - void __CtrlUpdateLatch() { - if (!ctrlInited) - sceCtrlInit(); - std::lock_guard guard(ctrlMutex); - u32 changed = ctrl.buttons ^ ctrlOldButtons; - latch.btnMake |= ctrl.buttons & changed; + u32 changed = ctrlCurrent.buttons ^ ctrlOldButtons; + latch.btnMake |= ctrlCurrent.buttons & changed; latch.btnBreak |= ctrlOldButtons & changed; - latch.btnPress |= ctrl.buttons; - latch.btnRelease |= (ctrlOldButtons & ~ctrl.buttons) & changed; - // TODO: This should really be happening based on the "sampling cycle"... + latch.btnPress |= ctrlCurrent.buttons; + latch.btnRelease |= (ctrlOldButtons & ~ctrlCurrent.buttons) & changed; ctrlLatchBufs++; - ctrlOldButtons = ctrl.buttons; + ctrlOldButtons = ctrlCurrent.buttons; + + // Copy in the current data to the current buffer. + memcpy(&ctrlBufs[ctrlBuf], &ctrlCurrent, sizeof(_ctrl_data)); + + ctrlBufs[ctrlBuf].frame = (u32) (CoreTiming::GetTicks() / CoreTiming::GetClockFrequencyMHz()); + if (!analogEnabled) + { + ctrlBufs[ctrlBuf].analog[0] = 128; + ctrlBufs[ctrlBuf].analog[1] = 128; + } + + ctrlBuf = (ctrlBuf + 1) % 64; + + // If we wrapped around, push the read head forward. + // TODO: Is this right? + if (ctrlBufRead == ctrlBuf) + ctrlBufRead = (ctrlBufRead + 1) % 64; } int __CtrlResetLatch() { - std::lock_guard guard(ctrlMutex); - int oldBufs = ctrlLatchBufs; memset(&latch, 0, sizeof(CtrlLatch)); ctrlLatchBufs = 0; @@ -96,10 +109,9 @@ int __CtrlResetLatch() u32 __CtrlPeekButtons() { - if (!ctrlInited) - sceCtrlInit(); + std::lock_guard guard(ctrlMutex); - return ctrl.buttons; + return ctrlCurrent.buttons; } // Functions so that the rest of the emulator can control what the sceCtrl interface should return @@ -107,55 +119,62 @@ u32 __CtrlPeekButtons() void __CtrlButtonDown(u32 buttonBit) { - if (!ctrlInited) - sceCtrlInit(); - std::lock_guard guard(ctrlMutex); - ctrl.buttons |= buttonBit; - __CtrlUpdateLatch(); + ctrlCurrent.buttons |= buttonBit; } void __CtrlButtonUp(u32 buttonBit) { - if (!ctrlInited) - sceCtrlInit(); - std::lock_guard guard(ctrlMutex); - ctrl.buttons &= ~buttonBit; - __CtrlUpdateLatch(); + ctrlCurrent.buttons &= ~buttonBit; } void __CtrlSetAnalog(float x, float y) { - if (!ctrlInited) - sceCtrlInit(); - std::lock_guard guard(ctrlMutex); // TODO: Circle! if (x > 1.0f) x = 1.0f; if (y > 1.0f) y = 1.0f; if (x < -1.0f) x = -1.0f; if (y < -1.0f) y = -1.0f; - ctrl.analog[0] = (u8)(x * 127.f + 128.f); - ctrl.analog[1] = (u8)(y * 127.f + 128.f); + ctrlCurrent.analog[0] = (u8)(x * 127.f + 128.f); + ctrlCurrent.analog[1] = (u8)(y * 127.f + 128.f); +} +void __CtrlVblank() +{ + // When in vblank sampling mode, this samples the ctrl data into the buffers and updates the latch. __CtrlUpdateLatch(); } -void sceCtrlInit() +void __CtrlInit() { - ctrlInited = true; - std::lock_guard guard(ctrlMutex); + if (!ctrlInited) + { + __DisplayListenVblank(__CtrlVblank); + ctrlInited = true; + } + + ctrlBuf = 0; + ctrlBufRead = 0; + ctrlOldButtons = 0; + ctrlLatchBufs = 0; + memset(&latch, 0, sizeof(latch)); // Start with everything released. latch.btnRelease = 0xffffffff; - memset(&ctrl, 0, sizeof(ctrl)); - ctrl.analog[0] = 128; - ctrl.analog[1] = 128; - ctrl.frame = 1; + memset(&ctrlCurrent, 0, sizeof(ctrlCurrent)); + memset(&ctrlBufs, 0, sizeof(ctrlBufs)); + ctrlCurrent.analog[0] = 128; + ctrlCurrent.analog[1] = 128; +} + +void sceCtrlInit() +{ + __CtrlInit(); DEBUG_LOG(HLE,"sceCtrlInit"); RETURN(0); @@ -163,7 +182,15 @@ void sceCtrlInit() u32 sceCtrlSetSamplingCycle(u32 cycle) { - ERROR_LOG(HLE, "UNIMPL sceCtrlSetSamplingCycle(%u)", cycle); + if (cycle == 0) + { + // TODO: Change to vblank when we support something else. + DEBUG_LOG(HLE, "sceCtrlSetSamplingCycle(%u)", cycle); + } + else + { + ERROR_LOG(HLE, "UNIMPL sceCtrlSetSamplingCycle(%u)", cycle); + } return 0; } @@ -202,83 +229,82 @@ void sceCtrlSetIdleCancelThreshold() RETURN(0); } -u32 sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) +int __CtrlReadBuffer(u32 ctrlDataPtr, u32 nBufs, bool negative, bool peek) { - // TODO: Test rescheduling. - DEBUG_LOG(HLE,"sceCtrlReadBufferPositive(%08x, %i)", ctrlDataPtr, nBufs); - _assert_msg_(HLE, nBufs > 0, "sceCtrlReadBufferPositive: trying to read nothing?"); - - // Pretend we have only 64 of them. if (nBufs > 64) return PSP_CTRL_ERROR_INVALID_NUM_BUFFERS; - if (!ctrlInited) - sceCtrlInit(); + int resetRead = ctrlBufRead; - std::lock_guard guard(ctrlMutex); - - _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); - if (Memory::IsValidAddress(ctrlDataPtr) && ctrlData) + int done = 0; + _ctrl_data data; + for (u32 i = 0; i < nBufs; ++i) { - memcpy(ctrlData, &ctrl, sizeof(_ctrl_data)); + // Ran out of buffers. + if (ctrlBuf == ctrlBufRead) + break; - ctrlData->frame = (u32) (CoreTiming::GetTicks() / CoreTiming::GetClockFrequencyMHz()); - if (!analogEnabled) + if (Memory::IsValidAddress(ctrlDataPtr)) { - ctrlData->analog[0] = 128; - ctrlData->analog[1] = 128; + memcpy(&data, &ctrlBufs[ctrlBufRead], sizeof(_ctrl_data)); + ctrlBufRead = (ctrlBufRead + 1) % 64; + + if (negative) + data.buttons = ~data.buttons; + + Memory::WriteStruct(ctrlDataPtr, &data); + done++; } + ctrlDataPtr += sizeof(_ctrl_data); } - return 1; + if (peek) + ctrlBufRead = resetRead; + + return done; } -u32 sceCtrlReadBufferNegative(u32 ctrlDataPtr, u32 nBufs) +void sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) { - // TODO: Test rescheduling. + // TODO: Wait for vblank if there are 0 buffers (resched.) + DEBUG_LOG(HLE,"sceCtrlReadBufferPositive(%08x, %i)", ctrlDataPtr, nBufs); + + RETURN(__CtrlReadBuffer(ctrlDataPtr, nBufs, false, false)); +} + +void sceCtrlReadBufferNegative(u32 ctrlDataPtr, u32 nBufs) +{ + // TODO: Wait for vblank if there are 0 buffers (resched.) DEBUG_LOG(HLE,"sceCtrlReadBufferNegative(%08x, %i)", ctrlDataPtr, nBufs); - _assert_msg_(HLE, nBufs > 0, "sceCtrlReadBufferNegative: trying to read nothing?"); - if (!ctrlInited) - sceCtrlInit(); + RETURN(__CtrlReadBuffer(ctrlDataPtr, nBufs, true, false)); +} - std::lock_guard guard(ctrlMutex); +int sceCtrlPeekBufferPositive(u32 ctrlDataPtr, u32 nBufs) +{ + DEBUG_LOG(HLE,"sceCtrlPeekBufferPositive(%08x, %i)", ctrlDataPtr, nBufs); + return __CtrlReadBuffer(ctrlDataPtr, nBufs, false, true); +} - _ctrl_data *ctrlData = (_ctrl_data*) Memory::GetPointer(ctrlDataPtr); - if (Memory::IsValidAddress(ctrlDataPtr) && ctrlData) - { - memcpy(ctrlData, &ctrl, sizeof(_ctrl_data)); - - ctrlData->buttons = ~ctrlData->buttons; - ctrlData->frame = (u32) (CoreTiming::GetTicks() / CoreTiming::GetClockFrequencyMHz()); - if (!analogEnabled) - { - ctrlData->analog[0] = 128; - ctrlData->analog[1] = 128; - } - } - - return 1; +int sceCtrlPeekBufferNegative(u32 ctrlDataPtr, u32 nBufs) +{ + DEBUG_LOG(HLE,"sceCtrlPeekBufferNegative(%08x, %i)", ctrlDataPtr, nBufs); + return __CtrlReadBuffer(ctrlDataPtr, nBufs, true, true); } u32 sceCtrlPeekLatch(u32 latchDataPtr) { - ERROR_LOG(HLE,"FAKE sceCtrlPeekLatch(%08x)", latchDataPtr); - - // TODO: We don't really want to do this here, it should be on an interval. - __CtrlUpdateLatch(); + ERROR_LOG(HLE, "sceCtrlPeekLatch(%08x)", latchDataPtr); if (Memory::IsValidAddress(latchDataPtr)) Memory::WriteStruct(latchDataPtr, &latch); + return ctrlLatchBufs; } u32 sceCtrlReadLatch(u32 latchDataPtr) { - ERROR_LOG(HLE,"FAKE sceCtrlReadLatch(%08x)", latchDataPtr); - - // TODO: We don't really want to do this here, it should be on an interval. - __CtrlUpdateLatch(); + ERROR_LOG(HLE, "sceCtrlReadLatch(%08x)", latchDataPtr); if (Memory::IsValidAddress(latchDataPtr)) Memory::WriteStruct(latchDataPtr, &latch); @@ -293,10 +319,10 @@ static const HLEFunction sceCtrl[] = {0x6A2774F3, WrapU_U, "sceCtrlSetSamplingCycle"}, {0x02BAAD91, WrapI_U,"sceCtrlGetSamplingCycle"}, {0xDA6B76A1, WrapI_U, "sceCtrlGetSamplingMode"}, - {0x1f803938, WrapU_UU, "sceCtrlReadBufferPositive"}, //(ctrl_data_t* paddata, int unknown) // unknown should be 1 - {0x3A622550, WrapU_UU, "sceCtrlPeekBufferPositive"}, - {0xC152080A, WrapU_UU, "sceCtrlPeekBufferNegative"}, - {0x60B81F86, WrapU_UU, "sceCtrlReadBufferNegative"}, + {0x1f803938, WrapV_UU, "sceCtrlReadBufferPositive"}, //(ctrl_data_t* paddata, int unknown) // unknown should be 1 + {0x3A622550, WrapI_UU, "sceCtrlPeekBufferPositive"}, + {0xC152080A, WrapI_UU, "sceCtrlPeekBufferNegative"}, + {0x60B81F86, WrapV_UU, "sceCtrlReadBufferNegative"}, {0xB1D0E5CD, WrapU_U, "sceCtrlPeekLatch"}, {0x0B588501, WrapU_U, "sceCtrlReadLatch"}, {0x348D99D4, 0, "sceCtrl_348D99D4"}, diff --git a/Core/HLE/sceCtrl.h b/Core/HLE/sceCtrl.h index 3e02e8feb3..2f658d73c0 100644 --- a/Core/HLE/sceCtrl.h +++ b/Core/HLE/sceCtrl.h @@ -32,6 +32,8 @@ void Register_sceCtrl(); #define CTRL_LTRIGGER 0x0100 #define CTRL_RTRIGGER 0x0200 +void __CtrlInit(); + void __CtrlButtonDown(u32 buttonBit); void __CtrlButtonUp(u32 buttonBit); // -1 to 1, try to keep it in the circle diff --git a/Core/HLE/sceDisplay.cpp b/Core/HLE/sceDisplay.cpp index 94e32b2e7c..5e42caebe4 100644 --- a/Core/HLE/sceDisplay.cpp +++ b/Core/HLE/sceDisplay.cpp @@ -72,6 +72,8 @@ double lastFrameTime = 0; // STATE END +std::vector vblankListeners; + // The vblank period is 731.5 us (0.7315 ms) const double vblankMs = 0.7315; const double frameMs = 1000.0 / 60.0; @@ -116,6 +118,20 @@ void __DisplayShutdown() ShutdownGfxState(); } +void __DisplayListenVblank(VblankCallback callback) +{ + vblankListeners.push_back(callback); +} + +void __DisplayFireVblank() +{ + for (std::vector::iterator iter = vblankListeners.begin(), end = vblankListeners.end(); iter != end; ++iter) + { + VblankCallback cb = *iter; + cb(); + } +} + void hleEnterVblank(u64 userdata, int cyclesLate) { int vbCount = userdata; @@ -124,6 +140,9 @@ void hleEnterVblank(u64 userdata, int cyclesLate) isVblank = 1; + // Fire the vblank listeners before we wake threads. + __DisplayFireVblank(); + // Wake up threads waiting for VBlank __KernelTriggerWait(WAITTYPE_VBLANK, 0, true); diff --git a/Core/HLE/sceDisplay.h b/Core/HLE/sceDisplay.h index bcb43f96ef..003b3164b9 100644 --- a/Core/HLE/sceDisplay.h +++ b/Core/HLE/sceDisplay.h @@ -23,3 +23,6 @@ void Register_sceDisplay(); // will return true once after every end-of-frame. bool __DisplayFrameDone(); + +typedef void (*VblankCallback)(); +void __DisplayListenVblank(VblankCallback callback); diff --git a/Core/HLE/sceKernel.cpp b/Core/HLE/sceKernel.cpp index 55ac175c8f..955c89c771 100644 --- a/Core/HLE/sceKernel.cpp +++ b/Core/HLE/sceKernel.cpp @@ -29,6 +29,7 @@ #include "__sceAudio.h" #include "sceAudio.h" +#include "sceCtrl.h" #include "sceDisplay.h" #include "sceGe.h" #include "sceIo.h" @@ -80,6 +81,7 @@ void __KernelInit() __PowerInit(); __UtilityInit(); __UmdInit(); + __CtrlInit(); // "Internal" PSP libraries __PPGeInit(); From 958fd29593f13ca2d80424fa213710f91d1bc193 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 2 Dec 2012 16:04:50 -0800 Subject: [PATCH 08/14] On Windows, separate analog and digital arrows. This fixes the Tales of Eternia menu and movement. --- Windows/KeyboardDevice.cpp | 48 +++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/Windows/KeyboardDevice.cpp b/Windows/KeyboardDevice.cpp index c69f97b90c..8d3cf30c37 100644 --- a/Windows/KeyboardDevice.cpp +++ b/Windows/KeyboardDevice.cpp @@ -17,30 +17,46 @@ static const unsigned short key_ctrl_map[] = { VK_LEFT, CTRL_LEFT, VK_RIGHT, CTRL_RIGHT, }; + +static const unsigned short analog_ctrl_map[] = { + 'I', CTRL_UP, + 'K', CTRL_DOWN, + 'J', CTRL_LEFT, + 'L', CTRL_RIGHT, +}; + int KeyboardDevice::UpdateState() { - float analogX = 0; - float analogY = 0; for (int i = 0; i < sizeof(key_ctrl_map)/sizeof(key_ctrl_map[0]); i += 2) { if (!GetAsyncKeyState(key_ctrl_map[i])) __CtrlButtonUp(key_ctrl_map[i+1]); else { __CtrlButtonDown(key_ctrl_map[i+1]); - switch (key_ctrl_map[i]) { - case VK_UP: - analogY -= .8f; - break; - case VK_DOWN: - analogY += .8f; - break; - case VK_LEFT: - analogX -= .8f; - break; - case VK_RIGHT: - analogX += .8f; - break; - } } } + + float analogX = 0; + float analogY = 0; + for (int i = 0; i < sizeof(analog_ctrl_map)/sizeof(analog_ctrl_map[0]); i += 2) { + if (!GetAsyncKeyState(analog_ctrl_map[i])) { + continue; + } + + switch (analog_ctrl_map[i + 1]) { + case CTRL_UP: + analogY -= .8f; + break; + case CTRL_DOWN: + analogY += .8f; + break; + case CTRL_LEFT: + analogX -= .8f; + break; + case CTRL_RIGHT: + analogX += .8f; + break; + } + } + __CtrlSetAnalog(analogX, analogY); return 0; } \ No newline at end of file From ec6c0e9ccd28553fd9c208cacf5a57b0eaaff05c Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 2 Dec 2012 16:19:14 -0800 Subject: [PATCH 09/14] The ctrl latch should work now, don't log as error. --- Core/HLE/sceCtrl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index 7eb63da4aa..0b5ca2e071 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -294,7 +294,7 @@ int sceCtrlPeekBufferNegative(u32 ctrlDataPtr, u32 nBufs) u32 sceCtrlPeekLatch(u32 latchDataPtr) { - ERROR_LOG(HLE, "sceCtrlPeekLatch(%08x)", latchDataPtr); + DEBUG_LOG(HLE, "sceCtrlPeekLatch(%08x)", latchDataPtr); if (Memory::IsValidAddress(latchDataPtr)) Memory::WriteStruct(latchDataPtr, &latch); @@ -304,7 +304,7 @@ u32 sceCtrlPeekLatch(u32 latchDataPtr) u32 sceCtrlReadLatch(u32 latchDataPtr) { - ERROR_LOG(HLE, "sceCtrlReadLatch(%08x)", latchDataPtr); + DEBUG_LOG(HLE, "sceCtrlReadLatch(%08x)", latchDataPtr); if (Memory::IsValidAddress(latchDataPtr)) Memory::WriteStruct(latchDataPtr, &latch); From aea9b16a04967b5ae41f7eee4b9515cbf9b08216 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 2 Dec 2012 17:03:13 -0800 Subject: [PATCH 10/14] Add ctrl waits when there are no buffers yet. --- Core/HLE/sceCtrl.cpp | 122 ++++++++++++++++++++++++----------- Core/HLE/sceKernelThread.cpp | 1 + Core/HLE/sceKernelThread.h | 1 + 3 files changed, 87 insertions(+), 37 deletions(-) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index 0b5ca2e071..730719816d 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -21,6 +21,8 @@ #include "StdMutex.h" #include "sceCtrl.h" #include "sceDisplay.h" +#include "sceKernel.h" +#include "sceKernelThread.h" /* Index for the two analog directions */ #define CTRL_ANALOG_X 0 @@ -32,6 +34,12 @@ const int PSP_CTRL_ERROR_INVALID_MODE = 0x80000107; const int PSP_CTRL_ERROR_INVALID_NUM_BUFFERS = 0x80000104; +enum +{ + CTRL_WAIT_POSITIVE = 1, + CTRL_WAIT_NEGATIVE = 2, +}; + // Returned control data struct _ctrl_data { @@ -62,6 +70,7 @@ static int ctrlBuf = 0; static int ctrlBufRead = 0; static CtrlLatch latch; +static std::vector waitingThreads; static std::recursive_mutex ctrlMutex; // STATE END @@ -141,10 +150,70 @@ void __CtrlSetAnalog(float x, float y) ctrlCurrent.analog[1] = (u8)(y * 127.f + 128.f); } +int __CtrlReadSingleBuffer(u32 ctrlDataPtr, bool negative) +{ + _ctrl_data data; + if (Memory::IsValidAddress(ctrlDataPtr)) + { + memcpy(&data, &ctrlBufs[ctrlBufRead], sizeof(_ctrl_data)); + ctrlBufRead = (ctrlBufRead + 1) % 64; + + if (negative) + data.buttons = ~data.buttons; + + Memory::WriteStruct(ctrlDataPtr, &data); + return 1; + } + + return 0; +} + +int __CtrlReadBuffer(u32 ctrlDataPtr, u32 nBufs, bool negative, bool peek) +{ + if (nBufs > 64) + return PSP_CTRL_ERROR_INVALID_NUM_BUFFERS; + + int resetRead = ctrlBufRead; + + int done = 0; + for (u32 i = 0; i < nBufs; ++i) + { + // Ran out of buffers. + if (ctrlBuf == ctrlBufRead) + break; + + done += __CtrlReadSingleBuffer(ctrlDataPtr, negative); + ctrlDataPtr += sizeof(_ctrl_data); + } + + if (peek) + ctrlBufRead = resetRead; + + return done; +} + void __CtrlVblank() { // When in vblank sampling mode, this samples the ctrl data into the buffers and updates the latch. __CtrlUpdateLatch(); + + // Wake up a single thread that was waiting for the buffer. +retry: + if (!waitingThreads.empty() && ctrlBuf != ctrlBufRead) + { + SceUID threadID = waitingThreads[0]; + waitingThreads.erase(waitingThreads.begin()); + + u32 error; + SceUID wVal = __KernelGetWaitID(threadID, WAITTYPE_CTRL, error); + // Make sure it didn't get woken or something. + if (wVal == 0) + goto retry; + + u32 ctrlDataPtr = __KernelGetWaitValue(threadID, error); + int retVal = __CtrlReadSingleBuffer(ctrlDataPtr, wVal == CTRL_WAIT_NEGATIVE); + __KernelResumeThreadFromWait(threadID, retVal); + } } void __CtrlInit() @@ -229,47 +298,19 @@ void sceCtrlSetIdleCancelThreshold() RETURN(0); } -int __CtrlReadBuffer(u32 ctrlDataPtr, u32 nBufs, bool negative, bool peek) -{ - if (nBufs > 64) - return PSP_CTRL_ERROR_INVALID_NUM_BUFFERS; - - int resetRead = ctrlBufRead; - - int done = 0; - _ctrl_data data; - for (u32 i = 0; i < nBufs; ++i) - { - // Ran out of buffers. - if (ctrlBuf == ctrlBufRead) - break; - - if (Memory::IsValidAddress(ctrlDataPtr)) - { - memcpy(&data, &ctrlBufs[ctrlBufRead], sizeof(_ctrl_data)); - ctrlBufRead = (ctrlBufRead + 1) % 64; - - if (negative) - data.buttons = ~data.buttons; - - Memory::WriteStruct(ctrlDataPtr, &data); - done++; - } - ctrlDataPtr += sizeof(_ctrl_data); - } - - if (peek) - ctrlBufRead = resetRead; - - return done; -} - void sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) { // TODO: Wait for vblank if there are 0 buffers (resched.) DEBUG_LOG(HLE,"sceCtrlReadBufferPositive(%08x, %i)", ctrlDataPtr, nBufs); - RETURN(__CtrlReadBuffer(ctrlDataPtr, nBufs, false, false)); + int done = __CtrlReadBuffer(ctrlDataPtr, nBufs, false, false); + if (done != 0) + RETURN(done); + else + { + waitingThreads.push_back(__KernelGetCurThread()); + __KernelWaitCurThread(WAITTYPE_CTRL, CTRL_WAIT_POSITIVE, ctrlDataPtr, 0, false); + } } void sceCtrlReadBufferNegative(u32 ctrlDataPtr, u32 nBufs) @@ -277,7 +318,14 @@ void sceCtrlReadBufferNegative(u32 ctrlDataPtr, u32 nBufs) // TODO: Wait for vblank if there are 0 buffers (resched.) DEBUG_LOG(HLE,"sceCtrlReadBufferNegative(%08x, %i)", ctrlDataPtr, nBufs); - RETURN(__CtrlReadBuffer(ctrlDataPtr, nBufs, true, false)); + int done = __CtrlReadBuffer(ctrlDataPtr, nBufs, true, false); + if (done != 0) + RETURN(done); + else + { + waitingThreads.push_back(__KernelGetCurThread()); + __KernelWaitCurThread(WAITTYPE_CTRL, CTRL_WAIT_NEGATIVE, ctrlDataPtr, 0, false); + } } int sceCtrlPeekBufferPositive(u32 ctrlDataPtr, u32 nBufs) diff --git a/Core/HLE/sceKernelThread.cpp b/Core/HLE/sceKernelThread.cpp index fb623ef17e..d4f2919b64 100644 --- a/Core/HLE/sceKernelThread.cpp +++ b/Core/HLE/sceKernelThread.cpp @@ -73,6 +73,7 @@ const char *waitTypeStrings[] = "Vblank", "Mutex", "LwMutex", + "Ctrl", }; struct SceKernelSysClock { diff --git a/Core/HLE/sceKernelThread.h b/Core/HLE/sceKernelThread.h index 36ec19c244..8a649b6557 100644 --- a/Core/HLE/sceKernelThread.h +++ b/Core/HLE/sceKernelThread.h @@ -70,6 +70,7 @@ enum WaitType //probably not the real values WAITTYPE_VBLANK = 12, // fake WAITTYPE_MUTEX = 13, WAITTYPE_LWMUTEX = 14, + WAITTYPE_CTRL = 15, // Remember to update sceKernelThread.cpp's waitTypeStrings to match. }; From f6009e477f05c877f3965885559bb545702288c9 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 2 Dec 2012 17:04:57 -0800 Subject: [PATCH 11/14] Start with one free buffer to match things. --- Core/HLE/sceCtrl.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index 730719816d..559c75ab11 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -226,7 +226,7 @@ void __CtrlInit() ctrlInited = true; } - ctrlBuf = 0; + ctrlBuf = 1; ctrlBufRead = 0; ctrlOldButtons = 0; ctrlLatchBufs = 0; @@ -239,6 +239,8 @@ void __CtrlInit() memset(&ctrlBufs, 0, sizeof(ctrlBufs)); ctrlCurrent.analog[0] = 128; ctrlCurrent.analog[1] = 128; + ctrlBufs[0].analog[0] = 128; + ctrlBufs[0].analog[1] = 128; } void sceCtrlInit() From 70efb2c3d1d3743cbabbebc811bf8d5423bd5337 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 2 Dec 2012 17:19:57 -0800 Subject: [PATCH 12/14] Test update. --- pspautotests | 2 +- test.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pspautotests b/pspautotests index 3c81649c4b..c725dde370 160000 --- a/pspautotests +++ b/pspautotests @@ -1 +1 @@ -Subproject commit 3c81649c4b4056ddabaca293c093ba0d6989502b +Subproject commit c725dde370fac6cb2776a9e1e0a200e0dab11f1e diff --git a/test.py b/test.py index 1378ed3018..3523fd8074 100755 --- a/test.py +++ b/test.py @@ -47,6 +47,8 @@ tests_good = [ "cpu/lsu/lsu", "cpu/fpu/fpu", + "ctrl/ctrl", + "ctrl/sampling/sampling", "display/display", "dmac/dmactest", "loader/bss/bss", @@ -95,7 +97,6 @@ tests_next = [ "threads/vpl/vpl", "threads/vtimers/vtimer", "threads/wakeup/wakeup", - "ctrl/ctrl", "gpu/simple/simple", "gpu/triangle/triangle", "hle/check_not_used_uids", From 8a801ff761856705b2e72c91a2d709832cfd356d Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 2 Dec 2012 19:53:20 -0800 Subject: [PATCH 13/14] Send the recent N ctrl buffers, fixing lag. --- Core/HLE/sceCtrl.cpp | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index 559c75ab11..cf1370460b 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -175,6 +175,11 @@ int __CtrlReadBuffer(u32 ctrlDataPtr, u32 nBufs, bool negative, bool peek) int resetRead = ctrlBufRead; + u32 availBufs = (ctrlBuf - ctrlBufRead + 64) % 64; + if (availBufs > nBufs) + availBufs = nBufs; + ctrlBufRead = (ctrlBuf - availBufs + 64) % 64; + int done = 0; for (u32 i = 0; i < nBufs; ++i) { @@ -296,50 +301,54 @@ int sceCtrlGetSamplingMode(u32 modePtr) void sceCtrlSetIdleCancelThreshold() { - DEBUG_LOG(HLE,"UNIMPL sceCtrlSetIdleCancelThreshold"); + ERROR_LOG(HLE,"UNIMPL sceCtrlSetIdleCancelThreshold"); RETURN(0); } void sceCtrlReadBufferPositive(u32 ctrlDataPtr, u32 nBufs) { - // TODO: Wait for vblank if there are 0 buffers (resched.) - DEBUG_LOG(HLE,"sceCtrlReadBufferPositive(%08x, %i)", ctrlDataPtr, nBufs); - int done = __CtrlReadBuffer(ctrlDataPtr, nBufs, false, false); if (done != 0) + { RETURN(done); + DEBUG_LOG(HLE, "%d=sceCtrlReadBufferPositive(%08x, %i)", done, ctrlDataPtr, nBufs); + } else { waitingThreads.push_back(__KernelGetCurThread()); __KernelWaitCurThread(WAITTYPE_CTRL, CTRL_WAIT_POSITIVE, ctrlDataPtr, 0, false); + DEBUG_LOG(HLE, "sceCtrlReadBufferPositive(%08x, %i) - waiting", ctrlDataPtr, nBufs); } } void sceCtrlReadBufferNegative(u32 ctrlDataPtr, u32 nBufs) { - // TODO: Wait for vblank if there are 0 buffers (resched.) - DEBUG_LOG(HLE,"sceCtrlReadBufferNegative(%08x, %i)", ctrlDataPtr, nBufs); - int done = __CtrlReadBuffer(ctrlDataPtr, nBufs, true, false); if (done != 0) + { RETURN(done); + DEBUG_LOG(HLE, "%d=sceCtrlReadBufferNegative(%08x, %i)", done, ctrlDataPtr, nBufs); + } else { waitingThreads.push_back(__KernelGetCurThread()); __KernelWaitCurThread(WAITTYPE_CTRL, CTRL_WAIT_NEGATIVE, ctrlDataPtr, 0, false); + DEBUG_LOG(HLE, "sceCtrlReadBufferNegative(%08x, %i) - waiting", ctrlDataPtr, nBufs); } } int sceCtrlPeekBufferPositive(u32 ctrlDataPtr, u32 nBufs) { - DEBUG_LOG(HLE,"sceCtrlPeekBufferPositive(%08x, %i)", ctrlDataPtr, nBufs); - return __CtrlReadBuffer(ctrlDataPtr, nBufs, false, true); + int done = __CtrlReadBuffer(ctrlDataPtr, nBufs, false, true); + DEBUG_LOG(HLE, "%d=sceCtrlPeekBufferPositive(%08x, %i)", done, ctrlDataPtr, nBufs); + return done; } int sceCtrlPeekBufferNegative(u32 ctrlDataPtr, u32 nBufs) { - DEBUG_LOG(HLE,"sceCtrlPeekBufferNegative(%08x, %i)", ctrlDataPtr, nBufs); - return __CtrlReadBuffer(ctrlDataPtr, nBufs, true, true); + int done = __CtrlReadBuffer(ctrlDataPtr, nBufs, true, true); + DEBUG_LOG(HLE, "%d=sceCtrlPeekBufferNegative(%08x, %i)", done, ctrlDataPtr, nBufs); + return done; } u32 sceCtrlPeekLatch(u32 latchDataPtr) From d07e51bff022c4e2fb3349e00ce06b787662325b Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 2 Dec 2012 21:07:17 -0800 Subject: [PATCH 14/14] Always allow peeking into ctrl buffers. --- Core/HLE/sceCtrl.cpp | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index cf1370460b..73e03def9d 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -34,6 +34,8 @@ const int PSP_CTRL_ERROR_INVALID_MODE = 0x80000107; const int PSP_CTRL_ERROR_INVALID_NUM_BUFFERS = 0x80000104; +const int NUM_CTRL_BUFFERS = 64; + enum { CTRL_WAIT_POSITIVE = 1, @@ -64,7 +66,7 @@ static bool analogEnabled = false; static int ctrlLatchBufs = 0; static u32 ctrlOldButtons = 0; -static _ctrl_data ctrlBufs[64]; +static _ctrl_data ctrlBufs[NUM_CTRL_BUFFERS]; static _ctrl_data ctrlCurrent; static int ctrlBuf = 0; static int ctrlBufRead = 0; @@ -100,12 +102,12 @@ void __CtrlUpdateLatch() ctrlBufs[ctrlBuf].analog[1] = 128; } - ctrlBuf = (ctrlBuf + 1) % 64; + ctrlBuf = (ctrlBuf + 1) % NUM_CTRL_BUFFERS; // If we wrapped around, push the read head forward. // TODO: Is this right? if (ctrlBufRead == ctrlBuf) - ctrlBufRead = (ctrlBufRead + 1) % 64; + ctrlBufRead = (ctrlBufRead + 1) % NUM_CTRL_BUFFERS; } int __CtrlResetLatch() @@ -156,7 +158,7 @@ int __CtrlReadSingleBuffer(u32 ctrlDataPtr, bool negative) if (Memory::IsValidAddress(ctrlDataPtr)) { memcpy(&data, &ctrlBufs[ctrlBufRead], sizeof(_ctrl_data)); - ctrlBufRead = (ctrlBufRead + 1) % 64; + ctrlBufRead = (ctrlBufRead + 1) % NUM_CTRL_BUFFERS; if (negative) data.buttons = ~data.buttons; @@ -170,23 +172,26 @@ int __CtrlReadSingleBuffer(u32 ctrlDataPtr, bool negative) int __CtrlReadBuffer(u32 ctrlDataPtr, u32 nBufs, bool negative, bool peek) { - if (nBufs > 64) + if (nBufs > NUM_CTRL_BUFFERS) return PSP_CTRL_ERROR_INVALID_NUM_BUFFERS; int resetRead = ctrlBufRead; - u32 availBufs = (ctrlBuf - ctrlBufRead + 64) % 64; - if (availBufs > nBufs) + u32 availBufs; + // Peeks always work, they just go go from now X buffers. + if (peek) availBufs = nBufs; - ctrlBufRead = (ctrlBuf - availBufs + 64) % 64; + else + { + availBufs = (ctrlBuf - ctrlBufRead + NUM_CTRL_BUFFERS) % NUM_CTRL_BUFFERS; + if (availBufs > nBufs) + availBufs = nBufs; + } + ctrlBufRead = (ctrlBuf - availBufs + NUM_CTRL_BUFFERS) % NUM_CTRL_BUFFERS; int done = 0; - for (u32 i = 0; i < nBufs; ++i) + for (u32 i = 0; i < availBufs; ++i) { - // Ran out of buffers. - if (ctrlBuf == ctrlBufRead) - break; - done += __CtrlReadSingleBuffer(ctrlDataPtr, negative); ctrlDataPtr += sizeof(_ctrl_data); } @@ -241,11 +246,11 @@ void __CtrlInit() latch.btnRelease = 0xffffffff; memset(&ctrlCurrent, 0, sizeof(ctrlCurrent)); - memset(&ctrlBufs, 0, sizeof(ctrlBufs)); ctrlCurrent.analog[0] = 128; ctrlCurrent.analog[1] = 128; - ctrlBufs[0].analog[0] = 128; - ctrlBufs[0].analog[1] = 128; + + for (int i = 0; i < NUM_CTRL_BUFFERS; i++) + memcpy(&ctrlBufs[i], &ctrlCurrent, sizeof(_ctrl_data)); } void sceCtrlInit()