git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

commit 7ae47f6e65052cfd4c4da26c343ce7972375752b
parent 4524a3f0b514c2a6650eeb666447bc21ab12cb1c
Author: Phidias618 <126189685+Phidias618@users.noreply.github.com>
Date:   Mon, 13 Apr 2026 11:30:20 +0200

Optimization of rand() for the sm83 and z80 targets
Diffstat:
Mgbdk-lib/libc/asm/sm83/rand.s56++++++++++++++++++++++++--------------------------------
Mgbdk-lib/libc/asm/z80/rand.s48++++++++++++++++--------------------------------
2 files changed, 40 insertions(+), 64 deletions(-)

diff --git a/gbdk-lib/libc/asm/sm83/rand.s b/gbdk-lib/libc/asm/sm83/rand.s @@ -47,7 +47,7 @@ ___rand_seed:: ;; Random number generator using the linear congruential method ;; X(n+1) = (a*X(n)+c) mod m - ;; with a = 17, m = 16 and c = $5c93 (arbitrarily) + ;; with a = 17, m = 65536 and c = $5c93 (arbitrarily) ;; The seed value is also chosen arbitrarily as $a27e ;; Ref : D. E. Knuth, "The Art of Computer Programming" , Volume 2 ;; @@ -60,43 +60,35 @@ ___rand_seed:: _rand:: ; Banked _randw:: ; Banked - LD A,(.randlo) - LD L,A - LD E,A ; Save randlo - LD A,(.randhi) - LD D,A ; Save randhi + ld hl, #.randlo + ld a, (hl+) + ld e, a + ld d, (hl) ; D = randhi - SLA L ; * 16 - RLA - SLA L - RLA - SLA L - RLA - SLA L - RLA - LD H,A ; Save randhi*16 + ; HL = 17 * DE + 0x5C93 + ld h, d + ld l, e - LD A,E ; Old randlo - ADD A,L ; Add randlo*16 - LD L,A ; Save randlo*17 - - LD A,H ; randhi*16 - ADC A,D ; Add old randhi - LD H,A ; Save randhi*17 - - LD A,L ; randlo*17 - ADD A,#0x93 - LD (.randlo),A - LD D,A ; Return register - LD A,H ; randhi*17 - ADC A,#0x5c - LD (.randhi),A - LD E,A ; Return register + add hl, hl + add hl, hl + add hl, hl + add hl, hl + add hl, de + ld de, #0x5C93 + add hl, de + + ld d, l + ld e, h ; de = return value + ld hl, #.randlo + ld a, d + ld (hl+), a + ld (hl), e + ;; Note D is the low byte,E the high byte. This is intentional because ;; the high byte can be slightly 'more random' than the low byte, and I presume ;; most will cast the return value to a uint8_t. As if someone will use this, tha! - RET + ret ;; This sets the seed value. Call it whenever you like ;; diff --git a/gbdk-lib/libc/asm/z80/rand.s b/gbdk-lib/libc/asm/z80/rand.s @@ -11,38 +11,22 @@ ___rand_seed:: _rand:: _randw:: - LD HL, (.randval) - EX DE, HL - LD L, E - LD A, D - - SLA L ; * 16 - RLA - SLA L - RLA - SLA L - RLA - SLA L - RLA - LD H, A ; Save randhi*16 - - LD A, E ; Old randlo - ADD A, L ; Add randlo*16 - LD L, A ; Save randlo*17 - - LD A, H ; randhi*16 - ADC A, D ; Add old randhi - LD H, A ; Save randhi*17 - - LD A, L ; randlo*17 - ADD A, #0x93 - LD L, A - LD A, H ; randhi*17 - ADC A, #0x5c - LD H, A - LD (.randval), HL - LD H, L - LD L, A + ld hl, (.randval) + LD d, h + ld e, l + + add hl, hl + add hl, hl + add hl, hl + add hl, hl + add hl, de + ld de, #0x5C93 + add hl, de + ld (.randval), hl + + ld a, h + ld h, l + ld l, a RET

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.