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; bool g_enhanced_poll_ready = false;
int (*usbPadRead)(uint32_t*); int (*usbPadRead)(uint32_t*);
int (*usbPadReadLast)(unsigned char*);
#if POLLING_DEBUG >=1 #if POLLING_DEBUG >=1
FILE *debug_fp; FILE *debug_fp;
@ -1929,11 +1928,7 @@ static unsigned int __stdcall enhanced_polling_proc(void *ctx)
HMODULE hinstLib = GetModuleHandleA("ezusb.dll"); HMODULE hinstLib = GetModuleHandleA("ezusb.dll");
usbPadRead = (int(*)(uint32_t*))GetProcAddress(hinstLib, "?usbPadRead@@YAHPAK@Z"); usbPadRead = (int(*)(uint32_t*))GetProcAddress(hinstLib, "?usbPadRead@@YAHPAK@Z");
static uint8_t button_debounce[9] = {0}; 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++) for (int i=0; i<9; i++)
{ {
g_button_state[i] = -1; g_button_state[i] = -1;
@ -1944,35 +1939,37 @@ static unsigned int __stdcall enhanced_polling_proc(void *ctx)
Sleep(500); Sleep(500);
} }
uint32_t curr_time = 0; SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
uint32_t end_time = 0;
#if POLLING_DEBUG >= 1 #if POLLING_DEBUG >= 1
fprintf(debug_fp, "Thread priority is 0x%x\n", GetThreadPriority(GetCurrentThread()));
uint32_t count = 0; uint32_t count = 0;
uint32_t count_time = timeGetTime(); uint32_t count_time = 0;
#endif #endif
uint32_t curr_poll_time = 0;
uint32_t prev_poll_time = 0;
while (g_enhanced_poll_ready) while (g_enhanced_poll_ready)
{ {
curr_time = timeGetTime();
#if POLLING_DEBUG >= 1 #if POLLING_DEBUG >= 1
if (count == 0) if (count == 0)
{ {
count_time = curr_time; count_time = timeGetTime();
} }
#endif #endif
uint32_t pad_bits; uint32_t pad_bits;
//usbPadReadLast(history); /* ensure at least 1ms has elapsed between polls
#if POLLING_DEBUG == 2 * (beware of SD cab hardware compatibility)
fprintf(stderr, "history : "); */
for (int i = 0; i<40; i++) curr_poll_time = timeGetTime();
if (curr_poll_time == prev_poll_time)
{ {
fprintf(stderr, "%02x ", history[i]); curr_poll_time++;
if (!((i+1)%16)) fprintf(stderr, "\n"); Sleep(1);
} }
fprintf(stderr, "--------------\n"); prev_poll_time = curr_poll_time;
#endif
int ret = usbPadRead(&pad_bits); usbPadRead(&pad_bits);
if (ret == 0)
g_last_button_state = pad_bits; g_last_button_state = pad_bits;
unsigned int buttonState = (g_last_button_state >> 8) & 0x1FF; 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) 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 else
{ {
@ -1999,26 +1997,20 @@ static unsigned int __stdcall enhanced_polling_proc(void *ctx)
} }
else 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 #if POLLING_DEBUG >= 1
count++; count++;
if (count == 100000) 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; count = 0;
} }
fflush(debug_fp); fflush(debug_fp);
#endif #endif
/* wait until a millisecond has elapsed */
while(end_time == curr_time) {
Sleep(0);
end_time = timeGetTime();
}
} }
return 0; return 0;
} }
@ -2033,8 +2025,8 @@ uint32_t buttonGetMillis(uint8_t button)
if (but <= curr) if (but <= curr)
{ {
uint32_t res = curr - but; uint32_t res = curr - but;
#if DEBUG >= 2 #if POLLING_DEBUG >= 2
/*enabling debug here will mess up timing eval */ /* these fprintf will mess up timing eval */
fprintf(debug_fp, "%ld: buttonGetMillis: button %d : %d millis\n", timeGetTime(), button, res); fprintf(debug_fp, "%ld: buttonGetMillis: button %d : %d millis\n", timeGetTime(), button, res);
fflush(debug_fp); fflush(debug_fp);
#endif #endif
@ -2047,11 +2039,12 @@ uint32_t buttonGetMillis(uint8_t button)
uint32_t usbPadReadHook_addr = 0; uint32_t usbPadReadHook_addr = 0;
int usbPadReadHook(uint32_t *pad_bits) 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; *pad_bits = g_last_button_state;
#if DEBUG >= 2 #if POLLING_DEBUG >= 2
fprintf(debug_fp, "%ld: usbPadReadHook: %x\n", timeGetTime(), *pad_bits); fprintf(debug_fp, "%ld: usbPadReadHook: %x\n", timeGetTime(), *pad_bits);
fflush(debug_fp); fflush(debug_fp);
#endif #endif
@ -2064,7 +2057,7 @@ void (*real_enhanced_poll)();
void patch_enhanced_poll() { void patch_enhanced_poll() {
/* eax contains button being checked [0-8] /* eax contains button being checked [0-8]
* esi contains delta about to be evaluated * 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("nop\n"); __asm("nop\n");
__asm("mov %0, al\n":"=m"(g_poll_index): :); __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 uint64_t patch_addr = (int64_t)data + pattern_offset - 0x04; // usbPadRead function address
patch_memory(patch_addr, (char*)&as_int, 4); 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_addr = (int64_t)data + pattern_offset - 20;
patch_memory(patch_addr, (char*)"\x90\x90\x90\x90\x90\x90", 6); 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); 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); int64_t pattern_offset = search(data, dllSize, "\xC6\x44\x24\x0C\x00\xE8", 6, 0);
if (pattern_offset == -1) { if (pattern_offset == -1) {
LOG("popnhax: keysound offset: cannot prepatch\n"); LOG("popnhax: keysound offset: cannot prepatch\n");