diff --git a/src/Platform.h b/src/Platform.h index b6effdc4..144abef2 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -67,6 +67,7 @@ inline bool LocalFileExists(const char* name) void* Thread_Create(void (*func)()); void Thread_Free(void* thread); void Thread_Wait(void* thread); +void Thread_Sleep(u32 ms); void* Semaphore_Create(); void Semaphore_Free(void* sema); @@ -74,6 +75,9 @@ void Semaphore_Reset(void* sema); void Semaphore_Wait(void* sema); void Semaphore_Post(void* sema); +u64 Perf_GetFrequency(); +u64 Perf_GetCounter(); + void* GL_GetProcAddress(const char* proc); // local multiplayer comm interface diff --git a/src/Wifi.cpp b/src/Wifi.cpp index ab827244..81c79f14 100644 --- a/src/Wifi.cpp +++ b/src/Wifi.cpp @@ -84,6 +84,12 @@ int MPNumReplies; bool MPInited; bool LANInited; +// TEST ZONE +void* WifiThread; +bool WifiThreadRunning; +void WifiThreadFunc(); +bool WifiOn; + // multiplayer host TX sequence: @@ -129,6 +135,11 @@ bool Init() WifiAP::Init(); + // HAX!!!!! + WifiThreadRunning = true; + WifiOn = false; + WifiThread = Platform::Thread_Create(WifiThreadFunc); + return true; } @@ -140,10 +151,16 @@ void DeInit() Platform::LAN_DeInit(); WifiAP::DeInit(); + + WifiThreadRunning = false; + Platform::Thread_Wait(WifiThread); + Platform::Thread_Free(WifiThread); } void Reset() { + WifiOn = false; + memset(RAM, 0, 0x2000); memset(IO, 0, 0x1000); @@ -1103,7 +1120,43 @@ void USTimer(u32 param) // TODO: make it more accurate, eventually // in the DS, the wifi system has its own 22MHz clock and doesn't use the system clock - NDS::ScheduleEvent(NDS::Event_Wifi, true, 33, USTimer, 0); + //NDS::ScheduleEvent(NDS::Event_Wifi, true, 33, USTimer, 0); +} +extern "C" int SDL_SetThreadPriority(int prio); +void WifiThreadFunc() +{ + SDL_SetThreadPriority(2); + + u64 perffreq = Platform::Perf_GetFrequency(); + float ustime = perffreq / 1000000.0f; + u64 perflast = Platform::Perf_GetCounter(); + printf("wifi: perf=%lld ustime=%f\n", perffreq, ustime); + + float error = 0; + + while (WifiThreadRunning) + { + if (!WifiOn) + { + Platform::Thread_Sleep(17); + continue; + } + + USTimer(0); + + // TODO better timing control!! + for (;;) + { + u64 perfcur = Platform::Perf_GetCounter(); + u64 time = perfcur - perflast; + u64 target = (u64)ustime; + if (time >= target) + { + perflast = perfcur; + break; + } + } + } } @@ -1350,7 +1403,8 @@ void Write(u32 addr, u16 val) if ((IOPORT(W_PowerUS) & 0x0001) && !(val & 0x0001)) { printf("WIFI ON\n"); - NDS::ScheduleEvent(NDS::Event_Wifi, false, 33, USTimer, 0); + //NDS::ScheduleEvent(NDS::Event_Wifi, false, 33, USTimer, 0); + WifiOn = true; if (!MPInited) { Platform::MP_Init(); @@ -1365,7 +1419,8 @@ void Write(u32 addr, u16 val) else if (!(IOPORT(W_PowerUS) & 0x0001) && (val & 0x0001)) { printf("WIFI OFF\n"); - NDS::CancelEvent(NDS::Event_Wifi); + //NDS::CancelEvent(NDS::Event_Wifi); + WifiOn = false; } break; diff --git a/src/libui_sdl/Platform.cpp b/src/libui_sdl/Platform.cpp index 54fa568a..1f4388cf 100644 --- a/src/libui_sdl/Platform.cpp +++ b/src/libui_sdl/Platform.cpp @@ -343,6 +343,11 @@ void Thread_Wait(void* thread) SDL_WaitThread((SDL_Thread*)((ThreadData*)thread)->ID, NULL); } +void Thread_Sleep(u32 ms) +{ + SDL_Delay(ms); +} + void* Semaphore_Create() { @@ -370,6 +375,17 @@ void Semaphore_Post(void* sema) } +u64 Perf_GetFrequency() +{ + return SDL_GetPerformanceFrequency(); +} + +u64 Perf_GetCounter() +{ + return SDL_GetPerformanceCounter(); +} + + void* GL_GetProcAddress(const char* proc) { return uiGLGetProcAddress(proc);