From 7fc434b709b68738efdc14b037632ac4b24c44ea Mon Sep 17 00:00:00 2001 From: WU Lang <55074733+lwu309@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:00:00 +0000 Subject: [PATCH 1/5] Wifi: fix MP CMD reply timeout --- src/Wifi.cpp | 30 ++++++++++++++++++++++-------- src/Wifi.h | 1 + 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Wifi.cpp b/src/Wifi.cpp index 89bdb0285f..5cbbe8251a 100644 --- a/src/Wifi.cpp +++ b/src/Wifi.cpp @@ -224,6 +224,7 @@ void Wifi::Reset() MPReplyTimer = 0; MPClientMask = 0; MPClientFail = 0; + MPCmdTimestamp = 0; memset(MPClientReplies, 0, sizeof(MPClientReplies)); MPLastSeqno = 0xFFFF; @@ -302,6 +303,7 @@ void Wifi::DoSavestate(Savestate* file) file->Var32((u32*)&MPReplyTimer); file->Var16(&MPClientMask); file->Var16(&MPClientFail); + file->Var64(&MPCmdTimestamp); file->VarArray(MPClientReplies, sizeof(MPClientReplies)); @@ -955,7 +957,23 @@ bool Wifi::ProcessTX(TXSlot* slot, int num) else if (slot->CurPhase == 2) { MPReplyTimer -= kTimerInterval; - if (MPReplyTimer <= 0 && MPClientMask != 0) + + // Non-blocking reply collection. The command timestamp is + // fixed at the CMD send time; using the current USTimestamp + // here would make normal replies look stale as the window + // advances. + bool first_replied = false; + if (MPClientMask) + { + const u16 res = Platform::MP_RecvReplies(MPClientReplies, MPCmdTimestamp, MPClientMask, NDS.UserData); + MPClientFail &= ~res; + + int nclient = 1; + while (!(MPClientMask & (1 << nclient))) nclient++; + first_replied = (MPClientFail & (1 << nclient)) == 0; + } + + if ((MPReplyTimer <= 0 || first_replied) && MPClientMask != 0) { int nclient = 1; while (!(MPClientMask & (1 << nclient))) nclient++; @@ -1057,18 +1075,14 @@ bool Wifi::ProcessTX(TXSlot* slot, int num) } SetStatus(5); - MPReplyTimer = 16 + PreambleLen(slot->Rate); - - u16 res = 0; - if (MPClientMask) - res = Platform::MP_RecvReplies(MPClientReplies, USTimestamp, MPClientMask, NDS.UserData); - MPClientFail &= ~res; - // TODO: 112 likely includes the ack preamble, which needs adjusted // for long-preamble settings slot->CurPhase = 2; slot->CurPhaseTime = 112 + ((10 + IOPORT(W_CmdReplyTime)) * NumClients(MPClientMask)); + MPReplyTimer = 16 + 10 + IOPORT(W_CmdReplyTime); + MPCmdTimestamp = USTimestamp; + break; } else if (num == 5) diff --git a/src/Wifi.h b/src/Wifi.h index 61d753548e..d8fae9edbf 100644 --- a/src/Wifi.h +++ b/src/Wifi.h @@ -236,6 +236,7 @@ class Wifi int MPReplyTimer; u16 MPClientMask, MPClientFail; + u64 MPCmdTimestamp = 0; // Wi-Fi time of the current CMD, for reply staleness u8 MPClientReplies[15*1024]; From f9b4ee14896f1f0097b1e0bcc3a510ffaad48334 Mon Sep 17 00:00:00 2001 From: WU Lang <55074733+lwu309@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:00:00 +0000 Subject: [PATCH 2/5] Wifi: poll MP replies without blocking --- src/Platform.h | 2 +- src/Wifi.cpp | 2 +- src/frontend/qt_sdl/Platform.cpp | 4 ++-- src/net/LAN.cpp | 8 ++++---- src/net/LAN.h | 4 ++-- src/net/LocalMP.cpp | 4 ++-- src/net/LocalMP.h | 2 +- src/net/MPInterface.cpp | 2 +- src/net/MPInterface.h | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Platform.h b/src/Platform.h index 4341f6d621..dad2f0dbe6 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -304,7 +304,7 @@ int MP_SendCmd(u8* data, int len, u64 timestamp, void* userdata); int MP_SendReply(u8* data, int len, u64 timestamp, u16 aid, void* userdata); int MP_SendAck(u8* data, int len, u64 timestamp, void* userdata); int MP_RecvHostPacket(u8* data, u64* timestamp, void* userdata); -u16 MP_RecvReplies(u8* data, u64 timestamp, u16 aidmask, void* userdata); +u16 MP_RecvReplies(u8* data, u64 timestamp, u16 aidmask, bool block, void* userdata); // network comm interface diff --git a/src/Wifi.cpp b/src/Wifi.cpp index 5cbbe8251a..abac62e1fa 100644 --- a/src/Wifi.cpp +++ b/src/Wifi.cpp @@ -965,7 +965,7 @@ bool Wifi::ProcessTX(TXSlot* slot, int num) bool first_replied = false; if (MPClientMask) { - const u16 res = Platform::MP_RecvReplies(MPClientReplies, MPCmdTimestamp, MPClientMask, NDS.UserData); + const u16 res = Platform::MP_RecvReplies(MPClientReplies, MPCmdTimestamp, MPClientMask, false, NDS.UserData); MPClientFail &= ~res; int nclient = 1; diff --git a/src/frontend/qt_sdl/Platform.cpp b/src/frontend/qt_sdl/Platform.cpp index dda7e7865c..fccc347d2b 100644 --- a/src/frontend/qt_sdl/Platform.cpp +++ b/src/frontend/qt_sdl/Platform.cpp @@ -505,10 +505,10 @@ int MP_RecvHostPacket(u8* data, u64* timestamp, void* userdata) return MPInterface::Get().RecvHostPacket(inst, data, timestamp); } -u16 MP_RecvReplies(u8* data, u64 timestamp, u16 aidmask, void* userdata) +u16 MP_RecvReplies(u8* data, u64 timestamp, u16 aidmask, bool block, void* userdata) { int inst = ((EmuInstance*)userdata)->getInstanceID(); - return MPInterface::Get().RecvReplies(inst, data, timestamp, aidmask); + return MPInterface::Get().RecvReplies(inst, data, timestamp, aidmask, block); } diff --git a/src/net/LAN.cpp b/src/net/LAN.cpp index 0f43803177..a4be3bbc65 100644 --- a/src/net/LAN.cpp +++ b/src/net/LAN.cpp @@ -795,7 +795,7 @@ void LAN::ProcessEvent(ENetEvent& event) // 0 = per-frame processing of events and eventual misc. frame // 1 = checking if a misc. frame has arrived // 2 = waiting for a MP frame -void LAN::ProcessLAN(int type) +void LAN::ProcessLAN(int type, bool block) { if (!Host) return; @@ -834,7 +834,7 @@ void LAN::ProcessLAN(int type) } } - int timeout = (type == 2) ? MPRecvTimeout : 0; + int timeout = (type == 2 && block) ? MPRecvTimeout : 0; time_last = (u32)Platform::GetMSCount(); ENetEvent event; @@ -1037,7 +1037,7 @@ int LAN::RecvHostPacket(int inst, u8* packet, u64* timestamp) return RecvPacketGeneric(packet, true, timestamp); } -u16 LAN::RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask) +u16 LAN::RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask, bool block) { if (!Host) return 0; @@ -1049,7 +1049,7 @@ u16 LAN::RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask) for (;;) { - ProcessLAN(2); + ProcessLAN(2, block); if (RXQueue.empty()) { // no more replies available diff --git a/src/net/LAN.h b/src/net/LAN.h index 162c95efa0..d61baf1b0e 100644 --- a/src/net/LAN.h +++ b/src/net/LAN.h @@ -105,7 +105,7 @@ class LAN : public MPInterface int SendReply(int inst, u8* data, int len, u64 timestamp, u16 aid) override; int SendAck(int inst, u8* data, int len, u64 timestamp) override; int RecvHostPacket(int inst, u8* data, u64* timestamp) override; - u16 RecvReplies(int inst, u8* data, u64 timestamp, u16 aidmask) override; + u16 RecvReplies(int inst, u8* data, u64 timestamp, u16 aidmask, bool block) override; private: bool Inited; @@ -145,7 +145,7 @@ class LAN : public MPInterface void ProcessHostEvent(ENetEvent& event); void ProcessClientEvent(ENetEvent& event); void ProcessEvent(ENetEvent& event); - void ProcessLAN(int type); + void ProcessLAN(int type, bool block = true); int SendPacketGeneric(u32 type, u8* packet, int len, u64 timestamp); int RecvPacketGeneric(u8* packet, bool block, u64* timestamp); diff --git a/src/net/LocalMP.cpp b/src/net/LocalMP.cpp index 03277243a5..eed5d9ca55 100644 --- a/src/net/LocalMP.cpp +++ b/src/net/LocalMP.cpp @@ -296,7 +296,7 @@ int LocalMP::RecvHostPacket(int inst, u8* packet, u64* timestamp) return RecvPacketGeneric(inst, packet, true, timestamp); } -u16 LocalMP::RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask) +u16 LocalMP::RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask, bool block) { u16 ret = 0; u16 myinstmask = (1 << inst); @@ -310,7 +310,7 @@ u16 LocalMP::RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask) for (;;) { - if (!Semaphore_TryWait(SemPool[16+inst], RecvTimeout)) + if (!Semaphore_TryWait(SemPool[16+inst], block ? RecvTimeout : 0)) { // no more replies available return ret; diff --git a/src/net/LocalMP.h b/src/net/LocalMP.h index fc912788c2..b3e93eb01a 100644 --- a/src/net/LocalMP.h +++ b/src/net/LocalMP.h @@ -59,7 +59,7 @@ class LocalMP : public MPInterface int SendReply(int inst, u8* data, int len, u64 timestamp, u16 aid); int SendAck(int inst, u8* data, int len, u64 timestamp); int RecvHostPacket(int inst, u8* data, u64* timestamp); - u16 RecvReplies(int inst, u8* data, u64 timestamp, u16 aidmask); + u16 RecvReplies(int inst, u8* data, u64 timestamp, u16 aidmask, bool block); private: void FIFORead(int inst, int fifo, void* buf, int len) noexcept; diff --git a/src/net/MPInterface.cpp b/src/net/MPInterface.cpp index 1d679fb3a7..bb8dbb7c48 100644 --- a/src/net/MPInterface.cpp +++ b/src/net/MPInterface.cpp @@ -37,7 +37,7 @@ class DummyMP : public MPInterface int SendReply(int inst, u8* data, int len, u64 timestamp, u16 aid) override { return 0; } int SendAck(int inst, u8* data, int len, u64 timestamp) override { return 0; } int RecvHostPacket(int inst, u8* data, u64* timestamp) override { return 0; } - u16 RecvReplies(int inst, u8* data, u64 timestamp, u16 aidmask) override { return 0; } + u16 RecvReplies(int inst, u8* data, u64 timestamp, u16 aidmask, bool block) override { return 0; } }; diff --git a/src/net/MPInterface.h b/src/net/MPInterface.h index 0f91e457ad..bb60c6ab61 100644 --- a/src/net/MPInterface.h +++ b/src/net/MPInterface.h @@ -67,7 +67,7 @@ class MPInterface virtual int SendReply(int inst, u8* data, int len, u64 timestamp, u16 aid) = 0; virtual int SendAck(int inst, u8* data, int len, u64 timestamp) = 0; virtual int RecvHostPacket(int inst, u8* data, u64* timestamp) = 0; - virtual u16 RecvReplies(int inst, u8* data, u64 timestamp, u16 aidmask) = 0; + virtual u16 RecvReplies(int inst, u8* data, u64 timestamp, u16 aidmask, bool block = true) = 0; protected: int RecvTimeout = 25; From 1ba815254b02fe4ddd96821a007c3eab99fdde82 Mon Sep 17 00:00:00 2001 From: WU Lang <55074733+lwu309@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:00:00 +0000 Subject: [PATCH 3/5] Wifi: use absolute MP reply deadlines --- src/Wifi.cpp | 20 +++++++++++++++++++- src/Wifi.h | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Wifi.cpp b/src/Wifi.cpp index abac62e1fa..443e98e805 100644 --- a/src/Wifi.cpp +++ b/src/Wifi.cpp @@ -224,6 +224,7 @@ void Wifi::Reset() MPReplyTimer = 0; MPClientMask = 0; MPClientFail = 0; + MPClientOrdinal = 0; MPCmdTimestamp = 0; memset(MPClientReplies, 0, sizeof(MPClientReplies)); @@ -303,6 +304,7 @@ void Wifi::DoSavestate(Savestate* file) file->Var32((u32*)&MPReplyTimer); file->Var16(&MPClientMask); file->Var16(&MPClientFail); + file->Var16(&MPClientOrdinal); file->Var64(&MPCmdTimestamp); file->VarArray(MPClientReplies, sizeof(MPClientReplies)); @@ -983,8 +985,23 @@ bool Wifi::ProcessTX(TXSlot* slot, int num) if (!(MPClientFail & curclient)) MPClientReplyRX(nclient); - MPReplyTimer += 10 + IOPORT(W_CmdReplyTime); MPClientMask &= ~curclient; + MPClientOrdinal++; + + if (MPClientMask != 0) + { + // Absolute deadline for the next client, based on its + // ordinal position in the masked client list. Replies + // that arrive early are still processed early, but they + // must not shorten the later clients' windows. + const u64 deadline = MPCmdTimestamp + 16 + (u64)MPClientOrdinal * (10 + IOPORT(W_CmdReplyTime)); + const s64 remaining = (s64)deadline - (s64)USTimestamp; + MPReplyTimer = (remaining > 0) ? (int)remaining : 0; + } + else + { + MPReplyTimer = 0; + } } } @@ -1082,6 +1099,7 @@ bool Wifi::ProcessTX(TXSlot* slot, int num) MPReplyTimer = 16 + 10 + IOPORT(W_CmdReplyTime); MPCmdTimestamp = USTimestamp; + MPClientOrdinal = 1; break; } diff --git a/src/Wifi.h b/src/Wifi.h index d8fae9edbf..0888cbd2d7 100644 --- a/src/Wifi.h +++ b/src/Wifi.h @@ -236,6 +236,7 @@ class Wifi int MPReplyTimer; u16 MPClientMask, MPClientFail; + u16 MPClientOrdinal; // 1-based ordinal of the next masked client u64 MPCmdTimestamp = 0; // Wi-Fi time of the current CMD, for reply staleness u8 MPClientReplies[15*1024]; From f567d6992c80ecffb6bd93ea0eec75b27669806e Mon Sep 17 00:00:00 2001 From: WU Lang <55074733+lwu309@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:00:00 +0000 Subject: [PATCH 4/5] MP: ignore replies from clients outside the current CMD mask --- src/net/LAN.cpp | 9 ++++++++- src/net/LocalMP.cpp | 13 ++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/net/LAN.cpp b/src/net/LAN.cpp index a4be3bbc65..f188047011 100644 --- a/src/net/LAN.cpp +++ b/src/net/LAN.cpp @@ -1067,12 +1067,19 @@ u16 LAN::RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask, bool blo if (good) { + u32 aid = header->Type >> 16; + if (aid == 0 || aid > 15 || !(aidmask & (1 << aid))) + { + // reply from a client outside the current CMD mask + enet_packet_destroy(enetpacket); + continue; + } + u32 len = header->Length; if (len) { if (len > 1024) len = 1024; - u32 aid = header->Type >> 16; memcpy(&packets[(aid-1)*1024], &enetpacket->data[sizeof(MPPacketHeader)], len); ret |= (1<> 16; + if (aid == 0 || aid > 15 || !(aidmask & (1 << aid))) + { + // reply from a client outside the current CMD mask + ReplyReadOffset[inst] += pktheader.Length; + if (ReplyReadOffset[inst] >= kReplyQueueSize) + ReplyReadOffset[inst] -= kReplyQueueSize; + + Mutex_Unlock(MPQueueLock); + continue; + } + if (pktheader.Length) { - u32 aid = (pktheader.Type >> 16); FIFORead(inst, 1, &packets[(aid-1)*1024], pktheader.Length); ret |= (1 << aid); } From f44a2cead23fafe440b057c56b74b776a47c1cef Mon Sep 17 00:00:00 2001 From: WU Lang <55074733+lwu309@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:00:00 +0000 Subject: [PATCH 5/5] Wifi: separate MP reply wait from ACK preamble --- src/Wifi.cpp | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/Wifi.cpp b/src/Wifi.cpp index 443e98e805..39841090a5 100644 --- a/src/Wifi.cpp +++ b/src/Wifi.cpp @@ -721,7 +721,8 @@ void Wifi::StartTX_Cmd() MPClientFail &= MPClientMask; u32 duration = PreambleLen(slot->Rate) + (slot->Length * (slot->Rate==2 ? 4:8)); - duration += 112 + ((10 + IOPORT(W_CmdReplyTime)) * NumClients(MPClientMask)); + duration += 16 + ((10 + IOPORT(W_CmdReplyTime)) * NumClients(MPClientMask)); + duration += PreambleLen(slot->Rate); // ACK preamble duration += (32 * (slot->Rate==2 ? 4:8)); if (CmdCounter > (duration + 100)) @@ -1092,10 +1093,9 @@ bool Wifi::ProcessTX(TXSlot* slot, int num) } SetStatus(5); - // TODO: 112 likely includes the ack preamble, which needs adjusted - // for long-preamble settings + // Reply wait; the ACK preamble is modeled as a separate phase. slot->CurPhase = 2; - slot->CurPhaseTime = 112 + ((10 + IOPORT(W_CmdReplyTime)) * NumClients(MPClientMask)); + slot->CurPhaseTime = 16 + ((10 + IOPORT(W_CmdReplyTime)) * NumClients(MPClientMask)); MPReplyTimer = 16 + 10 + IOPORT(W_CmdReplyTime); MPCmdTimestamp = USTimestamp; @@ -1154,7 +1154,40 @@ bool Wifi::ProcessTX(TXSlot* slot, int num) } return true; - case 2: // MP host transfer done + case 2: // MP reply wait done + { + // The reply wait is over. Finalize any clients whose replies + // were not collected while the timer was running, then switch + // to the ACK preamble. IRQ7 is raised after the preamble, + // when the ACK data starts (GBATEK: CMD ack preamble). + if (MPClientMask != 0) + { + const u16 res = Platform::MP_RecvReplies(MPClientReplies, MPCmdTimestamp, MPClientMask, false, NDS.UserData); + MPClientFail &= ~res; + + for (int nclient = 1; nclient < 16; nclient++) + { + const u16 curclient = 1 << nclient; + if (!(MPClientMask & curclient)) + continue; + + if (!(MPClientFail & curclient)) + MPClientReplyRX(nclient); + + MPClientMask &= ~curclient; + MPClientOrdinal++; + } + } + + MPReplyTimer = 0; + + SetStatus(7); + slot->CurPhase = 4; + slot->CurPhaseTime = PreambleLen(slot->Rate); + } + break; + + case 4: // ACK preamble done { SetIRQ(7); SetStatus(8); @@ -1740,8 +1773,9 @@ bool Wifi::CheckRX(int type) // 0=regular 1=MP replies 2=MP host frames u16 clienttime = *(u16*)&RXBuffer[12+24]; u16 clientmask = *(u16*)&RXBuffer[12+26]; - // include the MP reply time window - NextSync += 112 + ((clienttime + 10) * NumClients(clientmask)); + // include the MP reply time window and the ACK preamble + NextSync += 16 + ((clienttime + 10) * NumClients(clientmask)); + NextSync += PreambleLen((txrate == 0x14) ? 2 : 1); } else if (MACEqual(&RXBuffer[12 + 4], MPAckMAC)) {