summaryrefslogtreecommitdiff
path: root/src/home/random.asm
diff options
context:
space:
mode:
authorDaniel Harding <33dannye@gmail.com>2021-09-19 00:21:14 -0500
committerGitHub <noreply@github.com>2021-09-19 00:21:14 -0500
commitdf67aac83b466dadf5f74c881bf84dd9ef19bdfc (patch)
tree47501aced2d256052b8f78bc97328d5af5703add /src/home/random.asm
parente4bce9b7ee5e89f8edfd921de2379f0fa06af206 (diff)
parent8dee6b7a11e85d6d4b9f8ec9fb9d53a499fd37dc (diff)
Merge pull request #110 from ElectroDeoxys/master
Split Home bank
Diffstat (limited to 'src/home/random.asm')
-rw-r--r--src/home/random.asm66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/home/random.asm b/src/home/random.asm
new file mode 100644
index 0000000..d731b12
--- /dev/null
+++ b/src/home/random.asm
@@ -0,0 +1,66 @@
+; returns h * l in hl
+HtimesL:
+ push de
+ ld a, h
+ ld e, l
+ ld d, $0
+ ld l, d
+ ld h, d
+ jr .asm_887
+.asm_882
+ add hl, de
+.asm_883
+ sla e
+ rl d
+.asm_887
+ srl a
+ jr c, .asm_882
+ jr nz, .asm_883
+ pop de
+ ret
+
+; return a random number between 0 and a (exclusive) in a
+Random:
+ push hl
+ ld h, a
+ call UpdateRNGSources
+ ld l, a
+ call HtimesL
+ ld a, h
+ pop hl
+ ret
+
+; get the next random numbers of the wRNG1 and wRNG2 sequences
+UpdateRNGSources:
+ push hl
+ push de
+ ld hl, wRNG1
+ ld a, [hli]
+ ld d, [hl] ; wRNG2
+ inc hl
+ ld e, a
+ ld a, d
+ rlca
+ rlca
+ xor e
+ rra
+ push af
+ ld a, d
+ xor e
+ ld d, a
+ ld a, [hl] ; wRNGCounter
+ xor e
+ ld e, a
+ pop af
+ rl e
+ rl d
+ ld a, d
+ xor e
+ inc [hl] ; wRNGCounter
+ dec hl
+ ld [hl], d ; wRNG2
+ dec hl
+ ld [hl], e ; wRNG1
+ pop de
+ pop hl
+ ret