melonDS/src/Wifi.h

298 lines
6.6 KiB
C
Raw Normal View History

2016-12-06 16:32:51 +00:00
/*
2023-11-03 23:21:46 +00:00
Copyright 2016-2023 melonDS team
2016-12-06 16:32:51 +00:00
This file is part of melonDS.
melonDS is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with melonDS. If not, see http://www.gnu.org/licenses/.
*/
#ifndef WIFI_H
#define WIFI_H
2018-10-18 01:22:53 +00:00
#include "Savestate.h"
namespace melonDS
{
2023-11-04 16:00:12 +00:00
class WifiAP;
class NDS;
2023-11-04 16:00:12 +00:00
class Wifi
2017-04-12 14:58:09 +00:00
{
2023-11-04 16:00:12 +00:00
public:
enum
{
W_ID = 0x000,
W_ModeReset = 0x004,
W_ModeWEP = 0x006,
W_TXStatCnt = 0x008,
W_IF = 0x010,
W_IE = 0x012,
W_MACAddr0 = 0x018,
W_MACAddr1 = 0x01A,
W_MACAddr2 = 0x01C,
W_BSSID0 = 0x020,
W_BSSID1 = 0x022,
W_BSSID2 = 0x024,
W_AIDLow = 0x028,
W_AIDFull = 0x02A,
W_TXRetryLimit = 0x02C,
W_RXCnt = 0x030,
W_WEPCnt = 0x032,
W_TRXPower = 0x034,
2023-11-04 16:00:12 +00:00
W_PowerUS = 0x036,
W_PowerTX = 0x038,
W_PowerState = 0x03C,
W_PowerForce = 0x040,
W_PowerDownCtrl = 0x48,
2023-11-04 16:00:12 +00:00
W_Random = 0x044,
W_RXBufBegin = 0x050,
W_RXBufEnd = 0x052,
W_RXBufWriteCursor = 0x054,
W_RXBufWriteAddr = 0x056,
W_RXBufReadAddr = 0x058,
W_RXBufReadCursor = 0x05A,
W_RXBufCount = 0x05C,
W_RXBufDataRead = 0x060,
W_RXBufGapAddr = 0x062,
W_RXBufGapSize = 0x064,
W_TXBufWriteAddr = 0x068,
W_TXBufCount = 0x06C,
W_TXBufDataWrite = 0x070,
W_TXBufGapAddr = 0x074,
W_TXBufGapSize = 0x076,
W_TXSlotBeacon = 0x080,
W_TXBeaconTIM = 0x084,
W_ListenCount = 0x088,
W_BeaconInterval = 0x08C,
W_ListenInterval = 0x08E,
W_TXSlotCmd = 0x090,
W_TXSlotReply1 = 0x094,
W_TXSlotReply2 = 0x098,
W_TXSlotLoc1 = 0x0A0,
W_TXSlotLoc2 = 0x0A4,
W_TXSlotLoc3 = 0x0A8,
W_TXReqReset = 0x0AC,
W_TXReqSet = 0x0AE,
W_TXReqRead = 0x0B0,
W_TXSlotReset = 0x0B4,
W_TXBusy = 0x0B6,
W_TXStat = 0x0B8,
W_Preamble = 0x0BC,
W_CmdTotalTime = 0x0C0,
W_CmdReplyTime = 0x0C4,
W_RXFilter = 0x0D0,
W_RXLenCrop = 0x0DA,
W_RXFilter2 = 0x0E0,
W_USCountCnt = 0x0E8,
W_USCompareCnt = 0x0EA,
W_CmdCountCnt = 0x0EE,
W_USCount0 = 0x0F8,
W_USCount1 = 0x0FA,
W_USCount2 = 0x0FC,
W_USCount3 = 0x0FE,
W_USCompare0 = 0x0F0,
W_USCompare1 = 0x0F2,
W_USCompare2 = 0x0F4,
W_USCompare3 = 0x0F6,
W_ContentFree = 0x10C,
W_PreBeacon = 0x110,
W_CmdCount = 0x118,
W_BeaconCount1 = 0x11C,
W_BeaconCount2 = 0x134,
W_BBCnt = 0x158,
W_BBWrite = 0x15A,
W_BBRead = 0x15C,
W_BBBusy = 0x15E,
W_BBMode = 0x160,
W_BBPower = 0x168,
W_RFData2 = 0x17C,
W_RFData1 = 0x17E,
W_RFBusy = 0x180,
W_RFCnt = 0x184,
W_TXHeaderCnt = 0x194,
W_RFPins = 0x19C,
W_RXStatIncIF = 0x1A8,
W_RXStatIncIE = 0x1AA,
W_RXStatHalfIF = 0x1AC,
W_RXStatHalfIE = 0x1AE,
W_TXErrorCount = 0x1C0,
W_RXCount = 0x1C4,
W_CMDStat0 = 0x1D0,
W_CMDStat1 = 0x1D2,
W_CMDStat2 = 0x1D4,
W_CMDStat3 = 0x1D6,
W_CMDStat4 = 0x1D8,
W_CMDStat5 = 0x1DA,
W_CMDStat6 = 0x1DC,
W_CMDStat7 = 0x1DE,
W_TXSeqNo = 0x210,
W_RFStatus = 0x214,
W_IFSet = 0x21C,
W_RXTXAddr = 0x268,
};
Wifi(melonDS::NDS& nds);
2023-11-04 16:00:12 +00:00
~Wifi();
void Reset();
void DoSavestate(Savestate* file);
void SetPowerCnt(u32 val);
void USTimer(u32 param);
u16 Read(u32 addr);
void Write(u32 addr, u16 val);
const u8* GetMAC() const;
const u8* GetBSSID() const;
2023-11-04 16:00:12 +00:00
private:
melonDS::NDS& NDS;
2023-11-04 16:00:12 +00:00
u8 RAM[0x2000];
u16 IO[0x1000>>1];
static const u8 MPCmdMAC[6];
static const u8 MPReplyMAC[6];
static const u8 MPAckMAC[6];
static const int kTimerInterval = 8;
static const u32 kTimeCheckMask = ~(kTimerInterval - 1);
bool Enabled;
bool PowerOn;
s32 TimerError;
u16 Random;
// general, always-on microsecond counter
u64 USTimestamp;
u64 USCounter;
u64 USCompare;
bool BlockBeaconIRQ14;
u32 CmdCounter;
u8 BBRegs[0x100];
u8 BBRegsRO[0x100];
u8 RFVersion;
u32 RFRegs[0x40];
u32 RFChannelIndex[2];
u32 RFChannelData[14][2];
int CurChannel;
2023-11-04 16:00:12 +00:00
struct TXSlot
{
bool Valid;
u16 Addr;
u16 Length;
u8 Rate;
u8 CurPhase;
int CurPhaseTime;
u32 HalfwordTimeMask;
};
TXSlot TXSlots[6];
u8 TXBuffer[0x2000];
2016-12-06 16:32:51 +00:00
2023-11-04 16:00:12 +00:00
u8 RXBuffer[2048];
u32 RXBufferPtr;
int RXTime;
u32 RXHalfwordTimeMask;
merge local_wifi (#1516) * attempt at betterer wifi * add preliminary sync mechanism * fix gaps in wifi implementation * move local-MP comm to its own module instead of cramping Platform.cpp * remove some stupid cruft * as you wish, Sorer (starting work on shared-memory system) * shared-memory IPC that actually works (albeit Windows-only for now) * shut up logging from NULL writes on ARM7 (ffs Nintendo learn to code) * get this somewhat good * leave client sync mode when host deauths. makes download play actually work. * start implementing MP-comm error handling * * add MP-reply error counters * feeble attempt at fixing slowdown/desync/etc problems * somewhat better exchange/sync method * * when entering power-saving mode, be sure to finish transferring the current frame first * fix misc bug due to old cruft leftover makes for a more stable connection * remove a bunch of cruft * set wifi time interval to 34 cycles instead of 33. games seem sensitive to the general timing of wifi vs the rest of the system, and this seems to make things run better, atleast until I rewrite this to use a proper scheduler. * more graceful handling of disconnects * deal with FIFO overflow more gracefully * BAHAHAHAHAHAHAHAHHHH THE SNEAKY BASTARDS so, when the DS receives a beacon with the right BSSID that beacon's timestamp is copied to USCOUNTER * attempt at making the connection process smoother for weird games * * begin adding POWCNT2, only applies to wifi for now * begin work on wifi scheduler * implement the shitty timers * add the RF wakeup thing * begin work on receiving frames. for now it can just receive melonAP beacons, but hey, it's a start. * add enough TX functionality that online wifi is a possibility again. * there are problems with this scheduler thing. committing it anyway * kind of a rollback... we're gonna work out a compromise on this, I guess * don't transmit shit if RXCNT.bit15 isn't set * move RX-finish to its own function. more accurate filtering. implement RXFILTER. * remove some cruft * fix some of the shittiness when trying to connect more than two players * fix some more shittiness * fix more wifi shittiness (mainly don't try to receive shit while sending a frame) * run wifi every 8µs. improves performance. * fix IRQ14/IRQ15 * make this work under Linux * Make it work on macOS, for now using a custom sem_timedwait implementation. If anyone knows anything about mach ports and have an idea for how to make this work using mach IPC, please do let me know. * 25ms seems like a good timeout * begin work on proper multiplayer UI shito. for now, determine a global instance ID, and derivate the system MAC from it. remove 'randomize MAC' option. * finish removing RandomizeMAC * lay groundwork for instance-unique config * work some on the UI... make it not labelled Fart * more UI work: make it explicit that some things are instance-unique * separate firmware files for multiplayer instances * make instances save to different save files, too * more UI work, make things somewhat less shitty * lay base for the multiplayer settings dialog * actually hook up most of that dialog * actually implement the fun audio settings * ensure all the wifi shit is properly savestated and reset. properly update timings for the wifi region when wifi is disabled. * add more fun labels * * ignore WEP frames if WEP is off * implement RX_LEN_CROP * fake enough of WEP processing to make Inazuma Eleven work * * do not copy more ROM banner data than actually needed * avoid trying to read out of bounds if the banner offset is bad * Fix oversight with the preferences action causing the build to fail on macOS Co-authored-by: Nadia Holmquist Pedersen <nadia@nhp.sh>
2022-09-22 18:32:27 +00:00
2023-11-04 16:00:12 +00:00
u32 ComStatus; // 0=waiting for packets 1=receiving 2=sending
u32 TXCurSlot;
u32 RXCounter;
int MPReplyTimer;
u16 MPClientMask, MPClientFail;
merge local_wifi (#1516) * attempt at betterer wifi * add preliminary sync mechanism * fix gaps in wifi implementation * move local-MP comm to its own module instead of cramping Platform.cpp * remove some stupid cruft * as you wish, Sorer (starting work on shared-memory system) * shared-memory IPC that actually works (albeit Windows-only for now) * shut up logging from NULL writes on ARM7 (ffs Nintendo learn to code) * get this somewhat good * leave client sync mode when host deauths. makes download play actually work. * start implementing MP-comm error handling * * add MP-reply error counters * feeble attempt at fixing slowdown/desync/etc problems * somewhat better exchange/sync method * * when entering power-saving mode, be sure to finish transferring the current frame first * fix misc bug due to old cruft leftover makes for a more stable connection * remove a bunch of cruft * set wifi time interval to 34 cycles instead of 33. games seem sensitive to the general timing of wifi vs the rest of the system, and this seems to make things run better, atleast until I rewrite this to use a proper scheduler. * more graceful handling of disconnects * deal with FIFO overflow more gracefully * BAHAHAHAHAHAHAHAHHHH THE SNEAKY BASTARDS so, when the DS receives a beacon with the right BSSID that beacon's timestamp is copied to USCOUNTER * attempt at making the connection process smoother for weird games * * begin adding POWCNT2, only applies to wifi for now * begin work on wifi scheduler * implement the shitty timers * add the RF wakeup thing * begin work on receiving frames. for now it can just receive melonAP beacons, but hey, it's a start. * add enough TX functionality that online wifi is a possibility again. * there are problems with this scheduler thing. committing it anyway * kind of a rollback... we're gonna work out a compromise on this, I guess * don't transmit shit if RXCNT.bit15 isn't set * move RX-finish to its own function. more accurate filtering. implement RXFILTER. * remove some cruft * fix some of the shittiness when trying to connect more than two players * fix some more shittiness * fix more wifi shittiness (mainly don't try to receive shit while sending a frame) * run wifi every 8µs. improves performance. * fix IRQ14/IRQ15 * make this work under Linux * Make it work on macOS, for now using a custom sem_timedwait implementation. If anyone knows anything about mach ports and have an idea for how to make this work using mach IPC, please do let me know. * 25ms seems like a good timeout * begin work on proper multiplayer UI shito. for now, determine a global instance ID, and derivate the system MAC from it. remove 'randomize MAC' option. * finish removing RandomizeMAC * lay groundwork for instance-unique config * work some on the UI... make it not labelled Fart * more UI work: make it explicit that some things are instance-unique * separate firmware files for multiplayer instances * make instances save to different save files, too * more UI work, make things somewhat less shitty * lay base for the multiplayer settings dialog * actually hook up most of that dialog * actually implement the fun audio settings * ensure all the wifi shit is properly savestated and reset. properly update timings for the wifi region when wifi is disabled. * add more fun labels * * ignore WEP frames if WEP is off * implement RX_LEN_CROP * fake enough of WEP processing to make Inazuma Eleven work * * do not copy more ROM banner data than actually needed * avoid trying to read out of bounds if the banner offset is bad * Fix oversight with the preferences action causing the build to fail on macOS Co-authored-by: Nadia Holmquist Pedersen <nadia@nhp.sh>
2022-09-22 18:32:27 +00:00
2023-11-04 16:00:12 +00:00
u8 MPClientReplies[15*1024];
2016-12-06 16:32:51 +00:00
2023-11-04 16:00:12 +00:00
u16 MPLastSeqno;
2023-11-04 16:00:12 +00:00
int USUntilPowerOn;
2016-12-06 16:32:51 +00:00
2023-11-04 16:00:12 +00:00
// MULTIPLAYER SYNC APPARATUS
bool IsMP;
bool IsMPClient;
u64 NextSync; // for clients: timestamp for next sync point
u64 RXTimestamp;
2023-11-04 16:00:12 +00:00
class WifiAP* WifiAP;
2017-05-01 20:27:05 +00:00
2023-11-04 16:00:12 +00:00
void ScheduleTimer(bool first);
void UpdatePowerOn();
2016-12-06 16:32:51 +00:00
void CheckIRQ(u16 oldflags);
2023-11-04 16:00:12 +00:00
void SetIRQ(u32 irq);
void SetIRQ13();
void SetIRQ14(int source);
void SetIRQ15();
2023-11-04 16:00:12 +00:00
void SetStatus(u32 status);
void UpdatePowerStatus(int power);
2023-11-04 16:00:12 +00:00
int PreambleLen(int rate) const;
u32 NumClients(u16 bitmask) const;
void IncrementTXCount(const TXSlot* slot);
2023-11-04 16:00:12 +00:00
void ReportMPReplyErrors(u16 clientfail);
void TXSendFrame(const TXSlot* slot, int num);
2023-11-04 16:00:12 +00:00
void StartTX_LocN(int nslot, int loc);
void StartTX_Cmd();
void StartTX_Beacon();
void FireTX();
void SendMPDefaultReply();
void SendMPReply(u16 clienttime, u16 clientmask);
void SendMPAck(u16 cmdcount, u16 clientfail);
bool ProcessTX(TXSlot* slot, int num);
void IncrementRXAddr(u16& addr, u16 inc = 2);
void StartRX();
void FinishRX();
void MPClientReplyRX(int client);
bool CheckRX(int type);
void MSTimer();
void ChangeChannel();
2023-11-04 16:00:12 +00:00
void RFTransfer_Type2();
void RFTransfer_Type3();
};
2016-12-06 16:32:51 +00:00
}
2016-12-06 16:32:51 +00:00
#endif