From 9120e1de888212a17d5ca946ea94cc836dd2ddad Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 8 Mar 2021 23:21:52 +0100 Subject: [PATCH] fix ubyte/uword to float conversion crashes on Commander X16 --- compiler/res/prog8lib/cx16/floats.p8 | 13 +++++- examples/test.p8 | 70 ++++++++-------------------- 2 files changed, 31 insertions(+), 52 deletions(-) diff --git a/compiler/res/prog8lib/cx16/floats.p8 b/compiler/res/prog8lib/cx16/floats.p8 index bcd521897..2c84649d9 100644 --- a/compiler/res/prog8lib/cx16/floats.p8 +++ b/compiler/res/prog8lib/cx16/floats.p8 @@ -79,10 +79,10 @@ asmsub GIVUAYFAY (uword value @ AY) clobbers(A,X,Y) { ; ---- unsigned 16 bit word in A/Y (lo/hi) to fac1 %asm {{ phx - sta P8ZP_SCRATCH_W2 + sta _tmp sty P8ZP_SCRATCH_B1 tya - ldy P8ZP_SCRATCH_W2 + ldy _tmp jsr GIVAYF ; load it as signed... correct afterwards lda P8ZP_SCRATCH_B1 bpl + @@ -91,6 +91,7 @@ asmsub GIVUAYFAY (uword value @ AY) clobbers(A,X,Y) { jsr FADD + plx rts +_tmp .byte 0 _flt65536 .byte 145,0,0,0,0 ; 65536.0 }} } @@ -128,6 +129,14 @@ asmsub GETADRAY () clobbers(X) -> uword @ AY { }} } +asmsub FREADUY (ubyte value @Y) { + ; -- 8 bit unsigned Y -> float in fac1 + %asm {{ + lda #0 + jmp GIVAYF + }} +} + sub print_f (float value) { ; ---- prints the floating point value (without a newline). %asm {{ diff --git a/examples/test.p8 b/examples/test.p8 index d9f735b43..eea8e3969 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,67 +3,37 @@ %import test_stack %zeropage dontuse -; TODO fix float conversion crashes on Cx16 (ubyte as float, uword as float) main { sub start() { - uword total=0 uword xx float fl float fltotal=0.0 ubyte ub = 22 - for xx in 0 to 100 { - txt.print_uw(xx*xx) - txt.chrout(',') - } + ub = 22 + fl = ub as float + floats.print_f(fl) + txt.nl() + ub = 255 + fl = ub as float + floats.print_f(fl) + txt.nl() + xx = 123 + fl = xx as float + floats.print_f(fl) + txt.nl() + xx = 55555 + fl = xx as float + floats.print_f(fl) txt.nl() - total = 0 - c64.SETTIM(0,0,0) - repeat 5 { - for xx in 1 to 255 { - total += xx*xx - } + fltotal=0.0 + for xx in 1 to 255 { + fl = xx as float + fltotal = fl * fl } - txt.print_uw(total) - txt.nl() - txt.print_uw(c64.RDTIM16()) - txt.nl() - txt.nl() + test_stack.test() - -; fltotal=0.0 -; c64.SETTIM(0,0,0) -; repeat 5 { -; for xx in 1 to 255 { -; fl = xx as float -; ; fl = ub as float -; fltotal = fl * fl -; } -; } -; -; floats.print_f(fltotal) -; txt.nl() -; txt.print_uw(c64.RDTIM16()) -; txt.nl() -; txt.nl() -; -; fltotal=0.0 -; c64.SETTIM(0,0,0) -; repeat 5 { -; for xx in 1 to 255 { -; fl = xx as float -; ; fl = ub as float -; fltotal = fl ** 2 -; } -; } -; -; floats.print_f(fltotal) -; txt.nl() -; txt.print_uw(c64.RDTIM16()) -; txt.nl() -; txt.nl() - } }