Page 6: The Atari's 256 Bytes of Safe Memory
Ask an Atari BASIC programmer of the 1980s where to put a machine-language routine and the answer was almost always page 6. These are the 256 bytes from 1536 to 1791 ($0600–$06FF). They are protected from OS use: neither the operating system nor Atari BASIC normally touches them, so code placed there stays put while BASIC programs load, run and grow. This guide explains why that matters, where the protection breaks down, and gives you a fast, genuinely useful page-6 routine to type in.
You need: Atari BASIC on any machine. An assembler is optional, since the routine is supplied as DATA statements.
1. Where page 6 sits in the memory map
The 6502 sees memory in 256-byte "pages". The first few are spoken for:
| Pages | Addresses | Used by |
|---|---|---|
| 0 | $0000–$00FF | OS (0–127), BASIC and floating point (128–255) |
| 1 | $0100–$01FF | 6502 stack |
| 2–5 | $0200–$05FF | OS variables, IOCBs, cassette buffer, BASIC line buffer and floating point |
| 6 | $0600–$06FF | Nobody: free for the user |
| 7 upwards | $0700– | DOS (when booted), then your BASIC program |
The usable program area starts at $0600 and runs to $BFFF. Seen from the other side, locations 0–1535 belong to the OS, and from 1792 ($0700) upward to DOS. Page 6 is the gap between them.
2. Why BASIC programmers love it
A BASIC program's variables, strings and arrays move about in memory as you edit it. Machine code stored in a string (the other popular technique) therefore has to be relocatable: it cannot contain absolute JMPs or references to its own data. De Re Atari spells out the problem, and offers page 6 as the way round it: code loaded there "need not be relocatable". Page 6 is also the natural home for anything an interrupt must find at a fixed address, such as a vertical blank routine, a display list interrupt handler, or tables used by both BASIC and machine code.
3. The pitfalls
Page 6 is free by convention, not by hardware. The exceptions:
- Long INPUT lines. When an INPUT statement receives more than 128 characters, the excess spills into 1536–1663 ($0600–$067F). Keep INPUT short, or put your code in the upper half, $0680–$06FF (1664–1791). Many published routines start at 1664 for this reason.
- Other languages. BASIC A+ uses $0600–$067F, and ValFORTH uses the whole page for its boot code.
- Other people's routines. Magazines published dozens of "page 6" utilities, and they all assumed they had the page to themselves. Antic's "Page 6 Grab Bag" (March 1987) warns that its Joystick Expander and Console Key Enhancer "run from the same part of Page 6", so you should use only one of them in any program.
- Vectors don't survive RESET. Any OS vector you pointed into page 6 (VBI, DLI, keyboard IRQ) is restored to its default by SYSTEM RESET, so your routine must be installed again.
If you need more than 256 bytes, reserve memory instead. Lower RAMTOP (location 106) by a number of pages and issue a GRAPHICS command so that the screen moves below the reserved area.
4. Getting code into page 6
From DATA statements. This is the classic type-in method, used below. It is simple, but slow for long routines, and typos are fatal, so magazines added checksums.
From a binary file. Save the bytes once with DOS 2's option K (binary save), giving the start and end addresses, and load them later with option L. De Re Atari's Figure 8-16 shows how a BASIC program can read a file straight into 1536 with a single CIO "get characters" call (see CIO and IOCBs).
From an assembler. Assemble with an origin of *= $0600 (or $0680) and save as a binary/XEX file. Cross-assemblers such as MADS, ATasm or ca65 produce files that DOS loads directly.
5. A useful page-6 routine: fast memory move
BASIC copies memory with a FOR/NEXT loop of PEEKs and POKEs, which takes several seconds per kilobyte. This 52-byte routine copies any block in a fraction of a second. Call it with:
X=USR(1664,SOURCE,DESTINATION,LENGTH)
The assembly source (it uses page-zero bytes 203–208, the only page-zero bytes guaranteed free under BASIC):
SRC = $CB ; 203-204
DST = $CD ; 205-206
LEN = $CF ; 207-208
*= $0680 ; 1664, upper half of page 6
PLA ; number of arguments (3)
PLA
STA SRC+1 ; USR passes high byte first
PLA
STA SRC
PLA
STA DST+1
PLA
STA DST
PLA
STA LEN+1
PLA
STA LEN
LDY #0
LDX LEN+1 ; number of whole pages
BEQ PART
PAGE LDA (SRC),Y
STA (DST),Y
INY
BNE PAGE
INC SRC+1
INC DST+1
DEX
BNE PAGE
PART LDX LEN ; leftover bytes
BEQ DONE
LAST LDA (SRC),Y
STA (DST),Y
INY
DEX
BNE LAST
DONE RTS
And a demonstration that uses it to copy the 1,024-byte ROM character set (at 57344, $E000) into RAM, then redefines the letter A:
10 FOR I=1664 TO 1715:READ B:POKE I,B:NEXT I
20 POKE 106,PEEK(106)-8:GRAPHICS 0
30 CH=(PEEK(106)+4)*256
40 X=USR(1664,57344,CH,1024)
50 POKE 756,CH/256
60 FOR I=0 TO 7:READ B:POKE CH+33*8+I,B:NEXT I
70 PRINT "AAAA - THE A KEY NOW PRINTS A FACE"
100 DATA 104,104,133,204,104,133,203,104,133,206
110 DATA 104,133,205,104,133,208,104,133,207,160
120 DATA 0,166,208,240,14,177,203,145,205,200
130 DATA 208,249,230,204,230,206,202,208,242,166
140 DATA 207,240,8,177,203,145,205,200,202,208
150 DATA 248,96
160 DATA 60,66,165,129,165,153,66,60
How it works:
- Line 20 lowers RAMTOP by eight pages (2K) and re-opens the screen below the new limit. Line 30 places the font in the upper kilobyte of the reserved area, because GRAPHICS and screen-clear commands wipe the first 64 bytes above RAMTOP, and that text-window scrolling can clear up to 800 bytes there, so the lower kilobyte is a sacrificial buffer. Character sets for GRAPHICS 0 must start on a 1K boundary. RAMTOP is normally a multiple of four pages (160 on a 48K or 64K machine with BASIC), so the address is correctly aligned.
- Line 40 copies the ROM font in one call. Try the same copy with a PEEK/POKE loop and time the difference with the jiffy clock.
- Line 50 points CHBAS (756, $2F4) at the new set. The OS copies it to ANTIC's CHBASE register every vertical blank.
- Line 60 rewrites the eight bytes of internal character 33, the letter A. Internal codes differ from ATASCII: upper-case letters are 33–58.
Press SYSTEM RESET to get the normal font back. Because the routine copies forwards, it is safe for any non-overlapping blocks, and for overlapping blocks only when the destination is below the source.
6. Other classic page-6 jobs
- Console and joystick helpers. Antic reprinted routines originally released on Synapse Software's 1982 Page 6 disk, including a joystick reader that returns 0–8 clockwise for easy
ON ... GOTOdispatch, and a console-key reader that also detects SHIFT. - Interrupt handlers. VBI and DLI routines must live at a fixed address, and page 6 is the obvious one.
- Sound tables and small drivers. See the VBI music player in Programming music.
Troubleshooting
- The routine worked yesterday but crashes after a long INPUT. The input spilled into $0600–$067F. Move the code to $0680+.
- ERROR 3 (value error) from USR. An argument is outside 0–65535. Every USR argument becomes an unsigned 16-bit integer.
- Machine hangs on the USR call. Check the DATA against the listing, and check the argument count: the routine expects exactly three arguments and pulls six bytes. Calling it with a different number corrupts the stack.
- Two utilities fight. Both use page 6. Relocate one (reassemble at a new origin) or reserve memory below RAMTOP for it.
Further reading
- Mapping the Atari by Ian Chadwick (Small System Services, 1983), "Page six" and the page-zero map.
- De Re Atari (Atari, Inc., 1982), Chapter 8, memory management and the BASIC direct CIO call.
- Atari Assembly Language Programmer's Guide by Alan Moose and Marian Lorenz (Weber Systems, 1986): written for programmers who want to add machine-language subroutines to BASIC.
- On this site: USR and machine language from BASIC, Memory map highlights.
Credits
- Mapping the Atari (Ian Chadwick) — 1536–1791 Page six; 203–209; 106 RAMTOP; 756 CHBAS — AtariArchives.org
- Page 6 Grab Bag (Matt Loveless and Mike Eggers), Antic Vol. 5 No. 11, March 1987 — AtariMagazines.com (Kevin Savetz), 1987
- De Re Atari, Chapter 8 (memory management; loading page 6 from BASIC via CIO) — Jim Dunion (Section 8 of De Re Atari)
- comp.sys.atari.8bit FAQ (Michael Current), 1.14 memory map — Michael Current (comp.sys.atari.8bit FAQ)
