shithub: pokered

Download patch

ref: c44b52577b6ecb770ff9e7d7d67c11ed909b9642
parent: 0152e339095e928e689e67dd0da029fde756d853
author: IIMarckus <iimarckus@gmail.com>
date: Mon Dec 12 14:47:20 EST 2011

Add random number generation functions.

hg-commit-id: e7d865799d77


--- a/constants.asm
+++ b/constants.asm
@@ -112,7 +112,11 @@
 
 W_ENEMYMON6MOVE3 EQU $D98A
 
+H_RAND1 EQU $FFD3
+H_RAND2 EQU $FFD4
+
 ; hardware registers, from the pandocs http://nocash.emubase.de/pandocs.htm
+rDIV EQU $FF04
 rBGP EQU $FF47
 rOBP0 EQU $FF48
 rOBP1 EQU $FF49
--- a/pokered.asm
+++ b/pokered.asm
@@ -1121,8 +1121,22 @@
 	ld c,3
 	jp DelayFrames
 
-INCBIN "baserom.gbc",$3DDC,$3E6D - $3DDC
+INCBIN "baserom.gbc",$3DDC,$3E5C - $3DDC
 
+GenRandom: ; 3E5C
+; store a random 8-bit value in a
+	push hl
+	push de
+	push bc
+	ld b,BANK(GenRandom_)
+	ld hl,GenRandom_
+	call Bankswitch
+	ld a,[H_RAND1]
+	pop bc
+	pop de
+	pop hl
+	ret
+
 Predef: ; 3E6D
 ; runs a predefined ASM command, where the command ID is read from $D0B7
 
@@ -3558,7 +3572,7 @@
 	call $62B4
 	jp c,$6581
 Next625F:
-	call $3E5C
+	call GenRandom
 	srl a
 	jr c,Next6278
 	and 3
@@ -3729,8 +3743,22 @@
 ShrinkPic2:
 	INCBIN "pic/trainer/shrink2.pic"
 
-INCBIN "baserom.gbc",$13074,$13AA0-$13074
+INCBIN "baserom.gbc",$13074,$13A8F-$13074
 
+GenRandom_: ; 7A8F
+; generate a random 16-bit integer and store it at $FFD3,$FFD4
+	ld a,[rDIV]
+	ld b,a
+	ld a,[H_RAND1]
+	adc b
+	ld [H_RAND1],a
+	ld a,[rDIV]
+	ld b,a
+	ld a,[H_RAND2]
+	sbc b
+	ld [H_RAND2],a
+	ret
+
 SECTION "bank5",DATA,BANK[$5]
 INCBIN "baserom.gbc",$14000,$4000
 
@@ -5635,7 +5663,7 @@
 	ld a,[hli]
 	ld h,[hl]
 	ld l,a
-	call $3E5C
+	call GenRandom
 	jp [hl]
 
 TrainerAIPointers: ; 655C