Questions on writing a new Mega CD emulator

Ask anything your want about Mega/SegaCD programming.

Moderator: Mask of Destiny

Sik
Very interested
Posts: 939
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: Questions on writing a new Mega CD emulator

Post by Sik » Sat May 25, 2019 6:59 am

Chilly Willy wrote:
Fri May 24, 2019 3:00 pm
I think the primary benefit of an open BIOS for the CD would be to help facilitate CD replacements. The CD drives on these systems are now so old that they're not very good, even when "new". The replacement I got for my CDX is barely any better than the old drive. An open BIOS would allow replacing the drive with something entirely different that still worked at the BIOS level, since I don't know of anything commercial for the CD that bypasses the BIOS. That would allow for competing CD replacements since very few people are up to replacing the CD at the hardware level.
I'd have agreed with you before this thread… but it seems the important CDD commands are exactly the same on all models, so it'd be easier to tackle any such mods at the CDD level (which as a side effect means no need to replace the whole firmware on such a mod). Not like those mods are trivial anyway, having to get rid of a significant part of the hardware.

But yeah, emulators and clones made from scratch (i.e. full reimplementations) would be the biggest ones to benefit from a free BIOS.
Sik is pronounced as "seek", not as "sick".

Near
Very interested
Posts: 109
Joined: Thu Feb 28, 2008 4:45 pm

Re: Questions on writing a new Mega CD emulator

Post by Near » Sat May 25, 2019 1:27 pm

A reproduction BIOS doesn't need a full audio CD(+G) player interface. It doesn't even truly need a memory save interface (although that would help.) It just needs to run games. Emulators can make per-game backup RAM files. Or for Shining Force CD, backup RAM cart files per-game.

All we really need is the initial boot sequence, followed by whatever BIOS calls that officially released games make. That in itself would be very valuable. From there, add all the features one likes.

I'm sure there are hardware clones of the LC8951 that are still obtainable, so we might as well target that protocol.

The value to emudevs is not needing two codepaths: one HLE, one ... somewhat less HLE (since we don't have CDD firmware dumped.) The homebrew BIOS file lets Sega CD games work out of the box on emulators with nothing but a CD-ROM drive or game image.

Near
Very interested
Posts: 109
Joined: Thu Feb 28, 2008 4:45 pm

Re: Questions on writing a new Mega CD emulator

Post by Near » Sun Jun 02, 2019 6:32 am

I added CDC DMA emulation, but Popful Mail still hangs after the Sega logo (the one after pressing start with the sound effect.)
Both the main CPU and sub CPU are waiting on the other's messages, so a deadlock. Don't suppose anyone else ran into this issue with their emulators?

Also, why does CDD command 3 (seek and play) set the status to playing even while seeking, but CDD command 4 (seek and pause) sets the status to seeking, and then to pause once the seek is completed? Seems counter-intuitive?

...

This seems a bit strange:

https://github.com/ekeeke/Genesis-Plus- ... dd.c#L1917

cdd.latency is checked for null before setting the new latency. So if you wrote a pathlogical program that kept seeking to the start and end of the disc rapidly, the value would keep growing way above the limit.

I imagine this should be removed, and a cdd.latency=max(11,cdd.latency) line should be added after the latency calculation. And it should be there for the SeekPause command as well.

Eke
Very interested
Posts: 884
Joined: Wed Feb 28, 2007 2:57 pm
Contact:

Re: Questions on writing a new Mega CD emulator

Post by Eke » Sun Jun 02, 2019 12:41 pm

I already told you (I think at least) but when this was
written, there were barely any documentation about CDD and it was implemented to be mostly optimized while still working fine with all existing BIOS / game software.
CDD emulation code is therefore definitively not accurate to how the real micro-controller and CD mechanism worked and you shouldn't really use it that much as a reference or spend too much time trying to find flaws in this code as I am mostly already aware of all of them. I plan to rewrite it someday now that reference sourcecode surfaced but I have no idea when this will be done as this is not high priority to me and my available free time for coding is barely null these last years (don't make kids, emudevs ;-) )

As for your questions, yes, I am sure status is set to 'seeking' when 0x3 (play/read) command is sent until the needed sector is reached, it's just it was easier to handle it this way in emulator and still worked fine with BIOS (also keep in mind that latency thing was only added later to deal with some specific game issues but initially that command was handled without any delay). The latency check was added to detect the initial spinning latency and I agree it could have been handled better (by using drive status instead) but again it was simpler this way and worked fine with existing software. To be completeley accurate, drive mechanism latency should be emulated for all kind of commands (not just play or seek) and CDD status response timings should be measured on real hardware or copied from official CD drive emulation software.

Near
Very interested
Posts: 109
Joined: Thu Feb 28, 2008 4:45 pm

Re: Questions on writing a new Mega CD emulator

Post by Near » Mon Jun 03, 2019 2:56 am

Sorry, I thought it might be helpful to mention things in case you wanted to work on it again.

I understand, I'll stop giving you grief about it.

Thank you again for your code. Buggy or not, it works, and that's the most important bit. It would have taken me ten times as long if I had to reverse engineer all of this from those documents alone, and likely would have been impossible for me without either.

If you ever get back to the Sega CD, I'd be happy to work with you, but no rush and no obligation. Thanks again! ^-^

Eke
Very interested
Posts: 884
Joined: Wed Feb 28, 2007 2:57 pm
Contact:

Re: Questions on writing a new Mega CD emulator

Post by Eke » Mon Jun 03, 2019 6:30 pm

You are welcome. Gens and Picodrive sourcecode were also quite helpful to me when I was trying to figure some of these things or clarify some stuff so I am glad this can be helpful to others in return. That's also why I always try to comment the code as much as possible and indicate stuff that were needed to fix specific games but I guess I could have indicated better when something is not 100% hardware exact or a deliberate optimization/shortcut :oops:

Near
Very interested
Posts: 109
Joined: Thu Feb 28, 2008 4:45 pm

Re: Questions on writing a new Mega CD emulator

Post by Near » Tue Jun 04, 2019 9:52 am

Just for overkill, I thought it'd be fun to support the LC7883KM.
I understand that I'm emulating a DAC when both the input and outputs from the emulation are digital, but this is just for fun.

http://electronics-diy.com/pdf/LC7883.pdf

The chip has pins for:
* SOC1,SOC2 -> clock rate (384f, 448f, 392f, 512f): seems to be fixed at one of these with no software control
* EMPH1,EMPH2 -> de-emphasis mode (off, 44.1khz, 32khz, 48khz): can be configured via serial transfer ($ff8034)
* D/N -> 1 = double-rate, 0 = normal-rate: can be configured via serial transfer ($ff8034) [called "spindle speed", but it's not really]
Also there's attenuation bits for a15-a4, but the manual says a4-a5 are ignored so it's only 10-bits

The normal rate mode performs 8x oversampling at 18-bit precision.
The double rate mode performs 4x oversampling at 18-bit precision.

The normal rate filtering is:
Input -> 43rd-order FIR (unknown purpose) -> 1st-order IIR (de-emphasis) -> 11th-order FIR (unknown purpose) -> 3rd-order FIR (attenuator) ->noise shaper (lower 6 bits) -> Output at 16-bit

The double rate filtering is:
Input -> 1st-order IIR (de-emphasis) -> 43rd-order FIR (unknown purpose) -> 3rd-order FIR (attenuator) -> noise shaper (lower 6 bit) -> Output at 16-bit

Each FIR filter doubles the input frequency rate. So the normal rate output is 44.1khz*8=352.8khz, the double rate output is 44.1khz*4=176.4khz.

The IIR filter for de-emphasis is is a high-shelf filter with a gain of -9.477, a slope of 0.5, and a cutoff frequency of 5uS (3.1KHz) + 15uS (10KHz).
I'm using a biquad filter (second-order, but oh well, I don't have a first-order high-shelf algorithm handy) with arithmetic mean, so (10000+3100)/2.0 cutoff.

I presume the Sega CD will feed this DAC 44.1khz audio no matter what, so setting the 48khz and 32khz de-emphasis modes are just going to adjust the output frequency parameter of the IIR filter, which will basically throw off the coefficient calculations and produce the wrong shelf, which is how I'm emulating it for now.

The manual states the attenuator adjustment is: -20 * log((attenuator & ~63) / 256) dB. I'm not sure how to model that, but (sample*attenuator)/0x4000 seems to produce adequate results, although it may be incorrect. I'm also not clear why one would design a 3rd-order FIR filter to do this attenuation ...? Since 0x4000 is the baseline value at reset, it means we can quadruple the input volume with an attenuation of 0xffff, which would probably be fine given the filters internally work at 18-bit. So if we are outputting in normalized floats, we can then do:
int18 output = (4*sample*attenuator)/16384; return output / 131072.0; //-1.0 to +1.0

The attenuator is adjusted by one for each sample period, persumably one input sample at 44.1khz, which also produces adequately sounding results.

The manual does not say anything about what the 43rd-order and 11th-order FIR filters are for, short of for doubling the sampling rates. Without knowledge of this, modeling this step seems pointless. (I know, this whole exercise is pointless, but its fun to support what we can!)

The noise shaper is used to boost the SNR of the output, but again, the algorithm used is unknown. I don't understand why it's 6-bit when the input at this point will be 18-bit. You'd end up with 12-bit of unmodified input. That can't be right.

Finally, the PDF recommends low-pass filters on the final outputs from the DAC, but I don't know if those are present in the Sega CD, or what their cutoffs would be (but I'd bet money they'd be first-order if they were even there.)

I imagine absolutely none of this aside from just the attenuator is ever going to produce any kind of audible difference in the Sega CD output, but oh well.

...

Obviously, the WonderMega and WonderMega M2 use different DACs with their own peculiarities, but I'm not as interested in them.

If anyone wants to take this absurd tangent any further, let me know, otherwise I think I'll stop with de-emphasis and the attenuator stepping (hopefully) correctly emulated, and the oversampling and noise shaping omitted.

TmEE co.(TM)
Very interested
Posts: 2440
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Re: Questions on writing a new Mega CD emulator

Post by TmEE co.(TM) » Tue Jun 04, 2019 10:16 am

The big filters are for bandwidth limiting and are responsible for the characteristics of the passband ripple and whatever goes on in the stopband. This isn't really useful to model, especially since whatever plays the sound will do its own stuff like that, mangling whatever you did (and you need to use some high sample rate also to actually model these things which may not be available on all hardware).
(de)emphasis and attenuator are only things that matter.
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

Sik
Very interested
Posts: 939
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: Questions on writing a new Mega CD emulator

Post by Sik » Mon Jul 29, 2019 3:14 am

I forgot on which of the threads we had discussed this but whatever.

I recall that we discussed that a handful of sectors from the lead-in are actually readable because they end up in the buffers — not enough to build an entire TOC, but a quirk nonetheless. Is it possible to try to peek at the buffers while it's in the "reading TOC" state though?
Sik is pronounced as "seek", not as "sick".

Nemesis
Very interested
Posts: 791
Joined: Wed Nov 07, 2007 1:09 am
Location: Sydney, Australia

Re: Questions on writing a new Mega CD emulator

Post by Nemesis » Sat Aug 10, 2019 1:49 pm

Unfortunately the data is never passed on when it's in that mode. I'm on my phone and can't look things up properly, but IIRC whichever asic gets the raw data stream from the laser also exposes the TOC data, and it never passes the data on when it's doing TOC decoding. Only way I could get at that data in the MegaLD hardware was to hook the serial data stream with a logic analyzer and decode it in software. I believe the same situation exists on the normal MegaCD from memory.

Snake
Very interested
Posts: 206
Joined: Sat Sep 13, 2008 1:01 am

Re: Questions on writing a new Mega CD emulator

Post by Snake » Mon Mar 22, 2021 3:56 am

Stef wrote:
Mon Apr 29, 2019 10:28 am
It was Steve Snake
Nah, I believe he's talking about Mr. Quinn, of AGES. I forget his first name :-( He got all this stuff working well before any of us.
Stef wrote:
Mon Apr 29, 2019 10:28 am
he probably has some "internal" information
I had no more info than anybody else did at the time. I had to go out and buy a SegaCD and burn about a million discs too... In fact Kega only exists because I was being asked questions that I was only guessing at - only way I could find out was to do it myself.
Stef wrote:
Mon Apr 29, 2019 10:28 am
probably had a complete BIOS documentation
To this day I've never seen such a thing. If only... I don't believe it exists outside of Japan.
Stef wrote:
Mon Apr 29, 2019 10:28 am
i think Michel Gerrie did actually asked Steve Snake about the CDD infos but he refused... Steve Snake was always very secret about his discovers, he never wanted to share them
Well that's not very accurate, again, maybe you're thinking of Mr. Quinn. I shared the most important discoveries to the segadev yahoo group at the time, and told you/Gerrie anything I found out - that's literally why I started - except most of the time you would say "don't tell me!" because you wanted to work it out for yourself :-)
Last edited by Snake on Mon Mar 22, 2021 4:48 am, edited 2 times in total.

Snake
Very interested
Posts: 206
Joined: Sat Sep 13, 2008 1:01 am

Re: Questions on writing a new Mega CD emulator

Post by Snake » Mon Mar 22, 2021 4:37 am

Nemesis wrote:
Fri May 03, 2019 5:16 am
For years, the software to drive this C-Trac CD emulation hardware was missing from the public domain. It surfaced more recently
Is that the virus infected SEGA.EXE that was floating around a while back? If so, and nobody has done it already, I fixed that and was going to post it here - then got distracted and totally forgot about it. Let me know if you want it and I'll go fish for it on my old laptop.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Questions on writing a new Mega CD emulator

Post by Stef » Mon Mar 22, 2021 12:01 pm

Snake wrote:
Mon Mar 22, 2021 3:56 am
Stef wrote:
Mon Apr 29, 2019 10:28 am
It was Steve Snake
Nah, I believe he's talking about Mr. Quinn, of AGES. I forget his first name :-( He got all this stuff working well before any of us.
Stef wrote:
Mon Apr 29, 2019 10:28 am
he probably has some "internal" information
I had no more info than anybody else did at the time. I had to go out and buy a SegaCD and burn about a million discs too... In fact Kega only exists because I was being asked questions that I was only guessing at - only way I could find out was to do it myself.
Stef wrote:
Mon Apr 29, 2019 10:28 am
probably had a complete BIOS documentation
To this day I've never seen such a thing. If only... I don't believe it exists outside of Japan.
Stef wrote:
Mon Apr 29, 2019 10:28 am
i think Michel Gerrie did actually asked Steve Snake about the CDD infos but he refused... Steve Snake was always very secret about his discovers, he never wanted to share them
Well that's not very accurate, again, maybe you're thinking of Mr. Quinn. I shared the most important discoveries to the segadev yahoo group at the time, and told you/Gerrie anything I found out - that's literally why I started - except most of the time you would say "don't tell me!" because you wanted to work it out for yourself :-)
Oh Mr Snake, I'm surprised to see a reply from you :D
Indeed Quinn (AGES) was the first to get basic Sega CD emulation working and of course he was very reluctant about sharing its discoveries. I guess he made some reverse engineering from the BIOS. It's what i did in the end + lots of testing on a real Mega CD that Red5 offered me (he literally send me a Mega CD2).
Unfortunately AGES was buggy and not much advanced on Mega Drive emulation itself so in the end its emulator was barely making the BIOS working (very partially) and as far i remember no game was even booting. About sharing stuff, i remember asking you how you fixed the infamous YM emulation bug with Shaq-Fu (and not only that one) and you were always very obscure about it :p I think in the end i never fixed it in Gens :P (well i did but much later and never released it officially).
It was a fun period where lot of things need to be sorted by ourself, today we have much more documentations about all that.

Post Reply