Gavin Pugh - A Videogame Programming Blog

XNACPC – An Amstrad CPC Emulator for the Xbox 360

21 May, 2010 at 9:17am | Life, XNA / C#

A few weeks ago I mentioned on twitter that I was working on an Amstrad CPC emulator, written in XNA. There are plenty of the things for the PC and various other consoles, but not one for the Xbox 360 yet. So it seemed a cool little exercise to undertake.

A long time ago, I wrote one of my own for the PC. The website I setup for it is still live, here. I developed it initially around 1997. Back then I’d pretty much just gotten it to be able to boot, and be able to type things on the keyboard. I revisited it a few years later when I needed something flashy for my portfolio, around the time I was trying to break into the games industry. With that version I managed to get a handful of games running nicely.

Little bit of history

I used to be really into the emulation scene back in the day. The first one I stumbled upon was on a BBS, some random BBS I found in the pages of Computer Shopper (wow, that mag is still going!). My father used to go to his work to wash his car on the odd weekend here and there. I used to use his Apple Mac computer when he was doing that, was one of the old Mac Quadras. This would have been around 1993 I think, so I’d have been a spritely twelve or thirteen years old at the time!

Anyhow, I’d use some terminal software on the Mac, downloading various interesting programming bits and bobs via the trusty ZMODEM protocol. One weekend I was stunned to see something called CPCEMU. This was the first Amstrad emulator ever to see the light of day. I didn’t even know such a thing was possible at the time; I’d pretty much figured that it could well be a joke download. So after copying my weekend’s haul to a floppy as per-usual, I was excited to give it a try.

This software and much of the rest of the stuff I downloaded was PC-only. I didn’t have a PC at home at the time; I’d just use the ones at my school during lunchtimes. It was really cool to see the emulator fire up on that PC at school. Unfortunately though the Amstrad ROM wasn’t included in the package, so all that worked out of the box was a single sample game written to interface directly with the hardware. That year I remember my ‘big’ Christmas present was a 3.5” disc drive for the Amstrad, primarily so I could dump the ROMs from the machine so I could get the emulator working! What a nerd. 🙂

Around the time I’d also written in to the only remaining Amstrad CPC magazine in production: Amstrad Action. They had some feature where they asked readers to write in with any sort of Amstrad-related article, and they’d print them up in some sort of ‘readers articles’ feature. This never actually happened though unfortunately; they never did run any reader articles. I’d written one in about CPCEMU, whilst it never appeared I did get a mention in the magazine. After a little googling, I managed to find it:



Amstrad Action – #104 – May 1994 (click for larger image)

 

Brought a smile to my face seeing it after all these years!

Other emulators

There were a number of other emulators I ‘followed’ back in the day too. GenEm; the first SEGA Genesis / Megadrive emulator out there, was one in particular. Was really something to see it improve over time. From humble beginnings where only a handful of early-era games are only playable. Through to audio emulation working (the Genesis/MD has a second processor; a Z80 funnily enough, which is primarily used for audio in games). Around this time was an explosion in emulators in general, for all sorts of other videogame systems of similar eras. There was a bit of a race between coders to get their emulator to be the most fully featured, with the highest game-compatibility rate. Competition drives technology, I guess?

Some other ones I remember too were KGEN and DGEN. But the real leap at the time I recall was Genecyst. It was really leaps and bounds above the other ones out at the time. The same guy (or group) that coded it also wrote the impressive Callus, and NESticle. Both landmarks at the time, particularly Callus which I think was the first Capcom arcade emulator that ran at a playable speed.



The UI for Genecyst, all emulators for the era were in DOS. Unlike most, this one had a GUI too

 

I also remember Executor well too, an Apple Mac emulator. This one took an interesting approach of not making use of any System ROMs at all. They essentially reverse-engineered the Mac. The emulation effectively emulated the services/OS calls that the system ROM would have provided. This I think was more of a legal choice than a practical one, but it must have been an interesting technical challenge. They updated it pretty often, it seemed like every week there was a new one to try that had a new slate of improvements. Unfortunately it was a paid-for emulator. I think it had a time restriction on it, it worked for ten minutes and then quit out. That was a rarity back then in the emulator scene.



Screenshot of Executor in action

 

As I was looking for stuff for this article, I was surprised to see that it’s now open-source. I think now there’s probably several other Mac emulators which take the more traditional approach, and have a near 100% compatibility with a real 68k-era Mac. I wonder how well it sold throughout its lifetime?

An XNA CPC Emulator

Christ; I do go on like an old woman, don’t I? 🙂

The point of this article was meant to be about the Xbox 360 version of the emulator I’m working on. I did most of the work for it over a weekend, about a month ago. The missus was away for work, and I had a nice block of time to tackle it. I essentially just used my previous emulator I wrote a few years prior, in plain C. I rewrote it in C#, in a much more OO-way.

It was as a pretty straightforward process. As I’d done with my older emulator, I took the Z80 CPU emulation from an existing source. Someone has already coded a ZX Spectrum emulator in C#, so I took the CPU code from there. The Zx360 project. I had to make a few modifications though. One was based on how memory is accessed on the CPC, to handle the ROM and RAM banking. The other was for the way interrupts are handled. I know very little about the Spectrum, but it seems like they do behave pretty differently in that regard.

So, dusting off the cobwebby corners of my mind, I got to implementing all the various bits of hardware of the machine. Using both my old source, and the Unofficial Amstrad WWW Resource for reference. The code quality was like night and day compared to my old plain-C implementation. That’s I guess what thirteen years experience will help you out a bit with! It a joy to debug too; all the little components of the machine browsable in the Visual Studio debugger, in a nice OO-d tree!

I worked on the actual XNA-specific parts last. Getting the screen drawn, and getting input working that is. It’s just as cool as I remember when you get that startup screen rendering the very first time. This old thing:



XNACPC bootup, the Amstrad CPC 464 startup screen

 

I then went on to get snapshot (.SNA) support working so I could load up memory snapshots of games. That was awesome to see as well. It had the exact same level of compatibility as my older emulator. I was a little worried the new Z80 core code could have some bugs or issues, but it seems solid now. So anyway, the same games that ran fine worked fine on this too. But of course the same ones with issues, also had issues on my new emulator too. Once I’d played a few games on the Xbox itself, I took a break from the project for a while.



The classic ‘Roland in Time’ running on XNACPC, on an Xbox 360

 

A bit later on

What I really wanted to add was proper audio support. I dabbled for a while using XACT; playing a sound and messing with the pitch and volume of it. But it had pretty hideous results. It was encouraging to hear the tunes in there somewhere, but it really didn’t work well at all. What led me to stop bothering with this method was news that XNA 4.0 has nicer audio support. Specifically the class DynamicSoundEffectInstance. This class allows playback of raw audio data, not just samples. Exactly what an emulator needs.

The other avenue I wanted to make improvements on was to how I emulate the video hardware. My older emulator took a very naïve approach. It didn’t make any effort to emulate vsync, hsync or tie in the CPU interrupts with any of it. Over the past few weeks I’ve had some time here and there to mess with this stuff, and attempt to get something better working. I’m pretty pleased with the results. I managed to get split-screen working. Which means ‘Sorcery’, one of the classic CPC titles is now fully playable and looks just like the original.



Sorcery on XNACPC – A CRTC-splitscreen game

 

I’ve still a way to go though. My CRTC and video emulation isn’t quite up to par with some of the more accomplished emulators already out on PC. Well, that’s an understatement; it’s truly awful compared to most of those emulators! But it’s something I can improve at my leisure. 🙂

I plan to release the source code for the emulator, specifically once I’ve got my video code a little more stable. Whether that’s a matter of weeks or months, I don’t know. I’m pretty busy at the moment with work, so it may take a fair while. I’d probably expect it to be after XNA 4.0 is released too, since I’d probably get some crude level of audio support in once it’s out. I’d dabble with the CTP version that’s out now, but I’m really targeting Xbox which is not supported just yet. Yes, I could write the code on PC. But I really like firing up the emulator on the Xbox, and seeing the cool stuff running on there first. I can be patient…

Comments

Comment from John Hampson
Time: July 21, 2010, 12:05 am

I remember reading this article a few weeks ago, and I’ve just read it again now.

Really interesting stuff and I love the look of sorcery on that TV. Emulators have always interested me, but I have never taken the time to look how they are coded, and indeed I wouldn’t know where to even start. I’m looking forward to hearing more about the progress of your project.

Comment from Gavin
Time: July 28, 2010, 10:51 am

Thanks!

Back a long, long time ago when I first started to work on this project. I remember this text by Marat Fayzullin being a good starting point:
http://fms.komkon.org/EMUL8/HOWTO.html

Seems like XNA 4.0 isn’t too far off from being finished (and working with the 360), so hopefully won’t be long until I can revisit this and throw up some sourcecode.

Cheers.

Pingback from XNACPC: Xbox 360 Amstrad CPC Emulator, released! « gavpugh.com
Time: November 11, 2011, 9:45 am

[…] and off during the last month or so, I’ve worked toward finishing a project I wrote about last year. It’s an Amstrad CPC Emulator, written in C# using the Microsoft XNA library. I’ve […]

Write a comment



 (not shown when published)



 (comments will appear after moderation)

*