If you upgraded to 48K RAM for your ZX81, you may think it simple to add a few program lines and enlarge the variable arrays in existing 16K RAM programs to utilize the increased memory capacity. If you try this by setting RAMTOP to 65280 (for 48K RAM) and entering RUN, you get an error 4–not enough memory.
To understand what happens, refer to the ZX81 manual, chaps. 27&28. Note memory is partitioned into variably sized areas for storing different kinds of information. Before a program is entered, the area called SPARE occupies most of memory. As the program goes in, the area between D FILE and STKEND shifts upward and SPARE becomes smaller. Above SPARE are 3 additional areas, MACHINE stack (whose address is pointed to by ERR SP), GOSUB stack (whose beginning is 1 byte under RAMTOP), and USR routines (of no concern now).
When you enter a 16K program, RAMTOP is set at 32768, unless you choose a different value (see chap. 26 of the manual). ERR SP is set exactly 4 bytes below RAMTOP. The area between ERR SP and RAMTOP is reserved for the GOSUB stack. As the program runs, this area expands and contracts as the program goes to or returns from subroutines.
Since RAMTOP is a normally fixed value representing the first non-existent byte of memory, the first byte of the GOSUB stack is normally the last byte of RAM. Thus no provision is made to adjust the GOSUB stack even if RAMTOP is changed. So LOADing a 16K tape into 48K RAM does not automatically expand the available space.
Fortunately, the situation is easy to fix with a few entries in the immediate mode.
1. To set RAMTOP at the desired value (assumed to be 65280 here):
POKE 16388,0
POKE 16389,255
NEW
2. Test the RAMTOP value you just entered by:
PRINT PEEK 16388+256*PEEK 16389
This should give the value 65280.
3. LOAD your taped program in the normal way.
4. Enter in the immediate mode:
POKE 65279,62
POKE 65278,0
POKE 65277,6
POKE 65276,118
POKE 65386,252
POKE 65387,254
[Editor’s note: probably 16386/16387.]
These are the initial values that would exist had the program been entered with RAMTOP set at 65280.
5. Now clear the initial GOSUB stack and the first line of the machine stack. Enter:
POKE 32767,0
POKE 32766,0
POKE 32765,0
POKE 32764,0
You are now ready to expand that 16K program.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.