summaryrefslogtreecommitdiff
path: root/home/random.asm
diff options
context:
space:
mode:
authorPikalaxALT <PikalaxALT@gmail.com>2016-01-29 18:36:31 -0500
committerPikalaxALT <PikalaxALT@gmail.com>2016-01-29 18:36:31 -0500
commit2bf93c5905319e9181f87b3f83cd3bce7b9feeca (patch)
tree3ebf17c8879e5d6243d81aac8f1c36eb226fac26 /home/random.asm
parented3f9395f6d45f6554ed9d9c49c41ea86a8e2447 (diff)
Import stuff from pokecrystal; diff gold and silver
Diffstat (limited to 'home/random.asm')
-rw-r--r--home/random.asm84
1 files changed, 84 insertions, 0 deletions
diff --git a/home/random.asm b/home/random.asm
new file mode 100644
index 00000000..ae39f439
--- /dev/null
+++ b/home/random.asm
@@ -0,0 +1,84 @@
+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 [PredefTemp + 1], a
+ pop af
+ rst Bankswitch
+ ld a, [PredefTemp + 1]
+ ret
+; 2fb1
+
+
+RandomRange:: ; 2fb1
+; Return a random number between 0 and a (non-inclusive).
+
+ push bc
+ ld c, a
+
+ ; b = $100 % c
+ xor a
+ sub c
+.mod
+ sub c
+ jr nc, .mod
+ add c
+ ld b, a
+
+ ; Get a random number
+ ; from 0 to $ff - b.
+ push bc
+.loop
+ call Random
+ ld a, [hRandomAdd]
+ ld c, a
+ add b
+ jr c, .loop
+ ld a, c
+ pop bc
+
+ call SimpleDivide
+
+ pop bc
+ ret
+; 2fcb