From ff5af758625473ec6fb57a3dcc264fe48cd6d898 Mon Sep 17 00:00:00 2001 From: Kirill Zhumarin Date: Wed, 29 Nov 2023 04:23:11 +0300 Subject: [PATCH] fix timers --- src/swi.cpp | 2 +- src/swi.h | 5 ++++- src/swi/gbs.cpp | 25 +++++++++++++++++++------ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/swi.cpp b/src/swi.cpp index 91e2ecc..5642580 100644 --- a/src/swi.cpp +++ b/src/swi.cpp @@ -214,7 +214,7 @@ void loader_init_switab() { switab_functions[0x004D] = (void *) GBS_StartTimerProc; switab_functions[0x0143] = (void *) GBS_StartTimer; switab_functions[0x0144] = (void *) GBS_StopTimer; - switab_functions[0x01A6] = (void *) GBS_IsTimerRunning; + switab_functions[0x01A6] = (void *) GBS_IsTimerProcessing; switab_functions[0x0105] = (void *) GBS_CreateProc; switab_functions[0x0145] = (void *) GBS_GetCurCepid; switab_functions[0x0154] = (void *) GBS_KillProc; diff --git a/src/swi.h b/src/swi.h index 30998f3..3556574 100644 --- a/src/swi.h +++ b/src/swi.h @@ -287,6 +287,9 @@ typedef struct { void *data1; } GBS_MSG; +#define GBS_TMR_CREATED 0x74696d72 +#define GBS_TMR_DELETED 0x756e6466 + typedef struct { int param0; int param1; @@ -303,7 +306,7 @@ typedef void (*GbsTimerCallback)(GBSTMR *); void GBS_StartTimerProc(GBSTMR *tmr, long ms, GbsTimerCallback callback); void GBS_StartTimer(GBSTMR *tmr, int ms, int msg, int unk, int cepid); void GBS_StopTimer(GBSTMR *tmr); -int GBS_IsTimerRunning(GBSTMR * param1); +int GBS_IsTimerProcessing(GBSTMR * param1); void GBS_RunInContext(int cepid, std::function callback); void GBS_CreateProc(int cepid, const char *name, GbsProcCallback on_msg, int prio, int unk_zero); int GBS_GetCurCepid(void); diff --git a/src/swi/gbs.cpp b/src/swi/gbs.cpp index 4954fa9..4075452 100644 --- a/src/swi/gbs.cpp +++ b/src/swi/gbs.cpp @@ -50,6 +50,8 @@ void GBS_StartTimerProc(GBSTMR *tmr, long ticks, GbsTimerCallback callback) { assert(tmr != nullptr); assert(GBS_GetCurCepid() != -1); + GBS_DelTimer(tmr); + GbsTimerData *tmr_data = new GbsTimerData; tmr_data->callback = callback; tmr_data->cepid = GBS_GetCurCepid(); @@ -61,10 +63,14 @@ void GBS_StartTimerProc(GBSTMR *tmr, long ticks, GbsTimerCallback callback) { }, (ticks * 1000) / 216, false); tmr->param0 = reinterpret_cast(tmr_data); + tmr->param1 = GBS_TMR_CREATED; } void GBS_StartTimer(GBSTMR *tmr, int ticks, int msg, int unk, int cepid) { assert(tmr != nullptr); + + GBS_DelTimer(tmr); + GbsTimerData *tmr_data = new GbsTimerData; tmr_data->callback = nullptr; tmr_data->cepid = cepid; @@ -76,24 +82,31 @@ void GBS_StartTimer(GBSTMR *tmr, int ticks, int msg, int unk, int cepid) { }, (ticks * 1000) / 216, false); tmr->param0 = reinterpret_cast(tmr_data); + tmr->param1 = GBS_TMR_CREATED; } void GBS_StopTimer(GBSTMR *tmr) { - GBS_DelTimer(tmr); + if (GBS_IsTimerProcessing(tmr)) { + auto *tmr_data = reinterpret_cast(tmr->param0); + if (tmr_data->timer_id != -1) { + Loop::instance()->removeTimer(tmr_data->timer_id); + tmr_data->timer_id = -1; + } + } } -int GBS_IsTimerRunning(GBSTMR *tmr) { - assert(tmr != nullptr); - return tmr->param0 != 0; +int GBS_IsTimerProcessing(GBSTMR *tmr) { + return tmr->param1 == GBS_TMR_CREATED && tmr->param0 != 0; } void GBS_DelTimer(GBSTMR *tmr) { - auto *tmr_data = reinterpret_cast(tmr->param0); - if (tmr_data) { + if (GBS_IsTimerProcessing(tmr)) { + auto *tmr_data = reinterpret_cast(tmr->param0); if (tmr_data->timer_id != -1) Loop::instance()->removeTimer(tmr_data->timer_id); delete tmr_data; tmr->param0 = 0; + tmr->param1 = GBS_TMR_DELETED; } }