finally ok?

This commit is contained in:
CrazyRedMachine 2023-07-21 00:19:29 +02:00
parent 64aef456fe
commit 1e999ce1e5

View File

@ -1912,7 +1912,6 @@ static bool patch_add_to_base_offset(int8_t delta) {
bool g_enhanced_poll_ready = false;
int (*usbPadRead)(uint32_t*);
int (*usbPadReadLast)(unsigned char*);
#if POLLING_DEBUG >=1
FILE *debug_fp;
@ -1929,11 +1928,7 @@ static unsigned int __stdcall enhanced_polling_proc(void *ctx)
HMODULE hinstLib = GetModuleHandleA("ezusb.dll");
usbPadRead = (int(*)(uint32_t*))GetProcAddress(hinstLib, "?usbPadRead@@YAHPAK@Z");
static uint8_t button_debounce[9] = {0};
/*
usbPadReadLast = (int(*)(unsigned char*)) GetProcAddress(hinstLib, "?usbPadReadLast@@YAHPAE@Z");
uint8_t history[40] = {0};
*/
for (int i=0; i<9; i++)
{
g_button_state[i] = -1;
@ -1944,35 +1939,37 @@ static unsigned int __stdcall enhanced_polling_proc(void *ctx)
Sleep(500);
}
uint32_t curr_time = 0;
uint32_t end_time = 0;
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
#if POLLING_DEBUG >= 1
fprintf(debug_fp, "Thread priority is 0x%x\n", GetThreadPriority(GetCurrentThread()));
uint32_t count = 0;
uint32_t count_time = timeGetTime();
uint32_t count_time = 0;
#endif
uint32_t curr_poll_time = 0;
uint32_t prev_poll_time = 0;
while (g_enhanced_poll_ready)
{
curr_time = timeGetTime();
#if POLLING_DEBUG >= 1
if (count == 0)
{
count_time = curr_time;
count_time = timeGetTime();
}
#endif
uint32_t pad_bits;
//usbPadReadLast(history);
#if POLLING_DEBUG == 2
fprintf(stderr, "history : ");
for (int i = 0; i<40; i++)
/* ensure at least 1ms has elapsed between polls
* (beware of SD cab hardware compatibility)
*/
curr_poll_time = timeGetTime();
if (curr_poll_time == prev_poll_time)
{
fprintf(stderr, "%02x ", history[i]);
if (!((i+1)%16)) fprintf(stderr, "\n");
curr_poll_time++;
Sleep(1);
}
fprintf(stderr, "--------------\n");
#endif
int ret = usbPadRead(&pad_bits);
if (ret == 0)
prev_poll_time = curr_poll_time;
usbPadRead(&pad_bits);
g_last_button_state = pad_bits;
unsigned int buttonState = (g_last_button_state >> 8) & 0x1FF;
@ -1982,9 +1979,10 @@ static unsigned int __stdcall enhanced_polling_proc(void *ctx)
{
if (g_button_state[i] == -1)
{
g_button_state[i] = curr_time;
g_button_state[i] = curr_poll_time;
}
button_debounce[i] = g_debounce; //debounce on release (since we're forced to stub usbPadReadLast)
//debounce on release (since we're forced to stub usbPadReadLast)
button_debounce[i] = g_debounce;
}
else
{
@ -1999,26 +1997,20 @@ static unsigned int __stdcall enhanced_polling_proc(void *ctx)
}
else
{
g_last_button_state |= 1<<(8+i); //debounce period still running: button is still pressed
//debounce ongoing: flag button as still pressed
g_last_button_state |= 1<<(8+i);
}
}
}
end_time = timeGetTime();
#if POLLING_DEBUG >= 1
count++;
if (count == 100000)
{
fprintf(debug_fp, "did 100000 iterations in %u milliseconds\n", end_time - count_time);
fprintf(debug_fp, "did 100000 iterations in %ld milliseconds\n", timeGetTime() - count_time);
count = 0;
}
fflush(debug_fp);
#endif
/* wait until a millisecond has elapsed */
while(end_time == curr_time) {
Sleep(0);
end_time = timeGetTime();
}
}
return 0;
}
@ -2033,8 +2025,8 @@ uint32_t buttonGetMillis(uint8_t button)
if (but <= curr)
{
uint32_t res = curr - but;
#if DEBUG >= 2
/*enabling debug here will mess up timing eval */
#if POLLING_DEBUG >= 2
/* these fprintf will mess up timing eval */
fprintf(debug_fp, "%ld: buttonGetMillis: button %d : %d millis\n", timeGetTime(), button, res);
fflush(debug_fp);
#endif
@ -2047,11 +2039,12 @@ uint32_t buttonGetMillis(uint8_t button)
uint32_t usbPadReadHook_addr = 0;
int usbPadReadHook(uint32_t *pad_bits)
{
g_enhanced_poll_ready = true; // ioboard is ready, game can now poll inputs like crazy
// if we're here then ioboard is ready
g_enhanced_poll_ready = true;
/* return last known input */
// return last known input
*pad_bits = g_last_button_state;
#if DEBUG >= 2
#if POLLING_DEBUG >= 2
fprintf(debug_fp, "%ld: usbPadReadHook: %x\n", timeGetTime(), *pad_bits);
fflush(debug_fp);
#endif
@ -2064,7 +2057,7 @@ void (*real_enhanced_poll)();
void patch_enhanced_poll() {
/* eax contains button being checked [0-8]
* esi contains delta about to be evaluated
* we need to do esi -= pressed_since[%eax]; to fix the offset accurately */
* we need to do esi -= buttonGetMillis([%eax]); to fix the offset accurately */
__asm("nop\n");
__asm("nop\n");
__asm("mov %0, al\n":"=m"(g_poll_index): :);
@ -2128,7 +2121,7 @@ static bool patch_enhanced_polling(uint8_t debounce)
uint64_t patch_addr = (int64_t)data + pattern_offset - 0x04; // usbPadRead function address
patch_memory(patch_addr, (char*)&as_int, 4);
// don't call usbPadReadLast as it messes with the 1000Hz polling, we're running our own debouncing instead now
// don't call usbPadReadLast as it messes with the 1000Hz polling, we'll be running our own debouncing instead
patch_addr = (int64_t)data + pattern_offset - 20;
patch_memory(patch_addr, (char*)"\x90\x90\x90\x90\x90\x90", 6);
@ -2151,8 +2144,6 @@ static bool patch_keysound_offset(int8_t value)
patch_add_to_base_offset(value);
{
/* ou first occ of C6 44 24 0C 00 E8 , - 0x07 au lieu de +, so it works on eclale too? */
//int64_t pattern_offset = search(data, dllSize, "\x51\x53\x56\x57\x0f\xb7\xf8\x8b\x34\xfd", 10, 0);
int64_t pattern_offset = search(data, dllSize, "\xC6\x44\x24\x0C\x00\xE8", 6, 0);
if (pattern_offset == -1) {
LOG("popnhax: keysound offset: cannot prepatch\n");