Skip to content

Commit 66d6100

Browse files
committed
refact: improve rand routine
Instead of geting every byte from the lower byte of each 32 bit numbers (which require 4 loops), use just the 32 bit directly. Note this might reduce randomness and might be crypthograpycally less secure... (which for the Z80 is of little importance... I guess).
1 parent 905a5b9 commit 66d6100

4 files changed

Lines changed: 4 additions & 30 deletions

File tree

src/lib/arch/zx48k/runtime/random.asm

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,14 @@ TAKE_FRAMES:
3131
FRAMES EQU 23672
3232
ENDP
3333

34-
RANDOM_SEED_HIGH EQU RAND+6 ; RANDOM seed, 16 higher bits
34+
RANDOM_SEED_HIGH EQU RAND+1 ; RANDOM seed, 16 higher bits
3535
RANDOM_SEED_LOW EQU 23670 ; RANDOM seed, 16 lower bits
3636

3737

3838
RAND:
3939
PROC
40-
LOCAL RAND_LOOP
41-
ld b, 4
42-
RAND_LOOP:
43-
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
4440
ld de,0C0DEh ; yw -> zt
41+
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
4542
ld (RANDOM_SEED_LOW),de ; x = y, z = w
4643
ld a,e ; w = w ^ ( w << 3 )
4744
add a,a
@@ -59,14 +56,6 @@ RAND_LOOP:
5956
ld d,l ; y = z
6057
ld e,a ; w = t
6158
ld (RANDOM_SEED_HIGH),de
62-
push af
63-
djnz RAND_LOOP
64-
pop de
65-
pop af
66-
ld e, a
67-
pop hl
68-
pop af
69-
ld l, a
7059
ret
7160
ENDP
7261

src/lib/arch/zx48k/stdlib/random.bas

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ Function fastcall randomLimit(limit as uByte) as uByte
4141
rr c
4242
jp 1b ; loop back until we find a bit.
4343
2:
44-
push bc
4544
call RAND
46-
pop bc
4745
and c
4846
cp b
4947
ret z

src/lib/arch/zxnext/runtime/random.asm

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,14 @@ TAKE_FRAMES:
3131
FRAMES EQU 23672
3232
ENDP
3333

34-
RANDOM_SEED_HIGH EQU RAND+6 ; RANDOM seed, 16 higher bits
34+
RANDOM_SEED_HIGH EQU RAND+1 ; RANDOM seed, 16 higher bits
3535
RANDOM_SEED_LOW EQU 23670 ; RANDOM seed, 16 lower bits
3636

3737

3838
RAND:
3939
PROC
40-
LOCAL RAND_LOOP
41-
ld b, 4
42-
RAND_LOOP:
43-
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
4440
ld de,0C0DEh ; yw -> zt
41+
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
4542
ld (RANDOM_SEED_LOW),de ; x = y, z = w
4643
ld a,e ; w = w ^ ( w << 3 )
4744
add a,a
@@ -59,14 +56,6 @@ RAND_LOOP:
5956
ld d,l ; y = z
6057
ld e,a ; w = t
6158
ld (RANDOM_SEED_HIGH),de
62-
push af
63-
djnz RAND_LOOP
64-
pop de
65-
pop af
66-
ld e, a
67-
pop hl
68-
pop af
69-
ld l, a
7059
ret
7160
ENDP
7261

src/lib/arch/zxnext/stdlib/random.bas

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ Function fastcall randomLimit(limit as uByte) as uByte
4141
rr c
4242
jp 1b ; loop back until we find a bit.
4343
2:
44-
push bc
4544
call RAND
46-
pop bc
4745
and c
4846
cp b
4947
ret z

0 commit comments

Comments
 (0)