Skip to content

Commit 5d4bfff

Browse files
committed
float.rndseedf() now takes float seed value and is consistent for all CBM compilation targets
1 parent 207cdaf commit 5d4bfff

File tree

12 files changed

+51
-77
lines changed

12 files changed

+51
-77
lines changed

compiler/res/prog8lib/c128/floats.p8

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ romsub $af4e = ABS() clobbers(A,X,Y) ; fac1 = ABS(fac1)
5757
romsub $af51 = SIGN() clobbers(X,Y) -> ubyte @ A ; SIGN(fac1) to A, $ff, $0, $1 for negative, zero, positive
5858
romsub $af54 = FCOMP(uword mflpt @ AY) clobbers(X,Y) -> ubyte @ A ; A = compare fac1 to mflpt in A/Y, 0=equal 1=fac1 is greater, 255=fac1 is less than
5959
romsub $af57 = RND_0() clobbers(A,X,Y) ; fac1 = RND(fac1) float random number generator
60+
romsub $af57 = RND() clobbers(A,X,Y) ; alias for RND_0
6061
romsub $af5a = CONUPK(uword mflpt @ AY) clobbers(A,X,Y) ; load mflpt value from memory in A/Y into fac2
6162
romsub $af5d = ROMUPK(uword mflpt @ AY) clobbers(A,X,Y) ; load mflpt value from memory in current bank in A/Y into fac2
6263
romsub $af60 = MOVFRM(uword mflpt @ AY) clobbers(A,X,Y) ; load mflpt value from memory in A/Y into fac1 (use MOVFM instead)
@@ -161,21 +162,6 @@ sub rndf() -> float {
161162
}}
162163
}
163164

164-
asmsub rndseedf(ubyte s1 @A, ubyte s2 @X, ubyte s3 @Y) clobbers(X) {
165-
%asm {{
166-
sta _tmpseed
167-
stx _tmpseed+1
168-
sty _tmpseed+2
169-
stx _tmpseed+3
170-
sty _tmpseed+4
171-
lda #<_tmpseed
172-
ldy #>_tmpseed
173-
jsr MOVFM
174-
lda #-1
175-
jmp RND_0
176-
_tmpseed .byte 0,0,0,0,0
177-
}}
178-
}
179165

180166
%asminclude "library:c128/floats.asm"
181167
%asminclude "library:c64/floats_funcs.asm"

compiler/res/prog8lib/c64/floats.p8

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,6 @@ sub rndf() -> float {
182182
}}
183183
}
184184

185-
asmsub rndseedf(ubyte s1 @A, ubyte s2 @X, ubyte s3 @Y) clobbers(X) {
186-
%asm {{
187-
pha
188-
tya
189-
ora #128 ; make sure the seed is negative
190-
tay
191-
pla
192-
jsr FREADS24AXY
193-
jmp RND
194-
}}
195-
}
196185

197186
%asminclude "library:c64/floats.asm"
198187
%asminclude "library:c64/floats_funcs.asm"

compiler/res/prog8lib/cx16/floats.p8

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ romsub $fe4e = ABS() clobbers(A,X,Y) ; fac1 = ABS(fac1)
5858
romsub $fe51 = SIGN() clobbers(X,Y) -> ubyte @ A ; SIGN(fac1) to A, $ff, $0, $1 for negative, zero, positive
5959
romsub $fe54 = FCOMP(uword mflpt @ AY) clobbers(X,Y) -> ubyte @ A ; A = compare fac1 to mflpt in A/Y, 0=equal 1=fac1 is greater, 255=fac1 is less than
6060
romsub $fe57 = RND_0() clobbers(A,X,Y) ; fac1 = RND(fac1) float random number generator NOTE: incompatible with C64's RND routine
61+
romsub $fe57 = RND() clobbers(A,X,Y) ; alias for RND_0
6162
romsub $fe5a = CONUPK(uword mflpt @ AY) clobbers(A,X,Y) ; load mflpt value from memory in A/Y into fac2
6263
romsub $fe5d = ROMUPK(uword mflpt @ AY) clobbers(A,X,Y) ; load mflpt value from memory in current bank in A/Y into fac2
6364
romsub $fe60 = MOVFRM(uword mflpt @ AY) clobbers(A,X,Y) ; load mflpt value from memory in A/Y into fac1 (use MOVFM instead)
@@ -160,20 +161,8 @@ sub rndf() -> float {
160161
}}
161162
}
162163

163-
asmsub rndseedf(ubyte s1 @A, ubyte s2 @X, ubyte s3 @Y) clobbers(X) {
164-
%asm {{
165-
sta P8ZP_SCRATCH_REG
166-
lda #0
167-
php
168-
lda P8ZP_SCRATCH_REG
169-
ora #32 ; not sure why this is needed but without it the seed is not consistent
170-
plp ; Z=N=0
171-
jmp floats.RND_0
172-
}}
173-
}
174-
175-
176164
%asminclude "library:c64/floats.asm"
177165
%asminclude "library:c64/floats_funcs.asm"
178166

167+
179168
}

compiler/res/prog8lib/floats_functions.p8

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,20 @@ sub ceil(float value) -> float {
221221
}}
222222
}
223223

224+
sub rndseedf(float seed) {
225+
if seed>0
226+
seed = -seed ; make sure fp seed is always negative
227+
228+
%asm {{
229+
stx floats_store_reg
230+
lda #<seed
231+
ldy #>seed
232+
jsr MOVFM ; load float into fac1
233+
lda #-1
234+
jsr floats.RND
235+
ldx floats_store_reg
236+
rts
237+
}}
238+
}
239+
224240
}

compiler/res/prog8lib/virtual/floats.p8

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,9 @@ sub rndf() -> float {
131131
}}
132132
}
133133

134-
sub rndseedf(ubyte s1, ubyte s2, ubyte s3) {
134+
sub rndseedf(float seed) {
135135
%ir {{
136-
loadm.b r65500,floats.rndseedf.s1
137-
loadm.b r65501,floats.rndseedf.s2
138-
loadm.b r65502,floats.rndseedf.s3
136+
loadm.f fr65500,floats.rndseedf.seed
139137
syscall 32
140138
}}
141139
}

compiler/src/prog8/compiler/astprocessing/AstChecker.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ internal class AstChecker(private val program: Program,
310310
}
311311
else if(param.second.registerOrPair in arrayOf(RegisterOrPair.AX, RegisterOrPair.AY, RegisterOrPair.XY)) {
312312
if (param.first.type != DataType.UWORD && param.first.type != DataType.WORD
313-
&& param.first.type != DataType.STR && param.first.type !in ArrayDatatypes && param.first.type != DataType.FLOAT)
313+
&& param.first.type != DataType.STR && param.first.type !in ArrayDatatypes)
314314
err("parameter '${param.first.name}' should be (u)word (an address) or str")
315315
}
316316
else if(param.second.statusflag!=null) {
@@ -325,7 +325,7 @@ internal class AstChecker(private val program: Program,
325325
}
326326
else if(pair.second.registerOrPair in setOf(RegisterOrPair.AX, RegisterOrPair.AY, RegisterOrPair.XY)) {
327327
if (pair.first != DataType.UWORD && pair.first != DataType.WORD
328-
&& pair.first != DataType.STR && pair.first !in ArrayDatatypes && pair.first != DataType.FLOAT)
328+
&& pair.first != DataType.STR && pair.first !in ArrayDatatypes)
329329
err("return type #${index + 1} should be (u)word/address")
330330
}
331331
else if(pair.second.statusflag!=null) {

docs/source/libraries.rst

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,8 @@ tan(x)
259259
rndf()
260260
returns the next random float between 0.0 and 1.0 from the Pseudo RNG sequence.
261261

262-
rndseedf(ubyte s1, ubyte s2, ubyte s3)
263-
Sets a new seed for the float pseudo-RNG sequence. The seed consists of a three byte value.
264-
Do not use zeros for the seed!
265-
266-
.. attention::
267-
The rndseedf and maybe the rndf routines may change a bit in the future.
262+
rndseedf(seed)
263+
Sets a new seed for the float pseudo-RNG sequence. Use a negative non-zero number as seed value.
268264

269265

270266
graphics

docs/source/syntaxreference.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,16 +648,17 @@ the statement body of such a subroutine should consist of just an inline assembl
648648

649649
The ``@ <register>`` part is required for rom and assembly-subroutines, as it specifies for the compiler
650650
what cpu registers should take the routine's arguments. You can use the regular set of registers
651-
(A, X, Y), the special 16-bit register pairs to take word values (AX, AY and XY) and even a processor status
651+
(A, X, Y), special 16-bit register pairs to take word values (AX, AY and XY) and even a processor status
652652
flag such as Carry (Pc).
653653

654+
It is not possible to use floating point arguments or return values in an asmsub.
655+
654656
.. note::
655657
Asmsubs can also be tagged as ``inline asmsub`` to make trivial pieces of assembly inserted
656658
directly instead of a call to them. Note that it is literal copy-paste of code that is done,
657659
so make sure the assembly is actually written to behave like such - which probably means you
658660
don't want a ``rts`` or ``jmp`` or ``bra`` in it!
659661

660-
661662
.. note::
662663
The 'virtual' 16-bit registers from the Commander X16 can also be specified as ``R0`` .. ``R15`` .
663664
This means you don't have to set them up manually before calling a subroutine that takes

docs/source/todo.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ TODO
33

44
For next release
55
^^^^^^^^^^^^^^^^
6+
- ir: register allocation per data type a specific allocation, so we are certain when a reg is used it's just for one specific datatype
67
- ir: write addresses as hex into p8ir file
78

89
...
@@ -19,7 +20,6 @@ Future Things and Ideas
1920
^^^^^^^^^^^^^^^^^^^^^^^
2021
Compiler:
2122

22-
- ir: register allocation per data type a specific allocation, so we are certain when a reg is used it's just for one specific datatype
2323
- create BSS section in output program and put StStaticVariables in there with bss=true. Don't forget to add init code to zero out everything that was put in bss. If array in bss->only zero ONCE! So requires self-modifying code
2424
- ir: mechanism to determine for chunks which registers are getting input values from "outside"
2525
- ir: mechanism to determine for chunks which registers are passing values out? (i.e. are used again in another chunk)

examples/test.p8

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
%import gfx2
21
%import textio
2+
%import floats
33
%zeropage basicsafe
44

55
main {
66
sub start() {
7-
gfx2.screen_mode(6)
8-
ubyte pix1 = gfx2.pget(162,120)
9-
gfx2.plot(162,120,7)
10-
ubyte pix2 = gfx2.pget(162,120)
11-
gfx2.plot(162,120,231)
12-
ubyte pix3 = gfx2.pget(162,120)
13-
ubyte pix4 = gfx2.pget(163,120)
14-
ubyte pix5 = gfx2.pget(162,121)
15-
sys.wait(20)
16-
gfx2.screen_mode(0)
17-
txt.print_ub(pix1)
7+
float f1
8+
9+
floats.rndseedf(-1.2345)
10+
txt.spc()
11+
floats.print_f(floats.rndf())
12+
txt.spc()
13+
floats.print_f(floats.rndf())
1814
txt.spc()
19-
txt.print_ub(pix2)
15+
floats.print_f(floats.rndf())
16+
txt.nl()
17+
18+
floats.rndseedf(1.2345)
2019
txt.spc()
21-
txt.print_ub(pix3)
20+
floats.print_f(floats.rndf())
2221
txt.spc()
23-
txt.print_ub(pix4)
22+
floats.print_f(floats.rndf())
2423
txt.spc()
25-
txt.print_ub(pix5)
24+
floats.print_f(floats.rndf())
2625
txt.nl()
2726
}
2827
}

0 commit comments

Comments
 (0)