; *************************************************************************** ; *************************************************************************** ; ; core-startup.asm ; ; The "CORE(not TM)" PC Engine library startup code that runs at boot/reset. ; ; Copyright John Brandwood 2021-2024. ; ; 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) ; ; *************************************************************************** ; *************************************************************************** ; ; This is the initialization code in the 1st bank, and it is responsible for ; setting up a consistant runtime environment for the developer's program, ; so that the developer can concentrate on writing the program itself. ; ; The idea is that when a program is loaded, the first 40KB of it is mapped ; as $4000..$DFFF, and initialization starts at $4000, with the developer's ; program getting control after initialization, with a jump to "core_main". ; ; The initializtion sets up a small kernel of code that provides interrupt ; handling that mimics a subset of the System Card's behavior, designed to ; act in the same way on HuCARD and CD-ROM, with either the System Card in ; MPR7, or with an overlay in MPR7. ; ; The kernel handles reading the joypad/mouse, and it offers handler hooks ; for running the developer's interrupt code. On CD-ROM systems, it also ; handles the loading and running of subsequent overlay programs. ; ; On HuCARD, the kernel itself runs in MPR7; while on CD-ROM systems it is ; run from RAM in MPR1, so that overlay programs are independant from each ; other, and can be written in different programming languages. ; ; ; 1) If we're running on a HuCARD, the initialization is simple! ; ; The PC Engine's memory map is set to ... ; ; MPR0 = bank $FF : PCE hardware ; MPR1 = bank $F8 : PCE RAM with Stack & ZP ; MPR2 = bank $00 : HuCARD ROM ; MPR3 = bank $01 : HuCARD ROM ; MPR4 = bank $02 : HuCARD ROM ; MPR5 = bank $03 : HuCARD ROM ; MPR6 = bank $04 : HuCARD ROM ; MPR7 = bank $00 : HuCARD ROM ; ; ; 2) If we're running on a HuCARD that supports the Turbo Everdrive, then the ; first 2 banks are reserved for mapping the TED2 hardware and a RAM bank. ; ; The PC Engine's memory map is set to ... ; ; MPR0 = bank $FF : PCE hardware ; MPR1 = bank $F8 : PCE RAM with Stack & ZP ; MPR2 = bank $02 : HuCARD ROM ; MPR3 = bank $03 : HuCARD ROM ; MPR4 = bank $04 : HuCARD ROM ; MPR5 = bank $05 : HuCARD ROM ; MPR6 = bank $06 : HuCARD ROM ; MPR7 = bank $02 : HuCARD ROM ; ; ; 3) If we're running on an old CD System, the overlay is loaded from the ISO ; into banks $80-$87 (64KB max). ; ; The PC Engine's memory map is set to ... ; ; MPR0 = bank $FF : PCE hardware ; MPR1 = bank $F8 : PCE RAM with Stack & ZP ; MPR2 = bank $80 : CD RAM ; MPR3 = bank $81 : CD RAM ; MPR4 = bank $82 : CD RAM ; MPR5 = bank $83 : CD RAM ; MPR6 = bank $84 : CD RAM ; MPR7 = bank $80 : CD RAM or System Card's bank $00 ; ; ; 4) If we're running on a SuperCD System, the overlay is loaded from the ISO ; into banks $68-$87 (256KB max). ; ; The PC Engine's memory map is set to ... ; ; MPR0 = bank $FF : PCE hardware ; MPR1 = bank $F8 : PCE RAM with Stack & ZP ; MPR2 = bank $68 : SCD RAM ; MPR3 = bank $69 : SCD RAM ; MPR4 = bank $6A : SCD RAM ; MPR5 = bank $6B : SCD RAM ; MPR6 = bank $6C : SCD RAM ; MPR7 = bank $68 : SCD RAM or System Card's bank $00 ; ; *************************************************************************** ; *************************************************************************** .code .bank 0 .if SUPPORT_TED2 ; Do we want to use a TED2? .if !CDROM ; Only applies to a HuCARD! ; *************************************************************************** ; *************************************************************************** ; ; RESET VECTORS (when booted as a HuCARD for the Turbo Everdrive 2) ; ; Minimal HuCARD startup code that immediately trampoline's ; to the normal HuCARD startup code in bank 2. .org $FF00 ted2_hw_reset: sei ; Disable interrupts. csh ; Set high-speed mode. cld ldx #$FF ; Initialize stack pointer. txs txa ; MPR0 = $FF : PCE hardware tam0 ; MPR1 = $F8 : ZP & Stack lda #$F8 tam1 tii .reboot_bank, _ax, 7 ; Reboot with bank 2 in MPR7. jmp _ax .reboot_bank: lda #2 ; Put bank 2 in MPR7. tam7 jmp [$FFFE] ; Call reset, just like boot. ; This string at this location tells both TEOS and Mednafen ; that this HuCARD ROM is TED2-aware, and to run the HuCARD ; with 1MB RAM enabled. .org $FFD0 db "TED2CARD" ; Signature. dw 0 ; Reserved word. dw 0 ; Reserved word. ; Hardware reset and interrupt vectors. .org $FFF4 db CORE_VERSION ; CORE(not TM) Version. db $80 ; System Card compatibility. dw ted2_hw_reset ; IRQ2 (from CD/ADPCM) dw ted2_hw_reset ; IRQ1 (from VDC) dw ted2_hw_reset ; TIMER (from CPU) dw ted2_hw_reset ; NMI (unused) dw ted2_hw_reset ; RESET .data .bank 1 ; Keep bank 1 for MPR2 RAM! .org $5FFE ; Put something at the end so dw $FFFF ; that procedures are not put .org $4000 ; in this bank! .code .bank 2 ; Continue in bank 2. .if USING_NEWPROC ; If the ".proc" trampolines __trampolinebnk = 2 ; are in MPR7, tell PCEAS to .endif ; put them in bank 2. .else !CDROM .fail You cannot currently build for CD-ROM and SUPPORT_TED2! .endif !CDROM .endif SUPPORT_TED2 .if USING_MPR7 ; *************************************************************************** ; *************************************************************************** ; ; RESET VECTORS (when running in MPR7, either as a HuCARD, or a CD overlay) ; ; Hardware reset and interrupt vectors. .org $FFF4 core_version: db CORE_VERSION ; CORE(not TM) Version. db $80 ; System Card compatibility. dw core_irq2 ; IRQ2 (from CD/ADPCM) dw core_irq1 ; IRQ1 (from VDC) dw core_timer ; TIMER (from CPU) dw core_rti ; NMI (unused) .if CDROM dw core_sw_reset ; RESET (CD-ROM) .else dw core_hw_reset ; RESET (HuCARD) .endif CDROM .if USING_NEWPROC ; If the ".proc" trampolines __trampolineptr = $FFF3 ; are in MPR7, tell PCEAS to .endif ; put them below the vectors. .else USING_MPR7 ; Sanity Check. .if !CDROM .fail You cannot build for HuCARD without using MPR7! .endif !CDROM .if USING_NEWPROC ; If the ".proc" trampolines __trampolineptr = $5FFF ; are in MPR2, tell PCEAS to .endif ; put them right at the end. .endif USING_MPR7 ; *************************************************************************** ; *************************************************************************** ; ; !!! THE HuCARD/OVERLAY PROGRAM'S FIRST BANK STARTS HERE !!! ; ; Switch to MPR2 for the "CORE(not TM)" library init. ; ; This is also executed by a HuCARD once it has run ; its initial hardware-reset code. ; ; When run, MPR2-MPR6 are always mapped to the 1st 5 banks of ; the overlay program, and MPR7 contains the System Card. .org $4000 CORE_BANK = bank(*) - _bank_base ; It isn't always zero! ;-) .bank CORE_BANK, "CORE_BANK" ; Give it a label. .if USING_MPR7 CORE_PAGE = 7 ; User code runs in MPR7. .else CORE_PAGE = 2 ; User code runs in MPR2. .endif USING_MPR7 core_boot: jmp * + 6 ; Allow someone to patch this. ; Add an ident string so isoLINK can autodetect configuration. ; ; This allows isoLINK to decide whether to use the IPL-SCD boot ; sector hack, and whether to include a SuperGRAFX signature in ; the boot sector, without the user having to manually use some ; command line parameters, although they are free to do that if ; they wish. .if SUPPORT_SGX .if BUILDING_STAGE1 db "SG1" ; SGX SuperCD Stage1 loader. .else db "SGX" ; SGX SuperCD or SGX HuCARD. .endif BUILDING_STAGE1 .else .if CDROM .if CDROM == SUPER_CD .if BUILDING_STAGE1 db "SC1" ; PCE SuperCD Stage1 loader. .else db "SCD" ; PCE SuperCD. .endif BUILDING_STAGE1 .else db " CD" ; PCE CD. .endif CDROM == SUPER_CD .else db "PCE" ; PCE HuCARD. .endif CDROM .endif SUPPORT_SGX .if CDROM .if !USING_STAGE1 ; Because the Stage1 does it! lda core_kernel ; Did a previous overlay set bne .not_first ; up the kernel already? ; Is there a duplicate of the main ISO data track? .if SUPPORT_2ISO lda tnomax ; Set the last track as the sta <_al ; backup area for reading lda #$80 ; CD data in case of disc sta <_bh ; errors. lda #2 sta <_cl jsr cd_base .endif SUPPORT_2ISO sei ; Disable interrupts! ; Copy the ISO's directory from IPL memory to a safe spot. tii $35FF, $3DFF, 512 + 1 ; Copy the kernel code to its destination in MPR1. tii core_rom1st, core_ram1st, (core_ramiso - core_ram1st) ; Copy the ISO's directory into kernel memory in MPR1. tii $3DFF, iso_cderr, MAX_DIRSIZE + 1 tii $3F00, iso_dirhi, MAX_DIRSIZE ; Clear the rest of the RAM bank on the 1st time through. tai const_0000, core_ramend, ($4000 - core_ramend) .endif !USING_STAGE1 ; Subsequent overlays only clear zero-page, and it is up to ; the overlay itself to clear any part of the BSS RAM that ; needs to be cleared. .not_first: sei ; Disable interrupts! tai const_0000, core_zp1st, (core_zpend - core_zp1st) .else CDROM ; Set up HuCARD RAM in a compatible way to the System Card. ; ; Note that the entire RAM is cleared by "core_hw_reset". lda #%11111 ; Enable joypad soft-reset. sta joyena .endif CDROM ; Now that RAM is initialized ... tma2 ; Remember overlay's 1st bank sta