Wednesday, August 30, 2006

sharp-64 - A Commodore 64 emulator in C#

What is it?

As of today, I've scoured the internet, and think it is fair to say sharp-64 (#64) is the first Commodore 64 emulator for the .NET platform. I could go as far to say it's the first 'emulator' for the CLR, given various searches on google turned up nothing on the subject of emulation related to .NET / C# / CLR. #64 is written entirely in C#, using SDL.NET as the sound and audio library. By using SDL.NET, I expect to build #64 as a cross platform executable.

Why?

I've always had an interest in emulation and software emulators for old 8-bit platforms, such as the Commodore 64 and Atari 800XL (both of which I had as a youngster). I hacked around both machines quite a bit as a kid, learning 6502 assembler and playing with their excellent graphics and sound capabilities (of their time).
I also was interested in seeing how well the CLR handled the demands of an emulator, and how much I could optimize the code to gain every ounce of performance out of it.

How?

I cannot claim to have written the emulator from scratch - the credit goes to the excellent Frodo project. A very well designed and modular emulator written in C++. It is also a cycle exact emulator, meaning all the 'emulated' chips are in sync with each other, to the clock cycle. This allows emulation of practically every hardware hack on a real C64. Given the nature of cycle-exact emulators, they also require a greater demand of the platform it is executed upon, allowing me to further push the CLR.

Where is it at today?

It is a fully functioning C64 emulator, with sound and 1541 emulation via D64 virtual disks and mapping a directory. Sound and graphics use SDL.NET, with an extension to SDL.NET audio support for access to the streaming audio capabilities of SDL.
It has no user interface, besides the C64 itself, and therfore is not user friendly, nor is it recommended for a wide audience. Yet.
I have found a couple games do not work; however, they do fail with the official Frodo emulator, suggesting bugs in the original emulation code. I will try to troubleshoot this when I get some time.
#64 does have a few unsafe blocks, using pointers to access the emulated RAM, however I am planning to create some conditional defines and make these optional, to see how much overhead is incurred by the array bounds checks.

How does it perform?

When speed limiting is turned off, sharp-64 runs at greater than 400% of an original C64 on my 1.83GHz Core Duo Dell notebook.
I have tried various demos, such as those with FLI, FLD, DYCP, open borders, multiplexed sprites, timing dependent decoders, fast loaders etc, and they work great.
This is currently a straight port, so we may even be able to get closer to the C++ native version, which runs about twice the speed after some optimizations.

Can I play?

The full source is currently available via svn on sourceforge.net, https://svn.sourceforge.net/svnroot/sharp-c64. There is no documentation on getting it running, so it's up to you to hack around for now. The place to modify which d64 or directory is used for the 1541 disk drive is in prefs.cs.

Screen shots?

Basic CBM Basic

Decath Decathalon

BlueMax  Blue Max

14 comments:

Emulator.net said...

please can you tell me how to make emulators in c#

Thraka said...

Very cool! Thanks a bunch for the port! I wish more people would do ports to .NET. While I cannot do ASM nor any real C++ I can do C#. What is needed next is a port to XNA so we can play this on our XBOX 360 :):)

Reminds me how the Quake2 engine was ported to .net. But it was C++ .net so I don't know what the point was ;)

Thanks again! Long live C64!

Stuart said...

My ultimate goal is to port to XNA. I am contributing the the mono XNA project also.

nitrofurano said...

MonoXNA would be a huge goal! I'm curious seeing this working from the Debian repository! =)

Anonymous said...

Against what version of SDL.NET does the Sharp-C64 code compile? I've already tried versions 4.0.4, 6.0.0 and 6.1.0, but none of them seem to work.

Stuart said...

It should be 5.x

Anonymous said...

Thanks! So yes, #64 (as of SVN revision 13) builds successfully against SDL.NET version 5.0.0 (of 2006-10-17). By the way, what version of Frodo did you use as a baseline for the port? I'd like to do a comparison to see how you did it.

Stuart said...

No worries. It is using Frodo v4.1b, as per http://frodo.cebix.net/

Cheers

Anonymous said...

Just a hint for those trying to get this going on a 64-bit machine: You need to change the build properties of the projects to specify "x64" for the platform target, instead of the default "Any CPU" value (or else the application would simply crash due to a BadImageFormatException).

Anonymous said...

Oops, that should have read "x86", not "x64" (that is, you need to target a 32-bit platform explicitly).

Anonymous said...

Oops, that should have read "x86", not "x64" (that is, you need to target a 32-bit platform explicitly).

Stuart said...

That's a good point - it must be compiled for 32-bit as the native SDL libraries are 32-bit, thanks 'Anonymous'

PrinceVlad said...

So, where is Sharp-64 today? There hasn't been any news for months... is it dead?

Anonymous said...

I would love to marry this up to the COSMOS project and build a commodore 64 *operating system*. Youd have to do away with SDL and just use text and VGA modes, but it seems possible.