diff --git a/common/double_speed.asm b/common/double_speed.asm new file mode 100644 index 000000000..fc1234215 --- /dev/null +++ b/common/double_speed.asm @@ -0,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 + diff --git a/common/handshake.asm b/common/handshake.asm new file mode 100644 index 000000000..8ed1473e4 --- /dev/null +++ b/common/handshake.asm @@ -0,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 + diff --git a/common/init.asm b/common/init.asm index 3104c416d..ead75975c 100644 --- a/common/init.asm +++ b/common/init.asm @@ -156,7 +156,7 @@ Init: ; 17d ld a, [hCGB] and a jr z, .asm_22b - call Function2ff7 + call NormalSpeed .asm_22b xor a diff --git a/common/item.asm b/common/item.asm new file mode 100644 index 000000000..0906a19f6 --- /dev/null +++ b/common/item.asm @@ -0,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 + diff --git a/common/predef.asm b/common/predef.asm new file mode 100644 index 000000000..bd34a87a1 --- /dev/null +++ b/common/predef.asm @@ -0,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 + diff --git a/common/random.asm b/common/random.asm new file mode 100644 index 000000000..0f46da9e4 --- /dev/null +++ b/common/random.asm @@ -0,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 + diff --git a/common/sram.asm b/common/sram.asm new file mode 100644 index 000000000..3c42e7618 --- /dev/null +++ b/common/sram.asm @@ -0,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 + diff --git a/engine/scripting.asm b/engine/scripting.asm index e8921a265..12fd14283 100644 --- a/engine/scripting.asm +++ b/engine/scripting.asm @@ -539,7 +539,7 @@ Script_verbosegiveitem2: ; 0x96f8e 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 @@ Script_giveitem: ; 0x977ca 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 @@ Script_takeitem: ; 0x977f0 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 @@ Script_checkitem: ; 0x97812 call GetScriptByte ld [$d106], a ld hl, $d892 - call PickUpItem + call CheckItem ret nc ld a, $1 ld [$c2dd], a diff --git a/main.asm b/main.asm index e134d11ed..38d1b9cd5 100644 --- a/main.asm +++ b/main.asm @@ -475,45 +475,7 @@ Function1d19: ; 1d19 INCLUDE "common/menu.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/handshake.asm" INCLUDE "common/game_time.asm" @@ -543,60 +505,7 @@ Function2d43: ; 2d43 INCLUDE "common/farcall.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 +INCLUDE "common/predef.asm" ResetWindow: ; 2dba @@ -768,32 +677,37 @@ Function2ee4: ; 2ee4 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] 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,193 +716,12 @@ Function2f3e: ; 2f3e 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 - - 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 +INCLUDE "common/sram.asm" ; Register aliases @@ -1003,29 +736,7 @@ _de_: ; 2fed ; 2fef -Function2fef: ; 2fef - ld hl, rKEY1 - bit 7, [hl] - jr z, Function2ffd - ret -; 2ff7 - -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 +INCLUDE "common/double_speed.asm" ClearSprites: ; 300b @@ -14133,7 +13844,7 @@ Functionc309: ; c309 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 @@ Functionc3ae: ; c3ae ld a, $36 ld [CurItem], a ld hl, NumItems - call PickUpItem + call CheckItem jr nc, .asm_c3c9 and a ret @@ -16266,7 +15977,7 @@ UnknownText_0xd1d0: ; 0xd1d0 ; 0xd1d5 -Functiond1d5: ; d1d5 +_ReceiveItem: ; d1d5 call Functiond27b jp nz, Functiond29c push hl @@ -16313,7 +16024,7 @@ Functiond201: ; d201 ; d20d -Functiond20d: ; d20d +_TossItem: ; d20d call Functiond27b jr nz, .asm_d241 push hl @@ -16361,7 +16072,7 @@ Functiond20d: ; d20d jp Functiond2ff ; d244 -_PickUpItem: ; d244 +_CheckItem: ; d244 call Functiond27b jr nz, .asm_d278 push hl @@ -16419,7 +16130,7 @@ Functiond27b: ; d27b ; d283 Functiond283: ; d283 - ld c, $14 + ld c, 20 ld a, e cp TMsHMsEnd % $100 jr nz, .asm_d28e @@ -16428,7 +16139,7 @@ Functiond283: ; d283 ret z .asm_d28e - ld c, $32 + ld c, 50 ld a, e cp BallsEnd % $100 jr nz, .asm_d299 @@ -19464,7 +19175,7 @@ Functionf795: ; f795 ld hl, NumItems ld a, $1 ld [$d10c], a - jp Function2f53 + jp TossItem ; f7a0 Functionf7a0: ; f7a0 @@ -22291,7 +22002,7 @@ Function12cdf: ; 12cdf ld a, $1 ld [$d10c], a ld hl, NumItems - jp Function2f66 + jp ReceiveItem ; 12cea INCBIN "baserom.gbc", $12cea, $12cf5 - $12cea @@ -27029,14 +26740,14 @@ Function157e9: ; 0x157e9 ld a, [$d107] ld [Buffer2], a ld hl, NumItems - call Function2f66 + call ReceiveItem jr nc, .PackFull ld a, [Buffer1] ld [$d10c], a ld a, [Buffer2] ld [$d107], a ld hl, PCItems - call Function2f53 + call TossItem ld a, $3b call Predef ld hl, .WithdrewText @@ -27210,14 +26921,14 @@ Function1590a: ; 0x1590a ld a, [$d107] ld [Buffer2], a ld hl, PCItems - call Function2f66 + call ReceiveItem jr nc, .asm_15965 ld a, [Buffer1] ld [$d10c], a 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 @@ Function15cef: ; 15cef 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 @@ Function15efd: ; 15efd call Function15fd7 ld a, [$d107] ld hl, NumItems - call Function2f53 + call TossItem ld a, $3b call Predef ld hl, $c5b9 @@ -31292,7 +31003,7 @@ Function24c64: ; 24c64 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 @@ Function2a200: ; 2a200 jr .asm_2a27a .asm_2a235 - call Function2f17 + call IsInJohto and a ret z ld h, d @@ -50161,7 +49872,7 @@ Function4484a: ; 0x4484a 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 @@ Function4a927: ; 4a927 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 @@ Function88211: ; 88211 ld hl, NumItems ld a, b ld [$d10c], a - call Function2f53 + call TossItem pop bc ld a, c sub b @@ -62456,7 +62167,7 @@ Function8adef: ; 8adef 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 @@ Function8afd4: ; 8afd4 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 @@ Functionfd0c3: ; fd0c3 ld a, $1 ld [$d10c], a ld hl, PCItems - call Function2f66 + call ReceiveItem ret ; fd0eb @@ -76671,7 +76382,7 @@ Function1000a4: ; 1000a4 ld [$ffe9], a xor a ld [$ff9e], a - call Function2ff7 + call NormalSpeed xor a ld [rIF], a ld a, [BGMapBuffer] @@ -82123,7 +81834,7 @@ Function1183cb: ; 1183cb di ld a, [rIE] ld [$cd32], a - call Function2fef + call DoubleSpeed xor a ld [rIF], a ld [$c300], a @@ -82172,7 +81883,7 @@ Function118452: ; 118452 ld [$ffc9], a ld [$ffe9], a ld [$ff9e], a - call Function2ff7 + call NormalSpeed xor a ld [rIF], a ld a, [$cd32] diff --git a/stats/odd_eggs.asm b/stats/odd_eggs.asm index 6922a2de7..68062ec3b 100644 --- a/stats/odd_eggs.asm +++ b/stats/odd_eggs.asm @@ -45,7 +45,7 @@ GiveOddEgg: ; 1fb4b6 ld a, $ff ld [$d107], a ld hl, NumItems - call Function2f53 + call TossItem ld a, EGG ld [$cd2a], a ld a, $29