Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 82 additions & 16 deletions src/Wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ void Wifi::Reset()
MPReplyTimer = 0;
MPClientMask = 0;
MPClientFail = 0;
MPClientOrdinal = 0;
MPCmdTimestamp = 0;
memset(MPClientReplies, 0, sizeof(MPClientReplies));

MPLastSeqno = 0xFFFF;
Expand Down Expand Up @@ -302,6 +304,8 @@ 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));

Expand Down Expand Up @@ -717,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))
Expand Down Expand Up @@ -955,7 +960,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, false, 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++;
Expand All @@ -965,8 +986,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;
}
}
}

Expand Down Expand Up @@ -1057,17 +1093,13 @@ 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
// 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;
MPClientOrdinal = 1;

break;
}
Expand Down Expand Up @@ -1122,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);
Expand Down Expand Up @@ -1708,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))
{
Expand Down
2 changes: 2 additions & 0 deletions src/Wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ 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];

Expand Down
4 changes: 2 additions & 2 deletions src/frontend/qt_sdl/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
17 changes: 12 additions & 5 deletions src/net/LAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -1067,12 +1067,19 @@ u16 LAN::RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask)

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<<aid);
Expand Down
4 changes: 2 additions & 2 deletions src/net/LAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 14 additions & 3 deletions src/net/LocalMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -342,9 +342,20 @@ u16 LocalMP::RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask)
continue;
}

const u32 aid = pktheader.Type >> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/net/LocalMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/net/MPInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
};


Expand Down
2 changes: 1 addition & 1 deletion src/net/MPInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down