summaryrefslogtreecommitdiff
path: root/home/random.asm
diff options
context:
space:
mode:
Diffstat (limited to 'home/random.asm')
-rw-r--r--home/random.asm51
1 files changed, 41 insertions, 10 deletions
diff --git a/home/random.asm b/home/random.asm
index 5b2f4933..9d62b9b6 100644
--- a/home/random.asm
+++ b/home/random.asm
@@ -1,24 +1,46 @@
-Random:: ; 30a2 (0:30a2)
+Random::
+; 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
+
ldh a, [rDIV]
ld b, a
- ldh a, [hRandom]
+ ldh a, [hRandomAdd]
adc b
- ldh [hRandom], a
+ ldh [hRandomAdd], a
+
ldh a, [rDIV]
ld b, a
ldh a, [hRandomSub]
sbc b
ldh [hRandomSub], a
+
pop bc
ret
BattleRandom::
+; _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.
+
ldh a, [hROMBank]
push af
- ld a, BANK(BattleRandom_)
+ ld a, BANK(_BattleRandom)
rst Bankswitch
- call BattleRandom_
+
+ call _BattleRandom
+
ld [wPredefTemp + 1], a
pop af
rst Bankswitch
@@ -26,24 +48,33 @@ BattleRandom::
ret
RandomRange::
+; Return a random number between 0 and a (non-inclusive).
+
push bc
ld c, a
+
+ ; b = $100 % c
xor a
sub c
-.asm_30cb
+.mod
sub c
- jr nc, .asm_30cb
+ jr nc, .mod
add c
ld b, a
+
+ ; Get a random number
+ ; from 0 to $ff - b.
push bc
-.asm_30d1
+.loop
call Random
- ldh a, [hRandom]
+ ldh a, [hRandomAdd]
ld c, a
add b
- jr c, .asm_30d1
+ jr c, .loop
ld a, c
pop bc
+
call SimpleDivide
+
pop bc
ret