2023-10-13 06:38:10 +00:00
|
|
|
IF !DEF(RNG_ASM)
|
|
|
|
DEF RNG_ASM EQU 1
|
|
|
|
|
|
|
|
|
|
|
|
INCLUDE "globals.asm"
|
|
|
|
|
|
|
|
|
|
|
|
SECTION "RNG Variables", WRAM0
|
|
|
|
wRNGSeed: ds 4
|
|
|
|
wPieceHistory: ds 4
|
|
|
|
wNextPiece:: ds 1
|
|
|
|
|
|
|
|
|
2023-10-10 08:32:24 +00:00
|
|
|
section "RNG Functions", ROM0
|
2023-10-18 02:45:58 +00:00
|
|
|
RNGInit::
|
2023-10-13 06:38:10 +00:00
|
|
|
; Do some bit fuckery on the seed using the gameboy's free-running timers.
|
|
|
|
ld hl, wRNGSeed
|
|
|
|
ldh a, [rDIV]
|
|
|
|
xor a, [hl]
|
|
|
|
ld [hl+], a
|
|
|
|
|
|
|
|
ldh a, [rTIMA]
|
|
|
|
xor a, [hl]
|
|
|
|
ld [hl+], a
|
|
|
|
|
|
|
|
ldh a, [rDIV]
|
|
|
|
xor a, [hl]
|
|
|
|
ld [hl+], a
|
|
|
|
|
|
|
|
ldh a, [rTIMA]
|
|
|
|
xor a, [hl]
|
|
|
|
ld [hl], a
|
|
|
|
|
|
|
|
; Initialize the next history.
|
|
|
|
ld hl, wPieceHistory
|
|
|
|
ld a, PIECE_Z
|
|
|
|
ld [hl+], a
|
|
|
|
ld [hl+], a
|
|
|
|
ld a, PIECE_S
|
|
|
|
ld [hl+], a
|
|
|
|
ld [hl], a
|
|
|
|
|
|
|
|
; Get the first piece and make sure it's not Z, S or O.
|
|
|
|
: call NextPiece
|
|
|
|
cp a, PIECE_Z
|
|
|
|
jr z, :-
|
|
|
|
cp a, PIECE_S
|
|
|
|
jr z, :-
|
|
|
|
cp a, PIECE_O
|
|
|
|
jr z, :-
|
|
|
|
|
|
|
|
; Store it.
|
|
|
|
ld hl, wPieceHistory
|
|
|
|
ld [hl], a
|
|
|
|
ld hl, wNextPiece
|
|
|
|
ld [hl], a
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
GetNextPiece::
|
|
|
|
ld e, 7
|
|
|
|
: dec e
|
|
|
|
jr z, :+
|
|
|
|
|
|
|
|
call NextPiece
|
|
|
|
ld hl, wPieceHistory
|
|
|
|
cp a, [hl]
|
|
|
|
jr z, :-
|
|
|
|
inc hl
|
|
|
|
cp a, [hl]
|
|
|
|
jr z, :-
|
|
|
|
inc hl
|
|
|
|
cp a, [hl]
|
|
|
|
jr z, :-
|
|
|
|
inc hl
|
|
|
|
cp a, [hl]
|
|
|
|
jr z, :-
|
|
|
|
|
|
|
|
: ld hl, wNextPiece
|
|
|
|
ld [hl], a
|
|
|
|
ld b, a
|
|
|
|
ld hl, wPieceHistory+2
|
|
|
|
ld a, [hl+]
|
|
|
|
ld [hl], a
|
|
|
|
ld hl, wPieceHistory+1
|
|
|
|
ld a, [hl+]
|
|
|
|
ld [hl], a
|
|
|
|
ld hl, wPieceHistory
|
|
|
|
ld a, [hl+]
|
|
|
|
ld [hl-], a
|
|
|
|
ld a, b
|
|
|
|
ld [hl], a
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
NextPiece:
|
|
|
|
call NextByte
|
|
|
|
and a, $07
|
|
|
|
cp a, 7
|
|
|
|
ret nz
|
|
|
|
dec a
|
|
|
|
ret
|
|
|
|
|
|
|
|
NextByte:
|
2023-10-10 08:32:24 +00:00
|
|
|
; Load seed
|
|
|
|
ld hl,wRNGSeed+3
|
2023-10-13 06:38:10 +00:00
|
|
|
ld a, [hl-]
|
|
|
|
ld b, a
|
|
|
|
ld a, [hl-]
|
|
|
|
ld c, a
|
|
|
|
ld a, [hl-]
|
2023-10-10 08:32:24 +00:00
|
|
|
|
|
|
|
; Multiply by 0x01010101
|
|
|
|
add [hl]
|
2023-10-13 06:38:10 +00:00
|
|
|
ld d, a
|
2023-10-10 08:32:24 +00:00
|
|
|
adc c
|
2023-10-13 06:38:10 +00:00
|
|
|
ld c, a
|
2023-10-10 08:32:24 +00:00
|
|
|
adc b
|
2023-10-13 06:38:10 +00:00
|
|
|
ld b, a
|
2023-10-10 08:32:24 +00:00
|
|
|
|
|
|
|
; Add 0x31415927 and write back
|
2023-10-13 06:38:10 +00:00
|
|
|
ld a, [hl]
|
2023-10-10 08:32:24 +00:00
|
|
|
add $27
|
2023-10-13 06:38:10 +00:00
|
|
|
ld [hl+], a
|
|
|
|
ld a, d
|
2023-10-10 08:32:24 +00:00
|
|
|
adc $59
|
2023-10-13 06:38:10 +00:00
|
|
|
ld [hl+], a
|
|
|
|
ld a, c
|
2023-10-10 08:32:24 +00:00
|
|
|
adc $41
|
2023-10-13 06:38:10 +00:00
|
|
|
ld [hl+], a
|
|
|
|
ld c, a
|
|
|
|
ld a, b
|
2023-10-10 08:32:24 +00:00
|
|
|
adc $31
|
2023-10-13 06:38:10 +00:00
|
|
|
ld [hl], a
|
2023-10-10 08:32:24 +00:00
|
|
|
ret
|
2023-10-13 06:38:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
ENDC
|