Using the 130XE's Extra 64K: PORTB Bank Switching

An Atari 130XE computer in light grey, seen from the front at an angle

The Atari 130XE, introduced in January 1985 alongside the 65XE, has 128K of RAM, of which 126K is usable. The 6502 can only address 64K at a time, so the second 64K is reached through a 16K "window": write the right value to the PIA's PORTB register and the RAM at $4000–$7FFF is replaced by one of four extra banks. This guide gives the exact bit meanings, shows how to use them safely from BASIC and assembly, and explains how other memory expansions differ.

You need: a 130XE, or Altirra set to a 130XE / 128K configuration. The examples use Atari BASIC and optionally an assembler. Nothing here works on a stock 800XL or 65XE, which have no extended RAM.

1. PORTB on XL/XE machines

On the 400/800, location 54017 ($D301) read joystick ports 3 and 4. The XL/XE machines have only two joystick ports, so PORTB was turned into a memory-control register. The bit assignments:

Bit Value Function (0 / 1)
0 1 OS ROM: 0 = RAM at $C000–$FFFF, 1 = ROM enabled
1 2 BASIC: 0 = BASIC enabled, 1 = RAM at $A000–$BFFF
2 4 130XE bank select, low bit
3 8 130XE bank select, high bit
4 16 CPU access to the window: 0 = extended bank, 1 = main RAM
5 32 ANTIC access to the window: 0 = extended bank, 1 = main RAM
6 64 Unused on the 130XE (Missile Command ROM on the XE Game System)
7 128 Self-test ROM at $5000–$57FF: 0 = enabled, 1 = RAM

Bit 4 is named CPE (CPU bank enable) and bit 5 VBE (video bank enable). Both are active low: 1 means "use main memory".

Bits 2 and 3 select one of the four extended 16K blocks. In the 128K address space they are $10000–$13FFF, $14000–$17FFF, $18000–$1BFFF and $1C000–$1FFFF. Whichever one you choose, it always appears at $4000–$7FFF. From BASIC you still PEEK and POKE locations 16384–32767.

2. The four modes

Because the CPU and ANTIC have separate enable bits, there are four combinations. The 130XE was the only stock Atari with this feature:

VBE (bit 5) CPE (bit 4) CPU sees at $4000 ANTIC sees at $4000 Use
1 1 main main Normal / compatible
1 0 extended main Store program data in the banks; the display is unaffected
0 1 main extended Show a screen stored in a bank while the CPU works in main RAM
0 0 extended extended Run and display entirely from a bank

A convenient formula for BASIC:

POKE 54017, 193 + MODE*16 + BANK*4

Here MODE 0 = both extended, 1 = CPU main / ANTIC extended, 2 = CPU extended / ANTIC main, 3 = both main, and BANK = 0–3. The constant 193 ($C1) sets bits 7, 6 and 0 (self-test off, OS ROM on) and leaves bit 1 at 0, so BASIC stays enabled. That matters, because switching BASIC out from inside a BASIC program locks the machine up.

3. A BASIC demonstration and detector

This program writes a different value into the first byte of each bank, reads them back, and shows that main RAM was not touched. On a 64K machine without extended RAM, the PORTB writes have no effect on memory, so every "bank" is really main RAM and all the values come out the same. That makes the program a simple 130XE detector too.

10 REM 130XE BANK DEMO
20 PB=54017:ORIG=PEEK(PB):W=16384
30 IF PEEK(144)+256*PEEK(145)>=W THEN PRINT "PROGRAM TOO LONG":END
40 POKE W,0
50 FOR B=0 TO 3
60 POKE PB,225+B*4:POKE W,B+100:POKE PB,ORIG
70 NEXT B
80 FOR B=0 TO 3
90 POKE PB,225+B*4:V=PEEK(W):POKE PB,ORIG
100 PRINT "BANK ";B;" HOLDS ";V
110 NEXT B
120 PRINT "MAIN RAM HOLDS ";PEEK(W)
130 IF PEEK(W)=0 THEN PRINT "EXTENDED RAM PRESENT"

Line by line:

  • 225 = 193 + 2×16, which is mode 2: the CPU sees the extended bank and ANTIC keeps showing main memory, so the screen never flickers.
  • Each switch is undone on the same line (POKE PB,ORIG). Atari BASIC keeps your program and variables in main RAM, and it must be able to see them again before it executes another line.
  • Line 30 checks BASIC's top-of-program pointer (locations 144–145, MEMTOP). It refuses to run if the program, its variables or its strings reach $4000, because those would vanish when a bank is switched in. This is a classic lock-up: a program that grows past $4000 crashes as soon as it switches banks, because BASIC searches program lines from the start and the line links break when the wrong bank is in place.

4. Assembly: selecting banks properly

From machine code you should change only the bits you mean to change. Read PORTB, mask out bits 2–5, then OR in the new value. This is the technique used by a COMPUTE! bank-access routine, which ANDs PORTB with $C3 and ORs in $20 plus the bank bits:

PORTB = $D301

; Select extended bank X (0-3) for the CPU, keep ANTIC on main RAM
SETBANK TXA
        ASL A
        ASL A           ; bank number -> bits 2-3
        ORA #%00100000  ; VBE=1 (ANTIC main), CPE=0 (CPU extended)
        STA TEMP
        LDA PORTB
        AND #%11000011  ; clear bits 2-5, keep OS/BASIC/self-test bits
        ORA TEMP
        STA PORTB
        RTS

; Back to main RAM for both CPU and ANTIC
MAINRAM LDA PORTB
        ORA #%00110000  ; CPE=1, VBE=1
        STA PORTB
        RTS

TEMP    .BYTE 0

Rules that keep you out of trouble, mostly from Bill Wilkinson's Antic column "RAM Banks on the 130XE" (February 1987):

  1. Never run the switching code from inside $4000–$7FFF. As Bill Wilkinson warned, you can "turn off the piece of memory your program currently occupies, thereby causing the CPU to get completely lost". The same applies to any data the code needs while the other bank is active.
  2. Interrupt handlers must not live in the window. With the stock OS, the VBI and IRQ code is in ROM and its variables are in pages 0–5, so switching the window with interrupts enabled is fine. If your own VBI or DLI code or data is in $4000–$7FFF, disable interrupts while switching.
  3. Be extra careful when other software also changes PORTB. Turbo BASIC XL switches the OS ROM bit itself and keeps interrupt handlers in the RAM under the ROM. A routine running under it should save PORTB, mask IRQs (SEI) and NMIs (STA $D40E with 0), do its work, then restore PORTB, re-enable the VBI with LDA #$40 : STA $D40E and CLI.
  4. Wilkinson's advice on the area from $C000 up: leave the RAM under the OS alone unless you really know what you are doing. DOSes such as SpartaDOS and DOS XL already use it.

5. Video tricks: ANTIC banking

Mode 1 (CPU main, ANTIC extended) lets ANTIC display a screen that sits in an extended bank while the CPU keeps all of main memory for code and data. Put a display list and screen data in bank 0, switch ANTIC to it in your vertical blank routine, and draw the next frame into another bank. That gives you double buffering without losing any main RAM. Once the bits are set, you don't have to worry about where each access will occur: the hardware routes each chip's accesses automatically.

6. The easy route: RAMdisk

If you only want the extra memory as fast storage, use a RAMdisk. DOS 2.5's RAMDISK.SYS detects a 130XE at boot and turns the extended 64K into drive D8:, with 499 usable sectors. Your BASIC program simply OPENs "D8:DATA.DAT". Wilkinson's other suggestion for BASIC programmers was BASIC XE from OSS, which uses the banks for program storage. Remember that the RAMdisk occupies all four banks, so programs that bank-switch directly will corrupt it.

7. Other memory expansions

Many upgrades for the 800XL and other machines copy the 130XE scheme and add more banks by borrowing more PORTB bits. Here is how they differ:

Expansion Total Banking bits Separate ANTIC access
130XE 64K + 64K 2, 3 Yes
192K (RAMBO) 64K + 128K 2, 3, 6 No
320K (RAMBO) 64K + 256K 2, 3, 5, 6 No
320K (COMPY) 64K + 256K 2, 3, 6, 7 Yes
576K (COMPY) 64K + 512K 1, 2, 3, 6, 7 Yes

"RAMBO"-style expansions use bit 5 as a bank bit, so there is no separate ANTIC access, and the 256K RAMBO even aliases four banks onto main memory. Software that assumes 130XE behaviour (mode 1 or 2) may therefore display garbage on these machines. Code that sticks to mode 0/3 with bits 2–3 works on all of them.

Troubleshooting

  • Instant lock-up after a POKE to 54017: you cleared bit 0 (OS ROM off with no RAM OS) or bit 1 within BASIC, or your program extends into $4000–$7FFF. Use the formula values, and keep programs short or relocate them.
  • The self-test screen or garbage appears at $5000: bit 7 was cleared, so the self-test ROM is overlaying the window. The self-test ROM takes priority over extended RAM in that region.
  • The RAMdisk lost its files: your program wrote into the banks DOS 2.5 uses for D8:.
  • Works on a 130XE, not on an upgraded 800XL: the expansion lacks separate ANTIC access or uses different bank bits.

Further reading

  • Mapping the Atari, Revised Edition by Ian Chadwick (the first edition was published by Small System Services, 1983): location 54017 and Appendix 16, "Memory Management on the 130XE".
  • Atari 130XE Machine Language for the Absolute Beginner (Melbourne House, 1985).
  • The Atari 130XE Handbook by Peter Lupton and Frazer Robinson (Century Communications, 1985).
  • Altirra Hardware Reference Manual (Avery Lee), Chapter 2, extended memory.

Credits