Testing the new Fastspin Assembly, Spin, BASIC, and C for P1

2»

Comments

  • yetiyeti Posts: 602
    edited 2019-09-24 - 05:52:19
    FlexBASIC's "rnd(1)" yields floats.
    I'm wondering about...
    dim stdlib as class using "stdlib.h"
    
    for i=1 to 3
      print stdlib.rand()
    next i
    
    ...being "the right way" to get integer pseudorandom in FlexBASIC.
    Someone has a better idea?
    ◁ propeller-wiki ▷ ◁ FastSpin ▷ ◁ Help Spin at RosettaCode.org ▷ ◁ No Source – No Go! ▷ ◁ DK-E ▷ ◁ :-D ▷ ◁ Stay OmmmmmmPtimistic! ▷ ◁ Why Asimov's Laws of Robotics Don't Work ▷ ◁ DNA is a four letter word. ▷ ◁ Stop slavery! Free all mitochondria! ▷
  • yeti wrote: »
    FlexBASIC's "rnd(1)" yields floats.
    I'm wondering about...
    dim stdlib as class using "stdlib.h"
    
    for i=1 to 3
      print stdlib.rand()
    next i
    
    ...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:
    x = INT(RND(1) * r) + c
    
    to give a random integer between c and r+c-1.

    Worth a try?
  • yetiyeti Posts: 602
    edited 2019-09-24 - 16:33:15
    AJL wrote: »
    I'm not familiar enough with FlexBASIC to know if this will work, but in Applesoft we used to do things like:
    x = INT(RND(1) * r) + c
    
    to give a random integer between c and r+c-1.

    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.
    $ 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
    
    ◁ propeller-wiki ▷ ◁ FastSpin ▷ ◁ Help Spin at RosettaCode.org ▷ ◁ No Source – No Go! ▷ ◁ DK-E ▷ ◁ :-D ▷ ◁ Stay OmmmmmmPtimistic! ▷ ◁ Why Asimov's Laws of Robotics Don't Work ▷ ◁ DNA is a four letter word. ▷ ◁ Stop slavery! Free all mitochondria! ▷
  • yetiyeti Posts: 602
    edited 2019-09-24 - 15:23:40
    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.
    ◁ propeller-wiki ▷ ◁ FastSpin ▷ ◁ Help Spin at RosettaCode.org ▷ ◁ No Source – No Go! ▷ ◁ DK-E ▷ ◁ :-D ▷ ◁ Stay OmmmmmmPtimistic! ▷ ◁ Why Asimov's Laws of Robotics Don't Work ▷ ◁ DNA is a four letter word. ▷ ◁ Stop slavery! Free all mitochondria! ▷
  • yetiyeti Posts: 602
    edited 2019-09-24 - 18:40:17
    $ cat minimal-using-stdio.spin
    obj
      stdio : "stdio.h"
    
    pub main
      stdio.puts(string("Hello, Polyglot Propeller Compiler Collection!"))
    
    $ fastspin -O2 minimal-using-stdio.spin 
    Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
    Version 3.9.33 Compiled on: Sep 23 2019
    minimal-using-stdio.spin
    |-stdio.h
    fputs.c
    ftab.c
    minimal-using-stdio.pasm
    Done.
    Program size is 1324 bytes
    
    $ spinsim -b minimal-using-stdio.binary 
    Hello, Polyglot Propeller Compiler Collection!
    
    Oiski!
    This is like coding using wormholes!
    ◁ propeller-wiki ▷ ◁ FastSpin ▷ ◁ Help Spin at RosettaCode.org ▷ ◁ No Source – No Go! ▷ ◁ DK-E ▷ ◁ :-D ▷ ◁ Stay OmmmmmmPtimistic! ▷ ◁ Why Asimov's Laws of Robotics Don't Work ▷ ◁ DNA is a four letter word. ▷ ◁ Stop slavery! Free all mitochondria! ▷
  • Using (own) Spin code in BASIC programs:

    "mandelbrot-s8p24.hacks.spin" contains simple fixpoint multiplication helpers. These were easier to write in Spin than in BASIC because of Spin's "**" operation.
    pub mul(x,y)
      return (x**y)<<8 | (x*y)>>24
    
    pub multimes2(x,y)
      return (x**y)<<9 | (x*y)>>23
    
    That's included by the BASIC main program "mandelbrot-s8p24.bas" by these lines:
    class hacks using "mandelbrot-s8p24.hacks.spin"
    dim hack as hacks
    ' hack.mul(x,y) does s8p24 fp multiplication
    ' hack.multimes2(x,y) does mul(x,y)*2
    
    And running the compiled result shows the hacky multiplication really does work as expected:
    !!!!!!!!!!!!!!!"""""""""""""####################################""""""""""""""""
    !!!!!!!!!!!!!"""""""""#######################$$$$$$$%'0(%%%$$$$$#####"""""""""""
    !!!!!!!!!!!"""""""#######################$$$$$$$$%%%&&(++)++&$$$$$$$######""""""
    !!!!!!!!!"""""#######################$$$$$$$$$$%%%%&')* ;/*('&%%$$$$$$#######"""
    !!!!!!!!""""#####################$$$$$$$$$$%%%&&&''),       +'&%%%%%$$$$########
    !!!!!!!"""####################$$$$$$$$%%%&'())((())*-      .+))('&&&&+&%$$######
    !!!!!!""###################$$$$$%%%%%%&&&'+.   08               /+, // )%%$#####
    !!!!!"################$$$%%%%%%%%%%&&&&')-+7                         4(&&%$$####
    !!!!"##########$$$$$%%&(,('''''''''''((*-5                           3+)4&%$$###
    !!!!####$$$$$$$$%%%%%&'(*- 1.+/ -4+))**                               3+'&%$$$##
    !!!!#$$$$$$$$$%%%%%%'''++.7         9/0                               <6'%%$$$$#
    !!!#$$$$$$$%&&&&''().-2.6             >                                '&%%$$$$#
    !!!379<                                                            2+)'&&%%$$$$#
    !!!#$$$$$$$%&&&&''().-2.6             >                                '&%%$$$$#
    !!!!#$$$$$$$$$%%%%%%'''++.7         9/0                               <6'%%$$$$#
    !!!!####$$$$$$$$%%%%%&'(*- 1.+/ -4+))**                               3+'&%$$$##
    !!!!"##########$$$$$%%&(,('''''''''''((*-5                           3+)4&%$$###
    !!!!!"################$$$%%%%%%%%%%&&&&')-+7                         4(&&%$$####
    !!!!!!""###################$$$$$%%%%%%&&&'+.   08               /+, // )%%$#####
    !!!!!!!"""####################$$$$$$$$%%%&'())((())*-      .+))('&&&&+&%$$######
    !!!!!!!!""""#####################$$$$$$$$$$%%%&&&''),       +'&%%%%%$$$$########
    !!!!!!!!!"""""#######################$$$$$$$$$$%%%%&')* ;/*('&%%$$$$$$#######"""
    !!!!!!!!!!!"""""""#######################$$$$$$$$%%%&&(++)++&$$$$$$$######""""""
    !!!!!!!!!!!!!"""""""""#######################$$$$$$$%'0(%%%$$$$$#####"""""""""""
    !!!!!!!!!!!!!!!"""""""""""""####################################""""""""""""""""
    

    ◁ propeller-wiki ▷ ◁ FastSpin ▷ ◁ Help Spin at RosettaCode.org ▷ ◁ No Source – No Go! ▷ ◁ DK-E ▷ ◁ :-D ▷ ◁ Stay OmmmmmmPtimistic! ▷ ◁ Why Asimov's Laws of Robotics Don't Work ▷ ◁ DNA is a four letter word. ▷ ◁ Stop slavery! Free all mitochondria! ▷
  • yetiyeti Posts: 602
    edited 2019-10-16 - 03:59:41
    FlexBASIC and PEEK/POKE...

    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.

    a T d H v A a N n K c S e !

    ◁ propeller-wiki ▷ ◁ FastSpin ▷ ◁ Help Spin at RosettaCode.org ▷ ◁ No Source – No Go! ▷ ◁ DK-E ▷ ◁ :-D ▷ ◁ Stay OmmmmmmPtimistic! ▷ ◁ Why Asimov's Laws of Robotics Don't Work ▷ ◁ DNA is a four letter word. ▷ ◁ Stop slavery! Free all mitochondria! ▷
  • 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!
  • yetiyeti Posts: 602
    edited 2019-10-03 - 09:03:36
    VonSzarvas wrote: »
    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 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.
    ◁ propeller-wiki ▷ ◁ FastSpin ▷ ◁ Help Spin at RosettaCode.org ▷ ◁ No Source – No Go! ▷ ◁ DK-E ▷ ◁ :-D ▷ ◁ Stay OmmmmmmPtimistic! ▷ ◁ Why Asimov's Laws of Robotics Don't Work ▷ ◁ DNA is a four letter word. ▷ ◁ Stop slavery! Free all mitochondria! ▷
  • tritonium wrote: »
    No kidding; where do you guys find the time????
    This has an answer now... somehow...
    —▷ 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.
    ◁ propeller-wiki ▷ ◁ FastSpin ▷ ◁ Help Spin at RosettaCode.org ▷ ◁ No Source – No Go! ▷ ◁ DK-E ▷ ◁ :-D ▷ ◁ Stay OmmmmmmPtimistic! ▷ ◁ Why Asimov's Laws of Robotics Don't Work ▷ ◁ DNA is a four letter word. ▷ ◁ Stop slavery! Free all mitochondria! ▷
  • yetiyeti Posts: 602
    edited 2019-10-05 - 20:43:26
    One of my to do list items collecting dust for far to long now: Test calling an object compiled by FastSpin in an OpenSpin program.

    See Fastspin.md#spin-wrappers for details.
    $ 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
    
    $ spinsim -b hybrid-tx-test.binary 
    Hello Spin!
    Hello Spin!
    Hello Spin!
    

    --(to do list length) ... DONE! \o/
    ◁ propeller-wiki ▷ ◁ FastSpin ▷ ◁ Help Spin at RosettaCode.org ▷ ◁ No Source – No Go! ▷ ◁ DK-E ▷ ◁ :-D ▷ ◁ Stay OmmmmmmPtimistic! ▷ ◁ Why Asimov's Laws of Robotics Don't Work ▷ ◁ DNA is a four letter word. ▷ ◁ Stop slavery! Free all mitochondria! ▷
Sign In or Register to comment.