But I see two reasons not to want floats: Speed and program size.
$ cat prng-float.bas
dim as integer s,t
s=0 : t=CNT
for i=1 to 1000 : s=s+rnd(1) : next i
t=CNT-t
print "float prng",t/1000;" ticks per prng"
$ fastspin -O2 prng-float.bas
Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
Version 3.9.33 Compiled on: Sep 23 2019
prng-float.bas
random.c
fmt.c
strlen.c
prng-float.pasm
Done.
Program size is 4108 bytes
$ spinsim -b prng-float.binary
float prng 14936 ticks per prng
$ cat prng-int.bas
dim stdlib as class using "stdlib.h"
dim as integer s,t
s=0 : t=CNT
for i=1 to 1000 : s=s+stdlib.rand() : next i
t=CNT-t
print "int prng",t/1000;" ticks per prng"
$ fastspin -O2 prng-int.bas
Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
Version 3.9.33 Compiled on: Sep 23 2019
prng-int.bas
|-stdlib.h
rand.c
fmt.c
strlen.c
prng-int.pasm
Done.
Program size is 3184 bytes
$ spinsim -b prng-int.binary
int prng 1681 ticks per prng
Testing whether it makes a difference to only include "rand.c":
$ cat prng-int.bas
dim rand as class using "libc/stdlib/rand.c"
dim as integer s,t
s=0 : t=CNT
for i=1 to 1000 : s=s+rand.rand() : next i
t=CNT-t
print "int prng",t/1000;" ticks per prng"
$ fastspin -O2 prng-int.bas
Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
Version 3.9.33 Compiled on: Sep 23 2019
prng-int.bas
|-rand.c
fmt.c
strlen.c
prng-int.pasm
Done.
Program size is 3184 bytes
$ spinsim -b prng-int.binary
int prng 1681 ticks per prng
Same size, same speed.
Everything else would have surprised or even confused me.
"mandelbrot-s8p24.hacks.spin" contains simple fixpoint multiplication helpers. These were easier to write in Spin than in BASIC because of Spin's "**" operation.
Thinking about PEEK and POKE from the good olden BASIC days and looking at FlexBASIC's "cast" instruction, implementing classic PEEK and POKE seem to get a competitor by simply overlaying the whole hub RAM with a byte array.
File "cast.bas":
''
'' create a "byte pointer" pointing to the hub's start
''
var mem8 = cast(byte ptr,0)
''
'' test it
''
dim as string Question$
Question$ = "Surprise?"
'' check "?" being where expected.
'' just to demonstrate reading mem8()
''
if mem8(cast(byte ptr,Question$) + 8) == asc("?") then
print Question$
mem8(cast(byte ptr,Question$) + 8) = 33
print Question$
else
print "Strange things are happening!"
end if
$ fastspin cast.bas -o cast.bas.binary
Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
Version 3.9.34-beta-7d815c51 Compiled on: Sep 28 2019
cast.bas
fmt.c
strlen.c
cast.bas.pasm
Done.
Program size is 2032 bytes
$ spinsim -b cast.bas.binary
Surprise?
Surprise!
Modern BASIC starts to feel like C... \o/
I noticed the forum software crippling the code. This needs to be fixed where it is caused, not in all our code blocks.
Adding a space before the bracket seems to fix it for now.
I've forwarded this example to the forum code master; maybe he has a better idea!
Thanks!
Various amounts of "@"s cause even bigger problems. Try a line with multiple "@", "@@" and "@@@" and stuff in between. Then some of the line's contents silently vanishes. Even outside of code boxes! Compare the visible text of this message with its source.
I'll not change the contents of the code box now, the whole example is attached as file too. That will help until a solution is found. Maybe the authors of this forum software already have a solution? This will be not the only installation where code boxes are needed, so it should not only affect us.
Keeping code, scripts and notes together helped a lot. Comments and documentation still don't write themselves but it can grow easier that way than with having X files distributed in Y directories and losing overview.
$ cat hybrid-tx.spin
''
'' tx only uart - pin and baud hardcoded
''
CON
txpin = 30
baud = 115200
txmask = 1 << txpin
VAR
long t_fullbit
PUB start
t_fullbit := clkfreq / baud
OUTA |= txmask ' line idle = 1
DIRA |= txmask ' set for output
PUB stop ' to complete the start/stop duality
PUB tx(c) | t_next
c := (c | 256) << 1 ' 1_dddddddd_0
t_next := CNT ' remember now
repeat 10
if c & 1
OUTA |= txmask
else
OUTA &= !txmask
c >>= 1
waitcnt(t_next += t_fullbit) ' until old t_next plus a bit's duration
$ fastspin -O2 -w hybrid-tx.spin
Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
Version 4.0.0-beta-317bfbe5 Compiled on: Oct 5 2019
hybrid-tx.spin
$ ls -l hybrid-tx.cog.spin
-rw-r--r-- 1 yeti yeti 6680 Oct 5 02:28 hybrid-tx.cog.spin
$ cat hybrid-tx-test.spin
con
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
obj
out : "hybrid-tx.cog.spin"
pub main
out.__cognew
out.start
repeat 3
str(string("Hello Spin!", 13, 10))
out.__cogstop
pub str(s) | c
repeat while c := byte[s++]
out.tx(c)
$ openspin -u hybrid-tx-test.spin
Propeller Spin/PASM Compiler 'OpenSpin' (c)2012-2018 Parallax Inc. DBA Parallax Semiconductor.
Version 1.00.81 Compiled on Jul 15 2019 17:50:54
Compiling...
hybrid-tx-test.spin
|-hybrid-tx.cog.spin
Done.
Unused Method Elimination:
2 methods removed
0 objects removed
28 bytes saved
--------------------------
Program size is 656 bytes
Comments
I'm wondering about... ...being "the right way" to get integer pseudorandom in FlexBASIC.
Someone has a better idea?
I'm not familiar enough with FlexBASIC to know if this will work, but in Applesoft we used to do things like:
to give a random integer between c and r+c-1.
Worth a try?
Thats still the official example in the FlexDOCs: .../doc/basic.md#rnd
But I see two reasons not to want floats: Speed and program size.
Everything else would have surprised or even confused me.
This is like coding using wormholes!
"mandelbrot-s8p24.hacks.spin" contains simple fixpoint multiplication helpers. These were easier to write in Spin than in BASIC because of Spin's "**" operation. That's included by the BASIC main program "mandelbrot-s8p24.bas" by these lines: And running the compiled result shows the hacky multiplication really does work as expected:
Thinking about PEEK and POKE from the good olden BASIC days and looking at FlexBASIC's "cast" instruction, implementing classic PEEK and POKE seem to get a competitor by simply overlaying the whole hub RAM with a byte array.
File "cast.bas": Modern BASIC starts to feel like C... \o/
I noticed the forum software crippling the code. This needs to be fixed where it is caused, not in all our code blocks.
a T d H v A a N n K c S e !
I've forwarded this example to the forum code master; maybe he has a better idea!
Various amounts of "@"s cause even bigger problems. Try a line with multiple "@", "@@" and "@@@" and stuff in between. Then some of the line's contents silently vanishes. Even outside of code boxes! Compare the visible text of this message with its source.
I'll not change the contents of the code box now, the whole example is attached as file too. That will help until a solution is found. Maybe the authors of this forum software already have a solution? This will be not the only installation where code boxes are needed, so it should not only affect us.
—▷ https://forums.parallax.com/discussion/comment/1479243/#Comment_1479243
Keeping code, scripts and notes together helped a lot. Comments and documentation still don't write themselves but it can grow easier that way than with having X files distributed in Y directories and losing overview.
See Fastspin.md#spin-wrappers for details.
--(to do list length) ... DONE! \o/
Some not completely random lines of C code:
...compiled to FlexSpin-LMM and -ByteCode:
...and as expected both yield the same output...
...to be continued?