Automatically Set RAMTOP Without Destroying the Program

How would you like to be able to set RAMTOP within your program? I was using a program “HEXEDIT” published by a former popular T/S magazine, when the program and the computer locked up. The author erroneously thought that merely poking the system variable “RAMTOP”, in his program, would reset RAMTOP to a new location. This led to my research to find a method for setting RAMTOP within a T/S 1000 or 1500 program. The results of my research are presented in this article.

To see what happened with Doctor Hexedit’s program, type the listing of figure No. 1 into your computer. Run the program. Wait about 30 seconds and watch the “inverse ? monster” eat up the program. You will have to pull the plug to unlock the computer. A modification of lines 50 to 80 are used in figure No. 3 as a test to see that RAMTOP has actually been moved. In order to understand why the program was destroyed, you must know the structure of the memory map of the RAM in more detail than the diagram in your users manual. The T/S 1500 manual diagram shows USR routine space above RAMTOP. This is not the case unless the additional 16k Timex Rampack is attached or you have lowered RAMTOP.

Turn on your computer. Using immediate commands only PEEK into the system variable RAMTOP to determine its address. Then PEEK into each of the four Bytes under RAMTOP. Reading from the highest address down, you should find the decimal values: 62, 0, 6, and 118. The 62 is a flag to tell the computer “This Is The Top Of The GOSUB Stack”. The zero and the six are flags to tell the computer to generate a code seven error. The 118 is the code for ENTER (N/L). When we poked 24 into each of these four bytes with the crash demonstration program, the computer went crazy trying to figure out what to do.

--------------------------------
COMPUTER CRASH DEMONSTRATION
--------------------------------
8K ROM,ANY RAM
--------------------------------
10 REM COMPUTER "CRASH" DEMO
BY DUNNINGTON
20 SLOW
25 REM FIND PRESENT VALUE OF
RAMTOP
30 LET RAMTOP=PEEK 16388+256*P
EEK 16389
35 REM DR. "HEXEDIT" THINKS HE
SET RAMTOP AUTOMATICALLY TO
18176 IN HIS LINE 9710 BY THE
FOLLOWING POKE:
40 POKE 16389,71
45 REM TEST TO SEE IF RAMTOP
IS STILL AT THE ORIGINAL VALUE
BY POKING 24 INTO EACH OF THE 4
BYTES BELOW ITS ORIGINAL VALUE
50 FOR N=RAMTOP-4 TO RAMTOP-1
60 POKE N,24
65 REM PRINT ALLOWS YOU TO SEE
WHAT IS HAPPENING
70 PRINT
80 NEXT N

FIGURE NO. 1

Since RAMTOP had not really been lowered, when a large amout of data was entered, the HEXEDIT program over-ran the four bytes instead of giving an out of memory code 4 error report.

Peek into the system variable “ERR–SP”. You will find the address of the fourth byte below RAMTOP. The machine Stack Pointer, also known as SP, is a two byte internal set of registers that cannot be peeked into from BASIC. If we could then you would also find stored there the address of fourth byte below RAMTOP.

   1 REM 1234567890123456789012345
6789
2 REM CHANGE RAMTOP PROGRAM,
PART 1 BY EARL DUNNINGTON
3 REM FOR 16K RAM CHANGE LINE
20. THE 8TH VALUE TO 186 AND
9TH VALUE TO 119
4 REM FOR 2K RAM CHANGE LINE
20.THE 9TH VALUE TO 071
10 FAST
20 LET A$="205,035,015,205,042
,010,033,000,192,034,004,064,043
,054,062,043,054,000,043,054,006
,043,054,118,034,002,064,249,201
"
30 LET A=1
40 FOR N=16514 TO 16542
50 LET B=A+2
60 POKE N,VAL A$(A TO B)
70 LET A=A+4
80 NEXT N
90 REM TO CHANGE TO A VALUE OF
RAMTOP OTHER THAN 49152, REPLACE
THE EIGHTH VALUE IN LINE NO. 20
WITH THE LOW BYTE AND THE NINTH
VALUE WITH THE HIGH BYTE

FIGURE NO. 2
Timex Sinclair 1000 computer
Timex Sinclair 1000 computer

The OP codes of the USR subroutine that is poked into the line 1 REM statement by the listing in Figure No. 2 are as follows:

16514 CD230F    CALL 3875        :SET FAST MODE
16517 CD9A14    CALL 2602        :CLEAR SCREEN
16520 21BA77    LD HL, 49152     :LET HL=DESIRED RAMTOP
16523 220440    LD (16388),HL    :SET SYSTEM VARIABLE RAMTOP TO
                                  49152
16526 2B        DEC HL           :LET HL=HL-1 OR 49151
16527 36E3      LD (HL), 62      :POKE THE ADDRESS IN HL WITH 62
16529 2B        DEC HL           :LET HL=HL-1 OR 49150
16530 3600      LD (HL), 00      :POKE THE ADDRESS IN HL WITH 0
16532 2B        DEC HL           :LET HL=HL-1 OR 49149
16533 3606      LD (HL), 06      :POKE THE ADDRESS IN HL WITH 6
16535 2B        DEC HL           :LET HL=HL-1 OR 49148
16536 3676      LD (HL), 118     :POKE THE ADDRESS IN HL WITH 118
16538 220240    LD (16386),HL    :SET ERR--SP TO 49148
16541 F9        LD SP,HL         :SET STACK POINTER TO 49148
16542 C9        RET              :RETURN TO BASIC PROGRAM
   1 REM LN 77?LN E▒5 ""6▒RNDF0YF
0 F0▒F0
8706 TAN
2 REM CHANGE RAMTOP PROGRAM
PART 2 BY EARL DUNNINGTON
3 REM FOR 2K RAM DO NOT TYPE
IN LINES 2,3,4,60,70,AND 100 TO
150.
TO CHANGE TO A VALUE OF RAMTOP
OTHER THAN 49152, IF NOT ALREADY
DONE IN PART 1, POKE 16521 WITH
THE LOW BYTE AND POKE 16522 WITH
THE HIGH BYTE, BEFORE SAVING THE
PROGRAM.
4 REM SAVE THE PROGRAM ON
TAPE USING THE COMMAND GOTO 9910
RELOAD THE PROGRAM FROM TAPE
USING THE COMMAND LOAD "RAMTOP"
5 RETURN
10 SLOW
20 FOR N=1 TO 10
30 PRINT AT 17,7;"STOP THE TAP
E"
40 PRINT AT 17,7;"STOP THE TAP
E"
50 NEXT N
60 LET A=24
70 LET A$="RAMTOP IS NOW SET F
OR"
80 RAND USR 16514
90 GOSUB 5
100 PRINT A$;" ";PEEK 16388+256
*PEEK 16389,"THE VARIABLE A AND
THE STRING A$WERE NOT DESTROYED
NEITHER WAS THE PROGRAM."
110 PRINT AT 7,0;"RAMTOP TEST";
[illegible]
120 FOR N=32764 TO 32767
130 POKE N,A
140 PRINT PEEK N;" ";
150 NEXT N
9900 STOP
9910 SAVE "RAMTOP"
9920 GOTO 10

FIGURE NO. 3

Referring to Figure No. 3:

Line 1 is the REM line of figure No. 2 after the OP codes have been poked into it.
Line 5 can be any RETURN in your program
Lines 10 to 50 place the flashing STOP THE TAPE prompt on the screen.
Lines 60 and 70 store a variable and a string before the change in RAMTOP.
Line 80 calls the M/L subroutine to set up everything for the change in RAMTOP.
Line 90 in conjunction with line 5 activate the change of RAMTOP.
Line 100 prints the value of the new RAMTOP and the other remarks.
Lines 110 to 150 make the test to see that RAMTOP was moved.
Line 9900 keeps the program from going into an endless SAVE loop.
Line 9910 makes the program self running if the program is SAVED using GOTO 9910.
Line 9920 tells the computer where to start running the program.

The change in RAMTOP must not be activated in a subroutine or in a FOR/NEXT loop. You can only set RAMTOP as high as the first nonexistant byte above your particular RAM. You must change RAMTOP by a minimum of four bytes.

The formula for the high byte is:

HB=INT(n/256)

The formula for the low byte is:

LB=n-256*HB

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.