clownmdemu - The Greatest Mega Drive Emulator Ever (Someday)

Discussion in 'Showroom' started by Clownacy, Jun 23, 2022.

  1. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    948
    Since September last year, I've been working on-and-off on a Mega Drive emulator. It's not even close to being complete, but when will it ever be? So I figure that I might as well release it now, because there will always be some feature left to be implemented, or some game that doesn't work right. Waiting for it to be 'ready' is a fool's errand.

    clownmdemu!
    You'll never guess what the name's short for! :‎D

    [​IMG]

    [​IMG]



    I haven't done a whole lot of testing with this, but it does appear to work with Sonic 1, 2, 3, & Knuckles, Puyo Puyo, and ROM-hacks like Sonic 2 Recreation. Sonic 3 is a little glitchy at the moment.

    The basic hardware of the Mega Drive is emulated, but not to completion: things like the YM2612's SSG-EG and LFO are missing, as well as support for the VDP's Window Plane and the 68k's instruction cycle durations. Basically, games that do run in the emulator may be missing certain effects. Other games just don't boot, like Combat Cars and Micro Machines. If you want to see exactly which features are and aren't currently emulated, there's a list here.

    As you can see, the emulator comes with some debugging utilities. I figure that they'll come in handy for ROM-hack development, or even just finding out how a game works internally. For example, did you know that Sonic 3's Data Select menu uses Plane B for the foreground and Plane A for the background, rather than the other way around like it usually is?

    Why
    I've been programming for the Mega Drive since late 2012. That's almost 10 years ago! I know practically everything there is to know about how games use the Mega Drive hardware, which means that I know everything that would have to be done in order to run those games on other platforms.

    I figured that writing an emulator would be a good way to put that knowledge to the test.

    Code
    Unlike some other Mega Drive emulators, this one has been created entirely from scratch: no MAME or Gens code here! In fact, I think that the codebase is what makes this emulator unique: it's written in...
    • Rust? No, that's dumb and bad and you should be ashamed for suggesting it.
    • Cutting-edge C++20? Eww, who wants that overcomplicated mess?
    • x86 assembly? Yeah right, as if I want my code to be locked to one specific architecture.
    • Go? JavaScript? D? Python? Swift? Leave this thread now.
    • Portable C89...? You got it!
    You might be thinking 'Hey, that's not unique at all!', but I beg to differ: so many C projects claim to be "portable", but then go on to use fixed-size integer types like 'uint16_t' for no reason whatsoever (they aren't even guaranteed to exist), treat 'int' like it's always a 32-bit type (it's only guaranteed to be at least 16-bit), and use logic that only works on little-endian architectures (because who's heard of a big-endian CPU, am I right?). Additionally, being written in strict C89 means that the emulator can theoretically be built with vintage compilers for ancient platforms (16-bit DOS port, anyone?).

    Another novel feature is that the emulator is separated into two components: the core and the frontend. The core contains all of the emulation logic, while the frontend contains all of the platform-dependent code for reading input and presenting the video and audio to the user. Yes, I do intend to eventually leverage this to create a libretro core.

    Another major feature of the emulator's code is that it avoids global state: all of the emulator's subsystems access their state through a struct pointer which is passed as a parameter to every function. This is essentially 'proto-C++' object-oriented C. On top of allowing multiple states to be used (thus theoretically allowing multiple Mega Drives to be emulated at once), this also makes for incredibly efficient rewind support. In fact, both rewinding and save states are entirely features of the frontend: they have been completely abstracted away from the emulation core itself.

    You can read more about the quirks of clownmdemu's code in its README.

    Goal
    My goal with clownmdemu is not to create the most accurate Mega Drive emulator, nor is it to create the fastest. Rather, mine aims for an in-between: to make an emulator that produces 'correct' behaviour externally while being as efficient as possible internally. External accuracy, yes - internal accuracy, no. An example of this is how the FM and PSG are updated: rather than do so once every clock tick, the emulator only updates the FM and PSG when the 68k or Z80 attempt to access them, and it does so in bulk, batch-processing them until they have 'caught up' with the rest of the system.

    Download
    But what's the use in describing the emulator here? You can try it for yourself: here's a link to where you can download v0.1. Its EXE is 32-bit, and should work on versions of Windows as far back as Windows XP. If you're compiling it yourself on Linux, then that should be simple enough: the build script is CMake, and it depends on the FreeType and SDL2 libraries. Naturally, you can find the latest source code here.

    Usage
    The controls are currently hardcoded, but aren't hard to change if you edit the frontend's source code. You can find a list of key bindings in the emulator's README. Notably, you can quick-save with F5, quick-load with F9, fast-forward with the space key, and rewind with the R key.

    Development
    Since starting this emulator, I've been documenting its development on my blog. So far there have been 8 posts, which I'll list here:
    I'll try to cross-post future posts here, so that anyone who's interested in this emulator can stay up-to-date with its development.
     
    Last edited: Jun 28, 2022
  2. ⸸ devon ⸸

    ⸸ devon ⸸ I am CRUSHING YOUR HEAD!!!! Member

    Joined:
    Aug 26, 2013
    Messages:
    1,115
    Pretty cool stuff here. I don't have much time to really look too deeply in it, but a couple things I wanna note with a couple of ROMs I did test:
    • Dual PCM FlexEd does not seem to work properly at all. I have no idea if you are emulating the undocumented Z80 instructions or not, or if there's some kind of other issue.
    • Audio pitch seems to be a tad bit too high to my ears. PSG reminds me a bit of Regen's pitch, which is also off.
    • With my Grimoire of Alice in Fractal Sound ROM, the driver freezes up. I can still move my sound selection and go into the different viewing modes, but I can't do anything else. Might have something to do with Dual PCM?
    • Definitely a result of Dual PCM, but FM and DAC do not function in Dr. Yundong.
     
    Last edited: Jun 23, 2022
  3. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    948
    Thanks for testing it out!

    The undocumented instructions are already implemented, but a few of the regular instructions aren't, like DAA and CPIR. Dual PCM FlexEd might be falling victim to one of those, or maybe it's just an undiscovered bug in the Z80 interpreter.

    The pitch could be a couple of things... the audio doesn't run at the exact same sample rate as on a real Mega Drive: it tries to account for rounding error introduced by the framerate manager which might be throwing it off. The PSG, I was suspicious could be incorrect: I'm not sure whether the internal timer should check if it's reached zero before or after decrementing. Though, I'm not sure if that makes an audible difference. Now that I have my real Mega Drive set back up, I'll have to do some comparisons.
     
  4. ⸸ devon ⸸

    ⸸ devon ⸸ I am CRUSHING YOUR HEAD!!!! Member

    Joined:
    Aug 26, 2013
    Messages:
    1,115
    A quick look at Dual PCM's source code shows that it doesn't use either of them. Are there any other unimplemented instructions?
     
  5. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    948
    The complete list is DAA, HALT, OUT, IN, RETN, RETI, IM (though this should safely no-op), RRD, RLD, CPI, CPD, CPIR, CPDR, INI, IND, INIR, INDR, OTDI, OUTD, OTIR, and OTDR. Interrupt Modes 0 and 2 aren't implemented either.
     
  6. ⸸ devon ⸸

    ⸸ devon ⸸ I am CRUSHING YOUR HEAD!!!! Member

    Joined:
    Aug 26, 2013
    Messages:
    1,115
    Dual PCM uses RETI in its V-BLANK interrupt. IM is probably no big deal, since it sets it to mode 1. On the outside, RETI is basically just RET, but with no condition support. Internally in the Z80, it's designed to release the Z80 from an interrupt state (this is simplifying it, I know), and to be honest, for a Genesis emulator of your scope, it's probably not worth going much into. RETN is designed for returning from a nonmaskable interrupt, which I would say is also outside the scope of this project. You could just probably get away with just treating them both as RET instructions without condition support and call it a day. I wouldn't know if there's some kind of weird obscure thing you can do with them that's possible on Genesis, though.

    EDIT: Unfortunately, changing the RETI to a RET in Dual PCM did not fix it.
     
    Last edited: Jun 23, 2022
    MarkeyJester likes this.
  7. Lone Devil

    Lone Devil Internet Veteran Member

    Joined:
    Dec 29, 2010
    Messages:
    182
    Location:
    Dallas Texas
    20 seconds in and I can already see myself using this over Regen for its superior VRAM viewer as my upcoming hack rans very well, my only grip is the lack of keyboard rebinding, much prefer arrow keys for movement, ASD for A/B/C, and tab for fastforward.
     
    TheInvisibleSun likes this.
  8. Mohamad20ZX

    Mohamad20ZX Newcomer Trialist

    Joined:
    Oct 25, 2021
    Messages:
    3
    I hope this emulator come out it looks interesting to try out and figure how to make very great ROM hacks
     
  9. SpeedyGonzales

    SpeedyGonzales Newcomer Trialist

    Joined:
    Jul 5, 2022
    Messages:
    6
    Will try this on Windows XP later and it might replace gens on it.
     
  10. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    948
    It has already come out: there's a download link in the first post.
     
  11. nineko

    nineko I am the Holy Cat Member

    Joined:
    Mar 24, 2008
    Messages:
    1,883
    Location:
    italy
    Isn't that what everyone prefers?
     
    Dark Shamil Khan likes this.