git.y1.nz

gbdk-2020

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

commit 6fce22f6747aca7cf3b3f49a2d9389cd1b4d3bc7
parent ce3c57764996907a754e3989b156e921b83b5783
Author: Toxa <untoxa@mail.ru>
Date:   Sat, 28 Aug 2021 21:36:44 +0300

SMS/GG: rand.h()/arand.s() port

Diffstat:
Mgbdk-lib/include/rand.h27+++++++++++++++++----------
Mgbdk-lib/libc/asm/z80/Makefile1+
Agbdk-lib/libc/asm/z80/arand.s89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Agbdk-lib/libc/asm/z80/rand.s48++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 155 insertions(+), 10 deletions(-)

diff --git a/gbdk-lib/include/rand.h b/gbdk-lib/include/rand.h @@ -24,22 +24,25 @@ again to re-initialize with the same or a different seed. @see rand(), randw() */ -void -initrand(uint16_t seed) NONBANKED; /* Non-banked as called from asm in arand.s */ +#if defined(__PORT_gbz80) +void initrand(uint16_t seed); +#elif defined(__PORT_z80) +void initrand(uint16_t seed) __z88dk_fastcall; +#else +#error Unrecognized port +#endif /** Returns a random byte (8 bit) value. @ref initrand() should be used to initialize the random number generator before using rand() */ -int8_t -rand(void); +int8_t rand(void); /** Returns a random word (16 bit) value. @ref initrand() should be used to initialize the random number generator before using rand() */ -uint16_t -randw(void); +uint16_t randw(void); /** Random generator using the linear lagged additive method @@ -50,14 +53,18 @@ randw(void); @see initrand() for suggestions about seed values, arand() */ -void -initarand(uint16_t seed); +#if defined(__PORT_gbz80) +void initarand(uint16_t seed); +#elif defined(__PORT_z80) +void initarand(uint16_t seed) __z88dk_fastcall; +#else +#error Unrecognized port +#endif /** Returns a random number generated with the linear lagged additive method. @ref initarand() should be used to initialize the random number generator before using arand() */ -int8_t -arand(void); +int8_t arand(void); #endif diff --git a/gbdk-lib/libc/asm/z80/Makefile b/gbdk-lib/libc/asm/z80/Makefile @@ -13,6 +13,7 @@ ASSRC = __strreverse.s strcpy.s strlen.s \ memcpy.s memmove.s \ setjmp.s \ abs.s \ + rand.s arand.s \ __sdcc_call_hl.s __sdcc_call_iy.s \ atomic_flag_test_and_set.s __sdcc_critical.s \ crtenter.s diff --git a/gbdk-lib/libc/asm/z80/arand.s b/gbdk-lib/libc/asm/z80/arand.s @@ -0,0 +1,89 @@ + .module Random + + .globl .initrand + .globl _rand + + .area _BSS +.start_arand_vars: + +.randarr: + .ds 55 +.raxj: + .ds 0x01 +.raxk: + .ds 0x01 + +.end_arand_vars: + + .area _GSINIT + + ld hl, #.start_arand_vars + ld de, #(.start_arand_vars + 1) + ld (hl), #0 + ld bc, #(.end_arand_vars - .start_arand_vars) + ldir + + .area _CODE + +_arand:: ; Banked + PUSH BC + LD D, #0 + LD HL, #(.randarr - 1) + LD A, (.raxj) + LD E, A + DEC A ; Decrease the pointer + JR NZ, 1$ + LD A, #55 +1$: + LD (.raxj), A + ADD HL, DE + LD B, (HL) + + LD HL, #(.randarr - 1) ; Ooh... + LD A, (.raxk) + LD E, A + DEC A ; Decrease the pointer + JR NZ, 2$ + LD A, #55 +2$: + LD (.raxk), A + ADD HL, DE + LD A, (HL) + + ADD B + LD (HL), A ; Store new value + + POP BC + + LD H, #0 + LD L, A + + RET + +_initarand:: ; Banked + CALL .initrand + + LD A, #55 + LD HL, #.randarr +1$: + DEC A + LD (.raxj), A + LD B, H + LD C, L + CALL _rand + LD H, B + LD L, C + + LD (HL), E + INC HL + + LD A, (.raxj) + CP #0 + JP NZ, 1$ + + LD A, #24 ; Now the array has been filled,set the pointers + LD (.raxj), A + LD A, #55 + LD (.raxk), A + + RET diff --git a/gbdk-lib/libc/asm/z80/rand.s b/gbdk-lib/libc/asm/z80/rand.s @@ -0,0 +1,48 @@ + .module Random + + .area _BSS + +.randval: + .ds 0x02 + + .area _CODE + +_rand:: +_randw:: + + ld HL, (.randval) + ex DE, HL + ld L, E + + 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 + + RET + +_initrand:: ; Non banked +.initrand:: + ld (.randval), HL + 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.