Shop OBEX P1 Docs P2 Docs Learn Events
Testing the new Fastspin Assembly, Spin, BASIC, and C for P1 - Page 2 — Parallax Forums

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

2»

Comments

  • yetiyeti Posts: 818
    edited 2019-09-24 05:52
    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?
  • 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: 818
    edited 2019-09-24 16:33
    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
    
  • yetiyeti Posts: 818
    edited 2019-09-24 15:23
    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.
  • yetiyeti Posts: 818
    edited 2019-09-24 18:40
    $ 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!
  • 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(%%%$$$$$#####"""""""""""
    !!!!!!!!!!!!!!!"""""""""""""####################################""""""""""""""""
    

  • yetiyeti Posts: 818
    edited 2019-10-16 03:59
    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 !

  • 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: 818
    edited 2019-10-03 09:03
    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 "@@@" 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.
  • 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.
  • yetiyeti Posts: 818
    edited 2019-10-05 20:43
    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/
  • yetiyeti Posts: 818
    edited 2021-06-23 14:38

    Some not completely random lines of C code:

    #define XMIN ((-21*4096)/10)
    #define XMAX ((  7*4096)/10)
    #define YMIN ((-12*4096)/10)
    #define YMAX (( 12*4096)/10)
    
    #define MAXI (32)
    
    #define DX   ((XMAX-XMIN)/79)
    #define DY   ((YMAX-YMIN)/24)
    
    int main() {
      int cx,cy,x,y,x2,y2;
      int iter;
    
      for( cy=YMIN; cy<=YMAX; cy+=DY ) {
        for( cx=XMIN; cx<=XMAX; cx+=DX ) {
          x = y = x2 = y2 = 0;
          for( iter=0; iter<MAXI && x2+y2<=16384; iter++ ) {
            y = ((x*y)>>11)+cy;
            x = x2-y2+cx;
            x2 = (x*x)>>12;
            y2 = (y*y)>>12;
          }
          _tx(32+iter);
        }
        _tx(13);
        _tx(10);
      }
    }
    

    ...compiled to FlexSpin-LMM and -ByteCode:

    3-mandelbrot-s4p12.c$ ls -l *.binary
    -rw-r--r-- 1 yeti yeti  848 Jun 23 14:25 mandelbrot-s4p12.c.bc.binary
    -rw-r--r-- 1 yeti yeti 1208 Jun 23 14:25 mandelbrot-s4p12.c.lmm.binary
    

    ...and as expected both yield the same output...

    !!!!!!!!!!!!!!!"""""""""""""####################################""""""""""""""""
    !!!!!!!!!!!!!"""""""""#######################$$$$$$$%'+)%%%$$$$$#####"""""""""""
    !!!!!!!!!!!"""""""#######################$$$$$$$$%%%&&(+,)++&%$$$$$$######""""""
    !!!!!!!!!"""""#######################$$$$$$$$$$%%%%&')*5:/+('&%%$$$$$$#######"""
    !!!!!!!!""""#####################$$$$$$$$$$%%%&&&''),@@@@@@@,'&%%%%%$$$$########
    !!!!!!!"""####################$$$$$$$$%%%&'())((())*,@@@@@@/+))('&&&&)'%$$######
    !!!!!!""###################$$$$$%%%%%%&&&'+.@@=/<@@@@@@@@@@@@@@@/++@..93%%$#####
    !!!!!"################$$$%%%%%%%%%%&&&&'),+2@@@@@@@@@@@@@@@@@@@@@@@@@1(&&%$$####
    !!!!"##########$$$$$%%&(-(''''''''''''(*,5@@@@@@@@@@@@@@@@@@@@@@@@@@@@+)-&%$$###
    !!!!####$$$$$$$$%%%%%&'(*-@1.+.@-4+))**@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@4-(&%$$$##
    !!!!#$$$$$$$$$%%%%%%'''++.6@@@@@@@@@8/0@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@3(%%$$$$#
    !!!#$$$$$$$%&&&&''()/-5.5@@@@@@@@@@@@@>@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@?'&%%$$$$#
    !!!(**+/+<523/80/46@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@4+)'&&%%$$$$#
    !!!#$$$$$$$%&&&&''().-2.@@@@@@@@@@@@@@?@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'&%%$$$$#
    !!!!#$$$$$$$$$%%%%%&'''/,.7@@@@@@@@@;/0@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@0'%%$$$$#
    !!!!####$$$$$$$$%%%%%&'(*-:2.,/?-5+))**@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@4+(&%$$$##
    !!!!"##########$$$$$%%&(-(''''(''''''((*,4@@@@@@@@@@@@@@@@@@@@@@@@@@@4+).&%$$###
    !!!!!"################$$$%%%%%%%%%%&&&&')<,4@@@@@@@@@@@@@@@@@@@@@@@@@/('&%%$####
    !!!!!!""##################$$$$$$%%%%%%&&&'*.@@@0@@@@@@@@@@@@@@@@1,,@//9)%%$#####
    !!!!!!!"""####################$$$$$$$$%%%&(())((()**-@@@@@@/+)))'&&&')'%$$######
    !!!!!!!!""""#####################$$$$$$$$$$%%%&&&''(,@@@@@@@+'&&%%%%%$$$########
    !!!!!!!!!"""""#######################$$$$$$$$$$%%%%&')*7@0+('&%%%$$$$$#######"""
    !!!!!!!!!!!"""""""######################$$$$$$$$$%%%&&(+-).*&%$$$$$$######""""""
    !!!!!!!!!!!!!"""""""""#######################$$$$$$%%'3(%%%$$$$$######""""""""""
    !!!!!!!!!!!!!!!""""""""""""#####################################""""""""""""""""
    

    ...to be continued?

Sign In or Register to comment.