; Super Game Boy initialization
init_sgb:
xor a
ld [_SGB_F], a ; Indicates if the game is running on a Super Game Boy
ld a, SGB_BNK
ld [_CUR_BNK], a
ld [$2000], a ; Bank with the Super Game Boy related graphics and routines
call check_sgb
ret nc ; We return if the game is not running on a Super Game Boy
di
ld a, 1
ld [_SGB_F], a
ld hl, MaskEnFreeze
call sgbpackettransfer ; Freezes the visualization of the Super Game Boy screen to hide the graphic garbage during the VRAM transfers
call init_sgb_default ; 8 initialization data packet sending, according to the official documentation
xor a
ld [_AUXVAR], a
ld de, ChrTrn1
ld hl, SGBBorderTiles
call copysnes ; Copies to SNES RAM the first 128 tiles of the frame (256 Game Boy tiles)
xor a
ld [_AUXVAR], a
ld de, ChrTrn2
ld hl, SGBBorderTiles + 4096
call copysnes ; Copies to SNES RAM the second 128 tiles of the frame (256 Game Boy tiles)
xor a
ld [_AUXVAR], a
ld de, PctTrn
ld hl, SGBBorder
call copysnes ; Copies to SNES RAM the frame map
xor a
ld [_AUXVAR], a
ld de, PalTrn
ld hl, SGBSuperPalettes
call copysnes ; Copies to SNES RAM the custom game palettes
; VRAM reset
ld de, _VRAM
ld bc, $2000
ld l, $0
call fillmem
; Default game palette set
ld hl, PalSet
call sgbpackettransfer
ld hl, MaskEnCancel
call sgbpackettransfer ; Super Game Boy screen visualization unfreezing
ret