mirror of
https://gitlab.com/xCrystal/pokecrystal-board.git
synced 2024-11-16 11:27:33 -08:00
split Random into common/random.asm
This commit is contained in:
parent
3b70005fb0
commit
edf939a07b
76
common/random.asm
Normal file
76
common/random.asm
Normal file
@ -0,0 +1,76 @@
|
||||
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 [$cfb6], a
|
||||
pop af
|
||||
rst Bankswitch
|
||||
ld a, [$cfb6]
|
||||
ret
|
||||
; 2fb1
|
||||
|
||||
|
||||
Function2fb1: ; 2fb1
|
||||
push bc
|
||||
ld c, a
|
||||
xor a
|
||||
sub c
|
||||
.asm_2fb5
|
||||
sub c
|
||||
jr nc, .asm_2fb5
|
||||
add c
|
||||
ld b, a
|
||||
push bc
|
||||
.asm_2fbb
|
||||
call Random
|
||||
ld a, [hRandomAdd]
|
||||
ld c, a
|
||||
add b
|
||||
jr c, .asm_2fbb
|
||||
ld a, c
|
||||
pop bc
|
||||
call SimpleDivide
|
||||
pop bc
|
||||
ret
|
||||
; 2fcb
|
||||
|
76
main.asm
76
main.asm
@ -810,82 +810,8 @@ Function2f3e: ; 2f3e
|
||||
|
||||
INCLUDE "common/item.asm"
|
||||
|
||||
INCLUDE "common/random.asm"
|
||||
|
||||
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 [$cfb6], a
|
||||
pop af
|
||||
rst Bankswitch
|
||||
ld a, [$cfb6]
|
||||
ret
|
||||
; 2fb1
|
||||
|
||||
|
||||
Function2fb1: ; 2fb1
|
||||
push bc
|
||||
ld c, a
|
||||
xor a
|
||||
sub c
|
||||
.asm_2fb5
|
||||
sub c
|
||||
jr nc, .asm_2fb5
|
||||
add c
|
||||
ld b, a
|
||||
push bc
|
||||
.asm_2fbb
|
||||
call Random
|
||||
ld a, [hRandomAdd]
|
||||
ld c, a
|
||||
add b
|
||||
jr c, .asm_2fbb
|
||||
ld a, c
|
||||
pop bc
|
||||
call SimpleDivide
|
||||
pop bc
|
||||
ret
|
||||
; 2fcb
|
||||
|
||||
GetSRAMBank: ; 2fcb
|
||||
; load sram bank a
|
||||
|
Loading…
Reference in New Issue
Block a user