shithub: pokecrystal

Download patch

ref: 43eff93861b5b00473e567e6449c64f039f8f4ba
parent: 9a927c1b3efa2eca886f346a4fcca0eb57278faf
author: mid-kid <[email protected]>
date: Fri Feb 21 18:48:51 EST 2020

Small home/ reorganization

Time to move everything out of home.asm:
- InexplicablyEmptyFunction was moved to home/map.asm
- The wDebugFlags functions and xor_a brothers were moved to
  home/flag.asm because they're all flag-related.
- ret_2f3e was moved into home/region.asm
- The register alias sisters were moved to a new file called home/call_regs.asm
- IsInArray and SkipNames were joined by AddNTimes from home/math.asm
  into home/array.asm, as they're all used to index arrays.
- CallPointerAt was moved into home/print_text.asm because given the
  contents of that file it doesn't feel very out of place (that file
  isn't very aptly named...)
- CountSetBits was moved into home/pokedex_flags.asm because it's unique
  use is counting the amount of seen/caught mon in the podedex. GetWeekday
  was pulled into this by proximity.

Other changes were also made:
- PushLYOverrides was moved from home/sprite_anims.asm to
  home/battle.asm, because it's almost exclusively used for battle
  animations, with the lone exception being the Magnet Train.
- home/copy.asm was renamed to home/gfx.asm, as it's all gfx-related
- home/copy2.asm was renamed to home/copy.asm, now it's the only file
  called copy.
- SetHPPal and GetHPPal were moved from home/hp_pals.asm to
  home/tilemap.asm, as they're attrmap related, like many functions in
  that file are.
- home/rtc.asm was renamed to home/time_palettes.asm, as it had very
  little to do with the RTC at all, all RTC functions being in home/time.asm
- home/handshake.asm was renamed to home/printer.asm.
- home/mon_data_2.asm was renamed to home/mon_party.asm.

--- a/home.asm
+++ b/home.asm
@@ -24,7 +24,7 @@
 INCLUDE "home/init.asm"
 INCLUDE "home/vblank.asm"
 INCLUDE "home/delay.asm"
-INCLUDE "home/rtc.asm"
+INCLUDE "home/time_palettes.asm"
 INCLUDE "home/fade.asm"
 INCLUDE "home/lcd.asm"
 INCLUDE "home/time.asm"
@@ -32,7 +32,7 @@
 INCLUDE "home/joypad.asm"
 INCLUDE "home/decompress.asm"
 INCLUDE "home/palettes.asm"
-INCLUDE "home/copy.asm"
+INCLUDE "home/gfx.asm"
 INCLUDE "home/text.asm"
 INCLUDE "home/video.asm"
 INCLUDE "home/map_objects.asm"
@@ -41,169 +41,31 @@
 INCLUDE "home/menu.asm"
 INCLUDE "home/menu_window.asm"
 INCLUDE "home/menu2.asm"
-INCLUDE "home/handshake.asm"
+INCLUDE "home/printer.asm"
 INCLUDE "home/game_time.asm"
 INCLUDE "home/map.asm"
-
-InexplicablyEmptyFunction::
-; unused
-; Inexplicably empty.
-; Seen in PredefPointers.
-rept 16
-	nop
-endr
-	ret
-
 INCLUDE "home/farcall.asm"
 INCLUDE "home/predef.asm"
 INCLUDE "home/window.asm"
 INCLUDE "home/flag.asm"
-
-Unreferenced_CheckBPressedDebug::
-; Used in debug ROMs to walk through walls and avoid encounters.
-
-	ld a, [wDebugFlags]
-	bit DEBUG_FIELD_F, a
-	ret z
-
-	ldh a, [hJoyDown]
-	bit B_BUTTON_F, a
-	ret
-
-xor_a::
-	xor a
-	ret
-
-xor_a_dec_a::
-	xor a
-	dec a
-	ret
-
-Unreferenced_CheckFieldDebug::
-	push hl
-	ld hl, wDebugFlags
-	bit DEBUG_FIELD_F, [hl]
-	pop hl
-	ret
-
 INCLUDE "home/sprite_updates.asm"
 INCLUDE "home/string.asm"
 INCLUDE "home/region.asm"
-
-ret_2f3e::
-	ret
-
 INCLUDE "home/item.asm"
 INCLUDE "home/random.asm"
 INCLUDE "home/sram.asm"
-
-; Register aliases
-
-_hl_::
-	jp hl
-
-_de_::
-	push de
-	ret
-
+INCLUDE "home/call_regs.asm"
 INCLUDE "home/double_speed.asm"
 INCLUDE "home/clear_sprites.asm"
-INCLUDE "home/copy2.asm"
+INCLUDE "home/copy.asm"
 INCLUDE "home/copy_tilemap.asm"
 INCLUDE "home/copy_name.asm"
-
-IsInArray::
-; Find value a for every de bytes in array hl.
-; Return index in b and carry if found.
-
-	ld b, 0
-	ld c, a
-.loop
-	ld a, [hl]
-	cp -1
-	jr z, .NotInArray
-	cp c
-	jr z, .InArray
-	inc b
-	add hl, de
-	jr .loop
-
-.NotInArray:
-	and a
-	ret
-
-.InArray:
-	scf
-	ret
-
-SkipNames::
-; Skip a names.
-	ld bc, NAME_LENGTH
-	and a
-	ret z
-.loop
-	add hl, bc
-	dec a
-	jr nz, .loop
-	ret
-
+INCLUDE "home/array.asm"
 INCLUDE "home/math.asm"
 INCLUDE "home/print_text.asm"
-
-CallPointerAt::
-	ldh a, [hROMBank]
-	push af
-	ld a, [hli]
-	rst Bankswitch
-
-	ld a, [hli]
-	ld h, [hl]
-	ld l, a
-
-	call _hl_
-
-	pop hl
-	ld a, h
-	rst Bankswitch
-	ret
-
 INCLUDE "home/queue_script.asm"
 INCLUDE "home/compare.asm"
 INCLUDE "home/tilemap.asm"
-INCLUDE "home/hp_pals.asm"
-
-CountSetBits::
-; Count the number of set bits in b bytes starting from hl.
-; Return in a, c and [wNumSetBits].
-	ld c, 0
-.next
-	ld a, [hli]
-	ld e, a
-	ld d, 8
-
-.count
-	srl e
-	ld a, 0
-	adc c
-	ld c, a
-	dec d
-	jr nz, .count
-
-	dec b
-	jr nz, .next
-
-	ld a, c
-	ld [wNumSetBits], a
-	ret
-
-GetWeekday::
-	ld a, [wCurDay]
-.mod
-	sub 7
-	jr nc, .mod
-	add 7
-	ret
-
 INCLUDE "home/pokedex_flags.asm"
 INCLUDE "home/names.asm"
 INCLUDE "home/scrolling_menu.asm"
@@ -214,7 +76,7 @@
 INCLUDE "home/print_level.asm"
 INCLUDE "home/mon_data.asm"
 INCLUDE "home/print_bcd.asm"
-INCLUDE "home/mon_data_2.asm"
+INCLUDE "home/mon_party.asm"
 INCLUDE "home/battle.asm"
 INCLUDE "home/sprite_anims.asm"
 INCLUDE "home/audio.asm"
--- /dev/null
+++ b/home/array.asm
@@ -1,0 +1,44 @@
+IsInArray::
+; Find value a for every de bytes in array hl.
+; Return index in b and carry if found.
+
+	ld b, 0
+	ld c, a
+.loop
+	ld a, [hl]
+	cp -1
+	jr z, .NotInArray
+	cp c
+	jr z, .InArray
+	inc b
+	add hl, de
+	jr .loop
+
+.NotInArray:
+	and a
+	ret
+
+.InArray:
+	scf
+	ret
+
+SkipNames::
+; Skip a names.
+	ld bc, NAME_LENGTH
+	and a
+	ret z
+.loop
+	add hl, bc
+	dec a
+	jr nz, .loop
+	ret
+
+AddNTimes::
+; Add bc * a to hl.
+	and a
+	ret z
+.loop
+	add hl, bc
+	dec a
+	jr nz, .loop
+	ret
--- a/home/battle.asm
+++ b/home/battle.asm
@@ -224,3 +224,22 @@
 
 	ld a, [wBattleAnimByte]
 	ret
+
+PushLYOverrides::
+	ldh a, [hLCDCPointer]
+	and a
+	ret z
+
+	ld a, LOW(wLYOverridesBackup)
+	ld [wRequested2bppSource], a
+	ld a, HIGH(wLYOverridesBackup)
+	ld [wRequested2bppSource + 1], a
+
+	ld a, LOW(wLYOverrides)
+	ld [wRequested2bppDest], a
+	ld a, HIGH(wLYOverrides)
+	ld [wRequested2bppDest + 1], a
+
+	ld a, (wLYOverridesEnd - wLYOverrides) / 16
+	ld [wRequested2bpp], a
+	ret
--- /dev/null
+++ b/home/call_regs.asm
@@ -1,0 +1,8 @@
+; Register aliases
+
+_hl_::
+	jp hl
+
+_de_::
+	push de
+	ret
--- a/home/copy.asm
+++ b/home/copy.asm
@@ -1,140 +1,59 @@
-; Functions to copy data from ROM.
-
-Get2bpp_2::
-	ldh a, [rLCDC]
-	bit rLCDC_ENABLE, a
-	jp z, Copy2bpp
-
-	homecall _Get2bpp
-
+CopyBytes::
+; copy bc bytes from hl to de
+	inc b ; we bail the moment b hits 0, so include the last run
+	inc c ; same thing; include last byte
+	jr .HandleLoop
+.CopyByte:
+	ld a, [hli]
+	ld [de], a
+	inc de
+.HandleLoop:
+	dec c
+	jr nz, .CopyByte
+	dec b
+	jr nz, .CopyByte
 	ret
 
-Get1bpp_2::
-	ldh a, [rLCDC]
-	bit rLCDC_ENABLE, a
-	jp z, Copy1bpp
-
-	homecall _Get1bpp
-
-	ret
-
-FarCopyBytesDouble_DoubleBankSwitch::
-	ldh [hBuffer], a
-	ldh a, [hROMBank]
+SwapBytes::
+; swap bc bytes between hl and de
+.Loop:
+	; stash [hl] away on the stack
+	ld a, [hl]
 	push af
-	ldh a, [hBuffer]
-	rst Bankswitch
 
-	call FarCopyBytesDouble
+	; copy a byte from [de] to [hl]
+	ld a, [de]
+	ld [hli], a
 
+	; retrieve the previous value of [hl]; put it in [de]
 	pop af
-	rst Bankswitch
-	ret
+	ld [de], a
+	inc de
 
-OldDMATransfer::
-	dec c
-	ldh a, [hBGMapMode]
-	push af
-	xor a
-	ldh [hBGMapMode], a
-	ldh a, [hROMBank]
-	push af
+	; handle loop stuff
+	dec bc
 	ld a, b
-	rst Bankswitch
-
-.loop
-; load the source and target MSB and LSB
-	ld a, d
-	ldh [rHDMA1], a ; source MSB
-	ld a, e
-	and $f0
-	ldh [rHDMA2], a ; source LSB
-	ld a, h
-	and $1f
-	ldh [rHDMA3], a ; target MSB
-	ld a, l
-	and $f0
-	ldh [rHDMA4], a ; target LSB
-; stop when c < 8
-	ld a, c
-	cp $8
-	jr c, .done
-; decrease c by 8
-	sub $8
-	ld c, a
-; DMA transfer state
-	ld a, $f
-	ldh [hDMATransfer], a
-	call DelayFrame
-; add $100 to hl and de
-	ld a, l
-	add LOW($100)
-	ld l, a
-	ld a, h
-	adc HIGH($100)
-	ld h, a
-	ld a, e
-	add LOW($100)
-	ld e, a
-	ld a, d
-	adc HIGH($100)
-	ld d, a
-	jr .loop
-
-.done
-	ld a, c
-	and $7f ; pretty silly, considering at most bits 0-2 would be set
-	ldh [hDMATransfer], a
-	call DelayFrame
-	pop af
-	rst Bankswitch
-
-	pop af
-	ldh [hBGMapMode], a
+	or c
+	jr nz, .Loop
 	ret
 
-ReplaceKrisSprite::
-	farcall _ReplaceKrisSprite
+ByteFill::
+; fill bc bytes with the value of a, starting at hl
+	inc b ; we bail the moment b hits 0, so include the last run
+	inc c ; same thing; include last byte
+	jr .HandleLoop
+.PutByte:
+	ld [hli], a
+.HandleLoop:
+	dec c
+	jr nz, .PutByte
+	dec b
+	jr nz, .PutByte
 	ret
 
-LoadStandardFont::
-	farcall _LoadStandardFont
-	ret
-
-LoadFontsBattleExtra::
-	farcall _LoadFontsBattleExtra
-	ret
-
-LoadFontsExtra::
-	farcall _LoadFontsExtra1
-	farcall _LoadFontsExtra2
-	ret
-
-LoadFontsExtra2::
-	farcall _LoadFontsExtra2
-	ret
-
-DecompressRequest2bpp::
-	push de
-	ld a, BANK(sScratch)
-	call GetSRAMBank
-	push bc
-
-	ld de, sScratch
-	ld a, b
-	call FarDecompress
-
-	pop bc
-	pop hl
-
-	ld de, sScratch
-	call Request2bpp
-	call CloseSRAM
-	ret
-
-FarCopyBytes::
-; copy bc bytes from a:hl to de
-
+GetFarByte::
+; retrieve a single byte from a:hl, and return it in a.
+	; bankswitch to new bank
 	ldh [hBuffer], a
 	ldh a, [hROMBank]
 	push af
@@ -141,16 +60,21 @@
 	ldh a, [hBuffer]
 	rst Bankswitch
 
-	call CopyBytes
+	; get byte from new bank
+	ld a, [hl]
+	ldh [hBuffer], a
 
+	; bankswitch to previous bank
 	pop af
 	rst Bankswitch
+
+	; return retrieved value in a
+	ldh a, [hBuffer]
 	ret
 
-FarCopyBytesDouble::
-; Copy bc bytes from a:hl to bc*2 bytes at de,
-; doubling each byte in the process.
-
+GetFarHalfword::
+; retrieve a halfword from a:hl, and return it in hl.
+	; bankswitch to new bank
 	ldh [hBuffer], a
 	ldh a, [hROMBank]
 	push af
@@ -157,235 +81,51 @@
 	ldh a, [hBuffer]
 	rst Bankswitch
 
-; switcheroo, de <> hl
-	ld a, h
-	ld h, d
-	ld d, a
-	ld a, l
-	ld l, e
-	ld e, a
+	; get halfword from new bank, put it in hl
+	ld a, [hli]
+	ld h, [hl]
+	ld l, a
 
-	inc b
-	inc c
-	jr .dec
-
-.loop
-	ld a, [de]
-	inc de
-	ld [hli], a
-	ld [hli], a
-.dec
-	dec c
-	jr nz, .loop
-	dec b
-	jr nz, .loop
-
+	; bankswitch to previous bank and return
 	pop af
 	rst Bankswitch
 	ret
 
-Request2bpp::
-; Load 2bpp at b:de to occupy c tiles of hl.
-	ldh a, [hBGMapMode]
+FarCopyWRAM::
+	ldh [hBuffer], a
+	ldh a, [rSVBK]
 	push af
-	xor a
-	ldh [hBGMapMode], a
+	ldh a, [hBuffer]
+	ldh [rSVBK], a
 
-	ldh a, [hROMBank]
-	push af
-	ld a, b
-	rst Bankswitch
+	call CopyBytes
 
-	ldh a, [hTilesPerCycle]
-	push af
-	ld a, $8
-	ldh [hTilesPerCycle], a
-
-	ld a, [wLinkMode]
-	cp LINK_MOBILE
-	jr nz, .NotMobile
-	ldh a, [hMobile]
-	and a
-	jr nz, .NotMobile
-	ld a, $6
-	ldh [hTilesPerCycle], a
-
-.NotMobile:
-	ld a, e
-	ld [wRequested2bppSource], a
-	ld a, d
-	ld [wRequested2bppSource + 1], a
-	ld a, l
-	ld [wRequested2bppDest], a
-	ld a, h
-	ld [wRequested2bppDest + 1], a
-.loop
-	ld a, c
-	ld hl, hTilesPerCycle
-	cp [hl]
-	jr nc, .iterate
-
-	ld [wRequested2bpp], a
-.wait
-	call DelayFrame
-	ld a, [wRequested2bpp]
-	and a
-	jr nz, .wait
-
 	pop af
-	ldh [hTilesPerCycle], a
-
-	pop af
-	rst Bankswitch
-
-	pop af
-	ldh [hBGMapMode], a
+	ldh [rSVBK], a
 	ret
 
-.iterate
-	ldh a, [hTilesPerCycle]
-	ld [wRequested2bpp], a
-
-.wait2
-	call DelayFrame
-	ld a, [wRequested2bpp]
-	and a
-	jr nz, .wait2
-
-	ld a, c
-	ld hl, hTilesPerCycle
-	sub [hl]
-	ld c, a
-	jr .loop
-
-Request1bpp::
-; Load 1bpp at b:de to occupy c tiles of hl.
-	ldh a, [hBGMapMode]
+GetFarWRAMByte::
+	ldh [hBuffer], a
+	ldh a, [rSVBK]
 	push af
-	xor a
-	ldh [hBGMapMode], a
-
-	ldh a, [hROMBank]
-	push af
-	ld a, b
-	rst Bankswitch
-
-	ldh a, [hTilesPerCycle]
-	push af
-	ld a, $8
-	ldh [hTilesPerCycle], a
-
-	ld a, [wLinkMode]
-	cp LINK_MOBILE
-	jr nz, .NotMobile
-	ldh a, [hMobile]
-	and a
-	jr nz, .NotMobile
-	ld a, $6
-	ldh [hTilesPerCycle], a
-
-.NotMobile:
-	ld a, e
-	ld [wRequested1bppSource], a
-	ld a, d
-	ld [wRequested1bppSource + 1], a
-	ld a, l
-	ld [wRequested1bppDest], a
-	ld a, h
-	ld [wRequested1bppDest + 1], a
-.loop
-	ld a, c
-	ld hl, hTilesPerCycle
-	cp [hl]
-	jr nc, .iterate
-
-	ld [wRequested1bpp], a
-.wait
-	call DelayFrame
-	ld a, [wRequested1bpp]
-	and a
-	jr nz, .wait
-
+	ldh a, [hBuffer]
+	ldh [rSVBK], a
+	ld a, [hl]
+	ldh [hBuffer], a
 	pop af
-	ldh [hTilesPerCycle], a
-
-	pop af
-	rst Bankswitch
-
-	pop af
-	ldh [hBGMapMode], a
+	ldh [rSVBK], a
+	ldh a, [hBuffer]
 	ret
 
-.iterate
-	ldh a, [hTilesPerCycle]
-	ld [wRequested1bpp], a
-
-.wait2
-	call DelayFrame
-	ld a, [wRequested1bpp]
-	and a
-	jr nz, .wait2
-
-	ld a, c
-	ld hl, hTilesPerCycle
-	sub [hl]
-	ld c, a
-	jr .loop
-
-Get2bpp::
-	ldh a, [rLCDC]
-	bit rLCDC_ENABLE, a
-	jp nz, Request2bpp
-
-Copy2bpp::
-; copy c 2bpp tiles from b:de to hl
-
-	push hl
-	ld h, d
-	ld l, e
-	pop de
-
-; bank
-	ld a, b
-
-; bc = c * $10
+GetFarWRAMWord::
+	ldh [hBuffer], a
+	ldh a, [rSVBK]
 	push af
-	swap c
-	ld a, $f
-	and c
-	ld b, a
-	ld a, $f0
-	and c
-	ld c, a
+	ldh a, [hBuffer]
+	ldh [rSVBK], a
+	ld a, [hli]
+	ld h, [hl]
+	ld l, a
 	pop af
-
-	jp FarCopyBytes
-
-Get1bpp::
-	ldh a, [rLCDC]
-	bit rLCDC_ENABLE, a
-	jp nz, Request1bpp
-
-Copy1bpp::
-; copy c 1bpp tiles from b:de to hl
-
-	push de
-	ld d, h
-	ld e, l
-
-; bank
-	ld a, b
-
-; bc = c * $10 / 2
-	push af
-	ld h, 0
-	ld l, c
-	add hl, hl
-	add hl, hl
-	add hl, hl
-	ld b, h
-	ld c, l
-	pop af
-
-	pop hl
-	jp FarCopyBytesDouble
+	ldh [rSVBK], a
+	ret
--- a/home/copy2.asm
+++ /dev/null
@@ -1,131 +1,0 @@
-CopyBytes::
-; copy bc bytes from hl to de
-	inc b ; we bail the moment b hits 0, so include the last run
-	inc c ; same thing; include last byte
-	jr .HandleLoop
-.CopyByte:
-	ld a, [hli]
-	ld [de], a
-	inc de
-.HandleLoop:
-	dec c
-	jr nz, .CopyByte
-	dec b
-	jr nz, .CopyByte
-	ret
-
-SwapBytes::
-; swap bc bytes between hl and de
-.Loop:
-	; stash [hl] away on the stack
-	ld a, [hl]
-	push af
-
-	; copy a byte from [de] to [hl]
-	ld a, [de]
-	ld [hli], a
-
-	; retrieve the previous value of [hl]; put it in [de]
-	pop af
-	ld [de], a
-	inc de
-
-	; handle loop stuff
-	dec bc
-	ld a, b
-	or c
-	jr nz, .Loop
-	ret
-
-ByteFill::
-; fill bc bytes with the value of a, starting at hl
-	inc b ; we bail the moment b hits 0, so include the last run
-	inc c ; same thing; include last byte
-	jr .HandleLoop
-.PutByte:
-	ld [hli], a
-.HandleLoop:
-	dec c
-	jr nz, .PutByte
-	dec b
-	jr nz, .PutByte
-	ret
-
-GetFarByte::
-; retrieve a single byte from a:hl, and return it in a.
-	; bankswitch to new bank
-	ldh [hBuffer], a
-	ldh a, [hROMBank]
-	push af
-	ldh a, [hBuffer]
-	rst Bankswitch
-
-	; get byte from new bank
-	ld a, [hl]
-	ldh [hBuffer], a
-
-	; bankswitch to previous bank
-	pop af
-	rst Bankswitch
-
-	; return retrieved value in a
-	ldh a, [hBuffer]
-	ret
-
-GetFarHalfword::
-; retrieve a halfword from a:hl, and return it in hl.
-	; bankswitch to new bank
-	ldh [hBuffer], a
-	ldh a, [hROMBank]
-	push af
-	ldh a, [hBuffer]
-	rst Bankswitch
-
-	; get halfword from new bank, put it in hl
-	ld a, [hli]
-	ld h, [hl]
-	ld l, a
-
-	; bankswitch to previous bank and return
-	pop af
-	rst Bankswitch
-	ret
-
-FarCopyWRAM::
-	ldh [hBuffer], a
-	ldh a, [rSVBK]
-	push af
-	ldh a, [hBuffer]
-	ldh [rSVBK], a
-
-	call CopyBytes
-
-	pop af
-	ldh [rSVBK], a
-	ret
-
-GetFarWRAMByte::
-	ldh [hBuffer], a
-	ldh a, [rSVBK]
-	push af
-	ldh a, [hBuffer]
-	ldh [rSVBK], a
-	ld a, [hl]
-	ldh [hBuffer], a
-	pop af
-	ldh [rSVBK], a
-	ldh a, [hBuffer]
-	ret
-
-GetFarWRAMWord::
-	ldh [hBuffer], a
-	ldh a, [rSVBK]
-	push af
-	ldh a, [hBuffer]
-	ldh [rSVBK], a
-	ld a, [hli]
-	ld h, [hl]
-	ld l, a
-	pop af
-	ldh [rSVBK], a
-	ret
--- a/home/flag.asm
+++ b/home/flag.asm
@@ -101,3 +101,30 @@
 	ld a, c
 	and a
 	ret
+
+Unreferenced_CheckBPressedDebug::
+; Used in debug ROMs to walk through walls and avoid encounters.
+
+	ld a, [wDebugFlags]
+	bit DEBUG_FIELD_F, a
+	ret z
+
+	ldh a, [hJoyDown]
+	bit B_BUTTON_F, a
+	ret
+
+xor_a::
+	xor a
+	ret
+
+xor_a_dec_a::
+	xor a
+	dec a
+	ret
+
+Unreferenced_CheckFieldDebug::
+	push hl
+	ld hl, wDebugFlags
+	bit DEBUG_FIELD_F, [hl]
+	pop hl
+	ret
--- /dev/null
+++ b/home/gfx.asm
@@ -1,0 +1,391 @@
+; Functions to copy data from ROM.
+
+Get2bpp_2::
+	ldh a, [rLCDC]
+	bit rLCDC_ENABLE, a
+	jp z, Copy2bpp
+
+	homecall _Get2bpp
+
+	ret
+
+Get1bpp_2::
+	ldh a, [rLCDC]
+	bit rLCDC_ENABLE, a
+	jp z, Copy1bpp
+
+	homecall _Get1bpp
+
+	ret
+
+FarCopyBytesDouble_DoubleBankSwitch::
+	ldh [hBuffer], a
+	ldh a, [hROMBank]
+	push af
+	ldh a, [hBuffer]
+	rst Bankswitch
+
+	call FarCopyBytesDouble
+
+	pop af
+	rst Bankswitch
+	ret
+
+OldDMATransfer:
+	dec c
+	ldh a, [hBGMapMode]
+	push af
+	xor a
+	ldh [hBGMapMode], a
+	ldh a, [hROMBank]
+	push af
+	ld a, b
+	rst Bankswitch
+
+.loop
+; load the source and target MSB and LSB
+	ld a, d
+	ldh [rHDMA1], a ; source MSB
+	ld a, e
+	and $f0
+	ldh [rHDMA2], a ; source LSB
+	ld a, h
+	and $1f
+	ldh [rHDMA3], a ; target MSB
+	ld a, l
+	and $f0
+	ldh [rHDMA4], a ; target LSB
+; stop when c < 8
+	ld a, c
+	cp $8
+	jr c, .done
+; decrease c by 8
+	sub $8
+	ld c, a
+; DMA transfer state
+	ld a, $f
+	ldh [hDMATransfer], a
+	call DelayFrame
+; add $100 to hl and de
+	ld a, l
+	add LOW($100)
+	ld l, a
+	ld a, h
+	adc HIGH($100)
+	ld h, a
+	ld a, e
+	add LOW($100)
+	ld e, a
+	ld a, d
+	adc HIGH($100)
+	ld d, a
+	jr .loop
+
+.done
+	ld a, c
+	and $7f ; pretty silly, considering at most bits 0-2 would be set
+	ldh [hDMATransfer], a
+	call DelayFrame
+	pop af
+	rst Bankswitch
+
+	pop af
+	ldh [hBGMapMode], a
+	ret
+
+ReplaceKrisSprite::
+	farcall _ReplaceKrisSprite
+	ret
+
+LoadStandardFont::
+	farcall _LoadStandardFont
+	ret
+
+LoadFontsBattleExtra::
+	farcall _LoadFontsBattleExtra
+	ret
+
+LoadFontsExtra::
+	farcall _LoadFontsExtra1
+	farcall _LoadFontsExtra2
+	ret
+
+LoadFontsExtra2:
+	farcall _LoadFontsExtra2
+	ret
+
+DecompressRequest2bpp::
+	push de
+	ld a, BANK(sScratch)
+	call GetSRAMBank
+	push bc
+
+	ld de, sScratch
+	ld a, b
+	call FarDecompress
+
+	pop bc
+	pop hl
+
+	ld de, sScratch
+	call Request2bpp
+	call CloseSRAM
+	ret
+
+FarCopyBytes::
+; copy bc bytes from a:hl to de
+
+	ldh [hBuffer], a
+	ldh a, [hROMBank]
+	push af
+	ldh a, [hBuffer]
+	rst Bankswitch
+
+	call CopyBytes
+
+	pop af
+	rst Bankswitch
+	ret
+
+FarCopyBytesDouble:
+; Copy bc bytes from a:hl to bc*2 bytes at de,
+; doubling each byte in the process.
+
+	ldh [hBuffer], a
+	ldh a, [hROMBank]
+	push af
+	ldh a, [hBuffer]
+	rst Bankswitch
+
+; switcheroo, de <> hl
+	ld a, h
+	ld h, d
+	ld d, a
+	ld a, l
+	ld l, e
+	ld e, a
+
+	inc b
+	inc c
+	jr .dec
+
+.loop
+	ld a, [de]
+	inc de
+	ld [hli], a
+	ld [hli], a
+.dec
+	dec c
+	jr nz, .loop
+	dec b
+	jr nz, .loop
+
+	pop af
+	rst Bankswitch
+	ret
+
+Request2bpp::
+; Load 2bpp at b:de to occupy c tiles of hl.
+	ldh a, [hBGMapMode]
+	push af
+	xor a
+	ldh [hBGMapMode], a
+
+	ldh a, [hROMBank]
+	push af
+	ld a, b
+	rst Bankswitch
+
+	ldh a, [hTilesPerCycle]
+	push af
+	ld a, $8
+	ldh [hTilesPerCycle], a
+
+	ld a, [wLinkMode]
+	cp LINK_MOBILE
+	jr nz, .NotMobile
+	ldh a, [hMobile]
+	and a
+	jr nz, .NotMobile
+	ld a, $6
+	ldh [hTilesPerCycle], a
+
+.NotMobile:
+	ld a, e
+	ld [wRequested2bppSource], a
+	ld a, d
+	ld [wRequested2bppSource + 1], a
+	ld a, l
+	ld [wRequested2bppDest], a
+	ld a, h
+	ld [wRequested2bppDest + 1], a
+.loop
+	ld a, c
+	ld hl, hTilesPerCycle
+	cp [hl]
+	jr nc, .iterate
+
+	ld [wRequested2bpp], a
+.wait
+	call DelayFrame
+	ld a, [wRequested2bpp]
+	and a
+	jr nz, .wait
+
+	pop af
+	ldh [hTilesPerCycle], a
+
+	pop af
+	rst Bankswitch
+
+	pop af
+	ldh [hBGMapMode], a
+	ret
+
+.iterate
+	ldh a, [hTilesPerCycle]
+	ld [wRequested2bpp], a
+
+.wait2
+	call DelayFrame
+	ld a, [wRequested2bpp]
+	and a
+	jr nz, .wait2
+
+	ld a, c
+	ld hl, hTilesPerCycle
+	sub [hl]
+	ld c, a
+	jr .loop
+
+Request1bpp::
+; Load 1bpp at b:de to occupy c tiles of hl.
+	ldh a, [hBGMapMode]
+	push af
+	xor a
+	ldh [hBGMapMode], a
+
+	ldh a, [hROMBank]
+	push af
+	ld a, b
+	rst Bankswitch
+
+	ldh a, [hTilesPerCycle]
+	push af
+	ld a, $8
+	ldh [hTilesPerCycle], a
+
+	ld a, [wLinkMode]
+	cp LINK_MOBILE
+	jr nz, .NotMobile
+	ldh a, [hMobile]
+	and a
+	jr nz, .NotMobile
+	ld a, $6
+	ldh [hTilesPerCycle], a
+
+.NotMobile:
+	ld a, e
+	ld [wRequested1bppSource], a
+	ld a, d
+	ld [wRequested1bppSource + 1], a
+	ld a, l
+	ld [wRequested1bppDest], a
+	ld a, h
+	ld [wRequested1bppDest + 1], a
+.loop
+	ld a, c
+	ld hl, hTilesPerCycle
+	cp [hl]
+	jr nc, .iterate
+
+	ld [wRequested1bpp], a
+.wait
+	call DelayFrame
+	ld a, [wRequested1bpp]
+	and a
+	jr nz, .wait
+
+	pop af
+	ldh [hTilesPerCycle], a
+
+	pop af
+	rst Bankswitch
+
+	pop af
+	ldh [hBGMapMode], a
+	ret
+
+.iterate
+	ldh a, [hTilesPerCycle]
+	ld [wRequested1bpp], a
+
+.wait2
+	call DelayFrame
+	ld a, [wRequested1bpp]
+	and a
+	jr nz, .wait2
+
+	ld a, c
+	ld hl, hTilesPerCycle
+	sub [hl]
+	ld c, a
+	jr .loop
+
+Get2bpp::
+	ldh a, [rLCDC]
+	bit rLCDC_ENABLE, a
+	jp nz, Request2bpp
+
+Copy2bpp:
+; copy c 2bpp tiles from b:de to hl
+
+	push hl
+	ld h, d
+	ld l, e
+	pop de
+
+; bank
+	ld a, b
+
+; bc = c * $10
+	push af
+	swap c
+	ld a, $f
+	and c
+	ld b, a
+	ld a, $f0
+	and c
+	ld c, a
+	pop af
+
+	jp FarCopyBytes
+
+Get1bpp::
+	ldh a, [rLCDC]
+	bit rLCDC_ENABLE, a
+	jp nz, Request1bpp
+
+Copy1bpp::
+; copy c 1bpp tiles from b:de to hl
+
+	push de
+	ld d, h
+	ld e, l
+
+; bank
+	ld a, b
+
+; bc = c * $10 / 2
+	push af
+	ld h, 0
+	ld l, c
+	add hl, hl
+	add hl, hl
+	add hl, hl
+	ld b, h
+	ld c, l
+	pop af
+
+	pop hl
+	jp FarCopyBytesDouble
--- a/home/handshake.asm
+++ /dev/null
@@ -1,41 +1,0 @@
-PrinterReceive::
-	homecall _PrinterReceive
-	ret
-
-AskSerial::
-; send out a handshake while serial int is off
-	ld a, [wPrinterConnectionOpen]
-	bit 0, a
-	ret z
-
-; if we're still interpreting data, don't try to receive
-	ld a, [wPrinterOpcode]
-	and a
-	ret nz
-
-; once every 6 frames
-	ld hl, wHandshakeFrameDelay
-	inc [hl]
-	ld a, [hl]
-	cp 6
-	ret c
-
-	xor a
-	ld [hl], a
-
-	ld a, 12
-	ld [wPrinterOpcode], a
-
-; handshake
-	ld a, $88
-	ldh [rSB], a
-
-; switch to internal clock
-	ld a, (0 << rSC_ON) | (1 << rSC_CLOCK)
-	ldh [rSC], a
-
-; start transfer
-	ld a, (1 << rSC_ON) | (1 << rSC_CLOCK)
-	ldh [rSC], a
-
-	ret
--- a/home/hp_pals.asm
+++ /dev/null
@@ -1,17 +1,0 @@
-SetHPPal::
-; Set palette for hp bar pixel length e at hl.
-	call GetHPPal
-	ld [hl], d
-	ret
-
-GetHPPal::
-; Get palette for hp bar pixel length e in d.
-	ld d, HP_GREEN
-	ld a, e
-	cp (HP_BAR_LENGTH_PX * 50 / 100) ; 24
-	ret nc
-	inc d ; HP_YELLOW
-	cp (HP_BAR_LENGTH_PX * 21 / 100) ; 10
-	ret nc
-	inc d ; HP_RED
-	ret
--- a/home/map.asm
+++ b/home/map.asm
@@ -2277,3 +2277,12 @@
 	pop bc
 	pop hl
 	ret
+
+InexplicablyEmptyFunction::
+; unused
+; Inexplicably empty.
+; Seen in PredefPointers.
+rept 16
+	nop
+endr
+	ret
--- a/home/math.asm
+++ b/home/math.asm
@@ -1,13 +1,3 @@
-AddNTimes::
-; Add bc * a to hl.
-	and a
-	ret z
-.loop
-	add hl, bc
-	dec a
-	jr nz, .loop
-	ret
-
 SimpleMultiply::
 ; Return a * c.
 	and a
--- a/home/mon_data_2.asm
+++ /dev/null
@@ -1,34 +1,0 @@
-GetPartyParamLocation::
-; Get the location of parameter a from wCurPartyMon in hl
-	push bc
-	ld hl, wPartyMons
-	ld c, a
-	ld b, 0
-	add hl, bc
-	ld a, [wCurPartyMon]
-	call GetPartyLocation
-	pop bc
-	ret
-
-GetPartyLocation::
-; Add the length of a PartyMon struct to hl a times.
-	ld bc, PARTYMON_STRUCT_LENGTH
-	jp AddNTimes
-
-Unreferenced_GetDexNumber::
-; Probably used in gen 1 to convert index number to dex number
-; Not required in gen 2 because index number == dex number
-	push hl
-	ld a, b
-	dec a
-	ld b, 0
-	add hl, bc
-	ld hl, BaseData + BASE_DEX_NO
-	ld bc, BASE_DATA_SIZE
-	call AddNTimes
-	ld a, BANK(BaseData)
-	call GetFarHalfword
-	ld b, l
-	ld c, h
-	pop hl
-	ret
--- /dev/null
+++ b/home/mon_party.asm
@@ -1,0 +1,34 @@
+GetPartyParamLocation::
+; Get the location of parameter a from wCurPartyMon in hl
+	push bc
+	ld hl, wPartyMons
+	ld c, a
+	ld b, 0
+	add hl, bc
+	ld a, [wCurPartyMon]
+	call GetPartyLocation
+	pop bc
+	ret
+
+GetPartyLocation::
+; Add the length of a PartyMon struct to hl a times.
+	ld bc, PARTYMON_STRUCT_LENGTH
+	jp AddNTimes
+
+Unreferenced_GetDexNumber::
+; Probably used in gen 1 to convert index number to dex number
+; Not required in gen 2 because index number == dex number
+	push hl
+	ld a, b
+	dec a
+	ld b, 0
+	add hl, bc
+	ld hl, BaseData + BASE_DEX_NO
+	ld bc, BASE_DATA_SIZE
+	call AddNTimes
+	ld a, BANK(BaseData)
+	call GetFarHalfword
+	ld b, l
+	ld c, h
+	pop hl
+	ret
--- a/home/pokedex_flags.asm
+++ b/home/pokedex_flags.asm
@@ -1,3 +1,35 @@
+CountSetBits::
+; Count the number of set bits in b bytes starting from hl.
+; Return in a, c and [wNumSetBits].
+	ld c, 0
+.next
+	ld a, [hli]
+	ld e, a
+	ld d, 8
+
+.count
+	srl e
+	ld a, 0
+	adc c
+	ld c, a
+	dec d
+	jr nz, .count
+
+	dec b
+	jr nz, .next
+
+	ld a, c
+	ld [wNumSetBits], a
+	ret
+
+GetWeekday::
+	ld a, [wCurDay]
+.mod
+	sub 7
+	jr nc, .mod
+	add 7
+	ret
+
 SetSeenAndCaughtMon::
 	push af
 	ld c, a
--- a/home/print_text.asm
+++ b/home/print_text.asm
@@ -118,3 +118,20 @@
 	pop af
 	rst Bankswitch
 	ret
+
+CallPointerAt::
+	ldh a, [hROMBank]
+	push af
+	ld a, [hli]
+	rst Bankswitch
+
+	ld a, [hli]
+	ld h, [hl]
+	ld l, a
+
+	call _hl_
+
+	pop hl
+	ld a, h
+	rst Bankswitch
+	ret
--- /dev/null
+++ b/home/printer.asm
@@ -1,0 +1,41 @@
+PrinterReceive::
+	homecall _PrinterReceive
+	ret
+
+AskSerial::
+; send out a handshake while serial int is off
+	ld a, [wPrinterConnectionOpen]
+	bit 0, a
+	ret z
+
+; if we're still interpreting data, don't try to receive
+	ld a, [wPrinterOpcode]
+	and a
+	ret nz
+
+; once every 6 frames
+	ld hl, wHandshakeFrameDelay
+	inc [hl]
+	ld a, [hl]
+	cp 6
+	ret c
+
+	xor a
+	ld [hl], a
+
+	ld a, 12
+	ld [wPrinterOpcode], a
+
+; handshake
+	ld a, $88
+	ldh [rSB], a
+
+; switch to internal clock
+	ld a, (0 << rSC_ON) | (1 << rSC_CLOCK)
+	ldh [rSC], a
+
+; start transfer
+	ld a, (1 << rSC_ON) | (1 << rSC_CLOCK)
+	ldh [rSC], a
+
+	ret
--- a/home/region.asm
+++ b/home/region.asm
@@ -30,3 +30,6 @@
 .Kanto:
 	ld a, 1
 	ret
+
+ret_2f3e::
+	ret
--- a/home/rtc.asm
+++ /dev/null
@@ -1,22 +1,0 @@
-UpdateTimeAndPals::
-; update time and time-sensitive palettes
-
-; rtc enabled?
-	ld a, [wSpriteUpdatesEnabled]
-	cp 0
-	ret z
-
-	call UpdateTime
-
-; obj update on?
-	ld a, [wVramState]
-	bit 0, a ; obj update
-	ret z
-
-TimeOfDayPals::
-	callfar _TimeOfDayPals
-	ret
-
-UpdateTimePals::
-	callfar _UpdateTimePals
-	ret
--- a/home/sprite_anims.asm
+++ b/home/sprite_anims.asm
@@ -1,22 +1,3 @@
-PushLYOverrides::
-	ldh a, [hLCDCPointer]
-	and a
-	ret z
-
-	ld a, LOW(wLYOverridesBackup)
-	ld [wRequested2bppSource], a
-	ld a, HIGH(wLYOverridesBackup)
-	ld [wRequested2bppSource + 1], a
-
-	ld a, LOW(wLYOverrides)
-	ld [wRequested2bppDest], a
-	ld a, HIGH(wLYOverrides)
-	ld [wRequested2bppDest + 1], a
-
-	ld a, (wLYOverridesEnd - wLYOverrides) / 16
-	ld [wRequested2bpp], a
-	ret
-
 InitSpriteAnimStruct::
 	ld [wSpriteAnimIDBuffer], a
 	ldh a, [hROMBank]
--- a/home/tilemap.asm
+++ b/home/tilemap.asm
@@ -216,3 +216,21 @@
 
 .sgb
 	predef_jump LoadSGBLayout
+
+SetHPPal::
+; Set palette for hp bar pixel length e at hl.
+	call GetHPPal
+	ld [hl], d
+	ret
+
+GetHPPal::
+; Get palette for hp bar pixel length e in d.
+	ld d, HP_GREEN
+	ld a, e
+	cp (HP_BAR_LENGTH_PX * 50 / 100) ; 24
+	ret nc
+	inc d ; HP_YELLOW
+	cp (HP_BAR_LENGTH_PX * 21 / 100) ; 10
+	ret nc
+	inc d ; HP_RED
+	ret
--- /dev/null
+++ b/home/time_palettes.asm
@@ -1,0 +1,22 @@
+UpdateTimeAndPals::
+; update time and time-sensitive palettes
+
+; rtc enabled?
+	ld a, [wSpriteUpdatesEnabled]
+	cp 0
+	ret z
+
+	call UpdateTime
+
+; obj update on?
+	ld a, [wVramState]
+	bit 0, a ; obj update
+	ret z
+
+TimeOfDayPals::
+	callfar _TimeOfDayPals
+	ret
+
+UpdateTimePals::
+	callfar _UpdateTimePals
+	ret