Thursday, April 15, 2010

DeSmuME Wii : From Beginning to Today

This post is the culmination of about five days of writing detailing the entire development process DeSmuME Wii has gone through. A lot of people all over the Internet have been asking for this, I hope that everyone will enjoy it. For the most part, I keep things as easy to understand as possible so that anyone can read along and follow the development cycle.

Ever since Wii homebrew happened, people have been asking if a DS emulator was possible to be made for it. (I remember reading topics about the possibility since the first day of the WiiBrew Forums creation) Based on the hardware specifications alone, I was confident that a DS emulator could in fact happen on the Wii. So near the end of July of 2009, I decided that I would write one.

Whenever you program anything, it is always best not to reinvent the wheel but to use whatever already exists build it.  To that end, I had no will to write an original Nintendo DS emulator for the Wii but to take an existing one and port it to work on the Wii. Remember that port an emulator, it has to be open source. DeSmuME is without question the best available open source Nintendo DS emulator, so that is the one I decided to port.

I began with the 0.94 release of DeSmuME. The first step to porting anything is to first rearrange the project and it's makefile so that it will compile for your target platform in lieu of it's originally intended platform. From there, you compile the project. Any errors that come up are from code that is specific to the originally intended platform that needs to be replaced with code for the target platform instead.

Some of the code for DeSmuME is based around the SDL library. There's an almost perfect port of SDL for the Wii so by incorporating it into the project, about 50% of the project was automatically ported. There's also an awesome library called gl2gx which is a wrapper of the Wii's GX video system under OpenGL function calls. By incorporating this into the project, another huge chunk of code was automatically ported.

The remaining code that couldn't be easily ported had to be rewritten. Sometimes this was as simple as using printf() in place of GLib function calls. Other times it was complex as completely rewriting logical emulation flow from scratch (just to get rid of those few st00pid lines that wouldn't run on the Wii).

In the hopes that I would get help on the project (or at least have my mistakes pointed out), I documented everything I did in the porting process in great detail. You can read those posts here. Profetylen tried to help out but was never able to accomplish much beyond pitching ideas to me and rearranging files. None the less, I greatly appreciated his help and it is worthy of noting his contributions.

After porting the emulator, I had to write a front-end for it from scratch. If you think of all of the functions of DeSmuME as a library (I call it the DeSmuME core like most people), the front-end uses the core's functions to actually make emulation happen. The front-end I wrote was a simple one: It loaded a single ROM in the root of the SD Card and attempted to emulate it.

Like most ported things, the worst thing that could possibly happen happened. And that is, nothing happened. Once you have GX turned on, you can no longer print text to the screen with a console window. So I used a special console written by Twiizer dhewg so I could print text to the screen and debug the problem.

The problem turned out to be in the video frame buffer, but I really wasn't too sure what it was. For the next 2 months I would mess with it, never coming up with much. In October, DeSmuME 0.95 was released. I decided to restart the project from scratch using DeSmuME 0.95 as a base hoping that it would fix whatever was wrong between the 0.94 and the Wii. The process of porting DeSmuME 0.95 was identical from an outside view. From an inside view, there were a few less parts to be rewritten. You can read the SVN logs if you're truly interested.

Dancingninja of VBAGX fame had been carefully following my DeSmuME Wii project and decided to help me out. Thanks to him, DeSmuME 0.95 was ported much more quickly than it could have been if I did it alone again (again though, we basically followed the same porting process done with 0.94).

Once again, we had the same problem as in DeSmuME 0.94 when we were done. We made a little more progress in solving it than last time (we now knew the problem was memory related) but we still had no idea of what we were even doing. scanff of WiiRadio and BashTheCastle fame noticed that all we needed to do was allocate more memory to the video components and hola! We had (very very slow) DS emulation on the Wii.

To speed up the emulator, we first decided to place NDS ROMs into virtual memory and read them from there instead of directly reading them off of an external device. As reading NDS ROMs from RAM is faster to read them than from ROM, storing the NDS ROMs in virtual memory gave the emulator a huge speed boost.

The next thing we did was remove SDL from the project. SDL is nice to play with but it has a huge stack which is highly inconvenient for an emulator. I rewrote the input portion of the emulator to use direct libogc calls from the wiiuse library. ToadKing (aka baby.lueshi) of SDL MAME Wii fame joined the project and worked with scanff and I to write an emulation sound core that doesnt use SDL and to use direct GX to render emulated video instead of SDL. Removing SDL greatly increased the speed of the emulator.

The last notable change we made to increase the speed of DeSmuME Wii was to eliminate the OpenGL and video recording portions of it. In lieu of OpenGL, we use DeSmuME's faster (but ugly) rasterizer.

It's impossible to continue this post without giving a shout out to firnis, the author of PCSX-Revolution who has greatly contributed this project. Thanks to him, emulation is more accurate than ever and we have an awesome file browser too.

So where are we today?

We've achieved emulation much faster than ever considered possible. According to this video, we've actually got Super Mario 64 DS running a tad faster than DeSmuME running on PCs. 2D games are still a little slow compared to being run on PCs, but that's because of the Wii's limited hardware. We are working on rewriting code to maximize the power of the Wii. The progress we've made is simply incredible.

For the future, we're beginning a very a preliminary study into dynarec emulation. If we are able to write a dynarec to be used instead of DeSmuME's interpreter emulator, we'll have full speed on nearly every title. (Look at what dynarec emulation did for Wii64 and WiiSX). Right now though, are primary focus is to iron out all of the graphical glitches so that emulation is perfectly accurate despite being slow.

Stay tuned, many more exciting things are to come =)


  1. Nice writeup.
    Do you think full speed Ds emulation is possible on Wii?

  2. Nice, can't wait =D

  3. Kudos to all the members in this great project, I look forward to play Chrono Trigger DS in my Wii :3

  4. You are the best! Keep up the good work!!! :D

  5. wow, I didn't know how much work and different people was involved in this project, no wonder it has advanced so much hehe.

    Congratulations on your great work, and thanks for it.

  6. This comment has been removed by a blog administrator.

  7. wow amazing work arikado!!! 5 thumbs up! now the wii can officially run every nintendo game ever created. ill be following this project closely and if i had any programming knowledge id help out. best of luck man =)

  8. I love this for the fact that I can record DS games with my capture card now, but it's REALLY SLOW. I hope you guys will be able to fix that. ;) Other than that, it works great!

  9. Impressive work!

    I understand this is not yours to address,
    but I do you think a (right) analog stick on a pc gamepad can be used to control the stylus for the pc version of DeSmuME?

  10. i am playing new super mario bros and it is so slow

  11. i recently just modded my wii and got the desmume emulator. all games lag so far that ive tried. Lag to the point i dont even want to play them. its not fun. ive been reading alot about this today and see that the issue is not only me. When could we exepect a update that will fix this lag for the wii?