summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/double_speed.asm31
-rw-r--r--common/handshake.asm38
-rw-r--r--common/init.asm2
-rw-r--r--common/item.asm76
-rw-r--r--common/predef.asm54
-rw-r--r--common/random.asm76
-rw-r--r--common/sram.asm34
7 files changed, 310 insertions, 1 deletions
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
+