Sega CD Mode 1 support functions for SGDK

For anything related to sound (YM2612, PSG, Z80, PCM...)

Moderator: BigEvilCorporation

DarkKobold
Newbie
Posts: 5
Joined: Thu May 21, 2020 10:50 pm

Re: Sega CD Mode 1 support functions for SGDK

Post by DarkKobold » Thu Jun 04, 2020 3:42 am

As I mentioned, I'm probably far too unskilled to make that level of change. I really just changed how it handled registers. I'm super curious how he got it working, since there are so many issues with this code. Is there any way I could get help on this?

Manveru
Very interested
Posts: 85
Joined: Wed Sep 05, 2012 3:30 pm

Re: Sega CD Mode 1 support functions for SGDK

Post by Manveru » Thu Jun 04, 2020 4:16 pm

Now i see this thread up, i wonder if there is any tutorial or else to make "Mode 1" (sorry Sik) work in emulators. I tried with Kega but it has some glitches that does not draw the graphics after loading the CD. It is suppose to work with Genesis plus gx, but i can not find any guide for that, so any help is welcome.

EDIT: ok, i knew Kega DMA stuff stop working when you load a CD in Mode 1, but i forgot i was using DMA for some stuff and that was the reason it was not working for me. So no DMA, no problem using Kega for that.
The man who moves a mountain begins by carrying away small stones. Confucius, 551-479 BC

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Re: Sega CD Mode 1 support functions for SGDK

Post by Chilly Willy » Thu Jun 04, 2020 7:08 pm

Emulators generally don't handle things like Mode 1 since it wasn't used by anything commercial, so it was of no interest to emulator authors. It's like CD32X emulation - not something emulations handle. This is more a real hardware thing.

Manveru
Very interested
Posts: 85
Joined: Wed Sep 05, 2012 3:30 pm

Re: Sega CD Mode 1 support functions for SGDK

Post by Manveru » Thu Jun 04, 2020 11:08 pm

Chilly Willy wrote:
Thu Jun 04, 2020 7:08 pm
Emulators generally don't handle things like Mode 1 since it wasn't used by anything commercial, so it was of no interest to emulator authors. It's like CD32X emulation - not something emulations handle. This is more a real hardware thing.
Yeah, but finally Kega can be used for testing if you do not use DMA.

By the way i can see when i try your mode 1 rom in Kega and Console and you open the MegaCD but you close it with no disc, it seems to detect a disc somehow, and some time later the screen changes with a "no disc" message. The problem is the red led that keeps blinking forever. Maybe you need to do some extra test for empty cd slot?

Thanks

EDIT: well, i realize MegaCD led keeps blinking when it is switched on and no cd. It is a bad thing having a MD game with Mode 1 feature, but if you do not put a disc on the CD unit you will have a led blinking for hours... Is there a way to say to the Mega CD to stop detecting CD, or switch off the CD or whatever?
The man who moves a mountain begins by carrying away small stones. Confucius, 551-479 BC

DarkKobold
Newbie
Posts: 5
Joined: Thu May 21, 2020 10:50 pm

Re: Sega CD Mode 1 support functions for SGDK

Post by DarkKobold » Fri Jun 05, 2020 5:44 pm

I think there might be an issue with the KOS decompile.

I had to change

Code: Select all

 bra.b   Kos_Decomp_Loop
to

Code: Select all

 bra.w   Kos_Decomp_Loop
This suggests that the kos decomp asm isn't compiling the same, which doesn't make a ton of sense.

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Re: Sega CD Mode 1 support functions for SGDK

Post by Chilly Willy » Fri Jun 05, 2020 7:27 pm

Manveru wrote:
Thu Jun 04, 2020 11:08 pm
EDIT: well, i realize MegaCD led keeps blinking when it is switched on and no cd. It is a bad thing having a MD game with Mode 1 feature, but if you do not put a disc on the CD unit you will have a led blinking for hours... Is there a way to say to the Mega CD to stop detecting CD, or switch off the CD or whatever?
I suppose that after a certain amount of time, or if the user selects something from the menu to disable the cd, you could reset the asic and the sub cpu. That should do it.

Code: Select all

    /*
	 * Reset the Gate Array - this specific sequence of writes is recognized by
	 * the gate array as a reset sequence, clearing the entire internal state -
	 * this is needed for the LaserActive
	 */
	write_word(0xA12002, 0xFF00);
	write_byte(0xA12001, 0x03);
	write_byte(0xA12001, 0x02);
	write_byte(0xA12001, 0x00);

    /*
     * Reset the Sub-CPU, request the bus
     */
    write_byte(0xA12001, 0x02);
    while (!(read_byte(0xA12001) & 2)) write_byte(0xA12001, 0x02); // wait on bus acknowledge
You could also have another menu option to re-init the CD if the user decided later to actually use it.

Manveru
Very interested
Posts: 85
Joined: Wed Sep 05, 2012 3:30 pm

Re: Sega CD Mode 1 support functions for SGDK

Post by Manveru » Sat Jun 06, 2020 2:43 pm

Chilly Willy wrote:
Fri Jun 05, 2020 7:27 pm
I suppose that after a certain amount of time, or if the user selects something from the menu to disable the cd, you could reset the asic and the sub cpu. That should do it.

Code: Select all

    /*
	 * Reset the Gate Array - this specific sequence of writes is recognized by
	 * the gate array as a reset sequence, clearing the entire internal state -
	 * this is needed for the LaserActive
	 */
	write_word(0xA12002, 0xFF00);
	write_byte(0xA12001, 0x03);
	write_byte(0xA12001, 0x02);
	write_byte(0xA12001, 0x00);

    /*
     * Reset the Sub-CPU, request the bus
     */
    write_byte(0xA12001, 0x02);
    while (!(read_byte(0xA12001) & 2)) write_byte(0xA12001, 0x02); // wait on bus acknowledge
You could also have another menu option to re-init the CD if the user decided later to actually use it.
Thanks Chilly Willy, it works perfectly. May the videogames gods bless you.
The man who moves a mountain begins by carrying away small stones. Confucius, 551-479 BC

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Re: Sega CD Mode 1 support functions for SGDK

Post by Chilly Willy » Sun Jun 07, 2020 12:49 pm

Good to hear. I'll have to remember to make that change in my own stuff. That was a good point - while leaving the CD constantly checking probably doesn't hurt anything, it's better to just shut things down after a while. A menu option in case the user changes their mind later is probably also a good idea.

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

Re: Sega CD Mode 1 support functions for SGDK

Post by Eke » Thu Jul 23, 2020 8:18 pm

Manveru wrote:
Thu Jun 04, 2020 4:16 pm
It is suppose to work with Genesis plus gx, but i can not find any guide for that, so any help is welcome
Sorry, I didn't see this post earlier but it's indeed not well documented. It's quite simple to activate Mega CD mode 1 though (I call it "cartridge boot" mode): just put the cue file and associated CD image file(s) in same directory as the ROM file, using same basename for the cue file and the ROM file ("game.md" and "game.cue" for example) then load the ROM file. You will also need a CD BIOS file with correct name for each region as required by the emulator for Mega CD emulation (BIOS_CD_U.bin, etc).

DarkKobold
Newbie
Posts: 5
Joined: Thu May 21, 2020 10:50 pm

Re: Sega CD Mode 1 support functions for SGDK

Post by DarkKobold » Fri Aug 14, 2020 11:31 pm

So, I managed to fix the library, and I'll be updating it soon and distributing it. A couple things - I looked up the MegaCD documentation, and managed to add fade routines-

Code: Select all

SetVolume:    
	move.w #0x0400,%d1			/*MaxVolume*/
	move.w #0x0085,%d0   		/*FDRSET            EQU   $0085*/
	jsr	 0x5F22.w				/* call CDBIOS function */	

	move.b  #'D,0x800F.w /* sub comm port = DONE */
	bra	 WaitAck
			
FadeOut:
	move.l #0x00000008,%d1
	move.w #0x0086,%d0    /*FDRCHG            EQU   $0086*/
	jsr	 0x5F22.w				/* call CDBIOS function */	
	
	move.b  #'D,0x800F.w  /* sub comm port = DONE */
	bra	 WaitAck							
			
FadeIn:
	move.l #0x04000002,%d1
	move.w #0x0086,%d0    /*FDRCHG            EQU   $0086*/
	jsr	 0x5F22.w				/* call CDBIOS function */	
	
	move.b  #'D,0x800F.w  /* sub comm port = DONE */
	bra	 WaitAck
I want to be able to pass variables, rather than have fixed volume/fixed speed fades, but I really don't understand how it's done between ASM & C.

Further, someone asked if I could implement the MCD function MSPLAYT, but I really don't understand how the table works.

Code: Select all

table:
dl $00020000
subroutine:
lea.1 table (pc), a0
#MSPLAYT
)
move.w , d0
jsr _cdbios
rts
I tried to a the code from the Track playing, but it's far too confusing -

Code: Select all

PlayTrack:
		move.w  #0x0002,%d0			  /* MSCSTOP - stop playing */
		jsr	 0x5F22.w				/* call CDBIOS function */

		move.w  0x8010.w,%d1			 /* track number */
		move.w  #0x0011,%d0			  /* MSCPLAY - play from track on */
		move.b  0x8012.w,%d2			 /* flag */
		bmi.b   2f
		beq.b   1f
		move.w  #0x0013,%d0			  /* MSCPLAYR - play with repeat */
		bra.b   2f
1:
		move.w  #0x0012,%d0			  /* MSCPLAY1 - play once */
2:
		lea	 TrackNumber(%pc),%a0
		move.w  %d1,(%a0)
		jsr	 0x5F22.w				/* call CDBIOS function */

		move.b  #'D,0x800F.w			/* sub comm port = DONE */
		bra	 WaitAck
Specifically, what's with the LEA command? And why does it reference some word that's randomly placed in the code? Track Number is never called again. What gives?

Code: Select all

		.align  2
DriveInitParms:
		.byte   0x01, 0xFF			  /* first track (1), last track (all) */

TrackNumber:
		.word   0


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

Re: Sega CD Mode 1 support functions for SGDK

Post by Sik » Sat Aug 15, 2020 8:51 am

It's writing the track number there before doing the BIOS call (see the move d1, (a0) immediately after that lea).

Though… yeaaah not sure why those BIOS calls want a pointer to a single word when it could have been passed as-is through d1. Only explanation I can find is consistency since it seems that every call is like this and some do take multiple arguments through the pointer… but there are so many taking a single value as argument that I'm left wondering why didn't they make an alternate convention for those.

Then again it wouldn't be the only questionable decision in the BIOS code (e.g. there's a "microoptimized" routine to write bytes into back-up RAM running MOVEP in a loop… that isn't unrolled… and there are checks since it also supports lengths not multiple of 4 and so it needs to write the excess bytes separately… and then it proceeds to call it with lengths of 4 or 8 bytes… um wtf was the point of that?)
Sik is pronounced as "seek", not as "sick".

Post Reply