// This code makes every door load the right section of the background when using BG_Data type 000E
// Made by P.JBoy, 10/05/2009, for Super Metroid (JU)[!]


arch snes.cpu ; lorom ; noheader


//---------------------Hijacks----------------------


org $82E5D9     //\
jsr fromDoor    //} When coming out a door
nop #15         ///

org $82E9E7     //\
jsr fromShip    //} When loading from the ship
nop #15         ///


//----------------------Code------------------------


org $82F777
fromShip:
lda.w $079F             //\
asl                     //|
tax                     //|
lda.l $80C4B5,x         //|
sta.b $00               //|
lda.w $078B             //|
asl #3                  //} Load area load station Y/100h
sec                     //|
sbc.w $078B             //|
asl                     //|
adc.b $00               //|
tax                     //|
lda.l $800009,x         ///
bra +

fromDoor:
ldx.w $078D             //\
lda.l $830007,x         //} Load DDB screen-Y

+
and.w #$00FF
ldx.w #$4800            //\
bit.w #$0001            //|
beq +                   //} Gets the right section of VRAM to write to
ldx.w #$4C00            ///

+
stx.b $03               // Stores bg dest.
ldx.w #$0080            //\
stx.b $00               //} Stores the low byte of the bg source address
asl #3                  //\
adc.w #$8AB1            //} Gets the right section of BG to load
sta.b $01               // Stores bank and high bytes of the bg source
lda.w #$0800            //\
sta.b $05               //} Stores bg size
ldy.w #$0000            // Makes the game load from 7E0000 for BG_Data
sty.b $07               // Ends BG_Data loop
rts
