MULTI-PROGRAMMING
by Mark Jones
from Your Computer, April 1984

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 ZX81, his first customers found
great limitations in its use. The unexpanded ZX81 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 specification but
was still lacking in comparison say, to the BBC.

However, programmers have occasionally managed 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 toward 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 any one
time - a facility normally found only on more expensive machines. Each
program is directly accessible 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 program 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.

The program is easily stored in the computer: Type in program A and
save using line 9998 then type in program B. When this runs error free
save the code, after program A on the tape.

When loaded, program A will auto-run picking up the code from program
B. You will then 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 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 auto-start 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 multi-program routine uses the interrupts to check the keys
pressed. To change the program you are currently handling simply press
Capshift and Break/Space. You will then be given the following
message:

A = NEW P = ESCAPE----- WHICH PROG?   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 Microdrive 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/256-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 necessarily 23760.

When using the printer you will need to turn the interrupt routine
off. Do this by typing:
	RAND USR 29927
Turn it on again by typing:
	RAND 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
29322	Main routine to swap system variables
29455	Routine to move machine stack below new program RAMTOP
29475	Interrupt routine
	- Reads keyboard + check for Capshift, 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
