Skip to content

Commit

Permalink
merge local_wifi (melonDS-emu#1516)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Arisotura and nadiaholmquist authored Sep 22, 2022
1 parent b5073e6 commit b1e4bd5
Show file tree
Hide file tree
Showing 39 changed files with 3,433 additions and 1,033 deletions.
123 changes: 99 additions & 24 deletions src/NDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ bool RunningGame;
void DivDone(u32 param);
void SqrtDone(u32 param);
void RunTimer(u32 tid, s32 cycles);
void UpdateWifiTimings();
void SetWifiWaitCnt(u16 val);
void SetGBASlotTimings();

Expand Down Expand Up @@ -892,9 +893,7 @@ bool DoSavestate(Savestate* file)
InitTimings();
SetGBASlotTimings();

u16 tmp = WifiWaitCnt;
WifiWaitCnt = 0xFFFF;
SetWifiWaitCnt(tmp); // force timing table update
UpdateWifiTimings();
}

for (int i = 0; i < 8; i++)
Expand All @@ -918,6 +917,9 @@ bool DoSavestate(Savestate* file)
if (!file->Saving)
{
GPU::SetPowerCnt(PowerControl9);

SPU::SetPowerCnt(PowerControl7 & 0x0001);
Wifi::SetPowerCnt(PowerControl7 & 0x0002);
}

#ifdef JIT_ENABLED
Expand Down Expand Up @@ -1198,6 +1200,25 @@ void ScheduleEvent(u32 id, bool periodic, s32 delay, void (*func)(u32), u32 para
Reschedule(evt->Timestamp);
}

void ScheduleEvent(u32 id, u64 timestamp, void (*func)(u32), u32 param)
{
if (SchedListMask & (1<<id))
{
printf("!! EVENT %d ALREADY SCHEDULED\n", id);
return;
}

SchedEvent* evt = &SchedList[id];

evt->Timestamp = timestamp;
evt->Func = func;
evt->Param = param;

SchedListMask |= (1<<id);

Reschedule(evt->Timestamp);
}

void CancelEvent(u32 id)
{
SchedListMask &= ~(1<<id);
Expand Down Expand Up @@ -1323,15 +1344,29 @@ void MapSharedWRAM(u8 val)
}


void UpdateWifiTimings()
{
if (PowerControl7 & 0x0002)
{
const int ntimings[4] = {10, 8, 6, 18};
u16 val = WifiWaitCnt;

SetARM7RegionTimings(0x04800, 0x04808, Mem7_Wifi0, 16, ntimings[val & 0x3], (val & 0x4) ? 4 : 6);
SetARM7RegionTimings(0x04808, 0x04810, Mem7_Wifi1, 16, ntimings[(val>>3) & 0x3], (val & 0x20) ? 4 : 10);
}
else
{
SetARM7RegionTimings(0x04800, 0x04808, Mem7_Wifi0, 32, 1, 1);
SetARM7RegionTimings(0x04808, 0x04810, Mem7_Wifi1, 32, 1, 1);
}
}

void SetWifiWaitCnt(u16 val)
{
if (WifiWaitCnt == val) return;

WifiWaitCnt = val;

const int ntimings[4] = {10, 8, 6, 18};
SetARM7RegionTimings(0x04800, 0x04808, Mem7_Wifi0, 16, ntimings[val & 0x3], (val & 0x4) ? 4 : 6);
SetARM7RegionTimings(0x04808, 0x04810, Mem7_Wifi1, 16, ntimings[(val>>3) & 0x3], (val & 0x20) ? 4 : 10);
UpdateWifiTimings();
}

void SetGBASlotTimings()
Expand Down Expand Up @@ -1941,8 +1976,8 @@ void debug(u32 param)
//for (int i = 0; i < 9; i++)
// printf("VRAM %c: %02X\n", 'A'+i, GPU::VRAMCNT[i]);

/*FILE*
shit = fopen("debug/construct.bin", "wb");
FILE*
shit = fopen("debug/inazuma.bin", "wb");
fwrite(ARM9->ITCM, 0x8000, 1, shit);
for (u32 i = 0x02000000; i < 0x02400000; i+=4)
{
Expand All @@ -1954,9 +1989,14 @@ void debug(u32 param)
u32 val = ARM7Read32(i);
fwrite(&val, 4, 1, shit);
}
fclose(shit);*/
for (u32 i = 0x06000000; i < 0x06040000; i+=4)
{
u32 val = ARM7Read32(i);
fwrite(&val, 4, 1, shit);
}
fclose(shit);

FILE*
/*FILE*
shit = fopen("debug/directboot9.bin", "wb");
for (u32 i = 0x02000000; i < 0x04000000; i+=4)
{
Expand All @@ -1970,7 +2010,7 @@ void debug(u32 param)
u32 val = DSi::ARM7Read32(i);
fwrite(&val, 4, 1, shit);
}
fclose(shit);
fclose(shit);*/
}


Expand Down Expand Up @@ -2396,6 +2436,7 @@ u8 ARM7Read8(u32 addr)
case 0x04800000:
if (addr < 0x04810000)
{
if (!(PowerControl7 & (1<<1))) return 0;
if (addr & 0x1) return Wifi::Read(addr-1) >> 8;
return Wifi::Read(addr) & 0xFF;
}
Expand Down Expand Up @@ -2460,6 +2501,7 @@ u16 ARM7Read16(u32 addr)
case 0x04800000:
if (addr < 0x04810000)
{
if (!(PowerControl7 & (1<<1))) return 0;
return Wifi::Read(addr);
}
break;
Expand Down Expand Up @@ -2523,6 +2565,7 @@ u32 ARM7Read32(u32 addr)
case 0x04800000:
if (addr < 0x04810000)
{
if (!(PowerControl7 & (1<<1))) return 0;
return Wifi::Read(addr) | (Wifi::Read(addr+2) << 16);
}
break;
Expand Down Expand Up @@ -2614,7 +2657,8 @@ void ARM7Write8(u32 addr, u8 val)
return;
}

if (ARM7->R[15] > 0x00002F30) // ARM7 BIOS bug
//if (ARM7->R[15] > 0x00002F30) // ARM7 BIOS bug
if (addr >= 0x01000000)
printf("unknown arm7 write8 %08X %02X @ %08X\n", addr, val, ARM7->R[15]);
}

Expand Down Expand Up @@ -2662,6 +2706,7 @@ void ARM7Write16(u32 addr, u16 val)
case 0x04800000:
if (addr < 0x04810000)
{
if (!(PowerControl7 & (1<<1))) return;
Wifi::Write(addr, val);
return;
}
Expand Down Expand Up @@ -2691,7 +2736,8 @@ void ARM7Write16(u32 addr, u16 val)
return;
}

printf("unknown arm7 write16 %08X %04X @ %08X\n", addr, val, ARM7->R[15]);
if (addr >= 0x01000000)
printf("unknown arm7 write16 %08X %04X @ %08X\n", addr, val, ARM7->R[15]);
}

void ARM7Write32(u32 addr, u32 val)
Expand Down Expand Up @@ -2738,6 +2784,7 @@ void ARM7Write32(u32 addr, u32 val)
case 0x04800000:
if (addr < 0x04810000)
{
if (!(PowerControl7 & (1<<1))) return;
Wifi::Write(addr, val & 0xFFFF);
Wifi::Write(addr+2, val >> 16);
return;
Expand Down Expand Up @@ -2771,7 +2818,8 @@ void ARM7Write32(u32 addr, u32 val)
return;
}

printf("unknown arm7 write32 %08X %08X @ %08X\n", addr, val, ARM7->R[15]);
if (addr >= 0x01000000)
printf("unknown arm7 write32 %08X %08X @ %08X\n", addr, val, ARM7->R[15]);
}

bool ARM7GetMemRegion(u32 addr, bool write, MemRegion* region)
Expand Down Expand Up @@ -2931,7 +2979,8 @@ u8 ARM9IORead8(u32 addr)
return (u8)(emuID[idx]);
}

printf("unknown ARM9 IO read8 %08X %08X\n", addr, ARM9->R[15]);
if ((addr & 0xFFFFF000) != 0x04004000)
printf("unknown ARM9 IO read8 %08X %08X\n", addr, ARM9->R[15]);
return 0;
}

Expand Down Expand Up @@ -3077,7 +3126,8 @@ u16 ARM9IORead16(u32 addr)
return GPU3D::Read16(addr);
}

printf("unknown ARM9 IO read16 %08X %08X\n", addr, ARM9->R[15]);
if ((addr & 0xFFFFF000) != 0x04004000)
printf("unknown ARM9 IO read16 %08X %08X\n", addr, ARM9->R[15]);
return 0;
}

Expand Down Expand Up @@ -3220,7 +3270,8 @@ u32 ARM9IORead32(u32 addr)
return GPU3D::Read32(addr);
}

printf("unknown ARM9 IO read32 %08X %08X\n", addr, ARM9->R[15]);
if ((addr & 0xFFFFF000) != 0x04004000)
printf("unknown ARM9 IO read32 %08X %08X\n", addr, ARM9->R[15]);
return 0;
}

Expand Down Expand Up @@ -3748,14 +3799,16 @@ u8 ARM7IORead8(u32 addr)
case 0x04000241: return WRAMCnt;

case 0x04000300: return PostFlag7;
case 0x04000304: return PowerControl7;
}

if (addr >= 0x04000400 && addr < 0x04000520)
{
return SPU::Read8(addr);
}

printf("unknown ARM7 IO read8 %08X %08X\n", addr, ARM7->R[15]);
if ((addr & 0xFFFFF000) != 0x04004000)
printf("unknown ARM7 IO read8 %08X %08X\n", addr, ARM7->R[15]);
return 0;
}

Expand Down Expand Up @@ -3830,7 +3883,9 @@ u16 ARM7IORead16(u32 addr)
case 0x040001C2: return SPI::ReadData();

case 0x04000204: return ExMemCnt[1];
case 0x04000206: return WifiWaitCnt;
case 0x04000206:
if (!(PowerControl7 & (1<<1))) return 0;
return WifiWaitCnt;

case 0x04000208: return IME[1];
case 0x04000210: return IE[1] & 0xFFFF;
Expand All @@ -3846,7 +3901,8 @@ u16 ARM7IORead16(u32 addr)
return SPU::Read16(addr);
}

printf("unknown ARM7 IO read16 %08X %08X\n", addr, ARM7->R[15]);
if ((addr & 0xFFFFF000) != 0x04004000)
printf("unknown ARM7 IO read16 %08X %08X\n", addr, ARM7->R[15]);
return 0;
}

Expand Down Expand Up @@ -3912,6 +3968,7 @@ u32 ARM7IORead32(u32 addr)
case 0x04000210: return IE[1];
case 0x04000214: return IF[1];

case 0x04000304: return PowerControl7;
case 0x04000308: return ARM7BIOSProt;

case 0x04100000:
Expand Down Expand Up @@ -3945,7 +4002,8 @@ u32 ARM7IORead32(u32 addr)
return SPU::Read32(addr);
}

printf("unknown ARM7 IO read32 %08X %08X\n", addr, ARM7->R[15]);
if ((addr & 0xFFFFF000) != 0x04004000)
printf("unknown ARM7 IO read32 %08X %08X\n", addr, ARM7->R[15]);
return 0;
}

Expand Down Expand Up @@ -4140,6 +4198,7 @@ void ARM7IOWrite16(u32 addr, u16 val)
return;
}
case 0x04000206:
if (!(PowerControl7 & (1<<1))) return;
SetWifiWaitCnt(val);
return;

Expand All @@ -4155,7 +4214,15 @@ void ARM7IOWrite16(u32 addr, u16 val)
PostFlag7 = val & 0x01;
return;

case 0x04000304: PowerControl7 = val; return;
case 0x04000304:
{
u16 change = PowerControl7 ^ val;
PowerControl7 = val & 0x0003;
SPU::SetPowerCnt(val & 0x0001);
Wifi::SetPowerCnt(val & 0x0002);
if (change & 0x0002) UpdateWifiTimings();
}
return;

case 0x04000308:
if (ARM7BIOSProt == 0)
Expand Down Expand Up @@ -4277,7 +4344,15 @@ void ARM7IOWrite32(u32 addr, u32 val)
case 0x04000210: IE[1] = val; UpdateIRQ(1); return;
case 0x04000214: IF[1] &= ~val; UpdateIRQ(1); return;

case 0x04000304: PowerControl7 = val & 0xFFFF; return;
case 0x04000304:
{
u16 change = PowerControl7 ^ val;
PowerControl7 = val & 0x0003;
SPU::SetPowerCnt(val & 0x0001);
Wifi::SetPowerCnt(val & 0x0002);
if (change & 0x0002) UpdateWifiTimings();
}
return;

case 0x04000308:
if (ARM7BIOSProt == 0)
Expand Down
1 change: 1 addition & 0 deletions src/NDS.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ void SetLidClosed(bool closed);
void MicInputFrame(s16* data, int samples);

void ScheduleEvent(u32 id, bool periodic, s32 delay, void (*func)(u32), u32 param);
void ScheduleEvent(u32 id, u64 timestamp, void (*func)(u32), u32 param);
void CancelEvent(u32 id);

void debug(u32 p);
Expand Down
18 changes: 9 additions & 9 deletions src/NDSCart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,9 @@ bool LoadROM(const u8* romdata, u32 romlen)
if (CartInserted)
EjectCart();

memset(&Header, 0, sizeof(Header));
memset(&Banner, 0, sizeof(Banner));

CartROMSize = 0x200;
while (CartROMSize < romlen)
CartROMSize <<= 1;
Expand All @@ -1603,13 +1606,13 @@ bool LoadROM(const u8* romdata, u32 romlen)

memcpy(&Header, CartROM, sizeof(Header));

if (!Header.BannerOffset)
{
memset(&Banner, 0, sizeof(Banner));
}
else
u8 unitcode = Header.UnitCode;
bool dsi = (unitcode & 0x02) != 0;

size_t bannersize = dsi ? 0x23C0 : 0xA40;
if (Header.BannerOffset >= 0x200 && Header.BannerOffset < (CartROMSize - bannersize))
{
memcpy(&Banner, CartROM + Header.BannerOffset, sizeof(Banner));
memcpy(&Banner, CartROM + Header.BannerOffset, bannersize);
}

printf("Game code: %.4s\n", Header.GameCode);
Expand All @@ -1619,9 +1622,6 @@ bool LoadROM(const u8* romdata, u32 romlen)
(u32)Header.GameCode[1] << 8 |
(u32)Header.GameCode[0];

u8 unitcode = Header.UnitCode;
bool dsi = (unitcode & 0x02) != 0;

u32 arm9base = Header.ARM9ROMOffset;
bool homebrew = (arm9base < 0x4000) || (gamecode == 0x23232323);

Expand Down
Loading

0 comments on commit b1e4bd5

Please sign in to comment.