ref: 3a2dbb5289e745dfb5f26844148c0981f14e8fcf
parent: 37e1aa4e8ec2400ba97db4c767bd3a9fcd43a1ac
parent: abb7f2fa20e49cf56a55c81d4ec86431e3009098
author: Bryan Bishop <[email protected]>
date: Mon Sep 9 11:59:08 EDT 2013
Merge branch 'yenatch/split-predefs-specials-stds' into fix-split-predefs-specials-stds https://github.com/kanzure/pokecrystal/pull/198
--- /dev/null
+++ b/common/double_speed.asm
@@ -1,0 +1,31 @@
+; The CGB hardware introduces Double Speed Mode.
+; While active, the clock speed is doubled.
+
+; The hardware can switch between normal speed
+; and double speed at any time, but LCD output
+; collapses during the switch.
+
+DoubleSpeed: ; 2fef
+ ld hl, rKEY1
+ bit 7, [hl]
+ jr z, SwitchSpeed
+ ret
+; 2ff7
+
+NormalSpeed: ; 2ff7
+ ld hl, rKEY1
+ bit 7, [hl]
+ ret z
+; 2ffd
+
+SwitchSpeed: ; 2ffd
+ set 0, [hl]
+ xor a
+ ld [rIF], a
+ ld [rIE], a
+ ld a, $30
+ ld [rJOYP], a
+ stop ; rgbasm adds a nop after this instruction by default
+ ret
+; 300b
+
--- /dev/null
+++ b/common/handshake.asm
@@ -1,0 +1,38 @@
+AskSerial: ; 2063
+; send out a handshake while serial int is off
+ ld a, [$c2d4]
+ bit 0, a
+ ret z
+
+ ld a, [$c2d5]
+ and a
+ ret nz
+
+; once every 6 frames
+ ld hl, $ca8a
+ inc [hl]
+ ld a, [hl]
+ cp 6
+ ret c
+
+ xor a
+ ld [hl], a
+
+ ld a, $c
+ ld [$c2d5], a
+
+; handshake
+ ld a, $88
+ ld [rSB], a
+
+; switch to internal clock
+ ld a, %00000001
+ ld [rSC], a
+
+; start transfer
+ ld a, %10000001
+ ld [rSC], a
+
+ ret
+; 208a
+
--- a/common/init.asm
+++ b/common/init.asm
@@ -156,7 +156,7 @@
ld a, [hCGB]
and a
jr z, .asm_22b
- call Function2ff7
+ call NormalSpeed
.asm_22b
xor a
--- /dev/null
+++ b/common/item.asm
@@ -1,0 +1,76 @@
+DoItemEffect: ; 2f3f
+ callba _DoItemEffect
+ ret
+; 2f46
+
+CheckTossableItem: ; 2f46
+ push hl
+ push de
+ push bc
+ callba _CheckTossableItem
+ pop bc
+ pop de
+ pop hl
+ ret
+; 2f53
+
+TossItem: ; 2f53
+ push hl
+ push de
+ push bc
+ ld a, [hROMBank]
+ push af
+ ld a, BANK(_TossItem)
+ rst Bankswitch
+
+ call _TossItem
+
+ pop bc
+ ld a, b
+ rst Bankswitch
+ pop bc
+ pop de
+ pop hl
+ ret
+; 2f66
+
+ReceiveItem: ; 2f66
+ push bc
+ ld a, [hROMBank]
+ push af
+ ld a, BANK(_ReceiveItem)
+ rst Bankswitch
+ push hl
+ push de
+
+ call _ReceiveItem
+
+ pop de
+ pop hl
+ pop bc
+ ld a, b
+ rst Bankswitch
+ pop bc
+ ret
+; 2f79
+
+CheckItem: ; 2f79
+ push hl
+ push de
+ push bc
+ ld a, [hROMBank]
+ push af
+ ld a, BANK(_CheckItem)
+ rst Bankswitch
+
+ call _CheckItem
+
+ pop bc
+ ld a, b
+ rst Bankswitch
+ pop bc
+ pop de
+ pop hl
+ ret
+; 2f8c
+
--- /dev/null
+++ b/common/predef.asm
@@ -1,0 +1,54 @@
+Predef: ; 2d83
+; Call predefined function a.
+; Preserves bc, de, hl and f.
+
+ ld [PredefID], a
+ ld a, [hROMBank]
+ push af
+
+ ld a, BANK(GetPredefPointer)
+ rst Bankswitch
+ call GetPredefPointer ; stores hl in PredefTemp
+
+; Switch to the new function's bank
+ rst Bankswitch
+
+; Instead of directly calling stuff,
+; push it to the stack in reverse.
+
+ ld hl, .Return
+ push hl
+
+; Call the Predef function
+ ld a, [PredefAddress]
+ ld h, a
+ ld a, [PredefAddress + 1]
+ ld l, a
+ push hl
+
+; Get hl back
+ ld a, [PredefTemp]
+ ld h, a
+ ld a, [PredefTemp + 1]
+ ld l, a
+ ret
+
+.Return
+; Clean up after the Predef call
+
+ ld a, h
+ ld [PredefTemp], a
+ ld a, l
+ ld [PredefTemp+1], a
+
+ pop hl
+ ld a, h
+ rst Bankswitch
+
+ ld a, [PredefTemp]
+ ld h, a
+ ld a, [PredefTemp + 1]
+ ld l, a
+ ret
+; 2dba
+
--- /dev/null
+++ b/common/random.asm
@@ -1,0 +1,76 @@
+Random: ; 2f8c
+; A simple hardware-based random number generator (RNG).
+
+; Two random numbers are generated by adding and subtracting
+; the divider to the respective values every time it's called.
+
+; The divider is a register that increments at a rate of 16384Hz.
+; For comparison, the Game Boy operates at a clock speed of 4.2MHz.
+
+; Additionally, an equivalent function is executed in VBlank.
+
+; This leaves a with the value in hRandomSub.
+
+ push bc
+
+ 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
+
+ pop bc
+ ret
+; 2f9f
+
+BattleRandom: ; 2f9f
+; _BattleRandom lives in another bank.
+
+; It handles all RNG calls in the battle engine, allowing
+; link battles to remain in sync using a shared PRNG.
+
+ ld a, [hROMBank]
+ push af
+ ld a, BANK(_BattleRandom)
+ rst Bankswitch
+
+ call _BattleRandom
+
+ ld [$cfb6], a
+ pop af
+ rst Bankswitch
+ ld a, [$cfb6]
+ ret
+; 2fb1
+
+
+Function2fb1: ; 2fb1
+ push bc
+ ld c, a
+ xor a
+ sub c
+.asm_2fb5
+ sub c
+ jr nc, .asm_2fb5
+ add c
+ ld b, a
+ push bc
+.asm_2fbb
+ call Random
+ ld a, [hRandomAdd]
+ ld c, a
+ add b
+ jr c, .asm_2fbb
+ ld a, c
+ pop bc
+ call SimpleDivide
+ pop bc
+ ret
+; 2fcb
+
--- /dev/null
+++ b/common/sram.asm
@@ -1,0 +1,34 @@
+GetSRAMBank: ; 2fcb
+; load sram bank a
+; if invalid bank, sram is disabled
+ cp NUM_SRAM_BANKS
+ jr c, OpenSRAM
+ jr CloseSRAM
+; 2fd1
+
+OpenSRAM: ; 2fd1
+; switch to sram bank a
+ push af
+; latch clock data
+ ld a, 1
+ ld [MBC3LatchClock], a
+; enable sram/clock write
+ ld a, SRAM_ENABLE
+ ld [MBC3SRamEnable], a
+; select sram bank
+ pop af
+ ld [MBC3SRamBank], a
+ ret
+; 2fe1
+
+CloseSRAM: ; 2fe1
+ push af
+ ld a, SRAM_DISABLE
+; reset clock latch for next time
+ ld [MBC3LatchClock], a
+; disable sram/clock write
+ ld [MBC3SRamEnable], a
+ pop af
+ ret
+; 2fec
+
--- a/engine/scripting.asm
+++ b/engine/scripting.asm
@@ -539,7 +539,7 @@
ld a, [de]
ld [$d10c], a
ld hl, $d892
- call Function2f66
+ call ReceiveItem
ld a, $1
jr c, .asm_96fb0 ; 0x96fad $1
xor a
@@ -2320,7 +2320,7 @@
call GetScriptByte
ld [$d10c], a
ld hl, $d892
- call Function2f66
+ call ReceiveItem
jr nc, .asm_977eb ; 0x977e3 $6
ld a, $1
ld [$c2dd], a
@@ -2346,7 +2346,7 @@
ld a, $ff
ld [$d107], a
ld hl, $d892
- call Function2f53
+ call TossItem
ret nc
ld a, $1
ld [$c2dd], a
@@ -2363,7 +2363,7 @@
call GetScriptByte
ld [$d106], a
ld hl, $d892
- call PickUpItem
+ call CheckItem
ret nc
ld a, $1
ld [$c2dd], a
--- a/main.asm
+++ b/main.asm
@@ -475,46 +475,8 @@
INCLUDE "common/menu.asm"
+INCLUDE "common/handshake.asm"
-AskSerial: ; 2063
-; send out a handshake while serial int is off
- ld a, [$c2d4]
- bit 0, a
- ret z
-
- ld a, [$c2d5]
- and a
- ret nz
-
-; once every 6 frames
- ld hl, $ca8a
- inc [hl]
- ld a, [hl]
- cp 6
- ret c
-
- xor a
- ld [hl], a
-
- ld a, $c
- ld [$c2d5], a
-
-; handshake
- ld a, $88
- ld [rSB], a
-
-; switch to internal clock
- ld a, %00000001
- ld [rSC], a
-
-; start transfer
- ld a, %10000001
- ld [rSC], a
-
- ret
-; 208a
-
-
INCLUDE "common/game_time.asm"
INCLUDE "common/map.asm"
@@ -543,62 +505,9 @@
INCLUDE "common/farcall.asm"
+INCLUDE "common/predef.asm"
-Predef: ; 2d83
-; Call predefined function a.
-; Preserves bc, de, hl and f.
- ld [PredefID], a
- ld a, [hROMBank]
- push af
-
- ld a, BANK(GetPredefPointer)
- rst Bankswitch
- call GetPredefPointer ; stores hl in PredefTemp
-
-; Switch to the new function's bank
- rst Bankswitch
-
-; Instead of directly calling stuff,
-; push it to the stack in reverse.
-
- ld hl, .Return
- push hl
-
-; Call the Predef function
- ld a, [PredefAddress]
- ld h, a
- ld a, [PredefAddress + 1]
- ld l, a
- push hl
-
-; Get hl back
- ld a, [PredefTemp]
- ld h, a
- ld a, [PredefTemp + 1]
- ld l, a
- ret
-
-.Return
-; Clean up after the Predef call
-
- ld a, h
- ld [PredefTemp], a
- ld a, l
- ld [PredefTemp+1], a
-
- pop hl
- ld a, h
- rst Bankswitch
-
- ld a, [PredefTemp]
- ld h, a
- ld a, [PredefTemp + 1]
- ld l, a
- ret
-; 2dba
-
-
ResetWindow: ; 2dba
call Function1fbf
@@ -768,16 +677,21 @@
INCLUDE "common/string.asm"
-Function2f17: ; 2f17
+IsInJohto: ; 2f17
+; Return 0 if the player is in Johto, and 1 in Kanto.
+
ld a, [MapGroup]
ld b, a
ld a, [MapNumber]
ld c, a
call GetWorldMapLocation
- cp $5f
- jr z, .asm_2f39
- cp $0
- jr nz, .asm_2f35
+
+ cp $5f ; SS Aqua
+ jr z, .Johto
+
+ cp $0 ; Poke Center 2F
+ jr nz, .CheckRegion
+
ld a, [BackupMapGroup]
ld b, a
ld a, [BackupMapNumber]
@@ -784,16 +698,16 @@
ld c, a
call GetWorldMapLocation
-.asm_2f35
- cp $2f
- jr nc, .asm_2f3b
+.CheckRegion
+ cp $2f ; Pallet Town
+ jr nc, .Kanto
-.asm_2f39
+.Johto
xor a
ret
-.asm_2f3b
- ld a, $1
+.Kanto
+ ld a, 1
ret
; 2f3e
@@ -802,195 +716,14 @@
ret
; 2f3f
-DoItemEffect: ; 2f3f
- callba _DoItemEffect
- ret
-; 2f46
-CheckTossableItem: ; 2f46
- push hl
- push de
- push bc
- callba _CheckTossableItem
- pop bc
- pop de
- pop hl
- ret
-; 2f53
+INCLUDE "common/item.asm"
-Function2f53: ; 2f53
- push hl
- push de
- push bc
- ld a, [hROMBank]
- push af
- ld a, $3
- rst Bankswitch
+INCLUDE "common/random.asm"
- call $520d
- pop bc
- ld a, b
- rst Bankswitch
+INCLUDE "common/sram.asm"
- pop bc
- pop de
- pop hl
- ret
-; 2f66
-Function2f66: ; 2f66
- push bc
- ld a, [hROMBank]
- push af
- ld a, $3
- rst Bankswitch
-
- push hl
- push de
- call $51d5
- pop de
- pop hl
- pop bc
- ld a, b
- rst Bankswitch
-
- pop bc
- ret
-; 2f79
-
-PickUpItem: ; 2f79
- push hl
- push de
- push bc
- ld a, [hROMBank]
- push af
- ld a, BANK(_PickUpItem)
- rst Bankswitch
-
- call _PickUpItem
-
- pop bc
- ld a, b
- rst Bankswitch
- pop bc
- pop de
- pop hl
- ret
-; 2f8c
-
-
-Random: ; 2f8c
-; A simple hardware-based random number generator (RNG).
-
-; Two random numbers are generated by adding and subtracting
-; the divider to the respective values every time it's called.
-
-; The divider is a register that increments at a rate of 16384Hz.
-; For comparison, the Game Boy operates at a clock speed of 4.2MHz.
-
-; Additionally, an equivalent function is executed in VBlank.
-
-; This leaves a with the value in hRandomSub.
-
- push bc
-
- 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
-
- pop bc
- ret
-; 2f9f
-
-BattleRandom: ; 2f9f
-; _BattleRandom lives in another bank.
-
-; It handles all RNG calls in the battle engine, allowing
-; link battles to remain in sync using a shared PRNG.
-
- ld a, [hROMBank]
- push af
- ld a, BANK(_BattleRandom)
- rst Bankswitch
-
- call _BattleRandom
-
- ld [$cfb6], a
- pop af
- rst Bankswitch
- ld a, [$cfb6]
- ret
-; 2fb1
-
-
-Function2fb1: ; 2fb1
- push bc
- ld c, a
- xor a
- sub c
-.asm_2fb5
- sub c
- jr nc, .asm_2fb5
- add c
- ld b, a
- push bc
-.asm_2fbb
- call Random
- ld a, [hRandomAdd]
- ld c, a
- add b
- jr c, .asm_2fbb
- ld a, c
- pop bc
- call SimpleDivide
- pop bc
- ret
-; 2fcb
-
-GetSRAMBank: ; 2fcb
-; load sram bank a
-; if invalid bank, sram is disabled
- cp NUM_SRAM_BANKS
- jr c, OpenSRAM
- jr CloseSRAM
-; 2fd1
-
-OpenSRAM: ; 2fd1
-; switch to sram bank a
- push af
-; latch clock data
- ld a, 1
- ld [MBC3LatchClock], a
-; enable sram/clock write
- ld a, SRAM_ENABLE
- ld [MBC3SRamEnable], a
-; select sram bank
- pop af
- ld [MBC3SRamBank], a
- ret
-; 2fe1
-
-CloseSRAM: ; 2fe1
-; preserve a
- push af
- ld a, SRAM_DISABLE
-; reset clock latch for next time
- ld [MBC3LatchClock], a
-; disable sram/clock write
- ld [MBC3SRamEnable], a
- pop af
- ret
-; 2fec
-
-
; Register aliases
_hl_: ; 2fec
@@ -1003,31 +736,9 @@
; 2fef
-Function2fef: ; 2fef
- ld hl, rKEY1
- bit 7, [hl]
- jr z, Function2ffd
- ret
-; 2ff7
+INCLUDE "common/double_speed.asm"
-Function2ff7: ; 2ff7
- ld hl, rKEY1
- bit 7, [hl]
- ret z
-; 2ffd
-Function2ffd: ; 2ffd
- set 0, [hl]
- xor a
- ld [rIF], a
- ld [rIE], a
- ld a, $30
- ld [rJOYP], a
- stop ; rgbasm adds a nop after this instruction by default
- ret
-; 300b
-
-
ClearSprites: ; 300b
ld hl, Sprites
ld b, TileMap - Sprites
@@ -14133,7 +13844,7 @@
ld a, $1
ld [$d10c], a
ld hl, NumItems
- call Function2f66
+ call ReceiveItem
jr nc, .asm_c33d
xor a
ld [$abe2], a
@@ -14233,7 +13944,7 @@
ld a, $36
ld [CurItem], a
ld hl, NumItems
- call PickUpItem
+ call CheckItem
jr nc, .asm_c3c9
and a
ret
@@ -16266,7 +15977,7 @@
; 0xd1d5
-Functiond1d5: ; d1d5
+_ReceiveItem: ; d1d5
call Functiond27b
jp nz, Functiond29c
push hl
@@ -16313,7 +16024,7 @@
; d20d
-Functiond20d: ; d20d
+_TossItem: ; d20d
call Functiond27b
jr nz, .asm_d241
push hl
@@ -16361,7 +16072,7 @@
jp Functiond2ff
; d244
-_PickUpItem: ; d244
+_CheckItem: ; d244
call Functiond27b
jr nz, .asm_d278
push hl
@@ -16419,7 +16130,7 @@
; d283
Functiond283: ; d283
- ld c, $14
+ ld c, 20
ld a, e
cp TMsHMsEnd % $100
jr nz, .asm_d28e
@@ -16428,7 +16139,7 @@
ret z
.asm_d28e
- ld c, $32
+ ld c, 50
ld a, e
cp BallsEnd % $100
jr nz, .asm_d299
@@ -19464,7 +19175,7 @@
ld hl, NumItems
ld a, $1
ld [$d10c], a
- jp Function2f53
+ jp TossItem
; f7a0
Functionf7a0: ; f7a0
@@ -22291,7 +22002,7 @@
ld a, $1
ld [$d10c], a
ld hl, NumItems
- jp Function2f66
+ jp ReceiveItem
; 12cea
INCBIN "baserom.gbc", $12cea, $12cf5 - $12cea
@@ -27029,7 +26740,7 @@
ld a, [$d107]
ld [Buffer2], a
ld hl, NumItems
- call Function2f66
+ call ReceiveItem
jr nc, .PackFull
ld a, [Buffer1]
ld [$d10c], a
@@ -27036,7 +26747,7 @@
ld a, [Buffer2]
ld [$d107], a
ld hl, PCItems
- call Function2f53
+ call TossItem
ld a, $3b
call Predef
ld hl, .WithdrewText
@@ -27210,7 +26921,7 @@
ld a, [$d107]
ld [Buffer2], a
ld hl, PCItems
- call Function2f66
+ call ReceiveItem
jr nc, .asm_15965
ld a, [Buffer1]
ld [$d10c], a
@@ -27217,7 +26928,7 @@
ld a, [Buffer2]
ld [$d107], a
ld hl, NumItems
- call Function2f53
+ call TossItem
ld a, $3b
call Predef
ld hl, .DepositText
@@ -27785,7 +27496,7 @@
call Function1600b
jr c, .asm_15d79
ld hl, NumItems
- call Function2f66
+ call ReceiveItem
jr nc, .asm_15d6f
ld a, [$d107]
ld e, a
@@ -28003,7 +27714,7 @@
call Function15fd7
ld a, [$d107]
ld hl, NumItems
- call Function2f53
+ call TossItem
ld a, $3b
call Predef
ld hl, $c5b9
@@ -31292,7 +31003,7 @@
push hl
ld [CurItem], a
ld hl, NumItems
- call PickUpItem
+ call CheckItem
pop hl
jr nc, .asm_24c89
ld a, [hl]
@@ -36055,7 +35766,7 @@
jr .asm_2a27a
.asm_2a235
- call Function2f17
+ call IsInJohto
and a
ret z
ld h, d
@@ -50161,7 +49872,7 @@
ld a, $1
ld [$d10c], a
ld hl, NumItems
- call Function2f66
+ call ReceiveItem
jr c, .asm_4489e
ld hl, .PackFullText
jp Function1d67
@@ -51575,12 +51286,12 @@
ld a, [ScriptVar]
ld [CurItem], a
ld hl, PCItems
- call PickUpItem
+ call CheckItem
jr c, .asm_4a948
ld a, [ScriptVar]
ld [CurItem], a
ld hl, NumItems
- call PickUpItem
+ call CheckItem
jr c, .asm_4a948
xor a
ld [ScriptVar], a
@@ -60616,7 +60327,7 @@
ld hl, NumItems
ld a, b
ld [$d10c], a
- call Function2f53
+ call TossItem
pop bc
ld a, c
sub b
@@ -62456,7 +62167,7 @@
ld a, WATER_STONE
ld [CurItem], a
ld hl, NumItems
- call PickUpItem
+ call CheckItem
jr c, .asm_8ae24
ld a, [PartyCount]
@@ -62776,7 +62487,7 @@
ld a, $1
ld [$d10c], a
ld hl, NumItems
- call Function2f66
+ call ReceiveItem
pop hl
jr nc, .asm_8b04c
ld a, [hl]
@@ -76550,7 +76261,7 @@
ld a, $1
ld [$d10c], a
ld hl, PCItems
- call Function2f66
+ call ReceiveItem
ret
; fd0eb
@@ -76671,7 +76382,7 @@
ld [$ffe9], a
xor a
ld [$ff9e], a
- call Function2ff7
+ call NormalSpeed
xor a
ld [rIF], a
ld a, [BGMapBuffer]
@@ -82123,7 +81834,7 @@
di
ld a, [rIE]
ld [$cd32], a
- call Function2fef
+ call DoubleSpeed
xor a
ld [rIF], a
ld [$c300], a
@@ -82172,7 +81883,7 @@
ld [$ffc9], a
ld [$ffe9], a
ld [$ff9e], a
- call Function2ff7
+ call NormalSpeed
xor a
ld [rIF], a
ld a, [$cd32]
--- a/stats/odd_eggs.asm
+++ b/stats/odd_eggs.asm
@@ -45,7 +45,7 @@
ld a, $ff
ld [$d107], a
ld hl, NumItems
- call Function2f53
+ call TossItem
ld a, EGG
ld [$cd2a], a
ld a, $29