Moving RAMTOP in ZX-81/TS-1000

Author(s): Anthony Oresteen
Publication: ZXir QLive Alive!
Pub Details: ZXir QLive Alive! v4 n1 (Volume 4, Issue 1)
Date: Spring 1994
Pages: 22-24, 26

This article will explain how to change the amount of memory you have available on the ZX-81 and TS-1000 computers. More importantly it will explain how to use 32K and 64K RAM packs efficiently.

The ZX-81 (I will only refer to the ZX-81 but it all applies the TS-1000 as well) computer can have up to 48k RAM added that is LOADable and saveable from Basic. Since the ZX-81 doesn’t know how much memory is attached it checks to see how much memory is attached every time the power is turned on and the system self-boots. Unfortunately Sinclair never planned for more than 16K RAM of memory being added to the ZX-81 and initialization routine in the ROM does not check any memory above 32767 (the end of a 16K RAM pack). With a 64K pack attached you have memory locations up to 65535 but the ZX-81 will not know it and thinks that the memory is not there.

There are a number of RAM packs available (used and a few new) for the ZX-81. The most common sizes are 16K, 32K and 64K. Since the RAM area of the ZX-81 starts at 16384 and the highest memory location available is 65535 the most RAM that you can have is 65535- 16383 bytes or 49152 bytes on the ZX-81. As 1k of memory is 1024 bytes, 49152 is referred to as 48K. Some “64k” packs provide RAM UP TO the 64K location of 65535 and thus are called “64K” packs but actually have only 48K RAM while others provide 64K RAM from 0 to 65535 and allow you to switch in areas of RAM from 0 to 16383. Note that the ROM lies in the 0 to 8191 area and you will run into problems if you try to use that area. The area of 8192 to 16382 can have RAM but you can NOT put BASIC programs there so it’s generally used for machine code routines and other accessory items. The Memotech 64K RAM pack is the unit I use and it will allow you to switch in areas below 16384 if you need that area. Some 32K packs will let you add an additional 16K pack to get a full 48K ram up to 65535 (the Memotech is one that will do this with no problems).

The ZX-81 has a number of system variables that keep track of where everything is in the computer. One system variable is called RAMTOP. It is located at 16388 and 16389. RAMTOP tells the computer how much memory is available. The computer takes the number stored in 16389, multiplies it by 256 and then adds it the number in 16388. This tells the computer the location of the first memory location that is not there. With a 16K pack attached you will get 128 if you enter PEEK 16389 and 0 for PEEK 16388. Thus 128 x 256 + 0 = 32768 for RAMTOP. By putting different numbers in 16388 and 16389 with the POKE command the computer will know how much memory you are using (well, not quite but more on this later).

There are three basic reasons that you would want to change RAMTOP:

1. To lower RAMTOP to provide room for machine code routines

2.that can not be overwritten by BASIC programs.

3. To lower RAMTOP to speed up save and load times.

4. To raise RAMTOP to make more memory space available to the computer.

It would appear that all you would have to do is to POKE in new numbers in 16388 and 16389 and RAMTOP is changed. Sadly this is not true. The computer will not fully recognize a RAMTOP change unless the command NEW is executed after the new value is placed in RAMTOP. The drawback is that NEW wipes out all of your Basic program and clears the variables area. The reason for this is that the machine stack and it’s pointer (ERR_SP at 16386 &16387) are still in the old area of memory, blocking expansion of the program and variables area (see page 128 of the Sinclair manual for a diagram). NEW will move these items to just below the new RAMTOP value and the memory is opened up (or reduced as the case may be) for programming.

Method 1 to move RAMTOP.

The general format for method 1 is:

POKE 16388, L     (low value portion)
POKE 16389, H     (high value portion)
NEW

To find values of H & L first determine where you want RAMTOP to be. Let’s assume that you are using a 32K RAMPAK and you want to use every bit of memory. 32K ends at 49151, so divide 49151 by 256. This gives 191.99609. Round the number down to the nearest whole number and you have the value of H or 191. Now multiply 191 by 256 and you get 48896. Subtract 48896 from 49151 and you will get 225 or L. Now you can enter the following:

POKE 16388, 255
POKE 16389, 191          (32K values)
NEW

If all this math bothers you, just enter RAND 49151 in the immediate mode. The values of SEED at 16434 &16435 will have the correct values for L & H. The way that you could use this approach is:

RAND 49151
POKE 16388, PEEK 16434
POKE 16389, PEEK 16435
NEW

For a 64K pack the values of L & H are 255 and 255.

Method 2 to move RAMTOP.

In this method we will use a machine code routine that will do all the moving without using NEW. Thus we can execute a RAMTOP moving routine from within a program, without losing control of the computer (executing NEW from within a program wipes the program out and gives the K cursor).

You can use this program as a start-up program. For now let’s assume that the RAMTOP routine is in line 10:

10 REM 00000000000000000
20 RAND USR 16514
30 LOAD ""
40 STOP
50 SAVE "64KRAMTOP"
60 RUN

If you save the program by ENTERing GOTO 50 in the immediate mode the program will self-start the next time you load it. It will load the next program on the tape automatically, and the proper value of RAMTOP will have been set (provided we insert the machine code routine in line 10 after the REM). If the second program is also self starting then for all practical purposes you will have LOADed only one program.

The machine code routine will do the following:

a. Set new RAMTOP value
b. Move the machine stack
c. Set new ERR_SP value

LISTing 1 is the machine code routine. LISTing 2 is a byte-by-byte LISTing in decimal. Simply enter the first line of your program as 1 REM with at least 32 zeros following, then poke in each value starting with 16514. Then enter lines 20 to 60 as needed.

Rand USR 16514 tells the computer to go to memory location 16514 and start executing the machine code instructions until it comes to a number 201 which means return. Since the routine started from Basic it will return to Basic and execute the next basic line. A feature of the ZX-81 operating system is that the first line of Basic always starts at 16509. By making the first line a REM line with machine code instructions, we will always have the first machine code instruction at 16514.

To change the machine code routine to 32K, change 16516 to 191 and line 50 to: SAVE “32KRAMTOP.”

Method 3 to move RAMTOP using COMPUSA Disk Drive

When using the Compusa Floppy Disk Interface RAMTOP moves become very important. Many frustrating hours can pass trying to load a 16K Sinclair program that has a lot of machine code on to disk. The Compusa DOS (version .5) copies the disk directory into RAM. To ensure that these DIRectories are never written over by Basic, on turning on the power the CDOS lowers RAMTOP by 1388 bytes and resets all pointers. Unfortunately the Compusa DOS (CDOS) doesn’t check to see if you have more than 16K ram. If you are using 64K ram you must move RAMTOP as before, move the disk directory, and move the disk directory pointer. Then the CDOS will allow you to operate without getting lost.

The CDOS directory pointer is ALWAYS located at the byte pointed to by RAMTOP and the byte following. Wherever these two bytes point to the CDOS will look for the disk directory. If the directory is not there the system will crash and you will have to power down and then start over again. In a 16K system the pointer & RAMTOP are as follows:

16388 147  RAMTOP points to 31379
16389 122
31379 149  CDOS pointer points to 31381
31380 122
31381  CDOS directory starts here

I use the following locations for RAMTOP when using 64K and 32K packs:

For 64K: 16388, 147  RAMTOP at 64147
         16389, 250
         64147, 149  DIRectory at 64149
         64148, 250

For 32K: 16388, 147  RAMTOP at 47763
         16389, 186
         47763, 149  DIRectory at 47765
         47764, 186

These locations put the DIRectories at the top of memory and free up the most RAM for programming. While you could poke in the four new values, execute NEW, and then do a GET DIRectory CDOS command, the following program will do it in machine code and you never give up control of the computer. LISTing 3 is the machine code program. LISTing 4 is a decimal byte-by-byte breakdown that you can poke into a REM statement that has 53 zeros after it. LISTing 5 is the Basic LISTing. This program is for 64K. If you have a 32K pack use LISTing 6 & 7 for the correct values and change line 20 in the Basic program to read: IF PEEK 16389<186 then GOTO 70.

LISTing 5 needs some explanation. Start up the system and ensure that the CDOS is at the 16K setting. Load the RAMTOP routine. Enter in the immediate mode: GOTO 50. Line 50 makes the program a self-starting CDOS program. Line 60 MUST be a GOTO, do not change to RUN. For some unknown reason using RUN in a self-starting CDOS program sends the computer off into never- return land. GOTO 10 keeps everything in control. Line 30 looks for a menu program on the disk. If you don’t have a menu then the CDOS will give you an error message and stop.

You should now have a good understanding on how the ZX-81 sets RAMTOP and how you can change it to suite your needs. All of the programs are tested and do work. Good luck and happy computing!

Anthony J. Oresteen
[address omitted]
[phone number omitted]

LISTING 1

4082~11FFFF      LD DE,FFFF
4085 2A0440 LD HL,(RMTP)
4088 2B DEC HL
4089 ED530440 LD (RMTP),DE
408D 014000 LD BC,0040
4090 EDB8 LDDR
4092 EB EX DE,HL
4093 AF XOR A
4094 ED52 SBC HL,DE
4096 54 LD D,H
4097 5D LD E,L
4098 39 ADD HL,SP
4099 F9 LD SP,HL
409A 2A0240 LD HL,(ERSP)
409D 19 ADD HL,DE
409E 220240 LD (ERSP),HL
40A1 C9 RET

LISTIG 2

4082~11          17 16514 )
4083 FF 255 16515 COPY
4084 FF 255 16516 COPY
4085 2A 42 16517 E
4086 04 RMTP 4 16518
4087 40 64 16519 RND
4088 2B 43 16520 F
4089 ED 237 16521 GOSUB
408A 53 83 16522
408B 04 RMTP 4 16523
408C 40 64 16524 RND
408D 01 FLGS 1 16525
408E 40 64 16526 RND
408F 00 0 16527
4090 ED 237 16528 GOSUB
4091 B8 184 16529 s
4092 EB 235 16530 FOR
4093 AF 175 16531 j
4094 ED 237 16532 GOSUB
4095 52 82 16533
4096 54 84 16534
4097 5D 93 16535
4098 39 57 16536 T
4099 F9 249 16537 RAND
409A 2A 42 16538 E
409B 02 ERSP 2 16539
409C 40 64 16540 RND
409D 19 25 16541 ;
409E 22 34 16542 6
409F 02 ERSP 2 16543
40A0 40 64 16544 RND
40A1 C9 201 16545 TAN

LISTING 2A

  0~REM ) COPY COPY ERNDF GO[illegible]
B ?RNDRND GOSUB s FOR j GOSU[illegible]
???T RAND ERND;6RNDTAN
ANTHONY J ORESTEEN 64K RAM B[illegible]

10 RAND USR 16514
20 IF PEEK 16389<255 THEN G[illegible]
70
30 LOAD ""
40 STOP
50 SAVE "64KRAM"
60 GOTO 10
70 PRINT "RAMTOP ERROR. PLE[illegible]
RE-START."

LISTING 3

4082~AF          XOR A
4083 ED737B40 LD (HZET),SP
4087 ED5B7B40 LD DE,(HZET)
408B 21FF7F LD HL,7FFF
408E ED52 SBC HL,DE
4090 44 LD B,H
4091 4D LD C,L
4092 21FF7F LD HL,7FFF
4095 11FFFF LD DE,FFFF
4098 EDB8 LDDR
409A 110080 LD DE,8000
409D 2A7B40 LD HL,(HZET)
40A0 19 ADD HL,DE
40A1 F9 LD SP,HL
40A2 2A0240 LD HL,(ERSP)
40A5 19 ADD HL,DE
40A6 220240 LD (ERSP),HL
40A9 2A0440 LD HL,(RMTP)
40AC 19 ADD HL,DE
40AD 220440 LD (RMTP),HL
40B0 2195FA LD HL,FA95
40B3 2293FA LD (FA93),HL
40B6 C9 RET

LISTING 4

4082~AF         175 16514 j
4083 ED 237 16515 GOSUB
4084 73 115 16516
4085 7B HZET 123 16517
4086 40 64 16518 RND
4087 ED 237 16519 GOSUB
4088 5B 91 16520
4089 7B HZET 123 16521
408A 40 64 16522 RND
408B 21 33 16523 5
408C FF 255 16524 COPY
408D 7F 127 16525
408E ED 237 16526 GOSUB
408F 52 82 16527
4090 44 68 16528
4091 4D 77 16529
4092 21 33 16530 5
4093 FF 255 16531 COPY
4094 7F 127 16532
4095 11 17 16533 )
4096 FF 255 16534 COPY
4097 FF 255 16535 COPY
4098 ED 237 16536 GOSUB
4099 B8 184 16537 s
409A 11 17 16538 )
409B 00 0 16539
409C 80 128 16540
409D 2A 42 16541 E
409E 7B HZET 123 16542
409F 40 64 16543 RND
40A0 19 25 16544 ;
40A1 F9 249 16545 RAND
40A2 2A 42 16546 E
40A3 02 ERSP 2 16547
40A4 40 64 16548 RND
40A5 19 25 16549 ;
40A6 22 34 16550 6
40A7 02 ERSP 2 16551
40A8 40 64 16552 RND
40A9 2A 42 16553 E
40AA 04 RMTP 4 16554
40AB 40 64 16555 RND
40AC 19 25 16556 ;
40AD 22 34 16557 6
40AE 04 RMTP 4 16558
40AF 40 64 16559 RND
40B0 21 33 16560 5
40B1 95 149 16561 @
40B2 FA 250 16562 IF
40B3 22 34 16563 6
40B4 93 147 16564 _
40B5 FA 250 16565 IF
40B6 C9 201 16566 TAN

LISTING 5

  0~REM j GOSUB ??RND GOSUB ??[illegible]
D5 COPY kGOSUB ???5 COPY k) CC[illegible]
COPY GOSUB s) E?RND; RAND ER[illegible]
D/6RNDERND;6RND5@ IF 6_ IF T
N A ORESTEEN 64K CDOS BOOT

10 RAND USR 16514
20 IF PEEK 16389<250 THEN GOT[illegible]
70
30 LOAD "A:MENU,P"
40 STOP
50 SAVE "A:64KDOS,S,P"
60 GOTO 10
70 PRINT "RAMTOP ERROR. PLEAS[illegible]
RE-START."

LISTING 6

4082 AF         ~XOR A
4083 ED737B40 LD (HZET),SP
4087 ED5B7B40 LD DE,(HZET)
408B 21FF7F LD HL,7FFF
408E ED52 SBC HL,DE
4090 44 LD B,H
4091 4D LD C,L
4092 21FF7F LD HL,7FFF
4095 11FFBF LD DE,BFFF
4098 EDB8 LDDR
409A 110040 LD DE,ERNR
409D 2A7B40 LD HL,(HZET)
40A0 19 ADD HL,DE
40A1 F9 LD SP,HL
40A2 2A0240 LD HL,(ERSP)
40A5 19 ADD HL,DE
40A6 220240 LD (ERSP),HL
40A9 2A0440 LD HL,(RMTP)
40AC 19 ADD HL,DE
40AD 220440 LD (RMTP),HL
40B0 2195BA LD HL,BA95
40B3 2293BA LD (BA93),HL
40B6 C9 RET

LISTING 7

4082~AF         175 16514 j
4083 ED 237 16515 GOSUB
4084 73 115 16516
4085 7B HZET 123 16517
4086 40 64 16518 RND
4087 ED 237 16519 GOSUB
4088 5B 91 16520
4089 7B HZET 123 16521
408A 40 64 16522 RND
408B 21 33 16523 5
408C FF 255 16524 COPY
408D 7F 127 16525
408E ED 237 16526 GOSUB
408F 52 82 16527
4090 44 68 16528
4091 4D 77 16529
4092 21 33 16530 5
4093 FF 255 16531 COPY
4094 7F 127 16532
4095 11 17 16533 )
4096 FF 255 16534 COPY
4097 BF 191 16535 z
4098 ED 237 16536 GOSUB
4099 B8 184 16537 s
409A 11 17 16538 )
409B 00 ERNR 0 16539
409C 40 64 16540 RND
409D 2A 42 16541 E
409E 7B HZET 123 16542
409F 40 64 16543 RND
40A0 19 25 16544 ;
40A1 F9 249 16545 RAND
40A2 2A 42 16546 E
40A3 02 ERSP 2 16547
40A4 40 64 16548 RND
40A5 19 25 16549 ;
40A6 22 34 16550 6
40A7 02 ERSP 2 16551
40A8 40 64 16552 RND
40A9 2A 42 16553 E
40AA 04 RMTP 4 16554
40AB 40 64 16555 RND
40AC 19 25 16556 ;
40AD 22 34 16557 6
40AE 04 RMTP 4 16558
40AF 40 64 16559 RND
40B0 21 33 16560 5
40B1 95 149 16561 @
40B2 BA 186 16562 u
40B3 22 34 16563 6
40B4 93 147 16564 _
40B5 BA 186 16565 u
40B6 C9 201 16566 TAN

LISTING 8

  0~REM j GOSUB ??RND GOSUB ??R
[illegible]D5 COPY kGOSUB ???5 COPY k) COP
[illegible] z GOSUB s) RNDE?RND; RAND ERN
[illegible];6RNDERND;6RND5@u6_uTAN
[illegible] ORESTEEN 32K CDOS BOOT

10 RAND USR 16514
20 IF PEEK 16389<186 THEN GOTO
70
30 LOAD "A:MENU,P"
40 STOP
50 SAVE "A:32KDOS,S,P"
60 GOTO 10
70 PRINT "RAMTOP ERROR. PLEASE
RE-START."

LISTING 9

4082~11FFBF      LD DE,BFFF
4085 2A0440 LD HL,(RMTP)
4088 2B DEC HL
4089 ED530440 LD (RMTP),DE
408D 014000 LD BC,0040
4090 EDB8 LDDR
4092 EB EX DE,HL
4093 AF XOR A
4094 ED52 SBC HL,DE
4096 54 LD D,H
4097 5D LD E,L
4098 39 ADD HL,SP
4099 F9 LD SP,HL
409A 2A0240 LD HL,(ERSP)
409D 19 ADD HL,DE
409E 220240 LD (ERSP),HL
40A1 C9 RET

LISTING 10

4082~11          17 16514 )
4083 FF 255 16515 COPY
4084 BF 191 16516 z
4085 2A 42 16517 E
4086 04 RMTP 4 16518
4087 40 64 16519 RND
4088 2B 43 16520 F
4089 ED 237 16521 GOSUB
408A 53 83 16522
408B 04 RMTP 4 16523
408C 40 64 16524 RND
408D 01 FLGS 1 16525
408E 40 64 16526 RND
408F 00 0 16527
4090 ED 237 16528 GOSUB
4091 B8 184 16529 s
4092 EB 235 16530 FOR
4093 AF 175 16531 j
4094 ED 237 16532 GOSUB
4095 52 82 16533
4096 54 84 16534
4097 5D 93 16535
4098 39 57 16536 T
4099 F9 249 16537 RAND
409A 2A 42 16538 E
409B 02 ERSP 2 16539
409C 40 64 16540 RND
409D 19 25 16541 ;
409E 22 34 16542 6
409F 02 ERSP 2 16543
40A0 40 64 16544 RND
40A1 C9 201 16545 TAN

32K RAM Boot