shithub: pokecrystal

Download patch

ref: 3fe3cd1a46be17dd3f94b605ff7385a55358dd74
parent: 1aa28c7712cb23401f8c8af30e78672588743d87
author: yenatch <[email protected]>
date: Mon Jan 19 16:31:29 EST 2015

hVBlank

--- a/battle/anim_commands.asm
+++ b/battle/anim_commands.asm
@@ -35,7 +35,7 @@
 	ld c, 3
 
 .asm_cc0ff
-	ld hl, $ff9e
+	ld hl, hVBlank
 	ld a, [hl]
 	push af
 
@@ -43,7 +43,7 @@
 	call Functioncc11c
 
 	pop af
-	ld [$ff9e], a
+	ld [hVBlank], a
 
 	ld a, $1
 	ld [hBGMapMode], a
--- a/engine/credits.asm
+++ b/engine/credits.asm
@@ -188,10 +188,10 @@
 
 	call GetCreditsPalette
 	call Function32f9
-	ld a, [$ff9e]
+	ld a, [hVBlank]
 	push af
 	ld a, $5
-	ld [$ff9e], a
+	ld [hVBlank], a
 	ld a, $1
 	ld [$ffaa], a
 	xor a
@@ -215,7 +215,7 @@
 	ld [hLCDStatCustom], a
 	ld [hBGMapAddress], a
 	pop af
-	ld [$ff9e], a
+	ld [hVBlank], a
 	pop af
 	ld [rSVBK], a
 	ret
--- a/home/vblank.asm
+++ b/home/vblank.asm
@@ -12,14 +12,12 @@
 	push bc
 	push de
 	push hl
-	
-; get vblank type
-	ld a, [$ff9e]
-	and $7
-	
-; get fn pointer
+
+	ld a, [hVBlank]
+	and 7
+
 	ld e, a
-	ld d, $0
+	ld d, 0
 	ld hl, .VBlanks
 	add hl, de
 	add hl, de
@@ -26,13 +24,11 @@
 	ld a, [hli]
 	ld h, [hl]
 	ld l, a
-	
-; down to business
+
 	call _hl_
-	
-; since this is called once per frame
+
 	call GameTimer
-	
+
 	pop hl
 	pop de
 	pop bc
@@ -41,14 +37,14 @@
 ; 2a1
 
 .VBlanks ; 2a1
-	dw VBlank0 ; 0
-	dw VBlank1 ; 1
-	dw VBlank2 ; 2
-	dw VBlank3 ; 3
-	dw VBlank4 ; 4
-	dw VBlank5 ; 5
-	dw VBlank6 ; 6
-	dw VBlank0 ; 7
+	dw VBlank0
+	dw VBlank1
+	dw VBlank2
+	dw VBlank3
+	dw VBlank4
+	dw VBlank5
+	dw VBlank6
+	dw VBlank0 ; just in case
 ; 2b1
 
 
@@ -66,117 +62,91 @@
 ; joypad
 ; sound
 
-; inc frame counter
+	; inc frame counter
 	ld hl, $ff9b
 	inc [hl]
-	
-; advance rng
+
+	; advance random variables
 	ld a, [rDIV]
 	ld b, a
 	ld a, [hRandomAdd]
 	adc b
 	ld [hRandomAdd], a
-	
+
 	ld a, [rDIV]
 	ld b, a
 	ld a, [hRandomSub]
 	sbc b
 	ld [hRandomSub], a
-	
-; save bank
-	ld a, [hROMBank] ; current bank
-	ld [$ff8a], a
-	
-; scroll x
+
+	ld a, [hROMBank]
+	ld [hROMBankBackup], a
+
 	ld a, [hSCX]
 	ld [rSCX], a
-; scroll y
 	ld a, [hSCY]
 	ld [rSCY], a
-; window y
 	ld a, [hWY]
 	ld [rWY], a
-; window x + 7
 	ld a, [hWX]
 	ld [rWX], a
-	
-; some time management is in order
-; only have time for one of these during vblank
-	
-; bg map buffer has priority
+
+	; There's only time to call one of these in one vblank.
+	; Calls are in order of priority.
+
 	call UpdateBGMapBuffer
-	jr c, .doneframeaction
-	
-; then pals
+	jr c, .done
 	call UpdatePalsIfCGB
-	jr c, .doneframeaction
-	
-; dma transfer
+	jr c, .done
 	call DMATransfer
-	jr c, .doneframeaction
-	
-; bg map
+	jr c, .done
 	call UpdateBGMap
-	
-; these have their own timing checks
+
+	; These have their own timing checks.
+
 	call Serve2bppRequest
 	call Serve1bppRequest
 	call AnimateTileset
-	
-.doneframeaction
-; oam update off?
+
+.done
+
 	ld a, [hOAMUpdate]
 	and a
-	jr nz, .vblankoccurred
-	
-; update oam by dma transfer
+	jr nz, .done_oam
 	call hPushOAM
-;	@PushOAM:
-;		ld a, Sprites >> 8
-;		ld [rDMA], a
-;		ld a, $28
-;	.loop
-;		dec a
-;		jr nz, .loop
-;		ret
+.done_oam
 
 
-; vblank-sensitive operations are done
-	
-.vblankoccurred
-; tell other fns vblank happened
+	; vblank-sensitive operations are done
+
 	xor a
 	ld [VBlankOccurred], a
-	
-; dec OverworldDelay until 0
+
 	ld a, [OverworldDelay]
 	and a
-	jr z, .textdelay
+	jr z, .ok
 	dec a
 	ld [OverworldDelay], a
-	
-.textdelay
-; dec text delay counter until 0
+.ok
+
 	ld a, [TextDelayFrames]
 	and a
-	jr z, .joypad
+	jr z, .ok2
 	dec a
 	ld [TextDelayFrames], a
-	
-.joypad
+.ok2
+
 	call Joypad
-	
-; update sound
+
 	ld a, BANK(_UpdateSound)
-	rst Bankswitch ; bankswitch
+	rst Bankswitch
 	call _UpdateSound
-	ld a, [$ff8a]
-	rst Bankswitch ; restore bank
-	
-; 
+	ld a, [hROMBankBackup]
+	rst Bankswitch
+
 	ld a, [hSeconds]
 	ld [$ffe3], a
-	
+
 	ret
 ; 325
 
@@ -184,20 +154,16 @@
 VBlank2:: ; 325
 ; sound only
 
-; save bank
 	ld a, [hROMBank]
-	ld [$ff8a], a
-	
-; update sound
+	ld [hROMBankBackup], a
+
 	ld a, BANK(_UpdateSound)
-	rst Bankswitch ; bankswitch
+	rst Bankswitch
 	call _UpdateSound
-	
-; restore bank
-	ld a, [$ff8a]
+
+	ld a, [hROMBankBackup]
 	rst Bankswitch
-	
-; tell other fns vblank happened
+
 	xor a
 	ld [VBlankOccurred], a
 	ret
@@ -212,77 +178,60 @@
 ; oam
 ; sound / lcd stat
 
-; save bank
 	ld a, [hROMBank]
-	ld [$ff8a], a
-	
-; scroll x
+	ld [hROMBankBackup], a
+
 	ld a, [hSCX]
 	ld [rSCX], a
-	
-; scroll y
 	ld a, [hSCY]
 	ld [rSCY], a
-	
-; time-sensitive fns
+
 	call UpdatePals
-	jr c, .vblankoccurred
-	
-; these have their own timing checks
+	jr c, .done
+
 	call UpdateBGMap
 	call Serve2bppRequest@VBlank
-; update oam by dma transfer
+
 	call hPushOAM
-;	@PushOAM:
-;		ld a, Sprites >> 8
-;		ld [rDMA], a
-;		ld a, $28
-;	.loop
-;		dec a
-;		jr nz, .loop
-;		ret
-	
-.vblankoccurred
-; tell other fns vblank happened
+.done
+
 	xor a
 	ld [VBlankOccurred], a
-	
-; get requested ints
+
+	; get requested ints
 	ld a, [rIF]
 	ld b, a
-; discard requested ints
+	; discard requested ints
 	xor a
 	ld [rIF], a
-; enable lcd stat
+	; enable lcd stat
 	ld a, %10 ; lcd stat
 	ld [rIE], a
-; rerequest serial int if applicable (still disabled)
-; request lcd stat
+	; rerequest serial int if applicable (still disabled)
+	; request lcd stat
 	ld a, b
 	and %1000 ; serial
 	or %10 ; lcd stat
 	ld [rIF], a
-	
+
 	ei
-; update sound
 	ld a, BANK(_UpdateSound)
-	rst Bankswitch ; bankswitch
+	rst Bankswitch
 	call _UpdateSound
-; restore bank
-	ld a, [$ff8a]
+	ld a, [hROMBankBackup]
 	rst Bankswitch
 	di
-	
-; get requested ints
+
+	; get requested ints
 	ld a, [rIF]
 	ld b, a
-; discard requested ints
+	; discard requested ints
 	xor a
 	ld [rIF], a
-; enable ints besides joypad
+	; enable ints besides joypad
 	ld a, %1111 ; serial timer lcdstat vblank
 	ld [rIE], a
-; rerequest ints
+	; rerequest ints
 	ld a, b
 	ld [rIF], a
 	ret
@@ -292,21 +241,18 @@
 UpdatePals:: ; 37f
 ; update pals for either dmg or cgb
 
-; check cgb
 	ld a, [hCGB]
 	and a
 	jp nz, UpdateCGBPals
-	
-; update gb pals
+
+	; update gb pals
 	ld a, [$cfc7]
 	ld [rBGP], a
-	
 	ld a, [$cfc8]
 	ld [rOBP0], a
-	
 	ld a, [$cfc9]
 	ld [rOBP1], a
-	
+
 	and a
 	ret
 ; 396
@@ -320,77 +266,58 @@
 ; oam
 ; sound / lcd stat
 
-; save bank
 	ld a, [hROMBank]
-	ld [$ff8a], a
-	
-; scroll x
+	ld [hROMBankBackup], a
+
 	ld a, [hSCX]
 	ld [rSCX], a
-; scroll y
 	ld a, [hSCY]
 	ld [rSCY], a
-	
-; any pals to update?
+
 	ld a, [hCGBPalUpdate]
 	and a
 	call nz, ForceUpdateCGBPals
-	jr c, .vblankoccurred
-; else
+	jr c, .done
+
 	call UpdateBGMap
 	call Serve2bppRequest@VBlank
-	
-; update oam by dma transfer
+
 	call hPushOAM
-;	@PushOAM:
-;		ld a, Sprites >> 8
-;		ld [rDMA], a
-;		ld a, $28
-;	.loop
-;		dec a
-;		jr nz, .loop
-;		ret
-	
-.vblankoccurred
-; tell other fns vblank happened
+.done
+
 	xor a
 	ld [VBlankOccurred], a
-	
-; save int flag
+
 	ld a, [rIF]
 	push af
-; reset ints
 	xor a
 	ld [rIF], a
-; force lcdstat int during sound update
 	ld a, %10 ; lcd stat
 	ld [rIE], a
 	ld [rIF], a
-	
+
 	ei
-; update sound
 	ld a, BANK(_UpdateSound)
-	rst Bankswitch ; bankswitch
+	rst Bankswitch
 	call _UpdateSound
-; restore bank
-	ld a, [$ff8a]
+	ld a, [hROMBankBackup]
 	rst Bankswitch
 	di
-	
-; request lcdstat
+
+	; request lcdstat
 	ld a, [rIF]
 	ld b, a
-; and any other ints
+	; and any other ints
 	pop af
 	or b
 	ld b, a
-; reset ints
+	; reset ints
 	xor a
 	ld [rIF], a
-; enable ints besides joypad
+	; enable ints besides joypad
 	ld a, %1111 ; serial timer lcdstat vblank
 	ld [rIE], a
-; request ints
+	; request ints
 	ld a, b
 	ld [rIF], a
 	ret
@@ -405,40 +332,26 @@
 ; serial
 ; sound
 
-; save bank
 	ld a, [hROMBank]
-	ld [$ff8a], a
-	
+	ld [hROMBankBackup], a
+
 	call UpdateBGMap
 	call Serve2bppRequest
-	
-; update oam by dma transfer
+
 	call hPushOAM
-;	@PushOAM:
-;		ld a, Sprites >> 8
-;		ld [rDMA], a
-;		ld a, $28
-;	.loop
-;		dec a
-;		jr nz, .loop
-;		ret
-	
-; update joypad
+
 	call Joypad
-	
-; tell other fns vblank happened
+
 	xor a
 	ld [VBlankOccurred], a
-	
-; handshake
+
 	call AskSerial
-	
-; update sound
+
 	ld a, BANK(_UpdateSound)
-	rst Bankswitch ; bankswitch
+	rst Bankswitch
 	call _UpdateSound
-; restore bank
-	ld a, [$ff8a]
+
+	ld a, [hROMBankBackup]
 	rst Bankswitch
 	ret
 ; 400
@@ -452,52 +365,42 @@
 ; joypad
 ; 
 
-; save bank
 	ld a, [hROMBank]
-	ld [$ff8a], a
-	
-; scroll x
+	ld [hROMBankBackup], a
+
 	ld a, [hSCX]
 	ld [rSCX], a
-	
-; if we can update pals, skip this part
+
 	call UpdatePalsIfCGB
-	jr c, .vblankoccurred
-	
+	jr c, .done
+
 	call UpdateBGMap
 	call Serve2bppRequest
-	
-.vblankoccurred
-; tell other fns vblank happened
+.done
+
 	xor a
 	ld [VBlankOccurred], a
-	
-; joypad
+
 	call Joypad
-	
-; discard requested ints
+
 	xor a
 	ld [rIF], a
-; enable lcd stat
 	ld a, %10 ; lcd stat
 	ld [rIE], a
-; request lcd stat
+	; request lcd stat
 	ld [rIF], a
-	
+
 	ei
-; update sound
 	ld a, BANK(_UpdateSound)
-	rst Bankswitch ; bankswitch
+	rst Bankswitch
 	call _UpdateSound
-; restore bank
-	ld a, [$ff8a]
+	ld a, [hROMBankBackup]
 	rst Bankswitch
 	di
-	
-; discard requested ints
+
 	xor a
 	ld [rIF], a
-; enable ints besides joypad
+	; enable ints besides joypad
 	ld a, %1111 ; serial timer lcdstat vblank
 	ld [rIE], a
 	ret
@@ -510,32 +413,29 @@
 ; dma transfer
 ; sound
 
-; save bank
 	ld a, [hROMBank]
-	ld [$ff8a], a
-	
-; inc frame counter
+	ld [hROMBankBackup], a
+
+	; inc frame counter
 	ld hl, $ff9b
 	inc [hl]
-	
+
 	call UpdateCGBPals
-	jr c, .vblankoccurred
-	
+	jr c, .done
+
 	call Serve2bppRequest
 	call Serve1bppRequest
 	call DMATransfer
-	
-.vblankoccurred
-; tell other fns vblank happened
+.done
+
 	xor a
 	ld [VBlankOccurred], a
-	
-; update sound
+
 	ld a, BANK(_UpdateSound)
-	rst Bankswitch ; bankswitch
+	rst Bankswitch
 	call _UpdateSound
-; restore bank
-	ld a, [$ff8a]
+
+	ld a, [hROMBankBackup]
 	rst Bankswitch
 	ret
 ; 45a
--- a/hram.asm
+++ b/hram.asm
@@ -1,5 +1,6 @@
 hPushOAM           EQU $ff80
 
+hROMBankBackup     EQU $ff8a
 hBuffer            EQU $ff8b
 
 hRTCDayHi          EQU $ff8d
@@ -15,6 +16,7 @@
 hSeconds           EQU $ff98
 
 hROMBank           EQU $ff9d
+hVBlank            EQU $ff9e
 
 hJoypadReleased    EQU $ffa2
 hJoypadPressed     EQU $ffa3
--- a/main.asm
+++ b/main.asm
@@ -36515,7 +36515,7 @@
 	xor a
 	ld [hld], a
 	ld [hl], a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	push de
 	hlcoord 0, 12
 	ld b, $4
@@ -39695,7 +39695,7 @@
 	ld c, $28
 	call DelayFrames
 	xor a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	inc a
 	ld [InLinkBattle], a
 	ret
@@ -39744,7 +39744,7 @@
 	xor a
 	ld [hli], a
 	ld [hl], a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	ld [InLinkBattle], a
 	ret
 ; 29ce8
@@ -39860,12 +39860,12 @@
 	ld [hl], a
 	call WaitBGMap
 	ld a, $2
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call DelayFrame
 	call DelayFrame
 	call Function29e0c
 	xor a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	ld a, [ScriptVar]
 	and a
 	ret nz
@@ -39882,7 +39882,7 @@
 	ld [hl], a
 	call WaitBGMap
 	ld a, $2
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call DelayFrame
 	call DelayFrame
 	call Function29e0c
@@ -39916,7 +39916,7 @@
 
 .asm_29e08
 	xor a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	ret
 ; 29e0c
 
@@ -40019,7 +40019,7 @@
 	inc a
 	ld [InLinkBattle], a
 	xor a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	ld a, $1
 	ld [ScriptVar], a
 	ret
@@ -40037,7 +40037,7 @@
 	callab Function28000
 	call Function2ee4
 	xor a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	ret
 ; 29ec4
 
@@ -40048,7 +40048,7 @@
 	callab Function28000
 	call Function2ee4
 	xor a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	ret
 ; 29ed9
 
@@ -40059,7 +40059,7 @@
 	callab Function28000
 	call Function2ee4
 	xor a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	ret
 ; 29eee
 
@@ -40096,7 +40096,7 @@
 	ld [$cf56], a
 	ld [$cf57], a
 	ld a, $2
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call DelayFrame
 	call DelayFrame
 .asm_29f29
@@ -40114,7 +40114,7 @@
 
 .asm_29f40
 	xor a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	ld a, b
 	and $f
 	ret
@@ -64171,7 +64171,7 @@
 	ld a, $e4
 	call DmgToCgbBGPals
 	call DelayFrame
-	ld hl, $ff9e
+	ld hl, hVBlank
 	ld a, [hl]
 	push af
 	ld [hl], $4
@@ -64196,7 +64196,7 @@
 
 .asm_8449d
 	pop af
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call Function84411
 	xor a
 	ld [rIF], a
@@ -64237,7 +64237,7 @@
 	ld [rIF], a ; $ff00+$f
 	ld a, $9
 	ld [rIE], a ; $ff00+$ff
-	ld hl, $ff9e
+	ld hl, hVBlank
 	ld a, [hl]
 	push af
 	ld [hl], $4
@@ -64279,7 +64279,7 @@
 	call Function84559
 .asm_84545
 	pop af
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call Function84411
 	xor a
 	ld [rIF], a ; $ff00+$f
@@ -64307,7 +64307,7 @@
 	ld [rIF], a
 	ld a, $9
 	ld [rIE], a
-	ld hl, $ff9e
+	ld hl, hVBlank
 	ld a, [hl]
 	push af
 	ld [hl], $4
@@ -64343,7 +64343,7 @@
 
 .asm_845c0
 	pop af
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call Function84411
 	call Function30b4
 	xor a
@@ -64377,7 +64377,7 @@
 	ld [hBGMapMode], a
 	ld a, $13
 	call Function84419
-	ld hl, $ff9e
+	ld hl, hVBlank
 	ld a, [hl]
 	push af
 	ld [hl], $4
@@ -64385,7 +64385,7 @@
 	ld [$cf65], a
 	call Function843f0
 	pop af
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call Function84411
 	call Function84735
 	xor a
@@ -64414,7 +64414,7 @@
 	callba Function1dc381
 	ld a, $10
 	call Function84419
-	ld hl, $ff9e
+	ld hl, hVBlank
 	ld a, [hl]
 	push af
 	ld [hl], $4
@@ -64438,7 +64438,7 @@
 
 .asm_84671
 	pop af
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call Function84411
 	call Function84735
 	xor a
@@ -64464,7 +64464,7 @@
 	ld [rIF], a
 	ld a, $9
 	ld [rIE], a
-	ld hl, $ff9e
+	ld hl, hVBlank
 	ld a, [hl]
 	push af
 	ld [hl], $4
@@ -64492,7 +64492,7 @@
 
 .asm_846e2
 	pop af
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call Function84411
 	xor a
 	ld [rIF], a
@@ -73282,7 +73282,7 @@
 	ld a, [rOBP1]
 	ld [$cfc9], a
 	call DelayFrame
-	ld hl, $ff9e
+	ld hl, hVBlank
 	ld a, [hl]
 	push af
 	ld [hl], $1
@@ -73317,7 +73317,7 @@
 	ld a, $1
 	ld [rSVBK], a
 	pop af
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call DelayFrame
 	ret
 ; 8c26d
@@ -74572,7 +74572,7 @@
 	ld a, [hSCY]
 	push af
 	call Function8ccc9
-	ld hl, $ff9e
+	ld hl, hVBlank
 	ld a, [hl]
 	push af
 	ld [hl], $1
@@ -74595,7 +74595,7 @@
 
 .asm_8cc6b
 	pop af
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call WhiteBGMap
 	xor a
 	ld [hLCDStatCustom], a
@@ -96304,7 +96304,7 @@
 	ld [rSVBK], a
 	ld a, [$ffaa]
 	push af
-	ld a, [$ff9e]
+	ld a, [hVBlank]
 	push af
 	call Functione4901
 
@@ -96337,7 +96337,7 @@
 	ld a, $90
 	ld [hWY], a
 	pop af
-	ld [$ff9e], a
+	ld [hVBlank], a
 	pop af
 	ld [$ffaa], a
 	pop af
@@ -96347,7 +96347,7 @@
 
 Functione4901: ; e4901
 	xor a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	ld a, $1
 	ld [$ffaa], a
 	xor a
@@ -100510,7 +100510,7 @@
 	ld [$ffc9], a
 	ld [$ffe9], a
 	xor a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call NormalSpeed
 	xor a
 	ld [rIF], a
@@ -101857,13 +101857,13 @@
 Function1008e0: ; 1008e0
 	ld a, [hBGMapMode]
 	ld b, a
-	ld a, [$ff9e]
+	ld a, [hVBlank]
 	ld c, a
 	push bc
 	xor a
 	ld [hBGMapMode], a
 	ld a, $3
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call Function100970
 	call Function100902
 	call Function100989
@@ -101870,7 +101870,7 @@
 	call DelayFrame
 	pop bc
 	ld a, c
-	ld [$ff9e], a
+	ld [hVBlank], a
 	ld a, b
 	ld [hBGMapMode], a
 	ret
@@ -114136,7 +114136,7 @@
 	xor a
 	ld [$ffc9], a
 	ld [$ffe9], a
-	ld [$ff9e], a
+	ld [hVBlank], a
 	call NormalSpeed
 	xor a
 	ld [rIF], a