BBC BASIC program line format ============================= BBC BASIC programs are usually stored in a tokenised format. Most of the tokens used by different versions of BBC BASIC are the same, but extended tokens are different, and different versions have different line headers. The differences are between: * Acorn/Wilson BASIC descended from the 6502 version, the 32000, ARM, PDP11, 6809 and others * Russell BASIC descended from the Z80 version, 80x86, Windows, SDL. Acorn/Wilson format BASIC is stored as: {...} Russell format BASIC is stored as: {...} <00> BASIC can also be stored as text, as: { [|||]...} Tokenised line numbers are stored in the same format in all BASICs. They are encoded to make them easily found by RENUMBER, and to prevent them appearing to be control characters or tokens by forcing them into the range &40-&7F. Byte 0 : 141 Byte 1 :(((lsb AND &C0) DIV 4) OR ((msb AND &C0) DIV 16)) EOR &54 Byte 2 : (lsb AND &3F) OR &40 Byte 3 : (msb AND &3F) OR &40 6502 BASIC line numbers can go to a maximum of 32767 (&7FFF), other BASICs can use the full range to 65279 (&FEFF). It can be useful to be able to determine what format a BASIC file is stored in. This can be done by examining the last few bytes of the program: <00> - Russell format (Z80, 80x86, SDL) - Wilson/Acorn format (6502, 32000, ARM, PDP11, 6809) - text CR/LF terminated - text LF/CR terminated - text LF terminated (Brandy) - text CR terminated - unrecognised A running program can tell what format the program is in by examining TOP-3: IF ?(TOP-3)=0 THEN Russell Format IF ?(TOP-3)>0 THEN Acorn format The following code will examine the last four bytes of an open file and determine what format it is: PTR#in%=EXT#in%-4:FOR A%=0 TO 3:buffer%?A%=BGET#in%:NEXT type%=0 :REM unknown IFbuffer%?3=&0D :type%=7 :REM text cr IFbuffer%?3=&0A :type%=6 :REM text lf IF(!buffer% AND &FFFF0000)=&0D0A0000 :type%=5 :REM text lfcr IF(!buffer% AND &FFFF0000)=&0A0D0000 :type%=4 :REM text crlf IF(!buffer% AND &FFFF0000)=&FF0D0000 :type%=2 :REM 6502 format IF!buffer%=&FFFF000D :type%=1 :REM 80/86 format Here in% is the handle of the file that has been opened, buffer% is a pointer to a four byte DIMensioned block of memory, A% is a temporary variable and on exit type% contains the file type. Note that in some circumstances it is legitimate to append data to the end of a BBC BASIC program file. In that case this method of determining the format will not work, since the last four bytes of the file will not be the last four bytes of the program.