Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions src/lib/arch/zx48k/runtime/random.asm
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ TAKE_FRAMES:
FRAMES EQU 23672
ENDP

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


RAND:
PROC
LOCAL RAND_LOOP
ld b, 4
RAND_LOOP:
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
ld de,0C0DEh ; yw -> zt
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
ld (RANDOM_SEED_LOW),de ; x = y, z = w
ld a,e ; w = w ^ ( w << 3 )
add a,a
Expand All @@ -59,14 +56,6 @@ RAND_LOOP:
ld d,l ; y = z
ld e,a ; w = t
ld (RANDOM_SEED_HIGH),de
push af
djnz RAND_LOOP
pop de
pop af
ld e, a
pop hl
pop af
ld l, a
ret
ENDP

Expand Down
55 changes: 55 additions & 0 deletions src/lib/arch/zx48k/stdlib/random.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
' ----------------------------------------------------------------
' This file is released under the MIT License
'
' Copyleft (k) 2025
' by Jose Rodriguez-Rosa (a.k.a. Boriel) <http://www.boriel.com>
' ----------------------------------------------------------------

#pragma once
#pragma push(case_insensitive)
#pragma case_insensitive = TRUE

function fastcall randInt() as ULong ' Returns a random ULong
asm
push namespace core
call RAND
pop namespace
end asm
end function


Function fastcall randFixed() as Fixed ' Returns a random Fixed
Asm
push namespace core
call RAND
pop namespace
End Asm
End Function


Function fastcall randomLimit(limit as uByte) as uByte
Asm
push namespace core
PROC
and a
ret z ; Input zero, output zero.
ld b, a ; Save A
ld c,255
1:
rla
jr c, 2f
rr c
jp 1b ; loop back until we find a bit.
2:
call RAND
and c
cp b
ret z
jr nc, 2b
ENDP
pop namespace
End Asm
End Function

#pragma pop(case_insensitive)
#require "random.asm"
15 changes: 2 additions & 13 deletions src/lib/arch/zxnext/runtime/random.asm
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ TAKE_FRAMES:
FRAMES EQU 23672
ENDP

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


RAND:
PROC
LOCAL RAND_LOOP
ld b, 4
RAND_LOOP:
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
ld de,0C0DEh ; yw -> zt
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
ld (RANDOM_SEED_LOW),de ; x = y, z = w
ld a,e ; w = w ^ ( w << 3 )
add a,a
Expand All @@ -59,14 +56,6 @@ RAND_LOOP:
ld d,l ; y = z
ld e,a ; w = t
ld (RANDOM_SEED_HIGH),de
push af
djnz RAND_LOOP
pop de
pop af
ld e, a
pop hl
pop af
ld l, a
ret
ENDP

Expand Down
55 changes: 55 additions & 0 deletions src/lib/arch/zxnext/stdlib/random.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
' ----------------------------------------------------------------
' This file is released under the MIT License
'
' Copyleft (k) 2025
' by Jose Rodriguez-Rosa (a.k.a. Boriel) <http://www.boriel.com>
' ----------------------------------------------------------------

#pragma once
#pragma push(case_insensitive)
#pragma case_insensitive = TRUE

function fastcall randInt() as ULong ' Returns a random ULong
asm
push namespace core
call RAND
pop namespace
end asm
end function


Function fastcall randFixed() as Fixed ' Returns a random Fixed
Asm
push namespace core
call RAND
pop namespace
End Asm
End Function


Function fastcall randomLimit(limit as uByte) as uByte
Asm
push namespace core
PROC
and a
ret z ; Input zero, output zero.
ld b, a ; Save A
ld c,255
1:
rla
jr c, 2f
rr c
jp 1b ; loop back until we find a bit.
2:
call RAND
and c
cp b
ret z
jr nc, 2b
ENDP
pop namespace
End Asm
End Function

#pragma pop(case_insensitive)
#require "random.asm"
15 changes: 2 additions & 13 deletions tests/functional/arch/zx48k/mcleod.asm
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,12 @@ TAKE_FRAMES:
ret
FRAMES EQU 23672
ENDP
RANDOM_SEED_HIGH EQU RAND+6 ; RANDOM seed, 16 higher bits
RANDOM_SEED_HIGH EQU RAND+1 ; RANDOM seed, 16 higher bits
RANDOM_SEED_LOW EQU 23670 ; RANDOM seed, 16 lower bits
RAND:
PROC
LOCAL RAND_LOOP
ld b, 4
RAND_LOOP:
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
ld de,0C0DEh ; yw -> zt
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
ld (RANDOM_SEED_LOW),de ; x = y, z = w
ld a,e ; w = w ^ ( w << 3 )
add a,a
Expand All @@ -258,14 +255,6 @@ RAND_LOOP:
ld d,l ; y = z
ld e,a ; w = t
ld (RANDOM_SEED_HIGH),de
push af
djnz RAND_LOOP
pop de
pop af
ld e, a
pop hl
pop af
ld l, a
ret
ENDP
RND:
Expand Down
47 changes: 18 additions & 29 deletions tests/functional/arch/zx48k/opt4_keepix.asm
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ _test__leave:
pop ix
ret
;; --- end of user code ---
#line 1 "/zxbasic/src/arch/zx48k/library-asm/addf.asm"
#line 1 "/zxbasic/src/arch/zx48k/library-asm/stackf.asm"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/arith/addf.asm"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/stackf.asm"
; -------------------------------------------------------------
; Functions to manage FP-Stack of the ZX Spectrum ROM CALC
; -------------------------------------------------------------
Expand Down Expand Up @@ -111,7 +111,7 @@ __FPSTACK_I16: ; Pushes 16 bits integer in HL into the FP ROM STACK
ld b, a
jp __FPSTACK_PUSH
pop namespace
#line 2 "/zxbasic/src/arch/zx48k/library-asm/addf.asm"
#line 2 "/zxbasic/src/lib/arch/zx48k/runtime/arith/addf.asm"
; -------------------------------------------------------------
; Floating point library using the FP ROM Calculator (ZX 48K)
; All of them uses A EDCB registers as 1st paramter.
Expand All @@ -129,8 +129,8 @@ __ADDF: ; Addition
defb 38h; ; END CALC
jp __FPSTACK_POP
pop namespace
#line 53 "opt4_keepix.bas"
#line 1 "/zxbasic/src/arch/zx48k/library-asm/mulf.asm"
#line 53 "arch/zx48k/opt4_keepix.bas"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/arith/mulf.asm"
; -------------------------------------------------------------
; Floating point library using the FP ROM Calculator (ZX 48K)
; All of them uses A EDCB registers as 1st paramter.
Expand All @@ -148,13 +148,13 @@ __MULF: ; Multiplication
defb 38h; ; END CALC
jp __FPSTACK_POP
pop namespace
#line 54 "opt4_keepix.bas"
#line 1 "/zxbasic/src/arch/zx48k/library-asm/ploadf.asm"
#line 54 "arch/zx48k/opt4_keepix.bas"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/ploadf.asm"
; Parameter / Local var load
; A => Offset
; IX = Stack Frame
; RESULT: HL => IX + DE
#line 1 "/zxbasic/src/arch/zx48k/library-asm/iloadf.asm"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/iloadf.asm"
; __FASTCALL__ routine which
; loads a 40 bits floating point into A ED CB
; stored at position pointed by POINTER HL
Expand All @@ -181,21 +181,21 @@ __LOADF: ; Loads a 40 bits FP number from address pointed by HL
ld b, (hl)
ret
pop namespace
#line 7 "/zxbasic/src/arch/zx48k/library-asm/ploadf.asm"
#line 7 "/zxbasic/src/lib/arch/zx48k/runtime/ploadf.asm"
push namespace core
__PLOADF:
push ix
pop hl
add hl, de
jp __LOADF
pop namespace
#line 55 "opt4_keepix.bas"
#line 1 "/zxbasic/src/arch/zx48k/library-asm/pstoref.asm"
#line 55 "arch/zx48k/opt4_keepix.bas"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/pstoref.asm"
; Stores FP number in A ED CB at location HL+IX
; HL = Offset
; IX = Stack Frame
; A ED CB = FP Number
#line 1 "/zxbasic/src/arch/zx48k/library-asm/storef.asm"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/storef.asm"
push namespace core
__PISTOREF: ; Indect Stores a float (A, E, D, C, B) at location stored in memory, pointed by (IX + HL)
push de
Expand Down Expand Up @@ -223,7 +223,7 @@ __STOREF: ; Stores the given FP number in A EDCB at address HL
ld (hl), b
ret
pop namespace
#line 7 "/zxbasic/src/arch/zx48k/library-asm/pstoref.asm"
#line 7 "/zxbasic/src/lib/arch/zx48k/runtime/pstoref.asm"
; Stored a float number in A ED CB into the address pointed by IX + HL
push namespace core
__PSTOREF:
Expand All @@ -235,8 +235,8 @@ __PSTOREF:
pop de
jp __STOREF
pop namespace
#line 56 "opt4_keepix.bas"
#line 1 "/zxbasic/src/arch/zx48k/library-asm/random.asm"
#line 56 "arch/zx48k/opt4_keepix.bas"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/random.asm"
; RANDOM functions
push namespace core
RANDOMIZE:
Expand All @@ -262,15 +262,12 @@ TAKE_FRAMES:
ret
FRAMES EQU 23672
ENDP
RANDOM_SEED_HIGH EQU RAND+6 ; RANDOM seed, 16 higher bits
RANDOM_SEED_HIGH EQU RAND+1 ; RANDOM seed, 16 higher bits
RANDOM_SEED_LOW EQU 23670 ; RANDOM seed, 16 lower bits
RAND:
PROC
LOCAL RAND_LOOP
ld b, 4
RAND_LOOP:
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
ld de,0C0DEh ; yw -> zt
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
ld (RANDOM_SEED_LOW),de ; x = y, z = w
ld a,e ; w = w ^ ( w << 3 )
add a,a
Expand All @@ -288,14 +285,6 @@ RAND_LOOP:
ld d,l ; y = z
ld e,a ; w = t
ld (RANDOM_SEED_HIGH),de
push af
djnz RAND_LOOP
pop de
pop af
ld e, a
pop hl
pop af
ld l, a
ret
ENDP
RND:
Expand Down Expand Up @@ -336,5 +325,5 @@ RND_LOOP:
ret
ENDP
pop namespace
#line 57 "opt4_keepix.bas"
#line 57 "arch/zx48k/opt4_keepix.bas"
END
19 changes: 4 additions & 15 deletions tests/functional/arch/zx48k/randomize.asm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
ei
ret
;; --- end of user code ---
#line 1 "/zxbasic/src/arch/zx48k/library-asm/random.asm"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/random.asm"
; RANDOM functions
push namespace core
RANDOMIZE:
Expand All @@ -67,15 +67,12 @@ TAKE_FRAMES:
ret
FRAMES EQU 23672
ENDP
RANDOM_SEED_HIGH EQU RAND+6 ; RANDOM seed, 16 higher bits
RANDOM_SEED_HIGH EQU RAND+1 ; RANDOM seed, 16 higher bits
RANDOM_SEED_LOW EQU 23670 ; RANDOM seed, 16 lower bits
RAND:
PROC
LOCAL RAND_LOOP
ld b, 4
RAND_LOOP:
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
ld de,0C0DEh ; yw -> zt
ld hl,(RANDOM_SEED_LOW) ; xz -> yw
ld (RANDOM_SEED_LOW),de ; x = y, z = w
ld a,e ; w = w ^ ( w << 3 )
add a,a
Expand All @@ -93,14 +90,6 @@ RAND_LOOP:
ld d,l ; y = z
ld e,a ; w = t
ld (RANDOM_SEED_HIGH),de
push af
djnz RAND_LOOP
pop de
pop af
ld e, a
pop hl
pop af
ld l, a
ret
ENDP
RND:
Expand Down Expand Up @@ -141,5 +130,5 @@ RND_LOOP:
ret
ENDP
pop namespace
#line 23 "randomize.bas"
#line 23 "arch/zx48k/randomize.bas"
END
Loading
Loading