Skip to content

1K Tiny BASIC internals

WillStevens edited this page Mar 12, 2024 · 2 revisions

Implementation details

This page describes the memory layout used by the 1K Tiny BASIC interpreter, and outlines how the interpreter operates.

Memory layout

1K Tiny BASIC resides in the lower 1K of the 8080s 64K address space, and assumes that 1K of RAM is available immediately above this. (This can be changed up to a maximum of 63K).

RAM layout is as follows:

Bottom of RAM going upwards:

  • 400-401 : End-of-program pointer (PROG_PTR)
  • 402-435: Variables A-Z 52 bytes
  • 436-43B: Unused space, may be used for system variables in future
  • 43C-43E: End-of-parse area pointer (PROG_PARSE_PTR)
  • 43E-43F: Random Number Generator state
  • 440- : Program storage area. PROG_BASE=440, and the program is stored between PROG_BASE and PROG_PTR. When input is parsed, the result of parsing is appended to the program, and stored between PROG_PTR and PROG_PARSE_PTR. To incorporate the newly parsed line into the program, PROG_PTR is set to PROG_PARSE_PTR. (Out-of-order line number entry is allowed, how this is handled is described later).

Top of RAM going downwards:

  • 7F8-7FF : 8 byte input buffer, used by INPUT statement.

SP is set to 7F8, so the stack grows beneath that. In addition to being used by the BASIC interpreter as it runs, the stack is also used for storing operands during expression evaluation, and by FOR/NEXT and GOSUB/RETURN statements.

So in a system with 1K of RAM, about 900 bytes are available for storing the BASIC program.

Parsing and program storage

Parsing takes place at program entry (in contrast to other Tiny BASIC implementations, where parsing happens at run-time). Programs are stored in tokenised form.