-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathrandom.asm
More file actions
59 lines (58 loc) · 1.55 KB
/
random.asm
File metadata and controls
59 lines (58 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
initRandom:
push hl
push de
push af
setBankA(0x04) ; filesystem, probably pretty unpredictable
ld hl, 0x4010
ld de, random_seed
ld a, r \ add a, (hl) \ ld l, a \ ld (de), a \ inc de
ld a, r \ add a, (hl) \ ld l, a \ ld (de), a \ inc de
ld a, r \ add a, (hl) \ ld l, a \ ld (de), a \ inc de
ld a, r \ add a, (hl) \ ld l, a \ ld (de), a \ inc de
ld a, r \ or (hl) \ ld l, a \ ld (de), a \ inc de ;we use two ors to ensure this part of the seed is non-zero.
ld a, r \ add a, (hl) \ ld l, a \ ld (de), a \ inc de
ld a, r \ add a, (hl) \ ld l, a \ ld (de), a \ inc de
ld a, r \ or (hl) \ ld (de), a
pop af
pop de
pop hl
ret
;; getRandom [Miscellaneous]
;; Gets an 8-bit random number.
;; Outputs:
;; A: Random number (0-255)
;; Notes:
;; This is not cryptographically random.
getRandom:
push hl
push de
push bc
ld hl, (random_seed)
ld de, (random_seed+2)
ld b, h
ld c, l
add hl, hl \ rl e \ rl d
add hl, hl \ rl e \ rl d
inc l
add hl, bc
ld (random_seed), hl
ld hl, (random_seed+2)
adc hl, de
ld (random_seed+2), hl
ex de, hl
ld hl, (random_seed+4)
ld de, (random_seed+6)
ld bc, 54321
add hl, hl \ rl c \ rl b
ld (random_seed+6), bc
sbc a, a
and 0b11000101
xor l
ld l, a
ld (random_seed+4), hl
ld a, d
add a, b
pop bc
pop de
pop hl
ret