Classic Computer Magazine Archive COMPUTE! ISSUE 78 / NOVEMBER 1986 / PAGE 81

64 RAM EXPANDER

Alton C. Williams

Would you like to expand your Commodore 64's memory without hardware add-ons? This short utility program opens an extra 16K of memory for storing machine language programs which can be executed with a normal SYS from BASIC. It doesn't really change your 64—the memory has always been there, hidden under the computer's ROM chips. But the program does make this memory easily accessible from BASIC, almost as if you'd gotten a free 16K memory upgrade.

The Commodore 64 has more memory than meets the eye. However, most machine language programs use the 4K section of RAM located at 49152–53247 ($C000–$CFFF) or the small 191 bytes of cassette buffer space at 828–1019 ($033C–$03FB). That's not enough room for very long programs. And since many 64 programs use these same areas, it's hard to find a place for a new machine language program without consuming some of BASIC RAM, which in turn leaves less room for a BASIC program and variables.

Fortunately, there's another solution. The 64 has a full 16K (16,384 bytes) of RAM underlying the BASIC and Kernal ROM chip addresses. The BASIC language interpreter is located in the 8K section from 40960–49151 ($A0OO–$BFFF), and the Kernal operating system is located in the 8K section from 57344–65535 ($E000–$FFFF).

To use this extra memory from BASIC, however, is not easy—one reason why it's often called hidden RAM. If you SYS to an address in this area from BASIC, the computer executes the instructions recorded in the ROM addresses, ignoring the contents of the hidden RAM. For instance, SYS 59626 calls the Kernal ROM routine that scrolls the screen up one line. SYS 58726 calls the ROM routine to home the cursor, and so on.

"64 RAM Expander" opens up new programming space by allowing you to SYS to a machine language program stored in underlying RAM. It doesn't really expand your 64's memory (the RAM has been there all the time), but the effect is the same as if you suddenly gained 16K of extra memory space.

Expand Your RAM

Type in and save a copy of 64 RAM Expander (Program 1). To use the program, simply load and run it. Now you can install any machine language program designed to reside in the hidden RAM. Use the statement SYS 920 to tell the computer that subsequent SYS statements will use the hidden RAM area. To return SYS to normal, use the statement SYS 931. These two commands allow you to switch the hidden RAM in and out at will.

When you SYS to hidden RAM, 64 RAM Expander switches out the ROM overlying the area where the program is located. If your program doesn't call BASIC or Kernal routines, that poses no problems. But BASIC ROM routines are not available when you SYS to a program located under BASIC (however, you may still call Kernal ROM routines). If your program is located under the Kernal ROM, neither BASIC nor Kernal routines are available, since switching out the Kernal ROM switches out BASIC as well.

This program occupies the cassette buffer area from memory locations 920–1018 ($0398–$03FA). Do not use those locations when 64 RAM Expander is installed.

A Short Demonstration

Program 2 is a short BASIC loader which demonstrates 64 RAM Expander. Load and run 64 RAM Expander; then load and run Program 2. It POKEs a short machine language routine into the RAM underlying BASIC ROM, activates 64 RAM Expander with SYS 920, then activates the hidden ML program with a SYS to location 41000 (SA in line 90). The program cycles the screen border colors until you press a key.

If you're not familiar with bank switching on the 64, you may well wonder how one writes a program designed to live in the RAM under ROM. Most machine language monitors see only ROM at those addresses, since the monitor itself typically needs ROM routines to function. One way is to write fully relocatable code, which executes the same way no matter where it loads into memory. The longer the program, however, the more difficult it becomes to preserve full relocatability. Another way is to write the program in a more convenient area, then adjust all the absolute addresses by hand. Again, that's a tedious business for all but the simplest programs. The best solution is to use a good machine language assembler which allows you to assemble object code to a disk file rather than to memory. Once the object file has been created, you can load it with ,8 ,1. Like POKEs from BASIC, a relocating load stores data in underlying RAM rather than ROM.

For instructions on entering these listings. please refer to "COMPUTE!'s Guide to Typing In Programs" in this issue of compute!.

Program 1: 64 RAM Expander

KE 10 PRINT"{CLR}" : T = 0 : FOR I = 920 TO 1018: READ X : T = T+X : POKEI, X: NEXT
FC 20 IF T <> 9573 THEN PRINT "ERROR IN DATA STATEMENTS !! ":END
FM 30 PRINT "EASY 16K IS NOW READY FOR USE! " : END
ES 31 DATA 169, 174, 141, 8, 3, 169, 3, 141, 9, 3, 96, 169, 228, 141, 8, 3, 169
PG 32 DATA 167, 141, 9, 3, 96, 32, 115, 0, 8, 201, 158, 240, 4, 40, 76, 231, 167
EM 33 DATA 40, 32, 115, 0, 32, 138, 173, 32, 247, 183, 24, 165, 21, 105, 32, 144, 8
QX 34 DATA 120, 169, 48, 133, 1, 76, 227, 3, 165, 21, 105, 64, 176, 10, 165, 21, 105
GD 35 DATA 96, 144, 4, 169, 54, 133, 1, 169, 243, 56, 233, 1, 170, 169, 3, 233, 0
PD 36 DATA 72, 138, 72, 108, 20, 0, 169, 55, 133, 1, 88, 76, 174, 167

Program 2: RAM Expansion Demo

CS 10 REM POKE PROGRAM INTO RAM UNDER BASIC ROM
GB 20 ADR = 41000 : SA = ADR: CHK = 0
EF 30 READ BYTE: IF BYTE <> 256 THEN POKE ADR, BYTE : ADR=ADR+l : CHK = CHK+BYTE: GOTO 30
RD 40 IF CHK <> 1577 THEN PRINT {SPACE}"ERROR IN DATA STATEMENTS" : END
QE 50 SYS 920 : REM TURN ON RAM {SPACE}EXPANDER
BG 60 PRINT : PRINT
DX 70 PRINT "NOW RUNNING ML PROGRAM UNDER BASIC ROM."
ED 80 PRINT "PRESS ANY KEY TO {SPACE} QUIT."
EA 90 SYS SA : REM START OUR PROGRAM
CD 100 DATA 238, 32, 208, 32, 228, 255, 240
DS 110 DATA 248, 96, 256