SWITCHA
from Your Sinclair #3 (Mar.1986)

THREE INTO ONE WILL GO

Ever felt you needed a spare Speccy to save you from all
that SAVEing and LOADing? Save your money - Max Phillips
lets you run three programs at once with Switcha ...


Roll up, roll up, see amazing feats never before attempted
on a Spectrum. Yes, ladies and gentlemen, I'm not just going
to saw your Spectrum in half but into thirds and then load
and run a program in each of the pieces! Yes sir, yes you,
would you mind lending me your machine a moment? Of course
you'll get it back in one piece!

Well not quite. What I am going to do is give you a machine
code utility that divides your 48K memory into three 16K
chunks and lets you switch at any time between them. Each
'partition' is a fully working Spectrum so you can have
three programs loaded at once and can edit and run them as
normal. At any time, pressing Symbol-Shift and Space will
put the current program away and switch to the next one.

This trick is nicked from business machines where people
might want to flit between a database, a word processor and,
don't tell the boss, a Space Invaders. Of course, they do
have a bit more memory to play with - half a megabyte or so
- and they do have more formal operating systems so that
it's possible to switch between almost any group of
programs.

Switcha for the Spectrum is more limited but it's not just a
pretty face. It's great for messing about with - you can
have several versions of the program you're writing on hand
and you can doodle new routines and make calculations
without disturbing your main program and so on. It's
brilliant for Blue Peter demonstrations - here's one I wrote
earlier and so on. Switcha isn't really multi- tasking - the
three partitions don't actually run at once (the Speccy
ain't quick as it is). But what it does give you, if you
want the technospeak, is interruptability or context-
switching.

So nip off for a moment and type in and save the Basic
Loader. Next, get the Switcha code into memory using either
the hex loader or by assembling the source code with your
own assembler. This should be saved just after the Basic
Loader on your tape or onto the same microdrive cartridge.

To start, LOAD "". You'll see a brief message with
instructions while the code loads and then Switcha's flashy
green or blue border while it installs itself. The program
finishes with 0 OK, 70:1 and returns you to Basic as normal.
Just to convince yourself it's working, enter CLS: PRINT
"Partition 1". Now press Symbol-Shift and Space - the border
flashes and you'll get another copy of the 'OK' message.
Enter CLS: PRINT "Here's number 2", press Symbol-Shift /
Space and you'll get the third partition. Enter CLS: PRINT
"And here's the other one". Now by repeatedly pressing
Symbol-Shift / Space, you'll flick through the partitions.
Convinced?

You can LOAD a program into the current partition as normal
and edit it or RUN it or whatever. Switcha works while the
program is running - when you come back to it, it will
restart exactly where it left off. You can even NEW the
program in the current partition. If you do this, you'll
have to re-connect Switcha by entering:

	RANDOMIZE USR 64842

The other two programs you had loaded will have survived the
NEW and you can switch back to them! If you happen to run
any other routine that disables interrupts, this RANDOMIZE
should always restore Switcha. Probably the only way to get
rid of Switcha is the trusty old RANDOMIZE USR 0 - but
remember to save everything (don't forget your inactive
programs) before you do it!

You can't switch during I/O operations such as cassette
loading or microdrive access but Switcha will be back the
moment they've finished. Switcha works with all known
peripherals (including those damn Kempstons) but it might
not be a good idea to switch when, for example, a program is
driving a music box. In any event, Switcha won't let you
switch if an add-on has got its own ROM where the Speccy's
should be - you'll just have to wait a few microseconds and
try again ...


NOW THE BAD NEWS ...

Of course, you rarely get something for nothing in this
world even if the Ed seems to think so. So, what happens
when you divide a 48K machine into three pieces? You get
three 16K machines. Take away the memory for the display and
system variables and a bit for Switcha and you're left with
just under 8K for each Switcha program. So Switcha only lets
you run small programs. This will be a shock to many but not
so much a problem for those of us who had 16K Spectrums or
ZX81s in the past.

You can't use Switcha with any program that uses interrupts
without modifying it first and you can't use it with
commercial programs - well, I can't think of any that would
work. However, you can run a machine code program in a
partition provided it loads below address 32053. Go on,
reassemble that 4K Space Invaders you wrote years ago! A
Basic program that uses a machine code subroutine will work
if it installs it by looking at the value of RAMTOP,
lowering it and loading its code at the new RAMTOP. Those
that waltz in and plonk their code at 65000 or thereabouts
should be easily adapted.


GET SWITCHED ON

Switcha has all sorts of possibilities. What about a program
that looked at the saved programs in either partition? You
could stop a program at any point and then get the next
program to look at its variables, alter their contents,
change the display or whatever. To do this, just calculate a
couple of addresses:

LET nextoff = PEEK 65014 + PEEK 65015*256 - 16384
LET lastoff = 49152 - nextoff

POKE X+nextoff is equivalent to POKE X on a standard Speccy
but the POKE will affect the partition that will be switched
in next. Similarly, POKE X+lastoff is the same as POKE X
only it affects the partition just switched out. So, for
example, POKE 23609+lastoff,100 would set the key beep on
the program just switched out to 100. By using a lower
Ramtop with Switcha, you could even create a data buffer
where three different programs could exchange information
with each other. Suppose your database program could read
the results just produced by your calculator program!

And if you're feeling ambitious, there's a hundred different
things you could do to Switcha. The actual switching is a
bit boring - the nicest way to do it would be to scroll the
current screen off as the next one scrolls on ... or you
could do a configurable Switcha that lets you have two or
three partitions each with different sizes. Over to you!


A SWITCH IN TIME (SAVES THREE)

[see SWITCHA.GIF]

Switcha divides memory into three partitions - the current
one is a standard 16K Spectrum from locations 16384 to 32767
and the other two are at 32768 to 49151 and 49152 to 65535.
Each partition is a 'snapshot' of a running 16K machine and
contains everything needed to restore it to working order.
There's a copy of Switcha in each partition so that when the
49162 to 65535 partition is exchanged, the Switcha at 64842
is overwritten with itself and therefore remains intact!

Interrupt mode 2 is used so that the routine is called fifty
times a second; the interrupt register holds FEh and there's
a table of vectors from FE00h to FF00h, each byte being set
to FDh. At FDFDh, Switcha stores a jump to its actual
handler. This is horribly cumbersome but it ensures that the
interrupt works correctly no matter what's stuck on the back
of the Spectrum!

Next, it checks to see if the Spectrum ROM is present and
exits if it isn't. This might cause a slight pause before
switching if the Interface 1 Is fitted but it means that
ROM-based add-ons will never cause a crash. Next Switcha
checks for the Symbol-Shift / Space combination; if so, it
snapshots the current partition and exchanges it for one of
the two saved ones. The border colour is reset from the
system variable BORDCR. The 16-bit value EXBASE (address
65014) is the address of the partition to switch to next -
by alternating it between 32768 and 49152, the three
partitions are exchanged in sequence.

Switcha exits by jumpIng to location 38H so that the
Spectrum's normal interrupt processing, updating the clock
and reading the keyboard, happens as normal. You could alter
the exit jump to tack on an interrupt routine of your own.


--
Another Fine Product transcribed by:
Jim Grimwood (jimg@globalnet.co.uk), Weardale, England
--
