Shop OBEX P1 Docs P2 Docs Learn Events
FemtoBasic for P2 — Parallax Forums

FemtoBasic for P2

FemtoBasic (a subset of Basic that runs natively on the P1) now works on the P2 as well. This version uses flash memory for its flat filesystem and works nicely on the P2 EDGE board. It adds floating point, PRINT USING, single dimension arrays, smart I/O pin control as well as having more space available on the P2 and greater speed. It compiles with FlexProp and can be flashed using LOADP2 and P2ES_flashloader.bin which are in the distribution archive for FlexProp. The attached archive contains source files, simple documentation, and the .binary file.

Please report bugs, comments, suggestions here.

Plans include adding SD card FAT32 support and some simple string operations.

Comments

  • Mike,

    Thank you for releasing a newer version.

    One thing I noticed between this version and the previous one I was using "1.001v", is that even though I can see the previously stored BAS programs in Flash using "files", when I try to load any of them, I don't get any errors, but I also don't get any program loaded. Is this expected?

    So far I've only run this newer version "1.001(50)" by downloading into RAM, and not stored into Flash yet.

    When we store newer versions of FemtoBasic, should we expect that our previously stored files (programs, data) will be compromised? If so, what is the best way to "transfer" the old files so that they can be available in the new version?

  • Francis,
    There are some changes between the versions with a letter after the version number and the number in parentheses, but I wouldn't expect that the stored programs just wouldn't load without some kind of error message. I've started working on SD card support. On an SD card, data files would contain whatever your programs write to them and the Basic programs are stored in source form, so "transfer" wouldn't be needed. With flash memory, the FemtoBasic program itself might overstore areas of flash used for the filesystem, but then the file names would be overwritten as well and you wouldn't see them.

    You raise a good issue. I used to have a Basic program that would go through the flash filesystem much like the FILES statement and would display the contents of each file. I'll see if I can come up with something like that and you could copy and paste what you need from a capture file from a terminal emulator.
    Mike

  • Francis BauerFrancis Bauer Posts: 369
    edited 2022-02-13 07:21

    @"Mike Green" said:
    Francis,
    There are some changes between the versions with a letter after the version number and the number in parentheses, but I wouldn't expect that the stored programs just wouldn't load without some kind of error message. I've started working on SD card support. On an SD card, data files would contain whatever your programs write to them and the Basic programs are stored in source form, so "transfer" wouldn't be needed. With flash memory, the FemtoBasic program itself might overstore areas of flash used for the filesystem, but then the file names would be overwritten as well and you wouldn't see them.

    You raise a good issue. I used to have a Basic program that would go through the flash filesystem much like the FILES statement and would display the contents of each file. I'll see if I can come up with something like that and you could copy and paste what you need from a capture file from a terminal emulator.
    Mike

    I went ahead downloaded the "1.001(50)" version into Flash and still does the same thing where "files" shows the previously stored files, but they don't load any content. If I put the previous "1.001v" version back into Flash, the programs do load/run as normal.

    Anyway, no problem as I previously kept copies of the programs on my PC and have created a TeraTerm macro to aid in the loading of programs into FemtoBasic.

  • Francis BauerFrancis Bauer Posts: 369
    edited 2022-02-13 07:22

    In experimenting with version "1.001 (50)", I've found that the simple statement "DIM A[10]" seems to prevent a program from doing anything.
    For example:

    10 PRINT "TEST"
    20 DIM A[10]
    30 END
    

    yields the output:

    run
    OK
    

    If I remove the DIM or put a REM in front to comment it out, I get:

    run
    TEST
    OK
    

    If I use a previous version of FemtoBasic "1.001v", the DIM statement worked as expected.
    I tried using both PropTool 2.6.3 and PNut35s and got the same misbehavior.

    I also found that if I intentionally put a syntax error in the DIM statement ( like leaving off a "[" ), FemtoBasic doesn't report the error either.

    Something apparently happened to the DIM statement in "1.000 (50)" that is preventing text output I guess...

  • Francis,
    When I added floating point to FemtoBasic, this changed part of the DIM handling routine. Default is to initialize an array with integer values of zero. All elements of an array have the same type (integer or float) based on their initial value. You can also provide the default value like:

    100 DIM A[10]=6, B[10]=3.2

    For some reason, this doesn't work with floating point or the default form (without "="). Try the first form (integer value) with 1.001 (50). I've also fixed the error handling so it indicates the line with the DIM.

  • Francis,
    Attached is test version 51 which corrects the errors you found. I've also changed the manual to show the format of the DIM statement. Thanks for experimenting with FemtoBasic.

  • @"Mike Green" said:
    Francis,
    Attached is test version 51 which corrects the errors you found. I've also changed the manual to show the format of the DIM statement. Thanks for experimenting with FemtoBasic.

    Thanks Mike, I was able to get DIM happy now.

    In previous versions of FemtoBasic, there were a collection of Float commands (i.e. FDIV, FLOAT, etc...) in both the code and documentation. In these latest versions these commands don't appear to be available anymore. I'm confused, were they removed and/or replaced or ?

    Here is the example program I was trying to run, it worked in version "1.001v", but not in these () numbered versions, I get the following error:

    In Line 1030: Missing '='
    

    Program listing:

    100 a = FLOAT [0]
    110 GOTO 1000
    120 REM Newton Raphson Method to find real root
    130 REM of non-linear equation.  Function is f(x)
    140 REM with derivative of function as g(x).
    200 REM Compute f(x) with x in b and result in z
    210 REM z = 5 * b * b + 3 * b + 12
    220 z = FMUL [ FMUL [ FLOAT [5], B], B]
    230 z = FADD [ FMUL [ FLOAT [3], B], z]
    240 z = FADD [ z, FLOAT [12] ]
    250 RETURN 
    300 REM Compute g(x) with x in y and result in y
    310 REM y = 5 * y + 3
    320 y = FADD [FMUL [FLOAT [5],y],FLOAT [3]]
    330 RETURN 
    1000 INPUT "Initial guess (0.001)? ";x
    1010 INPUT "Tolerable error (0.001)? ";e
    1020 INPUT "Max. iteration? ";n
    1030 LET x = FDIV [ FLOAT [x], FLOAT [1000]]
    1040 LET e = FDIV [ FLOAT [e], FLOAT [1000]]
    1050 s = 1
    1060 REM y = g(x0)
    1070 y = x
    1080 GOSUB 200
    1090 IF y = 0 THEN GOTO 2000
    1100 b = x : z = b : y = b
    1105 REM Compute b, z = f(b), y = g(y)
    1110 GOSUB 200
    1120 GOSUB 300
    1125 REM Compute x1 = x0 - f(x0) / g(x0)
    1130 v = FSUB [ b, FDIV [ z, y ] ]
    1135 REM Compute x0 = x1
    1140 x = v
    1150 s = s + 1 : REM Compute Step = Step + 1
    1160 IF s > n THEN GOTO 2100
    1170 b = v : REM Compute abs(f(x1))
    1180 GOSUB 200
    1190 IF FCMP [ FABS [z], e ] > 0 THEN GOTO 1060
    1200 PRINT USING "Root is %f\n";v
    1210 STOP 
    2000 PRINT "Math error"
    2010 STOP 
    2100 PRINT "Not Convergent"
    2110 END
    
  • Mike GreenMike Green Posts: 23,101
    edited 2022-02-14 03:55

    The commands you mentioned (FADD, FMUL, FLOAT) were intended to be temporary and have been replaced with regular operators so we'd have:

    100 a = 0.0
    220 z = 5.0 * b * b + 3.0 * b + 12.0
    320 y = 5.0 * y + 3.0
    1000 input "initial guess?";x
    1010 input "tolerable error?";e
    1020 input "max. iteration?";n
    1130 v = b - z / y
    1190 if abs(z) > e then goto 1060

    Note that the input statements don't need scaling. You can enter floating point values directly.
    If one of the operands of +,-,*,/ is floating point, the other operand is converted to floating point
    and the result will be floating point. Similarly, if the operand of a unary operator is floating point,
    the result will usually be floating point as well (with some exceptions ... SIN, COS, ATAN, SQRT, EXP,
    LOG will always have a floating point result.

    You can force a value to be floating point by using FLOAT. You can force a value to be an integer by using ROUND or TRUNC. These functions check the value first to see if it's the type required, if not, it's converted.

  • Here's a simple Basic program that will read through the flash memory and display the names of files encountered along with the first 16 bytes of the file. Files are made up of a linked list of 4080 byte pieces and this program will list the link address, but not the block number in the file.

  • @"Mike Green" said:
    The commands you mentioned (FADD, FMUL, FLOAT) were intended to be temporary and have been replaced with regular operators so we'd have:

    100 a = 0.0
    220 z = 5.0 * b * b + 3.0 * b + 12.0
    320 y = 5.0 * y + 3.0
    1000 input "initial guess?";x
    1010 input "tolerable error?";e
    1020 input "max. iteration?";n
    1130 v = b - z / y
    1190 if abs(z) > e then goto 1060

    Note that the input statements don't need scaling. You can enter floating point values directly.
    If one of the operands of +,-,*,/ is floating point, the other operand is converted to floating point
    and the result will be floating point. Similarly, if the operand of a unary operator is floating point,
    the result will usually be floating point as well (with some exceptions ... SIN, COS, ATAN, SQRT, EXP,
    LOG will always have a floating point result.

    You can force a value to be floating point by using FLOAT. You can force a value to be an integer by using ROUND or TRUNC. These functions check the value first to see if it's the type required, if not, it's converted.

    That's what I get when I don't read your documentation more closely. I updated my program and it is working great, thank you

  • @"Mike Green" said:
    Here's a simple Basic program that will read through the flash memory and display the names of files encountered along with the first 16 bytes of the file. Files are made up of a linked list of 4080 byte pieces and this program will list the link address, but not the block number in the file.

    Cool, this is useful utility.

  • Mike GreenMike Green Posts: 23,101
    edited 2022-02-19 05:26

    This next version adds an SD card driver (thanks cheezus). Note that the statements that use filenames no longer require quotes around them. Flash files require a ^ prefix though. The filesystem drivers provide the rules for upper and lower case. SD card filenames are all upper case and lower case letters are converted. Flash filenames allow both upper and lower case which are distinguished.

    You can LOAD and SAVE programs to either SD files or flash files. You can list FILES and DELETE files. I have just begun to test the reading and writing of data files, but these use most of the same code as LOAD and SAVE and did work before, so should still work. There are new additions to READ and WRITE text lines using arrays to hold the strings (one character per array element). This is mostly untested.

    If you save programs or data to an SD card file, you can edit or otherwise process that text file using a standard editor.

    Edit: Columns of data look fine with Mac editors. Although LOAD and SAVE seem to work with some programs, other programs have problems/

  • @"Mike Green" said:
    This next version adds an SD card driver (thanks cheezus). Note that the statements that use filenames no longer require quotes around them. Flash files require a ^ prefix though. The filesystem drivers provide the rules for upper and lower case. SD card filenames are all upper case and lower case letters are converted. Flash filenames allow both upper and lower case which are distinguished.

    You can LOAD and SAVE programs to either SD files or flash files. You can list FILES and DELETE files. I have just begun to test the reading and writing of data files, but these use most of the same code as LOAD and SAVE and did work before, so should still work. There are new additions to READ and WRITE text lines using arrays to hold the strings (one character per array element). This is mostly untested.

    If you save programs or data to an SD card file, you can edit or otherwise process that text file using a standard editor.

    Edit: Columns of data look fine with Mac editors. Although LOAD and SAVE seem to work with some programs, other programs have problems/

    Mike,

    I tried your latest version and ran into some problems.

    The Files command does list both the Flash and SD card contents.

    FemtoBASIC 1.001 (54), 32K bytes free
    Flash size 16MB, 4044 4K blocks free
    OK
    files
    Flash Memory
    ------------
    readflsh.bas    TestProg.bas    pinread.bas     QUEEN.bas
    
    SD Root Files
    -------------
    PINREAD.BAS     QUEEN.BAS       READFLSH.BAS    TESTPR~1.BAS
    OK
    

    If I remove the SD card and restart FemtoBASIC, the command lists the Flash contents but then hangs for a very long time before giving me an OK. It appears that the routine checking for the SD card presence gets stuck checking for quite awhile.

    It appears that in the "storeFilename" routine (line 608), the wrong filename character is specified, there is a "~" instead of "^" character. I was getting filename errors until I changed the "~" to a "^".

    Even changing that doesn't seem allow me to load a previously stored program in Flash.

    I get the following error when trying to load "^pinread.bas".

    In Line 0: Can't LOAD file - Can't open
    

    I haven't tracked down the issue yet.

    I also haven't been able to get a program file to load from the SD card, that I had put program files onto using my PC.

    load "pinread.bas"
    SD card I/O error (-1)
    In Line 0: Can't open SD card file
    OK
    

    Anyway its late and I'll have to look into these issues more tomorrow...

  • The delay noted with FILES when there's no SD card is excessive, but was intended. I've shortened it. Now there are 10 attempts to mount an SD card with a delay of 100ms between attempts. This can be shortened further. See lines 556 and 557.

    File names can include A-Z, a-z, 0-9, "-", "_", "~", and ".". There can be a prefix of "^" whether or not quotes are used around the filename. The leading "^" designates a flash file.

    DISPLAY using an array name seems to work.

    I've been able to save, then re-load programs to/from both flash and SD cards. I'll check further on making program files on a PC, then loading them via SD card. I also need to test READLN and WRITELN further.

  • READLN and WRITELN appear to work. You can use READLN to read in lines of text which can be displayed with DISPLAY. You can also fill an array with characters and output them to a file with WRITELN.

    Because there currently can only be one file open at a time, you can't copy a file to another file. This could be changed in the future.

  • Mike GreenMike Green Posts: 23,101
    edited 2022-02-22 19:38

    The ENTER statement reads a text line from the keyboard into an array (much like READLN).

    OPEN, CLOSE, READ, WRITE, READLN, and WRITELN all allow an optional "channel number". More than one file can be open at a time in flash memory, but only one SD card file can be open. Currently, the
    total is 3 files, so you could have one SD card file and two flash files all open.

    You can supply a filename in an array rather than just having filenames literally in a program.

  • The above version of FemtoBasic will compile with Eric Smith's FlexProp. It will also compile with the Propeller Tool if you comment out line #1752 in FemtoBasic.spin2 as described in the comments just before that line.

  • Mike GreenMike Green Posts: 23,101
    edited 2022-02-23 23:06

    FemtoBasic hangs up in FILES statement when trying to list root directory and there's no SD card present
    It probably will hang up in similar circumstances ... any time a program attempts to use an SD card and there's none there.

    Edit: Fixed. Added test for SD card present signal.

Sign In or Register to comment.