Page 1 of 2

Sega CD Mode 1 support functions for SGDK

Posted: Sat Sep 26, 2015 9:48 pm
by mikejmoffitt
Hi everyone, first post here but it looks like the place to go for Sega development. I've been working on porting Lyle in Cube Sector to Sega Genesis and I thought it would be neat to offer the ability to put in a deluxe soundtrack CD to get the original game audio, so I set about implementing Mode 1 support.

Chilly Willy's example player code was very helpful, but much of it is not broken into neat functions and it relies on a lot of externally defined ASM code as well. My goal was more or less to rewrite it in a way that's a little less integrated into the player example so it can be used in other places. It's not finished yet, but it's finally working enough that it is somewhat presentable and it works in the game. I will probably add some more functions and clean it up a little, and give it a little repo with integration instructions. It relies on a little of the assembly provided with the original demo, but much less than before. I tried to give things clear function names so usage is clear.

You can look at what I have so far here: https://github.com/Mikejmoffitt/LICS/bl ... /cdaudio.c

Here is a video of it working: https://www.youtube.com/watch?v=P20fGWXYXaI

Re: Sega CD Mode 1 support functions for SGDK

Posted: Mon Sep 28, 2015 4:18 pm
by Sik
Am I the only one who's tired of it being called mode 1? (mode 1 is the Mega CD firmware booting the game from the cartridge and it doesn't work at all without the Mega CD being connected) I'd rather call this "mode 0" if we insist on giving it a name, but I don't like things that result in misinformation.

Re: Sega CD Mode 1 support functions for SGDK

Posted: Mon Sep 28, 2015 4:34 pm
by mikejmoffitt
I'm just calling it what people are already calling it, I don't care what name it's given as long as it's consistent.

Re: Sega CD Mode 1 support functions for SGDK

Posted: Wed Jul 05, 2017 4:54 pm
by Chilly Willy
Sik wrote:Am I the only one who's tired of it being called mode 1? (mode 1 is the Mega CD firmware booting the game from the cartridge and it doesn't work at all without the Mega CD being connected) I'd rather call this "mode 0" if we insist on giving it a name, but I don't like things that result in misinformation.
Hmm - sorry about the necromancy, but it IS on topic and I didn't see this until recently...

I called it "mode 1" because that's what SEGA called. It's part of the MEGA-CD BIOS Manual, specifically page 30. Mode 1 is when you boot from a cart (it specifically says "Boot from cartridge", not boot CD firmware and look at cart), manually setup the CD BIOS, and init the CD. I think you misunderstood that part of the manual thinking it was referring to the CD BIOS looking for a special signature in the cart area and then loading the sub-cpu program from the cart. That's not what the manual describes at all. It clearly states the MD side is booting from the cart, which means the CD side firmware is not active. The CD side is described as being held in reset until the MD side finishes setting up what the CD needs to run and releasing the reset. The entire sequence described in the manual is exactly what the "mode 1" code I published does.

That's beside the fact that the mode where the MD boots the firmware and looks for a special signature in the cart area is not actually present in all models, and is not described in any form in the BIOS manual.

Re: Sega CD Mode 1 support functions for SGDK

Posted: Wed Jul 05, 2017 4:59 pm
by KanedaFr
If I remember, you already answered to Sik on Sega16 back then, when you released your Mode1 demo ;)

(I read a lot on this topic lately, it's how I know!)


And, to the topic, it's exactly what's I'm doing this week ;)
RAW port was done, I'm trying to make it clear and easier to use before release

Re: Sega CD Mode 1 support functions for SGDK

Posted: Wed Jul 05, 2017 5:30 pm
by Sik
Chilly Willy wrote:That's beside the fact that the mode where the MD boots the firmware and looks for a special signature in the cart area is not actually present in all models,
Only the LaserActive lacks it, which happens to be also the one firmware modified to have two BIOSes, and the only one where the CD BIOS data isn't aligned to a 32KB boundary at that. The other firmwares were cosmetic changes and adapting the BIOS to different CDCs, the LaserActive one seems to have had bigger changes internally so it's no wonder they decided to remove a feature that ended up going unused.
Chilly Willy wrote:and is not described in any form in the BIOS manual.
This is another problem. The Mega CD docs feel like a trainwreck compared to the Mega Drive docs, information is either spread all over the place (often in irrelevant locations) or outright missing making it a freaking pain in the ass to figure out how anything works at all. I still can't remember how much memory is there or how it's arranged =/ (and probably never will until I actively program for it and learn it the hard way)

Also I wonder why did they ever mention its existence seeing as it was apparently never meant to be used in the released add-on anyway (the few licensed cartridges that do it resort to hunting down for the BIOS in the firmware which also makes them prone to not work on different revisions).

Re: Sega CD Mode 1 support functions for SGDK

Posted: Wed Jul 05, 2017 8:14 pm
by Chilly Willy
Sik wrote:
Chilly Willy wrote:That's beside the fact that the mode where the MD boots the firmware and looks for a special signature in the cart area is not actually present in all models,
Only the LaserActive lacks it, which happens to be also the one firmware modified to have two BIOSes, and the only one where the CD BIOS data isn't aligned to a 32KB boundary at that. The other firmwares were cosmetic changes and adapting the BIOS to different CDCs, the LaserActive one seems to have had bigger changes internally so it's no wonder they decided to remove a feature that ended up going unused.
You would think that if any system used that particular mode, it would be the LaserActive. :lol:
This is another problem. The Mega CD docs feel like a trainwreck compared to the Mega Drive docs, information is either spread all over the place (often in irrelevant locations) or outright missing making it a freaking pain in the ass to figure out how anything works at all. I still can't remember how much memory is there or how it's arranged =/ (and probably never will until I actively program for it and learn it the hard way)
Yeah, a lot of the SEGA CD stuff you have to learn by looking at the examples... ALL of them as no one example gives you all the info you need. The Genesis manual was nice... virtually everything you need in a single manual.
Also I wonder why did they ever mention its existence seeing as it was apparently never meant to be used in the released add-on anyway (the few licensed cartridges that do it resort to hunting down for the BIOS in the firmware which also makes them prone to not work on different revisions).
That is weird... they devote a half a page to pseudo-code on how to do this kind of boot, and then never speak of it again! We at least have the fortune of working on this decades later when all hardware is now known, and don't need to worry about any new systems. If they REALLY wanted it used, they should have set aside a vector at a set location in the firmware that the MD side could call that would setup the CD BIOS.

Re: Sega CD Mode 1 support functions for SGDK

Posted: Wed Jul 05, 2017 9:15 pm
by KanedaFr
Chilly Willy wrote:
This is another problem. The Mega CD docs feel like a trainwreck compared to the Mega Drive docs, information is either spread all over the place (often in irrelevant locations) or outright missing making it a freaking pain in the ass to figure out how anything works at all. I still can't remember how much memory is there or how it's arranged =/ (and probably never will until I actively program for it and learn it the hard way)
Yeah, a lot of the SEGA CD stuff you have to learn by looking at the examples... ALL of them as no one example gives you all the info you need. The Genesis manual was nice... virtually everything you need in a single manual.
I approve totally.
1/ the main vs sub via communication flag and share (or not) data is "easy" to understand
2/ I finally start to understand what can ONLY be done on sub part, after several read of the manual, when I learnt a point on every try.
3/ the way you should write your sub program was a real mystery too me, and unable to write it from scratch w/o Chilly and LukeCDProject
4/ I didn't start the image processing but the manual seems good on this part
5/ I didn't try to play PCM yet, because I still don't understand if I need to load a PCM driver or if the main bios does it for me (on cart boot vs on cd boot)

Re: Sega CD Mode 1 support functions for SGDK

Posted: Wed Jul 05, 2017 10:45 pm
by Chilly Willy
You need to read the software manual and BOTH hardware manuals to get ALL the info on the ASIC with regards to graphics. Weirdly, they put some info in ALL or them, and some other info in only ONE of them. :shock:

Look at my CD mod player code to see how you play music on the PCM chip. I made a small helper library for using the PCM chip itself, and then the mod player shows how easy it is to play 4 to 8 channel mods on the SEGA CD.

Re: Sega CD Mode 1 support functions for SGDK

Posted: Thu Jul 06, 2017 1:32 am
by Sik
Chilly Willy wrote:You would think that if any system used that particular mode, it would be the LaserActive. :lol:
Dunno, its main gimmick is booting from laserdiscs instead =P
Chilly Willy wrote:That is weird... they devote a half a page to pseudo-code on how to do this kind of boot, and then never speak of it again! We at least have the fortune of working on this decades later when all hardware is now known, and don't need to worry about any new systems. If they REALLY wanted it used, they should have set aside a vector at a set location in the firmware that the MD side could call that would setup the CD BIOS.
The mode 1 in the firmware renders the need for a vector useless: the firmware itself loads the BIOS for you before jumping to the code in the cartridge. This is also the part that doesn't work on the LaserActive (it boots to the cartridge, but the SUB-CPU program isn't loaded)

...actually, now I wonder if this is precisely why the LaserActive is the one where it's broken? The data for the BIOS is aligned to a 32KB boundary in every other firmware, the LaserActive is the exception to the rule. Maybe when they were customizing the firmware something broke in the build and they decided to dummy out that piece of code instead of trying to fix it? (I still don't get why the BIOS is aligned to a 32KB boundary when it's compressed and can't be used as-is anyway)

Re: Sega CD Mode 1 support functions for SGDK

Posted: Thu Jul 06, 2017 3:48 pm
by Chilly Willy
Sounds to me like maybe the engineers at Pioneer worked on the BIOS rather than Sony engineers. They didn't quite understand the nuances of the CD BIOS and wound up doing things in a way SEGA engineers didn't expect. Given the state of the CD manuals and examples, that would be easy to imagine.

Re: Sega CD Mode 1 support functions for SGDK

Posted: Fri May 29, 2020 1:06 am
by DarkKobold
Hi all, I've taken mikejmoffitt's code, and ported it so it compiles with the native SGDK, rather than his alternate compiler. However, it currently fails at the load Sub CPU on my CDX. The CD spins, but it gives me a "Sub CPU fails to load." I'm hoping someone can help figure out why this code isn't working.

https://drive.google.com/file/d/15R6G9N ... sp=sharing

Re: Sega CD Mode 1 support functions for SGDK

Posted: Fri May 29, 2020 12:24 pm
by Chilly Willy
You screwed up some of the conversion into C. Look at the original here

Code: Select all

    write_byte(0xA1200E, 0x00); // clear main comm port
    write_byte(0xA12002, 0x2A); // write-protect up to 0x05400
    write_byte(0xA12001, 0x01); // clear bus request, deassert reset - allow CD Sub-CPU to run
and now look at your conversion of it

Code: Select all

	comm_write(SEGACD_CMD_ACK); // Clear main comm port
	*reg_mem = 0x2A01;
Look at that last line in particular - you defined reg_mem like this

Code: Select all

#define SEGACD_REG_MEM 0xA12002
...
static volatile uint16_t *reg_mem = (volatile uint16_t *)SEGACD_REG_MEM;
So "*reg_mem = 0x2A01;" is writing 0x2A to 0xA12002, then writing 0x01 to 0xA12003, not 0xA12001. You need to go back and fix this and any other like conversions. One bit of advice: don't convert byte operations on hardware into word operations. There's usually a reason that they are done a byte at a time instead of a word at a time.

Re: Sega CD Mode 1 support functions for SGDK

Posted: Sat May 30, 2020 11:24 pm
by DarkKobold
Thanks Chilly Willy.

Its odd, because he demonstrated it working, but that seems like a big error.

Sadly, even with that change, its still not working. He replaced this code:

Code: Select all

write_long((uint32_t)&vblank_vector, (uint32_t)&gen_lvl2);
With this code:

Code: Select all

*reg_cpu = (*reg_cpu & 0xEFFF) | SEGACD_CPU_IEN_MASK;
I'm guessing that's the problem? No idea how to fix this, this code is a bit beyond my skill level.

Re: Sega CD Mode 1 support functions for SGDK

Posted: Sun May 31, 2020 12:16 am
by Chilly Willy
There's a table of vectors in the MD ram for CD related stuff, and you need to alter that if you want to use MD ints AND also keep the CD going. One of the things the CD NEEDS is the level 2 int bumped from the MD side periodically. So in the MD vertical blank, I generate a level 2 int to the CD. That write_long sets the table entry for the MD vertical blank routine to my routine. You need something similar in your code or the CD won't init or keep running.