Sinclair BASIC tokenized file

Sinclair BASIC is a dialect of the BASIC programming language created by by Nine Tiles Networks Ltd and  used in the 8-bit home computers from Sinclair Research and Timex Sinclair.

The original 4KB version was developed for the Sinclair ZX80, followed by an 8KB version for the ZX81 and 16 KB version for ZX Spectrum.

Some unusual features of the Sinclair BASIC:
 * There were keys on the keyboard for each BASIC keyword. For example, pressing P caused the entire command PRINT to appear. Some commands needed multiple keypresses to enter, For example, BEEP was keyed by pressing CAPS SHIFT plus SYMBOL SHIFT to access extended mode, keeping SYMBOL SHIFT held down and pressing Z.
 * When programs where SAVEd, the file written to disk or tape contained all of BASIC's internal state information, including the values of any defined basic variables, as well as the BASIC tokens.

BASIC File Layout
On a ZX81, a saved BASIC file is a snapshot of the computer memory from memory location 16393 through to the end of the variable table. There is no header. {| class="wikitable" ! title="Memory Address" | Address ! title="Name" | Name ! title="Description" |Description

16436||FRAMES||Counts the frames displayed on the television. Bit 15 is 1. Bits 0 to 14 are decremented for each frame set to the television. This can be used for timing, but PAUSE also uses it. PAUSE resets to 0 bit 15, & puts in bits 0 to 14 the length of the pause. When these have been counted down to zero, the pause stops. If the pause stops because of a key depression, bit 15 is set to 1 again. Each BASIC line is stored as: 2 byte line number (in big-endian format) 2 byte length of text including NEWLINE (in little endian format, length "excludes" the line number and length, i.e. to skip between lines you add "length of text" +4 bytes. text (BASIC tokens)  NEWLINE (0x76)
 * 16393||VERSN	||0 Identifies ZX81 BASIC in saved programs.
 * 16394||E_PPC||Number of current line (with program cursor).
 * 16396||D_FILE||Pointer to the start of the 'Display file', i.e. what is being displayed on screen
 * 16398||DF_CC||Address of PRINT position in display file. Can be poked so that PRINT output is sent elsewhere.
 * 16400||VARS||Pointer to start of BASIC Variable table
 * 16402||DEST||Address of variable in assignment.
 * 16404||E_LINE||Pointer to line currently being entered
 * 16406||CH_ADD||Address of the next character to be interpreted: the character after the argument of PEEK, or the NEWLINE at the end of a POKE statement.
 * 16408||X_PTR||Address of the character preceding the marker.
 * 16410||STKBOT||pointer to start (bottom) of stack
 * 16412||STKEND||pointer to end (top) of stack
 * 16414||BERG||Calculator's b register.
 * 16415||MEM||Address of area used for calculator's memory. (Usually MEMBOT, but not always.)
 * 16417|| ||not used
 * 16418||DF_SZ||The number of lines (including one blank line) in the lower part of the screen.
 * 16419||S_TOP||The number of the top program line in automatic listings.
 * 16421||LAST_K||Shows which keys pressed.
 * 16423|| || Debounce status of keyboard.
 * 16424||MARGIN||Number of blank lines above or below picture: 55 in Britain, 31 in America.
 * 16425||NXTLIN||Address of next program line to be executed.
 * 16427||OLDPPC||Line number of which CONT jumps.
 * 16429||FLAGX||Various flags.
 * 16430||STRLEN||Length of string type destination in assignment.
 * 16432||T_ADDR||Address of next item in syntax table (very unlikely to be useful).
 * 16434||SEED||The seed for RND. This is the variable that is set by RAND.
 * 16417|| ||not used
 * 16418||DF_SZ||The number of lines (including one blank line) in the lower part of the screen.
 * 16419||S_TOP||The number of the top program line in automatic listings.
 * 16421||LAST_K||Shows which keys pressed.
 * 16423|| || Debounce status of keyboard.
 * 16424||MARGIN||Number of blank lines above or below picture: 55 in Britain, 31 in America.
 * 16425||NXTLIN||Address of next program line to be executed.
 * 16427||OLDPPC||Line number of which CONT jumps.
 * 16429||FLAGX||Various flags.
 * 16430||STRLEN||Length of string type destination in assignment.
 * 16432||T_ADDR||Address of next item in syntax table (very unlikely to be useful).
 * 16434||SEED||The seed for RND. This is the variable that is set by RAND.
 * 16425||NXTLIN||Address of next program line to be executed.
 * 16427||OLDPPC||Line number of which CONT jumps.
 * 16429||FLAGX||Various flags.
 * 16430||STRLEN||Length of string type destination in assignment.
 * 16432||T_ADDR||Address of next item in syntax table (very unlikely to be useful).
 * 16434||SEED||The seed for RND. This is the variable that is set by RAND.
 * 16430||STRLEN||Length of string type destination in assignment.
 * 16432||T_ADDR||Address of next item in syntax table (very unlikely to be useful).
 * 16434||SEED||The seed for RND. This is the variable that is set by RAND.
 * 16432||T_ADDR||Address of next item in syntax table (very unlikely to be useful).
 * 16434||SEED||The seed for RND. This is the variable that is set by RAND.
 * 16434||SEED||The seed for RND. This is the variable that is set by RAND.
 * 16438||COORDS||x-coordinate of last point PLOTted.
 * 16439||		|| y-coordinate of last point PLOTted.
 * 16440||PR_CC||Less significant byte of address of next position for LPRINT to print as (in PRBUFF).
 * 16441||S_POSN||Column number for PRINT position.
 * 16442||		 ||Line number for PRINT position.
 * 16443||CDFLAG||Various flags. Bit 7 is on (1) during compute & display mode.
 * 16444||PRBUFF||Printer buffer (33rd character is NEWLINE).
 * 16477||MEMBOT||Calculator's memory area; used to store numbers that cannot conveniently be put on the calculator stack.
 * 16507|| || not used
 * 16509|| ||           First BASIC line.
 * 16443||CDFLAG||Various flags. Bit 7 is on (1) during compute & display mode.
 * 16444||PRBUFF||Printer buffer (33rd character is NEWLINE).
 * 16477||MEMBOT||Calculator's memory area; used to store numbers that cannot conveniently be put on the calculator stack.
 * 16507|| || not used
 * 16509|| ||           First BASIC line.
 * 16477||MEMBOT||Calculator's memory area; used to store numbers that cannot conveniently be put on the calculator stack.
 * 16507|| || not used
 * 16509|| ||           First BASIC line.
 * 16509|| ||           First BASIC line.
 * 16509|| ||           First BASIC line.

When a numeric constant is included in the text of a BASIC line, an ASCII string displaying the constant value will be inserted, followed by the token 0x7E, and the next 5 bytes are the value of the constant in floating point format.

Following the last BASIC line comes the VARIABLEs table.

#first byte of name implies type: #011nnnnn = single letter variable name, followed by 5 byte number #101nnnnn = multi letter variable name (last letter has high bit set), followed by 5 byte number #100nnnnn = array of numbers: #111nnnnnn = control variable of a for-next loop #010nnnnn = single letter variable name - 0x20, 2 byte string length, text of string #110nnnnn = array of characters