Adventures in the RAM Jungle and Other Mysteries [Conclusion]

Author(s): Earl Dunnington
Pub Details: Time Designs Magazine v2 n2 (Volume 2, Issue 2)
Date: January/February 1986
Pages: 9-12

The results presented in this article were obtained with a T/S 1500 and may differ from those obtained with the T/S 1000 without a 16K RAMPACK.

The readers with a T/S 1500 and those with a T/S 100 who own 3.25K or more of RAM have their choice of three trails (Operating Modes) they can use to traverse the RAM Jungle:

1. Both the Actual RAMTOP and the system variable RAMTOP set at or above address-19712.

2. Actual RAMTOP at or above address 19712, but the system variable RAMTOP POKEd to less than 19712, without ENTERing NEW.

3. Both the Actual RAMTOP and the system variable RAMTOP set below address 19712.

In all of the modes the program must be able to operate in the amount of RAM, to which Actual RAMTOP is set. Actual RAMTOP is one address higher than the top of the GOSUB stack (GS stack). Both Mode No.1 and Mode No.3 have a sub-mode available where the system variable RAMTOP is POKEd to an address less than the Actual RAMTOP whithout ENTERing NEW. The T/S 1000 without an additional RAMPACK, has 2K RAM (maximum Actual RAMTOP address 18432) and can only use Mode No. 3 and Submode 3. The Submodes are used in a program when it is designed to store data or machine code in the Safe Area with NEW in a program line so that the strings and variables, along with the program, will be destroyed, in order that a new program can be LOADed that will utilize the data or machine code. Mode No.2 can also be used for this purpose. The main use of Mode No.2 is to save memory when a program will not operate in less than 3.25K RAM and a full screen is not used in the program. Mode No.2 can also be used when recording a program in order to save time or tape, as there are 768 bytes less in the display file to be recorded. Mode No.3 can also be used for this purpose. A program SAVEd while in Modes 2 or 3 can be LOADed into the computer when it is in Mode No.1.

Part One of this article (published SEPT/OCT ’85) covered the GOSUB and Machine stacks also determining the upper limit of the Safe Area. Part Two of the article (published NOV/DEC ’85) covered the Calculator Stack (C stack) and determining, while in Mode No.1, the lower limit of the Safe Area and the minimum setting for RAMTOP, that would allow the program to operate. If a program will not use a full screen display, RAMTOP can be set even lower, saving additional memory above RAMTOP for storage of data. To determine this absolute minimum setting for RAMTOP that will allow the program to RUN and the computer to act normally, we can use either Mode No.2 or, if the program will operated in less than 3.25K of RAM, Mode No.3. For the purposes of illustration of the features of Mode No.2 to those readers with 3.25K or more RAM, we will use this mode for them. Readers owning the T/S 1000 but only 2K RAM will also be able to join in RUNning the examples, and obtain similar results.

Readers with more than 16K RAM, set RAMTOP to 32768 as follows:

ENTER POKE 16388,0
ENTER POKE 16389,128
ENTER NEW

All those readers with 3.25K RAM or more are now ready to cross over the Great RAM Divide, located at address 19712, into the domain of less than 3.25K RAM. We will do this by POKEing the system variable RAMTOP to address 18432 (the same as if we had 2K RAM) without ENTERing NEW as follows:

ENTER POKE 16388,0
ENTER POKE 16389, 72
ENTER CLS

A minimal display file has now been set up, but Actual RAMTOP is still 32768. To prove this, ENTER the following: PRINT PEEK 18431 The top of the GOSUB stack marker, decimal no. 62, will not be printed on the screen. If you PRINT PEEK 32767 it will be printed on the screen.

To prove that a minimum display file has been set up, PEEK the system variables VARS and D_FILE by ENTERing the following:

PRINT (PEEK 16400+256*PEEK 16401)-(PEEK 16396+256*PEEK 16397)

The value displayed should be 25, the number of bytes in a minimum display.

To find the absolute minimum address to which we can set RAMTOP for Program One that we used as an example in Part Two of this article, we must type in the program and SAVE it on tape while in Mode No.2 (T/S 1000 2K RAM in mode No.3). Type in Program One as follows:

10 FOR N=1 TO 48
20 PRINT PEEK 16412+256*PEEK 16413;" ";
30 NEXT N

SAVE this program on tape. As we want a completely clean memory in order to determine the upper limit of the Safe Area for Program One while in Mode No.2 (or 3), turn off the computer. Then power up. Those with over 16K RAM, set Actual RAMTOP to 32768 as covered above. All but those with 2K RAM, POKE the system variable RAMTOP to address 18432 without ENTERing NEW as you did before. Everyone LOAD and RUN Program One. Your results should agree with Figure No.1. Each time through the loop the program prints the value contained in the system variable STKEND and illustrates how everything above the expanding display file is moved up in memory. Find the upper limit of the Safe Area by PEEKing the addresses below RAMTOP-50 until you find the start of the Addresses containing only zeros. This should be 32715 for Mode 2 or 18379 (Mode 3).

PEEKing either the system variable STKEND or STKBOT using the direct double PRINT PEEK command, you should get 16667. We will investigate the reason for this value later.

Looking at Figure No.1, the first two values in line one and in line nine are not the addresses of the top of the C stack. They are due to a “Giant Bug”, beacause the system variable STKEND is used by the Wicked Wizard of ROM for some other purpose than pointing to the address of the top of the C stack. What about the other values of Fig. No.1? Change line 20 of Program One to read:

20 PRINT PEEK 16410+256*PEEK 16411;" ";

Each time through the loop the value stored in the system variable STKBOT will be printed. RUN the program and your results should agree with Figure No.2. Each time an address is printed, the bottom and the top of the C stack are moved up another six addresses as the display file is expanded by that amount. Note the correlation between Figures No.1 and No.2. The addresses in Figure No. 1, other than the first two of lines 1 and 9, are the same as those in Figure No.2, and are actually the addresses of the bottom of the C stack. Therefore we cannot locate the top of the C stack while the program is RUNning by inserting a line in the program to PEEK STKEND.

16886 16892 16642 16648 16654 16
660 16666 16672 16678 16684 1669
0 16696 16702 16708 16714 16720
16726 16732 16738 16744 16750 16
756 16762 16768 16774 16780 1678
6 16792 16798 16804 16810 16816
16822 16828 16834 16840 16846 16
852 16858 16864 16870 16876 1688
2 17144 17150 16900 16906 16912

FIGURE NO. 1
16630 16636 16642 16648 16654 16
660 16666 16672 16678 16684 1669
0 16696 16702 16708 16714 16720
16726 16732 16738 16744 16750 16
756 16762 16768 16774 16780 1678
6 16792 16798 16804 16810 16816
16822 16828 16834 16840 16846 16
852 16858 16864 16870 16876 1688
2 16888 16894 16900 16906 16912

FIGURE NO. 2

Let us investigate why we get 16667 as the bottom of the C stack using the direct command to PEEK STKBOT or STKEND when the programs print the last value as 16912. When a direct command is ENTERed, the first thing that happens is the screen is CLEARed. As we are dealing with a minimal display file, the C stack is moved down before the command picks up the value stored in STKEND or STKBOT, so we must add the number of characters on the screen to the 16667. The last six characters (including the space) are printed after the value is picked up. Therefore we have: 9*32-6=282 characters

The direct double PEEK command, as we found in Part Two of this article, increases the result by thirty-seven, so it must be subtracted. Therefore: 16667+282-37=16912 Which is the same as the last value of Figures No.1 and 2. We can locate the maximum address of the top of the C stack during the RUNning of the program by laying down some fly-paper with Program Two of Part Two of this series. Changing line 10 to fit the new addresses, the program now reads as follows:

10 FOR N=16912 TO 17150
20 POKE N,5
30 NEXT N

N is from the last address to the highest address in Fig. No.1. The program places a 5 in each address from 16912 to 17150.

1) Type into the computer lines 10 and 20 of Program Two which will wipe out these lines of the previous program. 2) RUN the program 3) ENTER CLEAR 4) LOAD Program One from tape 5) RUN Program One 6) ENTER CLEAR 7) In Mode No.2 (or 3), with 288 bytes of screen display used, we do not have to delete any program lines.

8) Using the direct command: PRINT PEEK n Where n is the address to be PEEKed, find the highest address whithout a 5. This address should be 16932 and is the top of the C stack when Program One is RUN and the computer is in Mode No.2 (or 3).

The formula for computing the minimum address to which we can set RAMTOP and have the program RUN and the computer act normally, determined while in Mode No.2 (or 3), is: Top of the C stack+Actual RAMTOP-Upper limit of Safe Area+36 Substituting the addresses we found:

For Mode 2
16932+32768-32715+36=17021
For Mode 3
16932+18432-18379+36=17021
The High Byte of 17021 is:
INT(17021/256)=66
The Low Byte of 17021 is:
17021-256*66=125
To set RAMTOP to 17021:
ENTER POKE 16388,125
ENTER POKE 16389,66
ENTER NEW

LOAD Program One from the tape and the program will RUN properly.

As repetition is the core of learning, let us find the minimum setting for RAMTOP of a more practical program. Figure No.3 is an outline for a “bare bones” text entry program. When complete, it will display the text as it is entered, and store the text above RAMTOP. Line 80 redefines the ENTER key as the SPACE key, because both the PAUSE and INKEY$ functions turn the SPACE key into BREAK when shifted or unshifted. To quit text entry, use STOP. The STOP is stored as the end of the text marker (line 100). The minimum value for A in line 10 is what we need to determine.

  5 FAST
10 LET A=?????
20 LET B=INITIAL RAMTOP-1
30 LET C=1
40 SCROLL
50 FOR N=A TO B
60 PAUSE 32767
70 LET D=CODE INKEY$
80 IF D=118 THEN LET D=0
90 POKE N,D
100 IF D=227 THEN STOP
110 IF C<33 THEN GOTO 140
120 SCROLL
130 LET C=1
140 PRINT CHR$ PEEK N;
150 LET C=C+1
160 NEXT N

FIGURE NO. 3

1) To avoid crashing the computer, temporarily change line 90 to:

90 POKE 16393,D

This continually POKEs the text to an unused location in the system variables area. We must also change line 140 to:

140 PRINT CHR$ PEEK 16393;

2) Power up, ENTER line 90, and find the no. of bytes in the changed line by PEEK-ing 16511. In the same manner, find the mumber of bytes in the original line 90. The difference is 10. Find the difference between the changed line 140 and the original line 140, the difference is also 10.

3) Assuming we do not know if the program will RUN in less than 3.25K RAM, readers with this amount or more should chose Mode No.2 in determining the minimum setting for RAMTOP. To set Mode No.2, ENTER:

POKE 16388,0
POKE 16389,72
CLS

4) The Final value for variable A in line 10 will be the minimum setting for RAMTOP+1. Temporarily, since we will be working in Mode No.2 (or 3) and we need to exactly fill the screen with text, so that the display file will be expanded to the maximum for 22 lines, we will use A=B-703 bytes. Change line 10 to read: For Mode 2

10 LET A=32064

For Mode 3

10 LET A=17728

5) Change line 20 to read: For Mode 2

20 LET B=32767

For Mode 3

20 LET B=18431

6) Type the program as changed above into the computer and record the program on tape, while the computer is in Mode No.2 (or 3).

7) To insure a clean memory, turn off the power. Power up. Set Mode No.2 as in 3 above (3.25K RAM or over only). LOAD the “Text Entry” program. RUN the program, typing in text until the screen is filled and you get a 0/160 report. There is no cursor or correctional features!

8) Find the upper limit of the Safe Area by PEEKing the addresses near RAMTOP-50. This will be 32717 (T/S 1500 Mode No.2) or 18389 (T/S 1500 Mode No.3, Actual RAMTOP at 18432) As you can see, setting Mode 3 in the T/S 1500, not only sets up a minimum display file, but also changes the operating system as regards the M stack. To be on the safe side, never use a value for the upper limit of the Safe Area less than RAMTOP-51.

9) To find the initial value for N in the “Flypaper” program, ENTER:

PRINT PEEK 16404+256*PEEK 16405

The value of the address of E_LINE is 16838 To this add 704 which is the number of addresses E_LINE is moved up when the display file is expanded. The top of the C stack should never be more than 50 addresses higher than the actual address of E_LINE during the program RUN.

10) To wipe out the program, turn the power off and then on. Set Mode No.2 as in (3) above. Type in the modified “Flypaper” program:

10 FOR N=17542 TO 17592
20 POKE N,5
30 NEXT N

RUN this program. 11) ENTER CLEAR, LOAD and RUN the Text Entry program, typing in text until you get a 0/160 report. PEEK up from 17542 to find the top of the C stack. THis should be 17551. Correcting this value for the extra 20 bytes we added to the program, the final value for the top of the C stack, is 17531.

12) Substituting in the formula for the minimum address to which we can set RAMTOP in Mode 2 (or 3): Top of the C stack+Actual RAMTOP-upper limit of Safe Area+36 we get 17618. Set RAMTOP to this value:

POKE 16388,210
POKE16389,68
NEW

13) LOAD Text Entry program and change the following lines:

10 LET A=17619
90 POKE N,D
140 PRINT CHR$ PEEK N;

14) SAVE and RUN. You can type in 15149 characters that will be stored above RAMTOP (16K). 813 with 2K RAM.

 5 FAST
10 LET A=?????
20 LET B=INITIAL RAMTOP-1
30 FOR N=A TO B
40 IF PEEK N=227 THEN STOP
50 LPRINT CHR$ PEEK N;
60 NEXT N

FIGURE NO. 4

Figure No. 4 is a companion program to be loaded after text entry, to print the text on a printer.

I hope you have enjoyed your Safari in the RAM Jungle as much as I have, being your guide.

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