FemtoBasic for P2
Mike Green Posts: 23,101
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.
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?
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.
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.
In experimenting with version "1.001 (50)", I've found that the simple statement "DIM A" seems to prevent a program from doing anything.
yields the output:
If I remove the DIM or put a REM in front to comment it out, I get:
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...
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=6, B=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.
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:
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.
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
Cool, this is useful utility.
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/
I tried your latest version and ran into some problems.
The Files command does list both the Flash and SD card contents.
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".
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.
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.
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.
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.