INTERMEDIATE SECTION J FILE INTER.J COMBINING FILES *************** All PCW programs, including BASIC programs, work within the Transient Program Area, a section of the computer's memory set aside for this purpose. The TPA, when empty, has about 61K of memory, and after the MALLARD BASIC interpreter (28K) has been loaded only 33K remains for your program. Any attempt to load a program greater than the available memory will be met with the message "Memory full" and the program will not be completely loaded. The same message will appear if the program 'grows' over the limit during execution. Thus the size of any MALLARD BASIC program is restricted to 28 or 30K. This is true of any Z80 based computer such as the PCW regardless of the total size of it's memory. Although a program of this size is quite a formidable project, there are times when it will not be big enough, and then it is usually possible to make it into two or more parts, switching each part into and out of the TPA as required. This course works on this principle. As you select a section from the main menu, the part of the course that you select is loaded for your use. Even this section has had to be split into two parts as you will have noticed. In addition all text has been typed in LOCOSCRIPT and put in ASCII text files, which are called up by the program as required. Thus several hundred K can be accessed from TUTOR.BAS, although you may have a short wait at times while the program discards one part and loads another. At no time can the part of the program in the TPA be over the size limit. Even though your projected program may fit into the TPA, a long program becomes unwieldy and difficult to follow. As the program grows, the time taken to "SAVE" it at intervals (good programming practice) and listing takes more time and is boring. The solution is to break the program down into "Modules" which can be saved as separate files and then combined later. This section discusses ways of performing the above tasks, introduces the relevant keywords and shows how to use them. The three main commands used are CHAIN, CHAIN MERGE, and MERGE. Which of these you use at any given time depends on what you need to achieve. In broad terms, CHAIN replaces the program already in memory by the new one and runs it directly it is loaded ; CHAIN MERGE combines the two programs and runs the combined program directly it is loaded, and MERGE combines the two programs in the TPA but doesn't run it. We shall now look into these three commands in greater detail. CHAIN If you have been running a program, another program can take ===== it's place in the TPA by pressing [STOP] to go into Direct Mode and then typing at the prompt:- CHAIN "Newprog ".(If no filetype is given,.BAS is assumed ) On pressing [RETURN] the new program will run automatically and the old program will have vanished from the TPA. The full command line is; CHAIN "Newprog",line-number, ALL where line-number is the line at which you want the program to commence running, and ALL retains all variables from the original program. Line-number and ALL are optional but if you want to use ALL but not line-number you must use two commas ; CHAIN "Newprog",,ALL or you'll get a rude error message. CHAIN MERGE It is often useful to write a program which contains a =========== number of standard functions like clearing the screen and common subroutines, and then call several subsiduary programs to work within it. CHAIN MERGE does this admirably. The full command line is :- CHAIN MERGE "Newprog", line-number, ALL, DELETE line-number-range Line-number and ALL are as for CHAIN, but DELETE is special to CHAIN MERGE. It allows you to delete lines in the original program where they could cause problems. Where the same line numbers exist in both programs the new lines replace the old. I have used CHAIN MERGE in TUTOR.BAS to call up INTRO, PRIMER etc. deleting lines 2000-30000 to make room for each program as you will see if you LIST it. Retaining Variables Both CHAIN and CHAIN MERGE offer the facility =================== to retain some or all variables. They offer ALL to retain all variables, but it doesn't seem to work for DEF FN (define function), although where a direct variable can be used to represent functions such as clearing the screen, reverse video etc, it works ok. This is easy to rectify, you simply have to repeat the DEF FN function in each program where it is required. If you don't want to retain all variables you can use the COMMON command. Suppose you have the following variables in one program :- a$="Anne"; b$=Basil; c$=Cecil; a=22; b=25; c=20 You want to retain a$, c$ and b only; Put in a program line such as 20 COMMON a$,c$,b and these variables will be retained across the CHAIN or CHAIN MERGE command whilst the others will be discarded. MERGE is of great interest for combining short programs or 'modules' ===== into a single, longer program. LOAD the first program then use the MERGE command to call the other programs in turn. You will know it is working by the whirring of the disc drive, but the programs will remain in memory until you decide to RUN or LIST them. MERGE can be written as a command line in a program but it's more obvious use is in DIRECT Mode. MERGE is less sophisticated than CHAIN or CHAIN MERGE. There is only the simple command MERGE "filename" ,( assuming a .BAS file ) It does nothing other than combine the files in memory. No variables can be retained, and if any line numbers of programs are the same the incoming file will replace those already in memory. This must obviously be borne in mind when setting up programs to use the command and the programs renumbered if necessary before MERGEing ************************************************ CHECKING MEMORY AVAILABLE When using MERGE or CHAIN MERGE it is ========================= helpful to be able to check program sizes and to ensure that there is room left in memory not only to fit the programs in but also to use them. Most programs have some operator participation, typing in text or figures perhaps, and this all takes up space in memory. A simple way to check program size is to load "Locoscript" and then to put your program disc in the drive, not forgetting to press f1. The size of all programs on the disc, rounded up to the nearest kilobyte (K) will be shown. Another way is to use the command FRE from BASIC. FRE is used to show the number of free kilobytes available for use === in memory, and it can be used in two ways PRINT FRE(0) or..... PRINT FRE("") When a program is being used, redundant strings clutter up the workspace and are known as Garbage. When no space is left BASIC does a 'Garbage collection' to free this workspace. PRINT FRE(0) gives the nominal free space available, PRINT FRE("") does a garbage collection first, thus in general giving a higher byte count. Note that PRINT FRE("") will not clear out variables that are in use, only the Garbage. FRE can be used to size a program exactly. LOAD 'BASIC' then type PRINT FRE(0) at the prompt and press [RETURN]. Make a note of the number returned. This is the number of free bytes left in memory. Now load the program you want to size and repeat the command. The second number is the number of free bytes now remaining. Take one number from the other and you have the program size in bytes. HIMEM is a function used to show the highest byte in memory ===== available to BASIC and indicates the upper limit of the Transient Program Area (TPA), within which BASIC works.Simply type PRINT HIMEM at the prompt and press [RETURN]. This value, normally set to a default of 62981 (check for yourself) can be altered by using the commands MEMORY or CLEAR. MEMORY is used to reset BASIC memory parameters; high-memory, ====== stack-size, number of files and maximum record length. The full MEMORY command is: MEMORY high-memory,stack-size,no of files, max-record length. In common with other multiple parameter commands, any parameter omitted retains it's current setting i.e if you don't want to change it, leave it out. The last two parameters are concerned with file handling and are not covered in this course. Above the TPA are areas of memory concerned with the functioning of CPM and it is not good to encroach on this, but if you need a section of memory that is safe to use, but segregated from BASIC you can declare a new,lower limit to the BASIC area, for example, 61000 and use the free section (62981-62000= 1981 bytes) for, perhaps, a machine code program to be called from BASIC. The section immediately below high-memory is called the stack and is used to store information on loops, GOSUB returns and other short-lived parameters. It's default value is set at 512 bytes and this is usually sufficient unless you intend to use deeply nested routines. The minimum value to which it may be set is 256 bytes. Typical commands: 1) MEMORY 62000 - sets high-memory (HIMEM) to 62000 2) MEMORY , 750 - increases stack size to 750 Note that any parameter left out must be represented by a comma. CLEAR This command does a similar job to MEMORY, but in addition ===== clears out all variables,arrays,cancels user functions and closes all files. In other words it resets all parameters. As with many key-words it is difficult to put convincingly into words it's true use, but when you need it you will know. When you see it used in a program try to assess why it is there. Forms;1) CLEAR 2) CLEAR,high-memory 3) CLEAR,high-memory,stack-size 4) CLEAR,high-memory,stack-size 5) CLEAR,high-memory,stack-size,no-of-files 6) CLEAR,high-memory,stack-size,no-of-files,max-record-length As with MEMORY, any combination of parameters can be set, those omitted retaining their current value. Parameters omitted must be represented by a comma ( note the extra comma after CLEAR compared with MEMORY). To illustrate this further, the command setting record length only might be CLEAR, , , , 72 ******************************************************************** Unfortunately it is not possible to demonstrate the above commands directly from the main course or a tremendous muddle might be caused, but you can see CHAIN MERGE used in this course by listing lines 300-330. However, READ ON !!!!! DEMONSTRATION PROGRAMS .... There are two programs on this disc which demonstrate CHAIN and CHAIN MERGE. They are "chaindem" and "newprog" They cannot be called from "TUTOR" so if you wish to try them now you must go into Direct Mode (press [STOP]), LOAD "chaindem" and RUN it A menu will appear on screen offering several ways of calling up 'newprog' from 'chaindem'. Try each option in turn and see for yourself what differences the various commands make. Each time you complete a run on "newprog' you will automatically be CHAINed back to 'chaindem',thus separating the programs ready for the next option. Using the programs in this obvious way is only the beginning, however, because I hope that you will take the trouble to LIST the programs to screen and see what makes them work. You will find for example that the menu lines do not issue the commands; this is done by lines further down:- 1000-1070. This 'cheat' and others is done to make the programs easy to use. LISTing after a CHAIN will reveal only one program in memory, whereas LISTing after a CHAIN MERGE will show the combined programs. When you have done this try variations on the instructions by going into direct mode and typing a command line at the prompt, perhaps varying start lines, deletions etc. to familiarise yourself with the commands. Try a plain MERGE; I didn't put it on the menu as there is nothing to see, but you could assure yourself that the programs do combine with this command. End of file INTER.J but you could assure yourself that the programs do combine with this command.