clownmdemu - The Greatest Mega Drive Emulator Ever (Someday)

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

  1. Dark Shamil Khan

    Dark Shamil Khan TASer lol Member

    Joined:
    Nov 7, 2021
    Messages:
    94
    Location:
    Pakistan
    Okay so interestingly. I am currently using the v0.3 of the emulator. And for some bizarre reason. DualPCM or AMPS driver, doesn't really work with the emulator. It either plays all the FM and psg channels and doesn't play the PCM channel. Or doesn't even play the music at all. Is the sound driver at fault or is it the emulator getting confused of something?
    Oh and the SHC splash screen glitches out. I think that was fixed.
     
    Last edited: Mar 23, 2023
  2. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    1,020
    The Z80 emulation was very inaccurate in v0.3, so I imagine that it's the emulator's fault. The Z80 emulation was improved in v0.4, so hopefully DualPCM and AMPS work in that version.
     
  3. Techokami

    Techokami For use only on NTSC Genesis Systems. Member

    Joined:
    Oct 29, 2007
    Messages:
    20
    Location:
    LINUX
    Can confirm that AMPS works in v0.4, as I tested my hack project in it and it uses AMPS, and audio playback was correct.
     
  4. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    1,020
    Yaaaay - I was hoping all that work on the Z80 emulator would pay off!
     
  5. Devon

    Devon A̸ ̴S̴ ̵C̵ ̷E̶ ̸N̸ ̴D̶ ̵E̶ ̸D̶ Member

    Joined:
    Aug 26, 2013
    Messages:
    1,380
    Location:
    your mom
    Want some stuff to test with when you decide to implement the H/V counter?

    I recently made a quick demo that displays high color images, and unfortunately, it doesn't work at all with your emulator.

    [​IMG]

    Basically, the demo DMAs 30 colors every 2 lines (or 15 colors every line). The palette is double buffered, so it also swaps between lines 0-1 and 2-3 every 2 lines as well. To maximize the timing for DMAing the colors without CRAM dots, I manually timed it using the H/V counter, since m68k interrupts are too slow, which is the root of why it doesn't work: the emulator doesn't support the H/V counter yet! I also disabled display while transferring the colors to make them go must quicker.

    For reference, it's supposed to look like this:
    [​IMG]

    I also have this older demo I made that does something similar, and I get the same result really, just all black, since the first colors to be copied are all black. For reference, it's supposed to look like this:
    [​IMG]

    So, here are the ROMs you can test with for whenever you feel like implementing the H/V counter:
    High Color Ronald McDonald
    Distorted Red Cat + Text

    Also, by the way, at least on a US Model 2 Genesis, reading from PSG causes it to lock up. No error handler, just a freeze. You are also correct in that the Z80 bank is a window into the m68k address space. I have successfully read things like the hardware version register using it on hardware. My memory is also fuzzy, but I also think I managed to also successfully request Z80 bus access too... from the Z80... lol (should double check that).
     
    Last edited: Mar 31, 2023
    ProjectFM, Clownacy and DeltaW like this.
  6. MemeMaster9000

    MemeMaster9000 Newcomer Member

    Joined:
    Feb 12, 2023
    Messages:
    21
    Location:
    Ice Cap Zone
    The distorted red cat could be used for an intro similar to Star Wars. As for the high-color images, you could make some FMVs (that is if you don't care about cartridge space.)
     
  7. Devon

    Devon A̸ ̴S̴ ̵C̵ ̷E̶ ̸N̸ ̴D̶ ̵E̶ ̸D̶ Member

    Joined:
    Aug 26, 2013
    Messages:
    1,380
    Location:
    your mom
    Been there, done that :p


    Audio not in ROM. Unfortunately, fitting more characters per line would require more palette entries, and realistically, I only have 61 to work with per scanline anyways, so rendering text with just this method wouldn't do.

    It's not that simple I'm afraid. I'm using 2 layers to display the 30 colors (since there's only 15 colors per tile + transparency/background color). I am using the VAST MAJORITY of VRAM just for the tiles alone, which means I have no room to double buffer loading new frames. I would need to be able to load in new tiles into a separate area in VRAM while the current frame is displayed and then swap to it in order to keep the video glitchless. Maybe if I limited it to 15 colors per line an only used 1 layer, then it might be possible (provided that I even have ROM space).
     
    Last edited: Mar 31, 2023
    ProjectFM and GenesisDoes like this.
  8. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    1,020
    Awesome - it's great to have a proper test for the H/V counters! I know next to nothing about what they're set to and when, so having proper tests like these helps a lot!
     
  9. Dark Shamil Khan

    Dark Shamil Khan TASer lol Member

    Joined:
    Nov 7, 2021
    Messages:
    94
    Location:
    Pakistan
    Now, correct me if it has been presented before. But yesterday I was playing Sonic 3 and Knuckles in Clownmdemu on yet again v0.3. And interestingly, the data save select screen's background image was kinda... Corrupted or at least squished and then corrupted. Not sure why that happened. I would present a screenshot but I'm currently on my phone and can't be bothered to go on my PC.
    But yeah. That's the only oddity I found. Still haven't tried 0.4. Will do today!
    Edit: I got this. It's doing it even in 0.4 for some reason.
    Basically plane A is kinda corrupted. Not sure if my actual S3K ROM is doing that. Or the emulator is doing that.
    Will edit it if I found anything else. wtf.PNG
     
    Last edited: May 1, 2023
    RealMalachi likes this.
  10. RealMalachi

    RealMalachi You look like an atheist seeing an angel... Member

    Joined:
    Oct 16, 2022
    Messages:
    18
    Location:
    Australia
    So here's some bugs and oddities I found. These are all on 0.4.2

    Bugs
    Recent builds of Hellfire Saga and SGDK projects just display a black screen, and the emulator lags quite a bit until you load something it can boot up. It's just a laggy black screen, not much I can add.

    Window plane bugs
    The Window Plane has two bugs of note:
    1. It doesn't actually cut through Plane A, instead acting more like another plane layer above A. This makes stock SCEs lagometer kind of useless, since it's almost completely transparent. The video shows the Window plane displaying a single blue pixel


    2. A more important issue, and one I'm more clueless on. Castlevania Bloodlines' intro doesn't render its text, which is on the window plane. Maybe it's a timing issue?

    Ecco the Dolphin sound issues
    Ecco 1 and 2 both have an issue with the dash attack sound
    Ecco 2 has a glaring music pausing issue, seemingly related to the 68K bug flag. This also happens in gameplay, though to a lesser extent

    SHIMAs Hint issue
    No idea what's causing this one, honestly

    Oddities
    Despite not having proper 6-button controller support yet, it does properly emulate a bug when pressing up and down, where most controller code will incorrectly interpret joypad input as 6 button input

    Like many emulators it inaccurately supports V30 60Hz, though I see that as an advantage

    According to Devons emulator detector, it's detected as a Mega SG console. Basically, that means Z80 accurately waits for DMA to end when it's reading 68K data, but the VDP debug register doesn't work at all. The VDP debug register "return(s) the 68000 instruction prefetch", and a lot of emulators don't emulate it right, usually returning 0. Not much practical use from what I can gather, beyond emulator detection
    Once I find a method to detect clownmdemu specifically (ignoring things that simply aren't implemented), I'll inform you about that

    Requests
    MD+ Support. Basically, the MegaSD and Everdrive Pro have this MSU-like function that bankswitches into ROM, but allows you to handle properly looping WAV files, among other things. GPGX partially supports it. I'd say only do it once you begin MegaCD and mapper emulation, since they're kind of linked together
    If you need the development doc, here's a mirror
    MSU-MD is just a basic Mode 1 CD player (ironic), so basic Mode 1 emulation should handle that just fine
     
    Clownacy and ProjectFM like this.
  11. Mega De Glitcher

    Mega De Glitcher Newcomer Trialist

    Joined:
    May 20, 2023
    Messages:
    6
    Location:
    Secret Jungle Zone
    I decided to give this emulator a shot on a simple sonic 1 rom hack i made and the game seems to crash everytime i jump, thats wacky!

    upload_2023-5-23_12-43-52.png

    ps: it works like charm on the kega emulator
     
    Last edited: May 23, 2023
  12. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    1,020
    That sounds like a bug in your hack: that error appears to be an address error, which is caused by accessing words and longwords on odd addresses. Kega ignores these errors while my emulator does not.
     
  13. Hame

    Hame Peepee Poopoo Member

    Joined:
    Jan 12, 2021
    Messages:
    60
    Location:
    Spain
    Something that has happened to me is that the hack I was doing worked fine on your emulator but on others it didn't work (no, not even on kega) on all the others I always got a black screen because of something I did wrong in the sound of the controller. As I said in yours it worked but in others it didn't.

    Another thing is that I have noticed that the pcm (or the dpcm, they are almost the same) sound with a higher pitch
     

    Attached Files:

    Last edited: May 26, 2023
  14. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    1,020
    frontend v0.4.3 & libretro v0.2.3
    Another small update:

    The plane debugger was broken back in v0.4. Thanks to Brainulator pointing this out, it has been fixed in this update.

    A new Qt-based file dialog has been added for Linux and the BSDs, which leverages the 'kdialog' tool. Like the Zenity-powered GTK file dialog, this is a soft dependency: it is not required to build or run the emulator, and the emulator will instead fall-back on the default Dear ImGui-based dialog if neither are available.

    I've also attempted to fix a curious hang that would occur when resuming the PC after leaving it asleep overnight. From what I can tell, it's the result of an overflow in the millisecond tick counter. To avoid this, the tick counter has been made 64-bit. Now it will only overflow after 2,000,000 years.

    An optimisation has been made to the Z80 interpreter which gives it a massive speed boost, improving the performance of the emulator overall. It's still not enough to get the emulator running at full-speed on the 3DS, but it's a big step towards it.

    Standalone: https://github.com/Clownacy/clownmdemu-frontend/releases/tag/v0.4.3
    libretro: https://github.com/Clownacy/clownmdemu-libretro/releases/tag/v0.2.3
     
  15. JGamer2151

    JGamer2151 Well-Known Member/Lurker Member

    Joined:
    Dec 1, 2020
    Messages:
    97
    So I suppose that the counter was 32-bit in previous versions before this one, causing a possible overflow issue.
     
  16. Dark Shamil Khan

    Dark Shamil Khan TASer lol Member

    Joined:
    Nov 7, 2021
    Messages:
    94
    Location:
    Pakistan
    Oh yeah, I forgot to show this. I made video on the emulator showcasing a ROM Hack. So yeah, here's the video.
    https://youtu.be/qQrCZK7G3KI
     
  17. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    1,020
    frontend v0.4.4
    Another small update that mainly fixes bugs in the frontend, with one bugfix in the emulator itself.

    The emulator also got a logo and icon at long last:
    [​IMG]
    It's meant to mimic the look of Windows 11's icons, like those of Notepad and Calculator.

    Support for Linux and macOS has been fixed: SDL2's unreliable DPI detection was throwing the scaling off. Unfortunately, this means that high-DPI support is currently Windows-only. I blame SDL2 for this one: its entire purpose is to be a platform abstraction layer, and it completely fails at this when it comes to managing DPI scaling.

    I also fixed the barebones 'save file' dialog, which has been broken since I first added it. Whoops. Now macOS users can save things properly.

    Games that are dropped onto the emulator window are now added to the 'Recent Software' list.

    Alt+Enter will now toggle full-screen. This is a fairly standard keyboard shortcut, so it may come more naturally than the default F11 key binding.

    Various instances of stuck inputs have been fixed. No more infinite rewinding!

    Finally, there's the emulator bugfix: sprites were being culled while they were still on-screen by one column of pixels at the right side of the screen. At first I mistook this for a bug in Sonic 2, which led me on a wild goose chase.

    https://github.com/Clownacy/clownmdemu-frontend/releases/tag/v0.4.4
     
  18. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    1,020
    [Blog post]

    I've been thinking of expanding my Mega Drive emulator with support for the Mega Drive's add-on - the Mega CD. On paper, this should be simple enough to do: while the Mega CD adds a second 68000 CPU, communications ports, a lot of extra RAM, hardware for graphics-transformation, a sound chip, the titular CD drive, and a BIOS to manage it all, many of these can be skipped or implemented in a quick and easy way to at least get some games booting.

    As of writing, I have achieved my first milestone, which is to get a couple of Sonic hacks to boot. These hacks are special because they make minimal use of the Mega CD hardware, making them the easiest software to add support for. One hack (Sonic Winter Adventures) can be seen below, declaring its detection and usage of the Mega CD:

    [​IMG]

    Sonic Winter Adventures exclusively uses the Mega CD for playing music from a CD. The other hack, my old Super Sonic & Hyper Sonic in Sonic 1 hack, uses the Mega CD for playing PCM samples using the extra sound chip.

    Unlike typical Mega CD games, these two hacks store their data on a cartridge instead of a CD. This bypasses the need for me to implement the Mega CD's boot-up process of reading the IP and SP binaries from the disc, and also the need to implement the various BIOS calls that are related to reading CD sectors.

    Games that utilise the Mega CD hardware, but run from a cartridge, are called 'Mode 1' games. This is because of Sega's official Mega CD developer documents, which dub the cartridge-booting process 'Mode 1' and the CD-booting process 'Mode 2'.

    Out of all of the Mega CD's hardware, the only things that these hacks rely on are the extra CPU, the communication ports, the extra RAM (and the bus logic that allows it to be accessed), the BIOS, and, in my Super Sonic hack's case, the extra sound chip. The sound chip and most of the BIOS can be stubbed-out to at least get the hacks to boot, leaving only the extra CPU, communication ports, RAM, bus logic, and some parts of the BIOS.

    The screenshot seen above is the result of implementing these essential features. So, how did I do it?

    The CPU (dubbed the 'SUB-CPU') is a 68000, just like the Mega Drive's main CPU (appropriately dubbed the 'MAIN-CPU'). Because of the modular design of my emulator's internals, the 68000 interpreter supports multiple states, allowing it to emulate two CPUs simultaneously. I just had to allocate a second state struct for this second CPU, and then add an additional call loop to invoke the interpreter using this struct for the duration of the emulated frame.

    However, for this CPU to do much of anything, it needs some RAM. The Mega CD adds two sets of memory: WORD-RAM and PRG-RAM. PRG-RAM essentially belongs to the sub-CPU, and is intended to hold its BIOS and other code. WORD-RAM is shared between the main-CPU and the sub-CPU, typically being used to ferry data between the Mega CD's CD drive or graphics-transformation hardware and the Mega Drive. Because PRG-RAM is 'owned' by the sub-CPU, the main-CPU must request the sub-CPU's bus to be able to access PRG-RAM. This freezes the sub-CPU. This is not the case for WORD-RAM, which can be exchanged between the two CPUs freely without freezing either of them. WORD-RAM and PRG-RAM are massive: the Mega Drive has 64 KiB of RAM, while WORD-RAM and PRG-RAM add a further 768 KiB. Implementing this all is just a matter of adding the appropriate logic to the sub-CPU's bus event call-backs, accessing an array that represents either WORD-RAM or PRG-RAM based on the specified address, just like a real bus.

    To allow the two 68000 CPUs to communicate, a couple of words are made available to both CPUs. Half of the words can be read and written by the main-CPU, but are read-only for the sub-CPU, and the other half are the other way around. No bus-requesting or exchanging is needed to access these.

    That leaves the final part of the puzzle: to manage the complicated CD drive hardware (among other things), the Mega CD comes equipped with an elaborate BIOS. Notably, this BIOS features system calls for reading sectors from the CD and playing CDDA music. Software that runs on the sub-CPU is intended to be managed by this BIOS, meaning that a partial BIOS implementation is needed for the sub-CPU programs of these ROM-hacks to run at all. Without the sub-CPU program running, Sonic Winter Adventures will refuse to boot, as it will infinitely wait for a response from the sub-CPU to be sent through the communication ports.

    Sub-CPU programs are loaded at address 0x6000 and come with a small header. This header features a magic number (string), a flag, and a couple of offset tables. One offset table specifies functions that will be called by the BIOS on start-up, after start-up, every vertical interrupt, and in response to a user-raised event. Without a functional BIOS, these functions will never be called.

    An additional requirement for the BIOS is exclusive to Mode 1 games: because these games boot directly from a cartridge rather than the Mega CD's BIOS, the sub-CPU's portion of the BIOS is not automatically loaded, and instead must be loaded manually. The typical method for doing this is to locate a magic number, then decompress a nearby Kosinski-compressed payload which contains the sub-CPU BIOS. For these games to correctly boot in my emulator, this magic number needs to be present, and a dummy Kosinski-compressed archive needs to be at the expected location. Without the magic number, the hack may not think that a Mega CD is attached at all, and, without the the Kosinski-compressed archive, the hack would crash as the decompressor would go haywire.

    To meet these various needs, I created my own stub BIOS in 68000 assembly, compressed it in the Kosinski format, and had the sub-CPU's bus logic place it at the same location as the original BIOS's payload. This way, the hacks' Mode 1 initialisation logic and sub-CPU programs all run correctly.

    With this done, Sonic Winter Adventures is able to detect the Mega CD, request the sub-CPU's bus, decompress the sub-CPU BIOS into PRG-RAM, release the sub-CPU's bus, reset the sub-CPU, run the sub-CPU program, and finally communicate with the sub-CPU program to complete its boot process, resulting in the above screenshot. It's not much, but it's a start!
     
    Last edited: Sep 27, 2023
  19. Devon

    Devon A̸ ̴S̴ ̵C̵ ̷E̶ ̸N̸ ̴D̶ ̵E̶ ̸D̶ Member

    Joined:
    Aug 26, 2013
    Messages:
    1,380
    Location:
    your mom
    If you ever wanted to see how the CDC and CDD work for lower level emulation, these resources can definitely help.
    Also would be worth looking into how redbook works, especially for subcode reading, since that's a thing that can be done on the MCD. It should also be noted that some models have an slightly different version of the CDC that has 5-bit registers instead of 4-bit.
     
    Pacca and Clownacy like this.
  20. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    1,020
    Awesome, thanks! I just hope that I won't need it, and that high-level emulation will be good enough.