Determining BBC BASIC file format ================================= J.G.Harston - 22-Jun-2002 BBC BASIC programs are 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. Acorn/Wilson format BASIC is stored as: { } Russell format BASIC is stored as: { } <00> BASIC can also be stored as text, as: {[] [|||]} It can be useful to be able to determine what format a BASIC file is stored in. Different file formats have defined RISC OS filetypes and DOS extensions: Format RISC OS File filetype extension Acorn/Wilson &FFB "BASIC" "." Russell &1C7 "Basic8" ".bbc" Text &FFF "Text" ".bas" However, you should never assume a file's format from its filetype or extension. The format can be easiy determined by looking at the *final* few bytes of the file: <00> - Russell format (Z80, 80x86) xx xx - Wilson/Acorn format (6502, 32000, ARM, PDP11) xx xx - text CR/LF terminated xx xx - text LF/CR terminated xx xx xx - text LF terminated xx xx xx - text CR terminated xx xx xx xx - unrecognised The following bit of 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 This assumes that the BASIC program has not got any extra bytes appended after the final terminator.