GBS = 1

INCLUDE "includes.asm"
INCLUDE "gbs_rst.asm"

SECTION "GBS Header", ROM0[$390]
	charmap " ", $20
	db "GBS"                         ;signature
	db 1                             ;version
	db NUM_MUSIC - 1                 ;number of songs
	db 1                             ;first song
	dw $400                          ;load address
	dw GBS_Init                      ;init address
	dw GBS_Play                      ;play address
	dw Stack                         ;stack pointer
	db $100 - 209                    ;rTMA
	db 1 << rTAC_ON | rTAC_262144_HZ ;rTAC
GBS_TitleText:
	db "Pokemon Prism"
	ds GBS_TitleText - @ + 32
GBS_AuthorText:
	db ""
	ds GBS_AuthorText - @ + 32
GBS_CopyrightText:
	db "2001 Nintendo 2017 RainbowDevs"
	ds GBS_CopyrightText - @ + 32

SECTION "GBS Main", ROM0[$440]
GBS_Init:
	di
	inc a
	ld e, a
	ld d, 0
	ld a, 1 << TIMER
	ld [rIE], a
	ld a, 1 << 5 ; stereo on
	ld [wOptions], a
	ei
	call TurnSoundOff
	call PlayMusic
	ld a, 1
	ld [hVBlankCounter], a
	ret

GBS_Play:
	ld a, [hVBlankCounter]
	dec a
	jr nz, .skip
	call UpdateSound
	xor a
	ld [wVBlankOccurred], a
	ld a, 21 ; 262144/(209*21) = 59.7275 -> VBlank rate
.skip
	ld [hVBlankCounter], a
	reti

DelayFrame::
; Wait for one frame
	ld a, 1
	ld [wVBlankOccurred], a

; Wait for the next VBlank, halting to conserve battery
.halt
	halt ; rgbasm adds a nop after this instruction by default
	ld a, [wVBlankOccurred]
	and a
	jr nz, .halt
	ret

INCLUDE "home/farcall.asm"
INCLUDE "home/jumptable.asm"
INCLUDE "home/audio.asm"
