Mips2cs is a tool that converts MIPS machine code into an equivalent C# program.
Some people suggested that I should port Knights to the Xbox 360. I looked into it, and read about the XNA tools, which allow anyone to write games for the 360. There was only one problem: XNA assumes you are going to write your game in C#, while Knights is 100% C++ code.
Clearly, rewriting all of that C++ code in C# was going to be a lot of work. So I looked into ways of converting C++ into C# automatically. Mips2cs was the result.
The main idea is that we compile the C++ code into MIPS machine code (using GCC) and then convert the MIPS code into C# (using Mips2cs). The C# code can then be compiled using any C# compiler.
C++ is a large and complex language. Writing a tool that can parse arbitrary C++ code, and convert it to C#, is no easy task. Therefore, while there are some existing tools that can do it (e.g. code2code.net), none of them are fully automatic. You always have to edit the generated C# files by hand afterwards.
Machine code, by contrast, is very simple to work with, and converting machine code to C# is relatively straightforward. Therefore Mips2cs can work without any human intervention.
The downside of the Mips2cs approach is that it produces fairly incomprehensible C# files (they read like disassembly listings). However, in my case, this was not a concern since I was not planning to edit the C# code manually. I was much more interested in being able to do a fully automatic translation.
The MIPS architecture is much simpler, and easier to work with, than x86. In particular:
In fact not. There are two other projects that came up with the idea before me. However, they are targetting Java rather than C#. Here are the links:
There is also Mips2Java which is an earlier version of NestedVM.
It certainly is. Visual Studio offers two settings, /CLR:SAFE and /CLR:PURE, that can be used to do exactly that. However, there are some issues:
There is also a CLI backend for GCC. However, it appears this supports only C (and not C++) at this time. Also, it is not clear whether the generated binaries will run on Xbox 360.
Due to these issues, I haven't looked any further into the "compiling C++ to bytecode" approach. I decided to use the Mips2cs approach instead.
It probably would be better. However, that would require learning details of CIL assembly language, CLR bytecode formats, etc. For now I haven't bothered to do this. However, it might be an interesting approach to take in future.
Mips2cs cannot translate code that makes calls to the operating system. This means that code such as file I/O, or code that calls DirectX or OpenGL for example, will not work without modification.
Instead, Mips2cs provides a mechanism for the C++ program to call arbitrary C# code at runtime. Therefore, if you need to draw graphics (for example), you would write the required functionality in C#, and then arrange for your C# code to be called from the C++ program as required.
Mips2cs comes with two ready-made libraries which illustrate this process. These are:
Mips2cs performs a few optimization passes (such as constant folding and dead variable elimination) in order to improve the quality of the generated C# code. Also, of course, the .NET runtime is performing JIT optimizations of its own. So, the performance isn't as bad as one might think.
I haven't done much performance testing yet, but initial testing showed that the generated C# code is (very roughly) 50% slower than the native code, when doing integer operations.
Floating point operations will be a lot slower, because Mips2cs doesn't support the MIPS floating point instructions yet (so all floating point calculations are using software emulation).
As a demo I have written a very simple Arkanoid-like game.

The starting point was to write the game code in C++. This came to about 500 lines of code (not counting the code for the "graphical library" – see above – which provides the interface between the C++ code and XNA). A source code listing can be found here: bouncy_ball_game.cpp.
This was then compiled to MIPS machine code with GCC, and converted to C# source code using Mips2cs. If you are interested in seeing what the generated C# looks like, see: bouncy_ball_game.cs.
Finally the C# was compiled with Visual Studio / XNA to a Windows executable. It can be downloaded here: BouncyBallGame.exe.
The process worked perfectly well and the game plays without any problems on Windows. In theory a 360 version could be made as well (but the code would have to be modified to use an Xbox controller instead of a mouse).
Mips2cs is written in Haskell. The source code can be downloaded here: mips2cs.tar.gz.
The download includes instructions on how to build the required GCC cross compiler, as well as how to build and use Mips2cs.
If you are interested in the technical details of how Mips2cs works, then the following documents are available:
Originally Mips2cs was intended as a way to port Knights to Xbox 360. The project is now at a "proof of concept" stage, i.e., the basic concept of converting C++ code to C#, via MIPS machine code, has been proven to work. However there are still several tasks that would need to be completed before a 360 version of Knights could be produced:
There are also possibilities for further work on Mips2cs itself, as follows: