BANK SWITCHING — VIRTUAL MEMORY
This is the last instalment of this series and we’ll start with some improvements and corrections to the listing provided last month. I decided that some routines to save (and reload) bank data on tape should be included within the 2K operating system. These are necessary for back-up and because the memory in most banks is volatile. If you have a mass-storage device other than a cassette recorder similar procedures can be used.
The trouble, of course, was that there was not much room left in the 2048 bytes. It would have been possible to spill over into the next 2K block but that would have precluded fitting the whole thing in a single 2716 EPROM.
So some squeezing was necessary and as a result some of the prompts (which take up over 25 percent of the space) were shortened or combined. Other changes are: an error in the pack routine (which occurred when an empty bank was packed) has been corrected; the bank-change routine has been separated from the main program so that it can be called separately; and a new routine to determine the extent to which a bank is used has been included.
Those sections that have been changed are reprinted with starting and ending addresses in Figure 1. The entire memory map is listed in Figure 2. The various character strings are printed in Figure 3 and the BSOS system variables and their addresses are listed in Figure 4.
POINTS ARISING
Those of you who have modified the computer continuum board as described in an earlier part may find that the interface works better without the 4.7K pull-up resistor on the ROMCS’ line.
In the operating system currently described, the keybounce delay parameter is stored at 49118. The default value is stored at 8223. If you find that the delay is too short and that you are typing multiple characters then increase the value (to 16 or 20 for example). For a permanent change alter the default value at 8223. For a particular application change only the value in 49118.
HOW TO SAVE AND RELOAD A BANK
If you load the operating system from the cassette tape (2 minutes) the following things happen automatically: 1. RAMTOP is reset to 49110 (USR 8195); 2. The operating system (BSOS) is transferred from a REM statement at line 10 to the 8-10K block (USR 8966); 3. The directory is loaded from the string variable A$ to the 48-64K bank currently enabled (USR 9539); and 4. The system variables at 49110-49151 are set to their default values and the directory is brought down and displayed with the menu (USR 9720). If you load the program (listed last month with this month’s changes) by hand, then subsequent loads from cassette can be done in much the same way.
The time will come when you will want to save the programs or data in a bank on cassette tape (back it up!). Use a program such as this:
9010 CLEAR
9020 DIM A$(VAL "USR 8565")
9030 RAND USR 10223
9040 STOP
9050 RAND USR 8195
9060 RAND USR 9539
9070 RAND USR 9720
9080 STOP
9090 SAVE "BANK"
9100 GOTO 9050
You can store this program in the bank — then calling it down into the system will cause it to run automatically up to line 9040. (First the variables are cleared; then a string variable A$ is dimensioned to a size equal to the amount of bank actually used; then the bank is dumped into the string variable.) If you print A$ at this point you will see the beginning of the bank — the directory — on the screen. In some cases the program will load but will not autorun — in this case initiate the procedure by GOTO 9010.
Now start the recorder and execute GOTO 9090 (do not use RUN). The bank will be saved on tape together with whatever program was resident in RAM. The GOTO 50 statement in line 9100 ensures that the program will autorun upon reloading. When reloaded, the program resets RAMTOP, reloads the bank, and brings down the directory as described earlier.
VIRTUAL MEMORY — SOME EXPERIMENTS WITH BSOS
The possibility of boundless variables space, or larger-than-16K programs running automatically, was mentioned last month. Limitless memory addressed within a limited space is called “virtual memory”. What happens is that program segments are brought into the addressable space when they are required and then taken out when their job is done. The same with data. Such manipulations in a sophisticated virtual system are transparent to the user.
The routines necessary for the operation of a virtual memory system are included in the BSOS — so now you can run ZXPROFILE with a huge data base or run a program up to 80K bytes in length if you have the full complement of 4 banks. Even if you have only a 64K RAM pack some of these techniques are useful. Let’s look at a fairly simple way to implement a virtual memory system on a 64K TS1000/ZX81. Bear in mind that you cannot normally duplicate line numbers so let’s assign the 9000 to 9999 range as changeable. Note also that you must segment your program and write the pieces with due regard to global vs. local variables. This example is trivial — the program is not of sufficient size to require breaking up — but it is intended to illustrate the possibilities with much larger programs.
Put the following two subroutines in the bank. We’ll end each subroutine with a RETURN statement although you can, if you wish, use two GOTO statements in place of the GOSUB and RETURN.
Subroutine C DEC-TO-HEX
9000 DIM A$(4)
9010 LET N=VAL N$
9020 LET M=INT (N/256)
9030 LET L=N-256*M
9040 LET A$(1)=CHR$ (28+INT (M/16))
9050 LET A$(2)=CHR$ (28+M-16*INT (M/16))
9060 LET A$(3)=CHR$ (28+INT (L/16))
9070 LET A$(4)=CHR$ (28+L-16*INT (L/16))
9080 PRINT "DECIMAL ";N;" IN HEX IS ";A$
9090 RETURN
Subroutine D HEX-TO-DEC
9000 LET A=CODE N$(1)-28
9010 FOR N=2 TO LEN N$
9020 LET A=16*A+(CODE N$(N)-28)
9030 NEXT N
9040 PRINT "HEX ";N$;" IN DECIMAL IS ";A
9050 RETURN
These two routines translate a number from a decimal to a hexadecimal base and vice versa. The directory should look like:
BANK 1 DIRECTORY ================================ NO. NAME .EXT ADDR SIZE --- ---- ---- ---- ---- A: DIRECTORY .DIR:49152:00704: B: SAVEROUTINE.PRG:49856:00135: C: DEC-TO-HEX .PRG:49991:00308: D: HEX-TO-DEC .PRG:50299:00134: E: F: G: H: I: J: K: L: M: N: O: ================================ 1:SAVE 3:PURGE 5:PACK 7:ROOM 2:LOAD 4:RECLAIM 6:BANK 8:QUIT
Now enter a main program such as:
5 REM LN 775C7LN ** FOR E#RNDLN 7YC?0A
10 CLS
20 PRINT AT 8,0;"DECIMAL-HEX CONVERSION"
30 PRINT AT 11,0;"ENTER ""H"" FOR DECIMAL TO HEX"
40 PRINT AT 12,0;"ENTER ""D"" FOR HEX TO DECIMAL"
50 PRINT AT 13,0;"ENTER ANY OTHER KEY TO QUIT"
60 INPUT R$
70 IF R$="H" THEN POKE 16531,40
80 IF R$="D" THEN POKE 16531,41
90 IF R$<>"H" AND R$<>"D" THEN STOP
100 PRINT AT 16,0;"ENTER NUMBER TO CONVERT:"
110 INPUT N$
120 PRINT
130 GOSUB 8990
140 SLOW
150 PRINT AT 20,0;"PRESS ANY KEY TO CONTINUE"
160 IF INKEY$="" THEN GOTO 150
170 GOTO 10
8990 RAND USR 16514
16514 205 35 15 CALL FAST
16517 33 40 35 LD HL 9000
205 216 9 CALL LINE-ADDR
235 EX HL DE
42 12 64 LD HL D_FILE
205 93 10 CALL RECLAIM-1
16530 62 40 LD A file #
195 156 38 JP 9884
A routine to switch banks can be incorporated at the beginning (LD A bank#; CALL 9524) so that files may be recovered from any designated bank. The byte at 16531 is poked in the BASIC program lines 70 and 80. The byte poked in is the code for the letter for the required subroutine in the directory. For example, if ‘H’ is entered when the main program is run, then the DEC-TO-HEX routine is required, and this subroutine is C in the directory. So the code for C, or 40, is poked in line 70. In your own program the data byte poked could of course be a variable.
At line 130 the subroutine at 8990 is called. The USR call in line 8990 executes the code in the REM statement — it calls FAST, clears 9000-9999, and loads the required subroutine from the 48-64K bank. The subroutine itself is then executed and eventually a return is made to the main program at line 140. A important thing to remember is to do all transfers to and from the bank in FAST mode (the call for FAST mode is incorporated in the USR call for this reason).
Run the program and try a few examples. You will see that the exchange of subroutines (although admittedly they are quite small in this example) happens so quickly as to be unnoticeable. If you list the program after trying the hex-decimal conversion in each direction you will see that indeed the program lines 9000-9090 do change. The ability of a program to change itself depending upon what is required of it imparts a suggestion of intelligence to its operation.
FIGURE 1
Changes in the operating system
Make the changes in the order listed
Ranges are inclusive
8565-8577 205 35 15 205 132 33 124 6 192 144 71 77 201
8638 14
8641-8642 101 34
8818-8841 data shifted up one address; 8818 becomes 0
8966-8977 33 130 64 17 0 32 1 0 8 237 176 201
8986 115
9463-9517 new string: FILE WILL BE FLAGGED(PACK TO DELETE)(F)LAG OR (U)NFLAG?
9518-9538 205 224 37 205 90 32 1 95 28 144 230 3 60 23 23 23 129 79 237 121 201
9552-9636 data shifted up three addresses to 9555-9639
9539-9554 42 16 64 1 6 0 9 17 0 192 1 0 64 237 176 201
9640-9667 62 118 215 1 20 0 17 247 36 205 107 11 62 118 215 215 1 16 0 205 107
11 62 118 215 215 1 19
9680 83
9947 168
9990 61
10027 24
10089 89
10031-10036 5 205 46 37 24 15
10054-10178 data shifted down 17 addresses to 10037-10161
10180-10199 data shifted down 18 addresses to 10162-10181
10182-10185 151 190 40 27
10200-10224 data shifted down 14 addresses to 10186-10210
10211-10222 24 225 225 205 35 15 205 106 32 195 0 32
10223-10239 205 117 33 17 6 0 42 16 64 25 17 0 192 235 237 176 201
FIGURE 2 MEMORY MAP
Address Routine Hex Description 8195 SET-RAMTOP 2003 Sets RAMTOP to 49110 8224 TOT-SPACE 2020 Determines space available in system 8235 PRG-SPACE 202B Determines space available for program 8246 FILE-SIZE-1 2036 Determines size of a file in the system 8255 FILE-SIZE-2 203F Determines size of file other than display 8268 MOVOUT 204C Moves a file from system to bank 8282 KEYBOARD 205A Scans keyboard and puts value in A 8298 DSP-OUT 206A Moves display file from system to bank 8317 TOT-IN 207D Loads an entire system from bank 8331 PRG-IN 208B Loads a program from bank into system 8353 CLR-NAMESTR 20A1 Clears the name-string buffer 8380 PRT-NAMESTR 20BC Prints the name-string buffer 8397 INP-NAMESTR 20CD Inputs name-string from keyboard 8453 VAR-IN 2105 Loads variables file into system 8475 STR-TO-BC 211B Finds value of a numeric string 8486 TIMEOUT 2126 Delay loop 8565 BYTES-IN-BANK 2175 Determines amount of bank in use* 8580 FIND-BANKEND 2184 Finds first free byte in bank 8622 FILE-TYPE 21AE Determines file type 8631 PRT-ROOM 21B7 Prints available space in bank 8653 ROOM-LEFT 21CD Calculates space available in bank 8665 PRINT-HL 21D9 Prints value in HL 8667 PRINT-BC 21DB Prints value in BC 8674 ENTER-DATASTR 21E2 Puts data string into directory 8693 DSP-IN 21F5 Loads display file into system 8712 PRT-ADDR/SIZE 2208 Prints file bank address and size 8966 BSOS-LOAD 2306 Loads BSOS from REM statement to 8-10K 8978 PRT-PROMPT 2312 Prints prompt message on screen 8995 PRT-CONFIRM 2323 Prints confirmation request 9009 PRT-OVERFLOW 2331 Prints 'not enough room' message 9023 PRT-INVALID 233F Prints 'invalid extension' message 9037 SET-VARLMTS 234D Sets limits for variables save 9049 SET-TOTLMTS 2359 Sets limits for system save 9060 SET-PRGLMTS 2364 Sets limits for program save 9071 LOAD-LMTS 236F Loads file-start and file-end variables 9081 PRT-RANGE-1 2379 Prints line range prompt 1 9094 PRT-RANGE-2 2386 Prints line range prompt 2 9104 ENTER-LINENO 2390 Inputs line number from keyboard 9148 SIZE-TO-BC 23BC Puts size of stored file into BC 9155 START-OF-LINE 23C3 Determines address of first byte of line 9164 END-OF-LINE 23CC Determines address of last byte of line 9175 RANGE-LMTS 23D7 Sets limits for line range save 9298 PRT-SAVE 2452 Prints 'save' message 9311 PRT-LOAD 245F Prints 'load' message 9384 PRT-FILENO 24A8 Prints prompt for file number 9411 PRT-NFF 24C3 Prints 'no file found' message 9425 FIND-FILE 24D1 Finds address of designated file in bank 9449 PRT-NOROOM 24E9 Prints 'no room' message 9518 BANK-CHANGE 252E Changes banks 9539 BANK-LOAD 2543 Transfers bank from A$ to 48-64K 9640 PRT-PURGE 25A8 Prints prompt for purge routine 9673 PRT-RECLAIM 25C9 Prints reclaim message 9696 PRT-BANK 25E0 Prints bank change prompt 9720 MAIN 25F8 Initialization / directory display 9752 SAVE 2618 1. Save 9863 LOAD 2687 2. Load 9946 PURGE 26DA 3. Purge 9995 RECLAIM 270B 4. Reclaim 10032 BANK 2730 6. Bank 10046 ROOM 273E 7. Room 10058 PACK 274A 5. Pack 10223 BANK-SAVE 27EF Transfers bank from 48-64K to A$
FIGURE 3 CHARACTER STRINGS
Address Character string
8496 ENTER "FILENAME.EXT";
8517 EXECUTE SAVE (S) OR ABORT (A):
8547 ADDRESS:
8556 SIZE;
8795 NOT ENOUGH ROOM IN BANK
8819 PRESS ANY KEY TO CONTINUE
8843 ENTER THE LINE RANGE OR ENTER
(A)LL FOR THE ENTIRE PROGRAM
8903 FIRST LINE OR (S)TART:
8926 LAST LINE OR (E)ND:
8949 INVALID EXTENSION
9244 SAVE A FILE
9255 LOAD A FILE
9266 CHANGE BANKS
9278 ENTER BANK REQUIRED:
9324 ENTER LETTER (A TO O) OF FILE:
"Z" WILL REDISPLAY DIRECTORY
9398 NO FILE FOUND
9463 FILE WILL BE FLAGGED
9483 (PACK TO DELETE)
9499 (F)LAG OR (U)NFLAG?
9555 RECLAIM PART OF SYSTEM
9575 ENTER V TO CLEAR ALL VARIABLES
OR P TO CLEAR (PART OF) PROGRAM
FIGURE 4 SYSTEM VARIABLES
Address Name Hex Description 49110 FILE-START BFD6 System address of start of file 49112 FILE-END BFD8 System address of end of file 49114 STORE-LOC BFDA Storage location of file 49116 PRINT-POS BFDC Print position on screen (line/col) 49118 DELAY BFDE Delay parameter for keyboard debounce 49120 FIRST-BYTE BFE0 First free byte in bank 49122 DIR-ENTRY BFE2 Address for next entry in directory 49124 DATA-STRING BFE4 String containing directory entry data 49136 FILE-TYPE BFF0 First letter of file extension
TSH
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.