dmgtris/src/rng.asm

156 lines
2.8 KiB
NASM
Raw Normal View History

2023-10-21 15:28:38 +00:00
; DMGTRIS
; Copyright (C) 2023 - Randy Thiemann <randy.thiemann@gmail.com>
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-10-13 06:38:10 +00:00
IF !DEF(RNG_ASM)
DEF RNG_ASM EQU 1
INCLUDE "globals.asm"
2023-10-21 18:49:52 +00:00
SECTION "High RNG Variables", HRAM
hRNGSeed: ds 4
hPieceHistory: ds 4
hNextPiece:: ds 1
2023-10-21 19:25:57 +00:00
hRNGRerolls:: ds 1
2023-10-13 06:38:10 +00:00
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.
ldh a, [rDIV]
xor a, [hl]
2023-10-21 18:49:52 +00:00
ldh [hRNGSeed], a
2023-10-13 06:38:10 +00:00
ldh a, [rTIMA]
xor a, [hl]
2023-10-21 18:49:52 +00:00
ldh [hRNGSeed+1], a
2023-10-13 06:38:10 +00:00
ldh a, [rDIV]
xor a, [hl]
2023-10-21 18:49:52 +00:00
ldh [hRNGSeed+2], a
2023-10-13 06:38:10 +00:00
ldh a, [rTIMA]
xor a, [hl]
2023-10-21 18:49:52 +00:00
ldh [hRNGSeed+3], a
2023-10-13 06:38:10 +00:00
; Initialize the next history.
ld a, PIECE_Z
2023-10-21 18:53:54 +00:00
ldh [hPieceHistory], a
ldh [hPieceHistory+1], a
2023-10-13 06:38:10 +00:00
ld a, PIECE_S
2023-10-21 18:53:54 +00:00
ldh [hPieceHistory+2], a
ldh [hPieceHistory+3], a
2023-10-13 06:38:10 +00:00
; 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.
2023-10-21 18:49:52 +00:00
ldh [hPieceHistory], a
ld [hNextPiece], a
2023-10-13 06:38:10 +00:00
ret
GetNextPiece::
2023-10-21 19:25:57 +00:00
ldh a, [hRNGRerolls]
cp a, 0
jr nz, :+
call NextPiece
jr .donerolling
: inc a
ld e, a
2023-10-13 06:38:10 +00:00
: dec e
2023-10-21 19:25:57 +00:00
jr z, .donerolling
2023-10-13 06:38:10 +00:00
call NextPiece
2023-10-21 18:49:52 +00:00
ld hl, hPieceHistory
2023-10-13 06:38:10 +00:00
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, :-
2023-10-21 19:25:57 +00:00
.donerolling
ldh [hNextPiece], a
2023-10-13 06:38:10 +00:00
ld b, a
2023-10-21 18:49:52 +00:00
ldh a, [hPieceHistory+2]
ldh [hPieceHistory+3], a
ldh a, [hPieceHistory+1]
ldh [hPieceHistory+2], a
ldh a, [hPieceHistory]
ldh [hPieceHistory+1], a
2023-10-13 06:38:10 +00:00
ld a, b
2023-10-21 18:49:52 +00:00
ldh [hPieceHistory], a
2023-10-13 06:38:10 +00:00
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
2023-10-21 18:49:52 +00:00
ld hl, hRNGSeed+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