Multi-Programming Mark Jones with a routine which will enable your Spectrum to hold up to 10 Basic programs at once - each one directly and easily accessible. When Clive Sinclair brought out the 1K ZX-81, his first customers found great limitations in its use. The unexpan- ded ZX-81 had limited memory, crude graphics and a most strange display system, which varied in size. Yet a whole host of impressive software developed for this computer, pushing it to its limits and further. Sinclair's next computer, the Spectrum, had a far more impressive specifi- fication but was still lacking in comparison to, say, the BBC. However, programmers have occasionally manager to produce excellent games for the Spectrum, showing up even the games-oriented machines such as the Atari. Admittedly, the Spectrum will never quite be in the class of these other, more expensive machines, but at least by using clever techniques one can push it towards their level. I hope this routine takes the Spectrum one step nearer. This routine is written to allow the Spectrum programmer the benefit of having up to 10 Basic programs stored in the computer at one time - a facility normally found only on more expensive machines. Each program is directly access- ible by two simple key strokes. Also, jumps to particular lines in different programs can be made from any Basic program. For example, you might jump from line 200 of pro- gram 4 to line 50 of program 6. Thus program 1 might be used as a menu, calling up games or utilities stored in programs 2-9. When [the program is loaded, you will be] asked how many programs you want, 1-9. Having selected a number, you will then step through each program deciding how many bytes to allocate. A minimum is 200; the maximum number of bytes is determined by the memory you have free. Once a fixed amount of memory is allocated to each program, it is not possible to change this without deleting all the programs. Finally you will be asked which program you wish to initially select. Having selected a program, program A [the main loading program] will "disappear", leaving you with a blank Basic program ready for your programming. Program A can be recalled at any time by selecting program 0. If program 0 is selected using keys, it will autostart at line 4. If you are sure you will not need to reassign memory to the programs. Incidentally, this has the effect of NEWing all the programs other than program 0 at one go. You can NEW program 0. There is little point, though, since you have nine other program stores at your disposal. The multiprogram routine uses the interrupt to check the keys pressed. To change the program you are currently handling, simply press Symbol Shift and Break/Space. You will then be given the following message: A=NEW--P=ESCAPE----PROG.? 0-X where X is the number of programs you are handling. Pressing P will bring you back to your current program. Key A will NEW the current program. Do not use the Sinclair NEW command, as this may crash the computer. Otherwise, selecting a number will exchange your current program with the program selected. All the program variables, current data read position, and Micro- drive channels of the old program will be saved. It is therefore very easy to hop between programs. Switching programs The other way to switch programs is from within a program itself. Use the following method to do this: Routine C 9000 POKE 23619,INT (l/256): POKE 23618,INT (256/l*PEEK 23619) 9010 POKE 29739,S 9020 POKE 23729,P 9030 RANDOMIZE USR 29643 where L = line number to jump to; S = statement number - normally 0 and P = program. Thus for a menu routine which calls three different programs you might use the following method: 10 PRINT "Game 1" 20 PRINT "Game 2" 30 PRINT "Game 3" 40 INPUT "Which game (1-3)? ";p 50 LET s=0: LET l=0 60 GO TO 9000 (Routine C) For those of you who program in machine code it is still possible to have a RAMtop area free for code. Do this by altering the variable RTOP in line 22 of program A. Different programs occur at different places in memory, so the first byte of a line 1 REM statement is not neces- sarily 23760. [Then again, neither is it without this program, if you have an Interface 1 or similar peripheral, so wise machine code programmers would take this into account anyway.] When using the printer you will need to turn the inter- rupt routine off. Do this by typing: RANDOMIZE USR 29927 Turn it on again by typing: RANDOMIZE USR 29930 You can jump between programs with the interrupt routine turned off using Routine C. The following is a list of subroutines used by the machine-code routine: Address Function 29149 Interrupt vector sends interrupt routine here: JP to 29475 29152 Initialisation routine 29226 Number store routine for above 29231 Calculates address of system variable storage area for each program 29248 Routine to either retrieve or store necessary system variables 29455 Routine to move machine stack below new program RAMtop 29475 Interrupt routine - Reads keyboard + check for Symbol Shift, Space/Break - Waits for P, A or number - Calls different routines depending on what is entered 29630 Delay loop 29643 Main routine which calls swap routine 29322 and checks for errors. If the program number is too high an error message will be given. 29857 Print a string routine 29883 Print a character routine 29927 Interrupt routine off 29930 Interrupt routine on