MelonDS updated to v0.3, Features Wifi Emulation

Just a few days again Developer StapleButter posted a picture showing Pictochat running in his Emulator MelonDS. Nothing interesting so far right? Wrong. This is very interesting since no emulator to date is able to emulate the DS WiFi connection.

Not even GBA, it could only get into the main menu and then loose connection if lucky and DeSmuMe, where StapleButter initially worked on WiFi Emulation,  sabotaged his work on WiFi Emulation and pointed out that they will never emulate WiFi. With MelonDS he wanted to fix this lack of support and with this he just got a lot closer to the goal of fully and completely emulating WiFi.

Here’s what StapleButter had to say about the whole Situation:

“If you have followed melonDS from the beginning, you’d know that WiFi was one of the goals. And well, it’s getting there.

The first melonDS release, 0.1, already included some WiFi code, but it was a very minimalistic stub. The point was merely to allow games to get past WiFi initialization successfully. And it even failed at that due to a bug.

Chocolate waffles to you if you can locate the bug, by the way 😉

But well, at that stage, the focus wasn’t much on WiFi.

It was eventually fixed in 0.2, and some functionality was added, but it still didn’t do much at all. Games finally got past WiFi initialization, but that was about it.

It wasn’t until 0.3 that some serious work was done. With the emulator core getting more robust, I could try going for the WiFi quest again. Not that 0.3 went far at all — it merely allowed players to see eachother, but it wasn’t possible to actually connect. But it was something, and infrastructure for sending and receiving packets was in place and working, as well as a good chunk of the WiFi hardware functionality.

You may already know how it went back in the DeSmuME days. As far as local multiplayer was concerned, I kept hitting a wall. Couldn’t get it working, no matter how hard I tried. WFC is a separate issue.

It didn’t help drive motivation knowing that my work was doomed to stay locked behind a permanent EXPERIMENTAL_WIFI wall, requiring a custom WiFi-enabled build, and that the DeSmuME team’s public attitude is to sweep WiFi under the carpet and pretend it doesn’t exist, but the main issue was the lack of documentation as far as local multiplayer is concerned.

The DS WiFi hardware isn’t a simple, standard transceiver. It is a custom, proprietary WiFi system made by Nintendo for their purposes. It has special features to assist local multi-play communication at a fast rate.

GBAtek documents the more regular WiFi features well enough to get things like WFC working, but the specific multi-play features weren’t well known until now. Many packet captures have been made, several multi-play protocols like Pictochat and download play have been reverse-engineered, but details on hardware operation were at a loss. The hardware operates in a very specific way during multi-play, and there’s some real clever design there.

As the DS doesn’t support ad-hoc communication, multi-play follows a host-client scheme. Clients can’t directly communicate between themselves, everything goes through the host. Who is the host depends on the game — it is generally the person who starts the multiplayer game.

The host sends beacon frames at a regular interval to advertise the game to potential clients, pretty much like how access points send beacons to advertise their presence. The DS WiFi hardware allows to automatically send beacons at a set interval, but there’s nothing too special there.

When a client connects, the regular 802.11 association procedure is followed. The client sends an auth frame, to which the host replies with an auth frame, then the client sends an association request, and the host replies with an association response which gives the client its ID.

At this point, multi-play communication begins, and this is where the real meat is.

The host sends data frames using a special TX slot (which GBAtek names CMD). It doesn’t simply send the data, it actually assists the whole exchange.

The host data frame has a bitmask telling which clients should reply. Each client is given an ID from 1 to 15.

So, after the data frame is sent, the hardware waits for those clients to reply. It uses said bitmask, as well as a register telling how long a client reply should last, to determine how long the wait should last. It can report when clients fail to reply within their window (assuming clients reply in order).

The client doesn’t have much control over the process. It has a register that tells where its reply frame is in memory, and a register that holds its client ID, and that’s about it. When receiving a data frame, the hardware checks its client bitmask against the client ID register to determine whether it should reply. The data frame also contains the client reply time. With these data, the client can determine when it should send its reply, and do so automatically. This happens even if no reply frame address is set — in that case, it sends a default empty reply.

When all this is done, the host sends an automatic acknowledgement frame to the connected clients. This frame is different from the standard 802.11 ack in that it is received like a regular frame and processed by the clients. The standard ack isn’t exposed to software.

If any replies are missing, the host will attempt to repeat the whole process (maybe altering the client bitmask so the clients that replied successfully aren’t polled again, I haven’t checked), if there is enough time left — the CMDCOUNT timer defines a window during which the transfer is possible. When sending packets, it needs to wait until noone else is sending things, crosstalk isn’t a good idea. Regular TX slots seem to keep waiting until the channel is free, but the CMD slot can abort if it has waited for too long.

All in all, a neat piece of hardware. You can guess that missing details on all this doesn’t help getting multiplay working, especially as there are various status bits that get set here and there during the process, some of which are crucial.

So I set to work. I managed to replicate multiplay communication between two DS’s with homebrew, and used it to work out the details. This, coupled with reverse-engineering of the Pictochat code, allowed to finally get it working in melonDS.

It isn’t perfect, but this is definitely progress, as neither DeSmuME nor NO$GBA have gotten this far. As far as Pictochat is concerned, it appears that host->client transfers work fine, but client->host transfers sometimes get partially corrupted or lost entirely (and cause the client to get stuck unable to send).

NSMB MvsL also works, and seems rather stable, even though it can disconnect on you. There is some slowdown which appears to be caused by data loss.

There are also issues stemming from the communication medium used, namely, plain old BSD sockets. With UDP, it’s easy and cross-platform, but there are issues. Binding to 127.0.0.1 gave the best results for me with Pictochat under Windows, but I got feedback that it doesn’t work under Linux. Binding to all possible addresses works under both Window and Linux, but seemed to be worse for Pictochat, while being better for some other games. So we need to work on this too. BSD sockets have the advantage that they allow playing on separate computers over LAN (in theory — this hasn’t been tested), but maybe we will need faster IPC methods. Multiplayer sends packets at an amazing rate (Pictochat sends every ~4ms), so the communication medium has to keep up with this.

Stay tuned for more exciting reports!”

This is really great news for everyone waiting for proper and complete DS Emulation. When this is complete we can theoretically play Mario Kart over LAN with other players, Trade Pokemon or do lots of other stuff. This is still WIP but it already works really well.

Source: GBATemp

About Darthsternie

Interested in everything Technical. Loves self-repairing Tech. Collector of Firmwares. Enthusiast Gamer and Anime Fan ^^

Check Also

Hardware Review: EZ Flash Parallel for NDS

I’m going to start by being entirely transparent at the beginning. I’m reviewing a product …