forge Documentation

memory-usage

Memory Usage

Guidelines for memory usage on the Atari 8-bit computers.

Page Organization

Page 0 ($00-$7F)

Operating system zero-page. The entire first half of page zero is reserved for the operating system.

Page 0 ($80-$FF)

Free zero-page. The top half of page zero is free if BASIC is disabled. BASIC uses all but $CB-$D1. The floating point math routines use $D4-$FF. If the floating point arithmetic package is not used this memory is free.

Page 1 ($100-$1FF)

6502 Stack. The stack pointer is initialized to $1FF and moves downward as the stack is filled.

Pages 2-5 ($200-$47F)

Operating system database variables. Parts which are not used in some particular programs, such as the cassette buffer or printer buffer, may then be used for other purposes.

Pages 2-5 ($480-$57D)

User workspace. This is free to be used by programs. If the floating point arithmetic package is not used, the user workspace extends to $6FF. This area is used by BASIC.

Note: $480-$6FF if no floating point.

Pages 2-5 ($57E-$5FF)

Floating point arithmetic package area. This area is used by the floating point arithmetic package. It is free if the package is not used.

Page 6 ($600-$6FF)

Free user space. Atari has solemnly sworn never to put anything in this page of memory. This is completely safe for user programs.

Memory Pointers

MEMLO

The address pointed to by MEMLO [$02E7,2 (743)] is the first byte of free memory. This pointer is usually changed by any program's initialization routine.

  • Upon power-up: MEMLO points to $700
  • When DOS loads: MEMLO changes to point to $2A80
  • AUTORUN.SYS programs: Change MEMLO to point above themselves

One important reason for this is to protect the program from BASIC. BASIC uses memory starting at MEMLO.

MEMTOP

MEMTOP [$02E5,2 (741)] is set by the OS whenever a graphics mode is entered. The graphics region is at the very top of RAM and extends downward. The address MEMTOP points to depends on how much memory the screen region uses.

APPMHI

APPMHI [$0E,2 (14)] should be set by any program to point to the highest address required by the program. If the OS cannot set up a screen without going below APPMHI it will return a not-enough-memory-for-screen-mode error.

Screen Region

Page 7+ ($700+)

Boot region. Most machine language programs which don't use DOS load at this address. DOS extends from $700-$1CFB.

The screen region is allocated at the top of RAM and extends downward. The size depends on the graphics mode.

Cartridge Slots

$8000 (32768)

Beginning of the 8K bytes used by the right cartridge slot of the 800. This is also where 16K cartridges begin. If there is no cartridge here it is RAM.

$A000 (40960)

Beginning of the left cartridge of the 800 or the only cartridge slot on all other models. This is where the BASIC ROM resides in the XL/XE models. This area is RAM if there is no cartridge or BASIC is disabled on XL/XE models.

Above Cartridges

$C000-$CFFF (49152-53247)

This area is empty on the 800. Sometimes special ROM chips, such as Omnimon are wired in here. On the XL/XE models $C000-C7FF is free RAM if there are no cartridges. On XL/XE models, the OS ROM starts at $C800.

$D000-$D7FF (53248-57373)

This area is taken up by the hardware chips. The chips actually take only a fraction of this space. If these addresses are further decoded there is space for many, many more hardware chips.

$E000-$E3FF (57344-58367)

This is the location of the ATASCII character set.

$E400-$FFF7 (58368-65527)

This is the operating system ROM.

$FFF8-$FFFF (65528-65535)

These last 8 bytes contain the addresses of the interrupt vectors. Upon power up the 6502 gets a reset pulse and looks up the reset routine here.

Safe Memory Ranges

Safe for User Programs

  • $0600-$06FF - Page 6 (guaranteed free)
  • $0480-$05FF - User workspace (if FP not used, extends to $6FF)
  • $0700+ - Above DOS (check MEMLO)
  • $8000-$9FFF - Right cartridge slot (if no cartridge)
  • $A000-$BFFF - Left cartridge slot (if no cartridge/BASIC)
  • $C000-$C7FF - Extended RAM (XL/XE, no cartridge)

Read-Only (Hardware/ROM)

  • $D000-$D7FF - Hardware registers (read/write safe)
  • $E000-$FFFF - Character set and OS ROM (read-only)

Dangerous (OS/System)

  • $0000-$03FF - Zero page and OS database (OS uses)
  • $0400-$047F - OS database (may be used by OS)
  • $C800-$FFFF - OS ROM (read-only)

Memory Allocation Tips

For FORGE Programs

  1. Check available memory:

    ? "Free memory: "; FRE(); " bytes"
    
  2. Set APPMHI if needed:

    DPOKE 14, $4000  ' Set APPMHI to $4000
    
  3. Use Page 6 for safe storage:

    ' Page 6 is always safe
    DATA buffer() BYTE = ...  ' Can use Page 6 addresses
    

Array Memory Usage

  • WORD arrays: 2 bytes per element
  • BYTE arrays: 1 byte per element
  • Floating point arrays: 6 bytes per element
  • String arrays: 2 bytes (empty) or 258 bytes (with string) per element

String Memory Usage

  • String variables: Always 256 bytes (1 byte length + up to 255 characters)

Best Practices

  1. Check FRE() - Monitor available memory
  2. Use appropriate array types - BYTE arrays save memory
  3. Clear unused arrays - Use CLR to free memory
  4. Set APPMHI - Protect your program from graphics mode changes
  5. Use Page 6 - Safest area for user data

Related Topics