You may be wondering, have I been dead for a few years? The real fate is much worse… I wrote a Mega Drive emulator. Yet again…
The great news is this time around, it actually works and I have yet to find some software that it doesn’t at least somewhat run. It’s fairly close to hardware accurate (in fact, I ended up building out some Verilog models of the VDP to understand why it works the way it does… that’s a whole different story).

Well, the ROM hacks that actually worked on real hardware, anyways. Which is surprisingly few, because I was too lazy to test on hardware back then…


Why?
It’s cheaper than therapy and I needed something to procrastinate on my actual work. Plus I said I was going to do this on IRC in like 2009, so basically I had no other choice. (That part isn’t bullshit1) I concocted the vast majority of this (that is, the VDP and YM2612 core) during a week and a half in Alaska last summer. I’ve done a bit of work on and off since then, but turns out a decade of farting around on the MD left a lasting impression in my brain. For better or worse…
Also, emulation is a really cool challenge - especially making it accurate and high performance. Currently the emulator sits around 4ms per frame, so roughly 4x real time speed. I think that’s not half bad, but there’s probably room for more optimizations: some that might be necessary whenever I try and run this on an x86 machine.

Mega Drive?
If you’re an US-ian, you’ll probably know this as the Sega Genesis instead. It’s a lovely ~7.67MHz 68000 CPU, 64K of work RAM, a complex but fun video processor, and a Z80 with its own 8K of RAM controlling a 4 channel PSG and 6 channel YM2612 FM synthesizer. (For a more in depth overview of the hardware, check out Rodrigo Copetti’s Mega Drive Architecture page. It’s bang on!)
If you’ve read this blog before (or just looked at the last post… three years ago) you’ll know that I’ve written about the Mega Drive before; so check that post out if you want some more information about the console from the perspective of programming it.

How?
It’s a fairly simple emulator as far as emulators go, there’s a global clock that ticks at the rate of the main clock (~53MHz) and all devices are ticked in sequence. Since basically everything but the VDP uses a divided down clock, that means most devices will run only every dozen clock cycles. For efficiency, most devices will bunch up their pending cycles: for example, the YM2612 needs 36 clock cycles to generate one output sample; the VDP will try to emulate an entire scanline at a time, unless the 68k writes to it.
Design
The entire emulator actually works as a standalone library: the GUI shown in the featured image just links against it and provides input and output via standardized interfaces for it. It’s all C++ and virtual functions all over the place (and there is actually a noticeable performance overhead from all of the virtual function calls – around 3% per frame – on my M2 Mac) but it works :D
The only part of the Mega Drive I had really not studied too closely over the years was the Yamaha YM2612, and that ended up being surprisingly straightforward once I actually got to it. I’m also kind of sort of cheating by using an existing Z80 CPU core instead of writing my own like I did for the 68k core. But the Z80 doesn’t really hold that special cult status in my heart that the 68k does :)
What’s Next?
There are still quite a few bugs. For one, the 68k seems to be running somewhat too fast compared to the rest of the system: perhaps I’ve screwed up integrating the thing or forgot to emulate some slowdown. It doesn’t seem to affect most software, but if you have a keen eye, you may have noticed some CRAM dots creeping into the active scan regions in some screenshots.
Right now I’d also like to see if I can port it to some other platforms: there’s nothing really tying it to macOS (other than I’m using Metal as the graphics backend: but it’s SDL3+imgui, nothing that won’t run elsewhere) so I want to try porting it to FreeBSD next.2 Maybe that will unlock Linux and maybe Windbowls, but I don’t use either of those so I don’t really care all that much.
Glitch Art
Surely you didn’t just come here for the cool looking fuck-ups, did you? Ugh. You’re filthy.
If you insist…

I hear you saying, the Mega Drive only has 64K of VRAM… well, buckle in for a wild ride because boy howdy does the VDP hold some secrets. No commercial software actually made use of this mode (because the retail hardware only shipped with 64K3) but some pretty neat demos by some cool people do.

Where acquire?
I haven’t really figured that out yet. I guess one of these days I’ll clean up the code and make it available, but I’m not really too keen on giving the techco’s™ more free training data. Until then, if you’re cool, email me and I’ll send you a source tarball.
If I didn’t know any better I’d say something along the lines of “oh I’ll write a series of posts about this emulator” but it’ll probably be 2029 before I remember this thing exists. Maybe not. Who’s to say?
