; *************************************************************************** ; *************************************************************************** ; ; TURBO-GRAFX / PC-ENGINE SUPER SYSTEM CARD 3.00 PATCH FOR TURBO EVERDRIVE 2 ; ; Copyright John Brandwood 2015. ; ; Distributed under the Boost Software License, Version 1.0. ; (See accompanying file LICENSE_1_0.txt or copy at ; http://www.boost.org/LICENSE_1_0.txt) ; ; *************************************************************************** ; *************************************************************************** ; ; * Fixes problem with the Turbo Everdrive 2 and DUO/DUO-R/SuperCDROM RAM. ; * Allows the TED2 to be used as a Super System Card on a TurboGrafx CD. ; * Expands SCD RAM memory to 512KB for translations/homebrew. ; * Mednafen-supported 512KB RAM using a MCGENJIN header. ; ; *************************************************************************** ; *************************************************************************** ; ; It is important to use a patch like this to avoid CPU bus-fighting on the ; Turbo Everdrive 2 when both the TED and the DUO/DUO-R/SuperCDROM RAM are ; trying to respond to CPU read requests at the same time. ; ; This occurs beacuse the built-in SCD RAM on the DUO/DUO-R/SuperCDROM does ; not disable itself on the CARD-DETECT signal that the Turbo Everdrive uses ; to disable the built-in System Card. ; ; An unpatched System Card unlocks the SCD RAM by doing 2 writes to the SCD ; hardware register at $18c0. ; ; This patch changes the System Card's SCD RAM detection routine to remove ; that unlocking sequence and so avoids the damaging bus-fighting. ; ; It also changes the TED2 into a 1MB RAM configuration, and adds some ; code to repair the System Card after the "Gate of Thunder" game writes ; to System Card memory. ; ; GATE OF THUNDER problem with System Card in RAM: ; ; "Gate Of Thunder" calls EX_MEMOPEN and then overwrites $FFF5 with the ; SCD RAM starting bank number. $FFF5 normally contains $80. ; ; "Gate Of Thunder" then immediately calls EX_SCRSIZE which is a good ; place to patch in code to fix the damage. ; ; EX_SCRSIZE is vector $e069, which points to code at $e280. ; .include "standard.inc" .include "system.inc" ; ; Decide which platform to build for. ; ; Pick one, and only one! ; MEDNAFEN_BUILD = 0 TED2_BUILD = 1 JPN_SYSCARD = 1 ; ; Build on top of an original US Super System Card image. ; ; MD5: 0754f903b52e3b3342202bdafb13efa5 *SYSCARD3.PCE.USA ; .bank 0 .org $0000 .if JPN_SYSCARD .incbin "syscard3.pce.jpn" SCRSIZ_ADR = $e267 MEMOPEN_ADR = $fe92 MEMOPEN_END = $fec2 UNLOCK_ADR = $c875 HACKINIT_ADR = $c887 DISPLAY_SUPER = $c950 MESSAGE_ADR = $c9d1 INITCODE_ADR = $ccf9 .else .incbin "syscard3.pce.usa" SCRSIZ_ADR = $e280 MEMOPEN_ADR = $feab MEMOPEN_END = $fedb UNLOCK_ADR = $c875 HACKINIT_ADR = $c887 DISPLAY_SUPER = $c943 MESSAGE_ADR = $c9c4 INITCODE_ADR = $cc8b .endif .if MEDNAFEN_BUILD ; ; Pad the System Card image out to 512KB for Mednafen. ; .bank 32 .org $0000 .incbin "padding.bin" ; ; If we're building for Mednafen, then supply a dummy ; MCGENJIN header that Mednafen will recognize and allow ; 512KB RAM. ; ; This is the minimum needed to get Mednafen to turn on ; it's MCGENJIN support, I'm not supplying the critical ; region-swapped vectors that you need on real hardware. ; .bank 0 .org $ffd0 .db "MCGENJIN" .db 0 .db 2 .db 0 .db $15 .db $15 .db 0 .db 0 .db 0 .ds $fff4-* .else ; ; If we're building for real hardware with the System ; Card in RAM then keep copies of the first and last ; 16-bytes of the original System Card so that we can ; restore any overwrites to registers or IRQ vectors. ; ; The idea is that these 2 backup areas and the signature ; area are all going to be at fixed locations in this and ; any future versions of the expanded-ram System Card so ; that programs can rely on them. ; .bank 0 .if JPN_SYSCARD .org $ffb0 original_e000: .db $4c,$f3,$e0,$4c ; Original JPN ROM $e000-$e00f. .db $e3,$e8,$4c,$8f .db $eb,$4c,$05,$ec .db $4c,$cb,$ed,$4c .org $ffc0 original_fff0: .db $e9,$4c,$dc,$e9 ; Original JPN ROM $fff0-$ffff. .db $68,$80,$36,$e7 .db $70,$e8,$b3,$e6 .db $a9,$e6,$f3,$e0 .else .org $ffb0 original_e000: .db $4c,$f3,$e0,$4c ; Original USA ROM $e000-$e00f. .db $fc,$e8,$4c,$a8 .db $eb,$4c,$1e,$ec .db $4c,$e4,$ed,$4c .org $ffc0 original_fff0: .db $e9,$4c,$f5,$e9 ; Original USA ROM $fff0-$ffff. .db $68,$80,$4f,$e7 .db $89,$e8,$cc,$e6 .db $c2,$e6,$f3,$e0 .endif .org $ffd0 ; ; Signature (same address as McGenjin card). ; card_signature: .db "TED2CARD" .db $00 ; Capabilities byte 0. .db $00 ; Capabilities byte 1. .db $00 ; Capabilities byte 2. .db $00 ; Capabilities byte 3. .endif ; ; Hack EX_MEMOPEN to say that SCD RAM exists and is unlocked. ; .bank 0 .org MEMOPEN_ADR my_ex_memopen: lda #$68 ldx #$03 clc rts .if MEDNAFEN_BUILD .else my_ex_scrsiz: jsr SCRSIZ_ADR lda #$80 sta $fff5 rts .endif .ds MEMOPEN_END-* ; ; If we're building for a TED2 or 1MB-RAM card then patch ; EX_SCRSIZ to repair the damage that "Gate of Thunder" ; does on startup. ; .if MEDNAFEN_BUILD .else .bank 0 .org $e069 jmp my_ex_scrsiz .endif ; ; Remove the writes that unlock the SCD RAM so that we don't ; get bus-fighting on cards with their own RAM. ; .bank 1 .org UNLOCK_ADR nop ; 1 nop ; 2 nop ; 3 nop ; 4 nop ; 5 nop ; 6 nop ; 7 nop ; 8 nop ; 9 nop ; 10 ; ; If we're building for a TED2 or 1MB-RAM card then patch the ; System Card's SCD RAM detect to do our initialization and ; to restore the original System Card contents in case it has ; been overwritten by the last program that ran. ; .if MEDNAFEN_BUILD .else .bank 1 .org HACKINIT_ADR jsr scd_ram_found ; Patch System Card's SCD RAM detect. .bank 1 .org INITCODE_ADR scd_ram_found: jsr DISPLAY_SUPER ; Display "SUPER" System Card. php ; Disable interrupts for the next bit. sei .if TED2_BUILD lda #$A5 ; Unlock TED2 registers. sta $e005 lda #$56 sta $e00a lda #$A5 ; Do it again to be sure. sta $e005 lda #$56 sta $e00a lda #$21 ; Enable SF2 mapper. sta $e006 stz $fff0 ; Reset current SF2 bank. lda #$03 ; Disable SF2 mapper, lock registers. sta $e006 .endif tii $ffb0,$e000,16 ; Restore System Card vectors. tdd $ffcf,$ffff,16 ; Restore any SF2 mapping damage. plp ; ; Clear SCD RAM. ; ; This isn't critical on the TED2 but it will stop ; bus-fighting on a 1MB-RAM card. ; .clear_ram: lda #$68 .bank_loop: tam #2 clx .page_loop: stz $4000,x stz $4100,x stz $4200,x stz $4300,x stz $4400,x stz $4500,x stz $4600,x stz $4700,x stz $4800,x stz $4900,x stz $4a00,x stz $4b00,x stz $4c00,x stz $4d00,x stz $4e00,x stz $4f00,x stz $5000,x stz $5100,x stz $5200,x stz $5300,x stz $5400,x stz $5500,x stz $5600,x stz $5700,x stz $5800,x stz $5900,x stz $5a00,x stz $5b00,x stz $5c00,x stz $5d00,x stz $5e00,x stz $5f00,x inx bne .page_loop inc a bpl .bank_loop tam #2 ; Restore original bank. rts ; Initialization complete. .endif ; ; Change on-screen identification so that we know what this ; code has been built for. ; ; Originally "VER. 3.00". ; .bank 1 .org MESSAGE_ADR .if MEDNAFEN_BUILD .db "MDFN 3.01" .else .db "TED2 3.01" .endif