ZXSpectrum+2aManual
ZXSpectrum+2aManual
Introduction 5
Software compatibility
The BASIC
How to read this book
Precautions!
Chapter 1 12
Open the box
Unpacking
Fitting a mains plug
Setting up
Chapter 2 15
Operating your +2A
Switching on
Tuning-in your TV
Using the +2A
The opening menu
Chapter 3 22
How to load disk software
Disks and drives for the +2A
Loading disk software
Abandoning loading
Chapter 4 24
How to load tape software
The datacorder
Loading Spectrum +3, +2(+2A) and Spectrum 128 software
Loading Spectrum 48 software
Abandoning loading
Chapter 5 28
Using a disk drive
Disks and drives for the +2A
Insertion
Write protection
Read/write indicator lamp
Eject button
Chapter 6 32
Introducing +3 BASIC
The editor
The edit menu
Renumbering a BASIC program
Swapping screens
Listing to the printer
Typing in a program
Moving the cursor
Running a program
Commands and instructions
Saving to tape
Verifying the tape
Loading from tape
Formatting a disk
Saving to disk
Filenames
Disk catalog
Loading from disk
Error reports
Chapter 7 48
Using 48 BASIC
Using the +2A as a 48K Spectrum
Entering 48 BASIC mode
The keyboard under 48 BASIC
Program entry
Editing the current line
Chapters 8 54
The +3 BASIC programmer's guide
Part 1 – Introduction 54
Part 2 - Simple programming concepts 58
Part 3 – Decisions 66
Part 4 – Looping 68
Part 5 – Subroutines 73
Part 6 - Data in programs 75
Part 7 – Expressions 78
Part 8 – Strings 82
Part 9 – Functions 85
Part 10 - Mathematical functions 92
Part 11 - Random numbers 98
Part 12 – Arrays 101
Part 13 – Conditions 105
Part 14 - The character set 109
Part 15 - More about PRINT and INPUT 118
Part 16 – Colours 125
Part 17 – Graphics 132
Part 18 – Timing 138
Part 19 – Sound l42
Part 20 - File operations 151
Part 21 - Printer operations 181
Part 22 – Streams 188
Part 23 - IN and OUT 191
Part 24 - The memory 195
Part 25 - The system variables 205
Part 26 - Using machine code 214
Part 27 - Guide to +3DOS 229
Part 28 - Spectrum character set 283
Part 29 – Reports 291
Part 30 - Reference information 302
Part 31 - The BASIC 305
Part 32 - Binary and hexadecimal 331
Part 33 - Example programs 335
Chapter 9 344
Using the calculator
Selecting the calculator
Entering numbers
Running total
Using built-in mathematical functions
Editing the screen
Assigning variables
User defined functions
Exit-ing from the calculator
Chapter 10 347
Peripherals for your +2A
Printer
Joystick(s)
VDU Monitor
Amplifier
Serial devices
MIDI device
Auxiliary interface
Expansion devices
Disk drive(s)
Index 356
Introduction
Software compatibility
The +2A may be used with software written for the earlier models
in the ZX Spectrum range. This means that a vast quantity of
software already exists for the +2A. There are literally thousands
of titles available covering every conceivable application: games,
utilities, music, scientific, educational and many many more.
The BASIC
The +2A uses a computer language called BASIC (Beginners' All-
purpose Symbolic Instruction Code). The version of BASIC provided
with the +2A is the same as that used by the +3. Known as +3
BASIC, it has been designed to be particularly easy to learn and
use.
If you have connected an external floppy disk drive, then the +2A
will be, to all intents and purposes, a +3. This will be indicated
by the opening message on the screen (which displays the model
number +3 (at the top of the menu box)).
The product described in this manual, and products for use with it
are subject to continuous development and improvement. All
information of a technical nature and particulars of the product
and its use (including the information and particulars in this
manual) are given by AMSTRAD in good faith.
Published by AMSTRAD
1. Always connect the mains lead of the power supply unit (PSU)
to a 3-pin plug following the instructions given in chapter
1.
3. After you have finished using the +2A, always disconnect the
PSU from the mains supply socket.
7. Never plug in (or unplug) any device from any of the rear
sockets while the +2A is switched on - doing so will probably
damage both the +2A and the device.
9. After you have switched off your TV (or VDU monitor), do not
immediately disconnect the +2A - wait a few seconds or so.
10. Do not switch off the +2A (or switch on or off any peripheral
devices connected to the +2A) while there is a program or
data in the memory that you wish to keep - doing so may make
the +2A 'crash', losing the program or data.
11. (If you are using disks), always keep the disk drive and any
disks away from magnetic fields. For maximum data
reliability, do not position the disk drive close to your TV
or monitor, or close to any source of electrical
interference.
12. (If you are using disks), never switch the system on or off
while a disk is inserted in the disk drive. Doing so may
corrupt your disk, losing valuable programs or data.
13. (If you are using disks), whenever possible, make back-up
(duplicate) copies of disks which contain valuable programs.
Otherwise, should you accidentally lose or corrupt the disk
replacing it may prove very expensive.
14. (If you are using disks), never touch the floppy disk surface
itself, inside its protective casing.
15. (If you are using disks), do not eject a disk while it is
being read from or written to.
16. (If you are using disks), always remember that formatting a
disk will erase its previous contents.
Chapter 1
Open the box
Subjects covered...
Unpacking
Fitting a mains plug
Setting up
Unpacking
Inside the carton, you'll find the following...
Fit a proper mains plug to the mains lead of the power supply
unit. If a 13 Amp (BS1363) plug is used, a 3 Amp fuse must be
fitted. The 13 Amp fuse supplied in a new plug must NOT be used.
If any other type of plug is used, a 5 Amp fuse must be fitted
either in the plug or adaptor or at the distributor board.
Blue : Neutral
Brown : Live
Disconnect the mains plug from the supply socket when not in use.
Do not attempt to remove any screws, nor open the casing of the
power supply unit. Always obey the warning on the rating label of
the power supply unit...
Setting up
We will now set up the standard +2A system. All you need (other
than the items you unpacked) is a standard TV set (UHF). You can
use a colour or black-and-white TV, but of course, with the latter
you will not be able to enjoy the full colour capabilities of your
+2A.
Finally, insert the 6-pin DIN plug coming from the power supply
unit into the socket marked PSU at the back of the +2A.
Subjects covered...
Switching on
Tuning-in your TV
Using the +2A
The opening menu
Switching on
Connect the mains plug of the power supply unit to the mains
supply socket, and switch on the socket-switch (if necessary). The
power indicator lamp on the top panel of the +2A should
illuminate.
Now switch on your TV. On the screen you will probably see either
a faint TV picture or just random 'white noise' and hear a loud
'hissing' sound from the TV's speaker. Adjust the TV's volume
control until the sound is at a comfortable listening level. The
next thing to do is set up the +2A ready for tuning-in.
When you are satisfied with the tuning, then you may (if your TV
is so equipped) set the AFC (or AFT) switch to the on position.
Now that you have tuned-in one of the TV's push-button channel
selectors specifically for the +2A, you may thereafter select that
particular channel whenever you wish to use the +2A with your TV.
You may now skip to the section ahead entitled 'Using the +2A'.
Manual tuning
If your TV isn't equipped with push-button channel selectors, then
you will have to use the TV's manual tuning knob to tune-in to
your +2A.
Having connected and switched on the +2A and TV, switch on the
+2A's test signal as described in the previous section entitled
'Preparing to tune-in your TV'.
Tune-in the TV's manual tuning knob until the test signal is
received. Make sure that both picture and sound are tuned-in for
the best possible results.
Finally, adjust the TV's brightness, contrast and colour controls
for the clearest display of the text characters within the colour
bars.
Each time that you wish to set up and use the +2A with your TV,
you should follow the above manual tuning procedure.
You may now skip to the section ahead entitled 'Using the +2A'.
Having problems?
If you have tuned-in your TV satisfactorily, you may now skip to
the section ahead entitled 'Using the +2A'.
If, however, you are unable to tune-in your TV, the following
check list may help you to ascertain where the problem lies, and
what remedial action you can take.
1. Problem...
The power indicator lamp (on the top panel of the +2A) is not
illuminated.
Action...
● Check 6-pin DIN plug from power supply unit is plugged into PSU
socket on computer.
● Check mains plug of PSU is plugged into mains supply socket.
● (If mains supply socket is switched) - Check supply socket
switch is on.
● Check connections and fuse in mains plug of PSU.
2. Problem...
Action...
3. Problem...
Only a poor signal from the computer can be tuned-in on the TV.
Action...
4. Problem...
A signal from the computer is being tuned-in, but it's not the
test signal described above.
Action...
5. Problem...
The test signal colour bars appear, but no sound (repeating tone)
is audible from the TV's speaker.
Action...
6. Problem...
The test signal sound (repeating tone) can be heard, but no colour
bars can be seen on the TV.
Action...
7. Problem...
The test signal colour bars and sound are tuned-in, but none of
the text characters can be read.
Action...
We will now switch off the test signal and start using the +2A.
Press and release the RESET button (at the left hand side of the
+2A). The test signal will disappear from the screen, and in its
place will be the opening menu.
The opening menu will appear whenever you first plug in and switch
on the +2A, or whenever you press and release the RESET button.
Cursor Keys
The computer then switches to the +3 BASIC mode. You will see a
black horizontal bar (containing the words +3 BASIC) towards the
bottom of the screen, and a flashing blue and white blob (called
the cursor) at the top left-hand corner.
Don't worry if you know nothing about BASIC - we're not going to
do any programming just yet - we'll simply return to the opening
menu again. To do this, we use a different menu - this one's
called the edit menu. Call up the edit menu by pressing the EDIT
key.
Again, using the cursor keys and ENTER, select the option Exit to
return to the opening menu.
You may now select whichever opening menu option you require.
Depending upon your selection, refer to the following chapters for
further information...
Subjects covered...
1. Set up the +2A and FD-1 together. First switch on the FD-1
(using the POWER ON/OFF switch at the back of the disk
drive), then switch on the +2A. The opening menu will appear
on the screen...
(As you have connected an external disk drive, the +2A is, to all
intents and purposes, a +3. This will be indicated by the opening
message on the screen (which displays the model number +3 (at the
top of the menu box)).
3. Press the ENTER key to select the option Loader from the
opening menu. (If you don't know about selecting menu
options, refer back to chapter 2).
The software will start to load from disk. On the disk drive, you
will see the read/write indicator lamp start to flash on and off
(indicating that the disk is being read from). After a few
seconds, the screen display will change and the software will be
loaded, ready to use.
When you have finished using the software and wish to use the +2A
for something else, press and release the RESET button (at the
left-hand side of the +2A). Always remember that whenever the
RESET button is pressed, everything in the computer's memory (RAM)
is cleared. You should therefore always make sure that you have
completely finished with any program in the +2A's memory, before
you press the button.
Abandoning loading
If you wish to abandon a loading operation, simply press and
release the RESET button. The +2A will return to the opening menu.
Chapter 4
How to load tape software
Subjects covered...
The datacorder
Loading Spectrum +3, +2 (+2A)
and Spectrum 128 software
Loading Spectrum 48 software
Abandoning loading
The datacorder
Familiarise yourself with the keys on the datacorder...
3. Press the ENTER key to select the option Loader from the
opening menu. (If you don't know about selecting menu
options, refer back to chapter 2.)
(If you have connected an external disk drive to the +2A, note
that when you select the Loader option from the opening menu, the
+2A knows that you wish to load from tape (instead of disk) by
automatically detecting the absence of a disk in the disk drive.
If a disk is inserted, the tape will be ignored.)
Spectrum 48 software
To load Spectrum 48 software (a game, an utility program, etc.)
from tape, carry out the following instructions...
2. Select the option 48 BASIC from the opening menu. (If you
don't know how to select a menu option, refer back to chapter
2.) The opening menu will disappear and the following message
will be displayed at the bottom of the screen...
3. Now press the J key once, followed by the " (double quotes)
key twice. The screen should look like this...
(If the screen does not correspond to the above picture, then you
may have selected the wrong menu option or pressed the wrong key.
In this case, press and release the RESET button (at the left-hand
side of the +2A) and carry out steps 2 and 3 again.)
When the program has loaded, stop the cassette. The software is
then ready to use.
When you have finished using the software and wish to use the +2A
for something else, press and release the RESET button (at the
left-hand side of the +2A). Always remember that whenever the
RESET button is pressed, everything in the computer's memory (RAM)
is cleared. You should therefore always make sure that you have
completely finished with any program in the +2A's memory, before
you press the button.
Abandoning loading
If, while loading software from tape, you wish to abandon the
loading operation, then simply press and release the RESET button.
The +2A will return to the opening menu.
NOTE - Holding the BREAK key down while loading Spectrum +3, +2
(+2A) or Spectrum 128 software will return the +2A to the opening
menu; holding the key down while loading Spectrum 48 software will
return the +2A to the 48 BASIC mode.
Chapter 5
Using a disk drive
Subjects covered...
If you have connected a single disk drive to the +2A, then note
that the drive is known as drive A:. If you have connected two
disk drives to the +2A, then note that the first drive is known as
drive A: and the second drive is known as drive B:.
Insertion
Each side of a disk may be used separately. A disk should be
inserted with its label facing outward from the drive, and with
the side that you wish to use face up...
Write protection
In the left-hand corner of each side of a blank disk, you will see
an arrow pointing to a small shuttered hole. This is called the
write protect hole, and allows you to protect the contents of the
disk from erasure or 'overwriting'...
When the hole is closed, data can be 'written' onto the disk by
the computer. When the hole is open, however, the disk will not
allow data to be written onto it, thus enabling you to avoid the
accidental erasure of valuable programs.
To open the write protect hole, slide back the small shutter
located at the left-hand corner of the disk and the hole will be
opened...
To close the write protect hole, simply slide the shutter to its
closed position. Write protection is then OFF.
Other manufacturers' disks employ a small lever located in a slot
at the left-hand corner...
To open the write protect hole on this type of disk, slide the
lever towards the middle of the disk (using the tip of a ball-
point pen or similar object)...
Note that regardless of the method employed to open and close the
write protect hole, opening the hole in all cases facilitates
protection against overwriting.
Eject button
Pressing in the eject button allows you to remove your disk from
the disk drive.
Do not press the eject button while the disk is being read from or
written to.
Always eject your disk from the disk drive before switching the
system off.
Chapter 6
Introducing +3 BASIC
Subjects covered...
The editor
The edit menu
Renumbering a BASIC program
Swapping screens
Listing to the printer
Typing in a program
Moving the cursor
Running a program
Commands and instructions
Saving to tape
Verifying the tape
Loading from tape
Formatting a disk
Saving to disk
Filenames
Disk catalog
Loading from disk
Error reports
The +2A has an advanced editor to create, modify and run BASIC
programs. To enter the editor, select the option +3 BASIC from the
opening menu, using the cursor keys and ENTER. (If you don't know
how to select a menu option, refer back to chapter 2.)
Firstly, there is a flashing blue and white blob in the top left-
hand corner. This is called the cursor, and if you type any
letters at the keyboard, then they will appear on the screen at
the position of the cursor.
The last item of note at the moment is the small screen. This fits
between the footer bar and the bottom of the screen, and is
currently blank. It only has room for two lines of text, and is
most often used by the +2A when it detects an error and needs to
print a report to say so. It does have other uses, however, and
these will be described later.
Now press the EDIT key. You will notice two things happen - the
cursor vanishes, and a new menu appears. This is called the edit
menu...
The edit menu's options are selected in the same way as for the
opening menu (by using the cursor keys and ENTER).
+3 BASIC - This option simply cancels the edit menu and restores
the cursor. On the face of it - not very useful; however, if EDIT
is pressed accidentally, then this option allows you to return to
your program with no damage done.
Screen - This option moves the cursor into the smaller (bottom)
part of the screen, and allows BASIC to be entered and edited
there. This is most useful for working with graphics, as any
editing in the bottom screen does not disturb the top screen. To
switch back to the top screen (which you can do at any time whilst
editing), select the edit menu option Screen again.
Exit - This option returns you to the opening menu - the +2A
retains any program that you were working on in the memory. If you
wish to go back to the program again, select the option +3 BASIC
from the opening menu.
If you select the opening menu option 48 BASIC (or if you switch
off or reset the +2A), then any program in the memory will be
lost. (You may, however, use the opening menu option Calculator
without losing a program in the memory.)
Reset the computer and select +3 BASIC. Now type in the line
below. As you type it in, the characters will appear on the screen
(a character is a letter, number, space, etc.). Note that to type
in the equals sign = you should hold down the SYMB SHIFT key, then
press the L key once. Try typing in the line now...
The +2A should have also emitted a short high-pitched bleep, and
moved the cursor to the start of the next line.
(The colon : is obtained by SYMB SHIFT and Z, and the minus sign -
is obtained by SYMB SHIFT and J.)
Don't worry about line 20 'spilling over' onto the next line of
the screen - the computer will take care of this and align the
text so that it is easier to read. Unlike a typewriter, there's no
need for you to do anything when you approach the end of a screen
line because the +2A detects this automatically and moves the
cursor to the beginning of a new line.
30 next f
The numbers at the beginning of each line are called line numbers
and are used to identify each line. The line you just typed in is
line 30, and the cursor should be positioned just below it. As an
exercise, we will now edit line 10 (to change the number 100 to
255). Press the cursor up ↑ key (four times) until the cursor has
moved up to line 10. Now press the cursor right → key until the
cursor has moved to the right of 100. Press DELETE three times and
you will see the 100 disappear. Now type in 255 and press ENTER.
Line 10 of the program has now been edited...
The computer has opened up a new line in preparation for some new
text. Type...
run
Press ENTER and watch what happens. Firstly, the footer bar and
the program lines are cleared off the screen as the +3 BASIC
editor prepares to hand over control to the program you've just
typed in. Then the program starts, draws a pattern, and stops with
the report...
0 OK, 30:1
Press ENTER. The screen will clear and the footer bar will come
back, as will the program listing. This takes about a second or
so, during which time the +2A won't be taking input from the
keyboard, so don't try to type anything while it's all happening.
Commands are just like instructions, only they don't have line
numbers and the +2A carries them out immediately (as soon as ENTER
is pressed). In general, any instruction can be used as a command,
and vice versa - it all depends on the circumstances. Every
instruction or command must have at least one keyword. Keywords
make up the vocabulary of the computer, and many of them require
parameters. In the command DRAW 40,200 for example, DRAW is the
keyword, while 40 and 200 are the parameters (telling the computer
exactly where to do the drawing). Everything the computer does in
BASIC will follow these rules.
Now press EDIT and select the Screen option. The editor moves the
program down into the bottom screen, and gets rid of the footer
bar. You can only see line 10 of the program as the rest is
'hiding' off-screen (you can prove this by moving the cursor up
and down).
run
Press ENTER again, and the program will run exactly the same as
before. But this time, if you press ENTER afterwards, the screen
doesn't clear, and you can move up and down the program listing
(using the cursor keys) without disturbing the top screen. If you
press EDIT to get the edit menu, you might think that this would
mess up the top screen. However, the +2A remembers whatever's
behind the edit menu and restores it when the menu is removed.
Now type...
go to 10
(Press ENTER.) The keywords GO TO tell the +2A not to clear the
screen before starting the program. The modified program draws a
slightly different pattern on top of the old one. You may continue
editing the program to add further patterns, if you wish.
One thing you may notice while you're typing away is that CAPS
SHIFT and the number keys used together do strange things: CAPS
SHIFT with 5, 6, 7 and 8 move the cursor about, CAPS SHIFT with 1
calls up the edit menu, CAPS SHIFT with 0 deletes a character,
CAPS SHIFT with 2 is equivalent to CAPS LOCK, and finally CAPS
SHIFT with 9 selects graphics mode. All of these functions are
available using the dedicated keys on the +2A, and so there is no
reason why you should ever want to use the above CAPS SHIFT and
number key alternatives.
The final part of this chapter, therefore, deals with saving and
loading to and from tape using the +2A's built-in datacorder. If
you have connected a disk drive to the +2A, then the section ahead
entitled 'Simple disk operations' will show you how to save and
load to and from disk.
We will now save the program below...
list
(Press ENTER again.) If the program isn't in the memory (or you
have since switched off the +2A), then switch it on, select +3
BASIC and type in the above program).
This is the program that you are going to save onto tape. Any
standard tape cassette should work, although low noise cassettes
may be better.
Saving to tape
If you have connected a disk drive to the +2A, then the system
assumes that you will want to save programs to disk. If, instead,
you wish to save programs to tape, you must first type in the
command...
save "t:"
...and press ENTER. (If you have not connected a disk drive to the
+2A, there is no need to issue this command.)
Although we chose the filename 'my pattern', you can choose any
filename you like for the program to be saved under. (You are
allowed up to ten characters in a tape filename.)
We shall first go through a 'dry run' so that you can see what
will happen when we actually do save the program later. This time,
therefore, don't press the record and play keys on the datacorder
- just press a key on the keyboard (for example ENTER) and watch
the border of the TV screen. You will see patterns of coloured
horizontal stripes as follows:
A short pause.
Two seconds of the red and cyan stripes again, followed by another
short burst of blue and yellow stripes.
While the stripes appear on the screen, you can also hear the
'sound' of the data through your TV's speaker.
Keep trying out the above SAVE command (without actually operating
the datacorder) until you can recognise these patterns. What's
actually happening is that the information is being saved in two
blocks and both blocks have a 'lead-in' (which corresponds to the
red and cyan stripes) followed by the information itself (which
corresponds to the blue and yellow stripes). The first block is a
preliminary one containing the name and various other bits of
information about the program, and the second is the program
itself together with any variables present. The pause between them
is just a gap.
2. Type...
load "t:"
...and press ENTER. (If you have not connected a disk drive to the
+2A, there is no need to issue this command.)
2. Rewind the tape to just before the point at which you saved
the program.
3. Type...
...press ENTER and play the tape. The border will alternate
between red and cyan until the +2A finds the program you
specified. Again, you will see the stripes appearing on the border
(as you did when you saved the program) and you will hear the
'sound' of the data through your TV's speaker. During the pause
between the blocks, the message Program: my pattern will be
displayed on the screen. (When the +2A is searching for something
on tape, it displays the name of everything it comes across.) If,
after the program name appears, the computer displays the report 0
OK, then your program is safely stored and you may stop the tape
and skip to the section ahead entitled 'Loading from tape'. If
not, something has gone wrong - take the following steps to find
out what.
If the program name has not been displayed, then either the
program was not saved properly in the first place, or it was
saved, but was not 'read back' properly. You need to find out
which. To see if it was saved properly, rewind the tape to just
before the point at which you saved the program, then play it back
while listening to the TV's speaker. The (red and cyan) lead-in
should produce a clear, steady high pitched note, while the (blue
and yellow) information part gives a much harsher screech.
If you do not hear these noises, then the program was probably not
saved. Check that you were not trying to save the program onto the
plastic 'leader' at the beginning of the tape. When you have
checked this, try saving again.
If you can hear the sounds as described, then SAVE was probably
alright and your problem is with reading back.
It could be that you mistyped the program name when you saved it
(in which case, when the +2A finds the program, it will display
the mistyped name on the screen). On the other hand, perhaps you
mistyped the program name when you verified it, in which case the
computer will ignore the correctly saved program and carry on
looking for the wrong name, flashing red and cyan as it goes.
1. Press the RESET button and select the option +3 BASIC from
the opening menu.
load "t:"
3. Rewind the tape to just before the point at which you saved
the program.
4. Type in...
Once a program has been loaded, the report 0 OK will appear, and
you may stop the tape. If you then press ENTER, the program will
be listed on the screen. To start the program, simply type...
run
If your tape has more than one program stored on the same side,
then each program will have a name. You can choose the program you
wish to load using the LOAD command - for instance, if the program
you wanted to load was called 'helicopter', you would enter the
command...
load "helicopter"
load ""
...then the +2A will load the first program it finds on tape. This
can be useful if you wish to load a program whose name you can't
remember.
The option Loader from the opening menu has the same action as
LOAD "" and is much quicker to use - simply switch on the +2A and
press ENTER. If you have connected a disk drive to the +2A, make
sure that no disk is inserted.
(Note that you cannot use the Loader option to load Spectrum 48
software.)
If you have not connected an external disk drive to the +2A, then
you may skip the rest of this chapter.
You will need to have a brand new blank disk to hand as you work
through this section. Whatever you do - don't use a disk with any
valuable software, games, etc. on it.
Disks and tapes
Even if you are familiar with tape saving and loading, it is worth
pointing out two important points which must be remembered when
using disks:
Firstly, a brand new blank disk cannot be simply taken out of its
wrapper and recorded onto (as is the case with a tape) - instead,
each side of a disk must be formatted first. Note that the
formatting process completely erases that side of the disk and
prepares it for future use.
Formatting a disk
Formatting can be likened to building a series of shelves and
pigeonholes on a disk prior to the storage of information on those
shelves. In other words, formatting lays down an organised
framework around which data can be put in or taken out.
There are 40 concentric tracks from the outside of the disk (track
0) to the inside (track 39), and each track is divided into 9
sectors.
Each portion of track in a sector can store up to 512 bytes of
data; hence the total available space on each side of a disk is
180 kilobytes (180K). Note that 7K of the 180K is reserved for the
computer's own use; this leaves 173K per side for your programs.
We will now format a new blank disk, and save the program below...
list
Insert side 1 of a new blank disk into the disk drive. (If you
have connected two disk drives to the +2A, use the first drive
(drive A:) for all operations shown in this section.)
Type...
format "a:"
0 OK, 0:1
You have now formatted side 1 of the disk. Once you have done
this, you should not need to format side 1 of that disk ever
again.
(If you don't receive the above report (and some other message
appears instead), check the section entitled 'Error reports' at
the end of this chapter.)
Saving to disk
Having formatted side 1, it is now ready for saving programs onto.
(Press ENTER.) After a few seconds, you will see the report...
0 OK, 0:1
(If you don't receive the above report (and some other message
appears instead), check the section entitled 'Error reports' at
the end of this chapter.)
Filenames
Note that a filename on disk consists of two parts (fields). The
first field is obligatory and can contain up to 8 characters
(letters and numbers may be used but no spaces or punctuation
marks). In the above example filename, 'my' is the first field.
Disk catalog
A catalog of the disk (in alphabetical order) can be displayed by
typing in...
cat
MY .PAT 1K
172K free
load "my.pat"
(Press ENTER.) After a few seconds, you will see the report...
0 OK, 0:1
The program is now loaded from disk. Press ENTER and you will see
the program listing displayed.
(If you don't receive the above report (and some other message
appears instead), check the next section entitled 'Error
reports'.)
run
Error reports
If you don't correctly carry out the instructions in this section,
you may receive various error reports. If so, identify the report
(from those shown below), read the explanation given, and then
take the necessary corrective action.
The above report means that you have probably forgotten to insert
a disk into the disk drive. If there is a disk inserted in the
disk drive, then eject it, re-insert it and try again.
The above report means that you are trying to format, or save a
program to, a disk which has its write protect hole open. Eject
the disk, close its write protect hole, re-insert the disk and try
again.
The above report means that you are trying to load a program which
doesn't exist on that side of the disk. Eject the disk, make sure
that the correct disk is inserted (the right way up) and try
again. Take care to ensure that you accurately type in the
filename to load.
Bad filename
...or...
Invalid filename
The above reports mean that you are trying to load or save a
program using an illegal filename (or no filename at all). Read
the section entitled 'Filenames' earlier in this chapter, and try
again.
Disk is already formatted
A to abandon, other key continue
The above report means that you are trying to format a disk that
has already been formatted. In general, a disk should need
formatting only once (at the beginning of its life). In rare
cases, a disk may become corrupted and there will be no
alternative other than to format it again. However, unless this is
the case, you should always type A (to abandon) when you see the
above report.
If you find that one particular disk (or side of a disk) keeps
requiring formatting, then it is likely that the disk itself is
damaged and you should avoid using it in future.
Some commands that fail will produce reports that offer you the
options...
...typing I makes the computer ignore the reason that the command
failed in the first place and continue regardlessly (typing I is
therefore not recommended unless you know exactly what you're
doing);
Further information
Further information on tape and disk operations (together with
details of how to use the +2A's RAMdisk) can be found in chapter 8
part 20. A guide to +3DOS (the disk operating system provided with
the +2A) will be found in chapter 8 part 27.
Chapter 7
Using 48 BASIC
Subjects covered...
The +2A has the ability to act exactly like a 48K Spectrum (or
Spectrum +). This is achieved by selecting the option 48 BASIC
from the opening menu. In 48 BASIC mode, many of the enhanced
features of the +2A (such as facilities for disk drive(s), extra
memory, full screen editor, multi-channel sound, RS232/MIDI/AUX
interfaces and RAMdisk) cannot be used. The JOYSTICK 1 and
JOYSTICK 2 sockets will still operate, however.
There are, in fact, two methods of entering the 48 BASIC mode: the
first is by selecting the 48 BASIC option from the opening menu
(if you don't know how to select a menu option, refer back to
chapter 2). When 48 BASIC starts up, you will see the following on
the screen...
The second method allows you to enter the 48 BASIC mode while
editing a +3 BASIC program. To do this (while in +3 BASIC mode),
type...
spectrum
...and press ENTER. The +2A will respond with an OK message and
will have changed to 48 BASIC mode, retaining any program that you
had in memory. Once in 48 BASIC mode, there is no way back to +3
BASIC mode apart from resetting the +2A (or switching off, then on
again).
Applying CAPS SHIFT while in E mode, the next key pressed will be
interpreted as follows...
Applying SYMB SHIFT while in E mode, the next key pressed will be
interpreted as follows...
Part 1
Introduction
Whether you read chapter 6 first, or came straight here, you
should be aware that...
Instructions begin with a line number and are stored away for
later use.
The Keyboard
The characters used on the +2A comprise not only single symbols
(letters, digits, etc.) but also compound tokens (keywords,
function names, etc.). Everything must be typed in full, and in
most cases it doesn't matter whether capital letters (known as
UPPER CASE) or small letters (lower case) are used. There are
three sorts of keys on the keyboard: letter and number keys
(called alphanumeric keys); symbol keys (punctuation marks); and
control keys (things like CAPS SHIFT, DELETE and so on).
The most commonly used keys for BASIC are the alphanumeric keys.
When a letter key is pressed, a lower case letter will appear on
the screen together with a flashing blue and white blob called the
cursor. To get an upper case letter, the CAPS SHIFT key should be
held down while the letter is typed.
...simply hold down the SYMB SHIFT key while the alphanumeric key
with the required symbol on it is pressed (see the following
diagram)...
[ ] © ~ | \ { }
scroll?
Pressing any key (except N, BREAK or the space bar) will let
scrolling continue.
Pressing one of the keys N, BREAK or the space bar will make the
program stop with the report...
The smaller (bottom) part of the screen is used for editing short
programs, entering input data, entering direct commands (where the
main screen must not be used, eg. graphics programs), and also for
displaying reports.
Program entry
If the program being entered gets bigger than the screen size,
then the +2A attempts to display the area of most interest
(usually the last line entered together with its surrounding
lines). You may, however, specify a different area of the program
to be displayed using the command...
LIST xxx
While the +2A is running a BASIC program, the BREAK key is checked
every so often. This happens at the end of a statement, during use
of the datacorder or printer (if connected), or while music is
being played. If the +2A finds that the BREAK key is pressed, then
program execution stops and displays a report. The program may
then be edited.
Part 2
Simple programming concepts
Subjects covered...
Programs
Line numbers
Editing programs using ← → ↑ ↓
RUN, LIST
GO TO, CONTINUE, INPUT, NEW, REM
PRINT
Stopping a program
20 print a
10 let a=10
10 LET a=10
█
20 PRINT a
Note also that although we typed each line in lower case letters,
the keywords (ie. PRINT and LET) were converted to upper case as
soon as the line was entered and accepted by the +2A. From now on,
we will show keywords to be typed in upper case letters; however,
you may continue to type in lower case letters.
(By the way, if you don't know what a keyword is, you should have
studied chapter 6 before reading this chapter.)
15 LET b=15
You could type out the replacement line in full, but it is far
easier to move the cursor (using the cursor keys) to just after
the a, and then type...
20 PRINT a+b
...then press ENTER. The cursor will move to the line below, and
the screen should look like this...
10 LET a=10
15 LET b=15
20 PRINT a+b
█
What you have done in this program is to have assigned the value
10 to the variable called a, and the value 15 to the variable
called b. You have then instructed the computer to print the sum
of these two values by simply adding the two variables.
RUN
25
Run the program again and then afterwards, press ENTER and type...
PRINT a,b
Now press ENTER again and notice how the values of the variables a
and b are still in the +2A's memory, even though the program has
finished...
10 15
Mistakes
If you enter a line by mistake, say...
12 LET b=8
12
...and press ENTER. Line 12 will vanish, and the cursor will
reappear where line 12 used to be.
Now type...
30
...and press ENTER. The +2A will search for line 30, and since
there isn't one, it will 'fall off' the end of the program. The
cursor will be positioned just after the last line. If you enter
any non-existent line number (such as 30), then the +2A will place
the cursor where it thinks the line would have been if it really
existed. This can be a useful way of moving about large programs,
but beware - it can also be very dangerous because if the line
really did exist before you entered the line number - it certainly
wouldn't exist afterwards!
LIST
...and press ENTER. You may (particularly when working with more
lengthy programs) wish to list from a certain point onwards. This
can be achieved by typing an appropriate line number after the
LIST command.
Type...
LIST 15
When we were developing the above program, note how we were able
to insert line 15 between the other two lines - this would have
been impossible if they had been numbered 1 and 2 instead of 10
and 20. It is always good practice, therefore, to leave gaps
between line numbers.
If, at some time, you find that you haven't left enough space
between line numbers, then you may use the edit menu to renumber a
program. To do this, press the EDIT key then select the Renumber
option from the menu that appears; this sets the gap between each
line number to 10. Try this out and see how the line numbers
change.
We are now going to use the BASIC command NEW. This erases any
existing programs and variables in the +2A's memory. The command
should be used whenever you are about to start afresh, so type...
NEW
...and press ENTER. From now on, we won't mention 'press ENTER'
every time - we'll assume that you'll remember.
Although you can type in all of line 10 in lower case, only the
REM will be converted to upper case on entry as it's the only
keyword that the +2A recognises. Also, although the words GO TO
will appear with a space between them, they may be typed in as one
word (GOTO) if you prefer.
Now run the program. The instructions will start being carried out
in the order determined by the line numbers. First of all, you'll
see the headings deg F and deg C printed on the screen (as
instructed by line 20), but what has line 10 done? It looks like
the +2A has completely ignored it - in fact, it has! The REM in
line 10 stands for remark, so line 10 is solely to remind you of
what the program does. A REM command consists of REM followed by
anything you like - the +2A will ignore everything after the REM,
right up to the end of the line.
After line 20, the +2A carries out line 30 which simply prints a
blank line. When the +2A gets to the INPUT command in line 40 it
waits for you to type in a value for the variable f - you can tell
this because at the bottom of the screen is a flashing cursor.
Type in a number (then press ENTER). The +2A displays the result
and then waits for you to enter another number. This is because
the instruction in line 70 says GO TO 40 - in other words,
'instead of running out of program and stopping, jump back to line
40 and continue running from there'.
CONTINUE
When CONTINUE is used, the +2A remembers the line number in the
last report that it sent you (as long as the report was not 0 OK)
and jumps back to that line, which in this case is line 40 (the
INPUT command).
70 GO TO 31
Keep entering numbers until the screen starts getting full. When
it is full, the +2A will move the whole of the top half of the
screen up one line to make room, losing the heading off the top -
this is called scrolling.
When you are tired of entering numbers, stop the program as before
and enter the editor by pressing ENTER.
Look at the PRINT statement in line 50. The , comma in this line
is very important.
Commas are used to make the printing start either at the left-hand
margin, or in the middle of the screen (depending upon which comes
next). Thus in line 50, the comma causes the Celsius temperature
to be printed in the middle of the line.
Another punctuation mark you can use like this in PRINT commands
is the ' apostrophe. This makes whatever is printed next appear at
the beginning of the next line on the screen. This also happens by
default at the end of each PRINT command.
PRINT f: GO TO 40
This is a separate program from the last one, but you may keep
them both in the +2A at the same time. To run the new one, type...
RUN 100
Next time round, you will get two string quotes again, but you
don't have to use them if you don't want to. Try this, for
example: rub out the quotes by pressing cursor right → then DELETE
twice, and type...
n$
Since there are no string quotes, the +2A knows that it has to do
some calculation - the calculation in this case is to find the
value of the string variable called n$ (which is whatever name you
happen to have typed in last time round). In this way, the INPUT
statement acts like LET n$=n$, so the value of n$ is unchanged.
If you wish to stop the program, delete the quotes then hold down
SYMB SHIFT and type A, then ENTER.
Now look back at that RUN 100 instruction which jumps to line 100
and runs the program from there. You may be asking, 'What's the
difference between RUN 100 and GO TO 100?' Well, RUN 100 first of
all clears all the variables and the screen, and after that works
just like GO TO 100. On the other hand, GO TO 100 doesn't clear
anything, and there may well be occasions where you wish to run a
program without clearing any variables; here GO TO would be
necessary and RUN could be disastrous, so it is better not to get
into the habit of automatically typing RUN to start a program.
200 GO TO 200
RUN 200
Run the 'name' program again and when it asks you for input,
type...
LET n$="Fremsley"
CONTINUE
...you will find that you can use n$ as input data without any
trouble.
You have now seen the statements, PRINT, LET, INPUT, RUN, LIST, GO
TO, CONTINUE, NEW and REM, and they can all be used either as
direct commands or in program lines - this is true of almost all
commands in +3 BASIC, however, RUN, LIST, CONTINUE and NEW are not
usually of much use in a program.
Exercises...
2. Write a program to input prices and print out the tax due (at
15 percent). Put in PRINT statements so that the +2A
announces what it is going to do, and asks for the input
price with extravagant politeness. Modify the program so that
you can also input the tax rate (to allow for zero ratings
or future changes).
All the programs we have seen so far have been pretty predictable
- they went straight through the instructions, and then went back
to the beginning again. This is not very useful, as in practice,
we would want the +2A to make decisions and act accordingly. The
instruction to do this in BASIC takes the form...
Note that the CLS command (at the end of line 20) means 'clear the
screen'. We have used it in this program to stop the other person
seeing the secret number after it is entered.
(If you keep getting mixed up about the meanings of < and >, it
may help you to remember that the thin end of the symbol points to
the number which is supposed to be smaller.)
...shows that the 3rd statement (ie. the STOP command) in line 40
caused the program to halt.
...in +3 BASIC.
Exercise...
10 LET a=1
20 LET b=1
30 IF a>b THEN PRINT a;" is higher"
40 IF a<b THEN PRINT b;" is higher"
Before you run it, try to work out what will be printed on the
screen.
Part 4
Looping
Subjects covered...
FOR, NEXT
TO, STEP
Suppose you wish to input five numbers and add them together.
One way (don't type this in unless you are feeling dutiful) is as
follows...
10 LET total=0
20 INPUT a
30 LET total=total+a
40 INPUT a
50 LET total=total+a
60 INPUT a
70 LET total=total+a
80 INPUT a
90 LET total=total+a
100 INPUT a
110 LET total=total+a
120 PRINT total
10 LET total=0
20 LET count=1
30 INPUT a
40 REM count is number of time
s that a has been input so
far
50 LET total=total+a
60 LET count=count+1
70 IF count <= 5 THEN GO TO 30
80 PRINT total
10 LET total=0
20 FOR c=1 TO 5
30 INPUT a
40 REM c is number of times th
at a has been input so far
50 LET total=total+a
60 NEXT c
80 PRINT total
(To get this program from the previous one, you just have to edit
lines 20, 40 and 60, then delete line 70.)
...this will step the control variable by the amount 3/2 each time
the FOR loop is executed. Note that we could have simply said STEP
1.5, or we could have assigned the step value to a variable, say
s, and then said STEP s.
With the above modification, c will run through the values 1, 2.5
and 4. Notice that you don't have to restrict yourself to whole
numbers, and also that the control value does not have to hit the
limit exactly; it carries on looping as long as it is less than or
equal to the limit.
10 FOR m=0 TO 6
20 FOR n=0 TO m
30 PRINT m;":";n;" "; n loop m loop
40 NEXT n
50 PRINT
60 NEXT m
You can see that the n loop is entirely inside the m loop. This
means that they are properly nested.
The step size of zero here makes the command repeat itself
forever.
Exercises...
PRINT c
5. Delete line 30 from the fourth program. When you run the new
curtailed program, it will print the first number and stop
with the message 0 OK. If you then type...
NEXT n
...the program will go once round the loop, printing out the next
number.
Part 5
Subroutines
Subjects covered...
GO SUB, RETURN
GO SUB xxx
(In case you are interested, the +2A does this by remembering at
which point in the program the GO SUB command was issued (in other
words where it should continue from afterwards) and storing this
return address on top of a pile called the GO SUB stack.)
RETURN
...is met (at the end of the subroutine itself), the +2A takes the
top return address off the GO SUB stack, and continues from the
next statement.
10 READ a,b,c
20 PRINT a,b,c
30 DATA 1,2,3
Let's see how all this works in the program you've just typed in.
Line 10 tells the +2A to read three pieces of data and assign them
to the variables a, b and c. Line 20 then says PRINT these
variables. The DATA statement in line 30 provides the values of a,
b and c for line 10 to read.
Note from the above two programs that a DATA statement can appear
anywhere - before or after the READ statement.
When the above program is run, the READ statement moves through
the DATA list with each pass of the FOR...NEXT loop.
10 FOR a=1 TO 7
20 READ n$
30 PRINT n$
40 DATA "Bob","Edith","Carole"
,"Jacquie","Gavin","Charles
","Holly"
50 NEXT a
The +2A doesn't have to READ the DATA statements in order - it can
be made to 'jump about' between DATA statements by using the
RESTORE command. The form of the command is...
RESTORE xxx
10 DATA 1,2,3,4,5
20 DATA 6,7,8,9
30 GO SUB 110
40 GO SUB 110
50 GO SUB 110
60 RESTORE 20
70 GO SUB 110
80 RESTORE
90 GO SUB 110
100 STOP
110 READ a,b,c
120 PRINT a'b'c
130 PRINT
140 RETURN
The command GO SUB 110 calls a subroutine which READs the next
three items of DATA and then PRINTs them. Notice how the RESTORE
command affects which items are read.
Delete line 60 and run this program again to see what happens.
Part 7
Expressions
Subjects covered...
Operations: +, -, *, /
Expressions, scientific notation,
variable names
You have already seen some of the ways in which the +2A can
calculate with numbers. It can perform the four arithmetic
operations +, -, * and / (remember that * is used for
multiplication, and / is used for division), and it can find the
value of a variable, given its name.
The example...
LET tax=sum*15/100
PRINT 8-12/4+2*2
PRINT 8-12/(4+2)*2
We really ought to tell you what you can and cannot use as the
names of variables. As we have already said, the name of a string
variable has to be a single letter followed by $, and the name of
the control variable in a FOR...NEXT loop must be a single letter;
however, the names of ordinary numeric variables are less
restricted - they can use any letters or digits as long as the
first one is a letter. You can put spaces in as well to make it
easier to read, but they won't count as part of the name. Also, it
doesn't make any difference to the name whether you type it in
upper or lower case letters. There are some restrictions about
variable names which are the same as commands, however. In
general, if the variable contains a BASIC keyword in it (with
spaces around it) then it won't be accepted.
x
any old thing
t42
this name is impractical because it is too long
tobeornottobe
mixed cases spaces
MixEdCAsEsSpAcES
(Note that these last two names (mixed cases spaces and
MixEdCAsEsSpAcES) are considered the same, and refer to the same
variable).
pi (PI is a keyword)
any new thing (contains the separated keyword NEW)
42t (begins with a digit)
2001 (contains digits only)
to be or not to be (contains TO, OR and NOT, which are
all separated keywords)
3 bears (begins with a digit)
M*A*S*H (* is not a letter or a digit)
Lloyd-Webber (- is not a letter or a digit)
PRINT 2.34e0
PRINT 2.34e1
PRINT 2.34e2
...and so on upto...
PRINT 2.34e15
This proves that the computer can hold the digits of 4294967295,
even though it is not prepared to display them all at once.
The +2A uses floating point arithmetic, which means that it keeps
separate the digits of a number (its mantissa) and the position of
the point (the exponent). This is not always exact, even for whole
numbers. Type...
PRINT 1e10+1-1e10,1e10-1e10+1
Numbers are held to about nine and a half digits accuracy, so 1e10
is too big to be held exactly right. The inaccuracy (actually
about 2) is more than 1, so the numbers 1e10 and 1e10+1 appear to
the computer to be equal.
PRINT 5e9+1-5e9
The string "" with no character at all is called the empty or null
string. Remember that spaces are significant and an empty string
is not the same as one containing nothing but spaces.
Try...
When you press ENTER you will get the flashing red cursor that
shows there is a mistake somewhere in the line. When the +2A finds
the double quotes at the beginning of "The Times" it imagines that
these mark the end of the string "Did you read ", and it then
can't work out what The Times means.
As you can see from what is printed on the screen, each double
quote is only really there once - you just have to type it twice
to get it recognised.
Part 8
Strings
Subjects covered...
Slicing, using TO
"abcdef"(2 TO 5)
If you omit the start, then 1 is assumed; if you omit the finish,
then the length of the string is assumed. Thus...
A slightly different form misses out the TO and just has one
number.
"abcdef"(5 TO 7)
"abcdef"(8 TO 7)
...and...
"abcdef"(1 TO 0)
...are both equal to the empty string "" and are therefore
permitted.
The start and finish must not be negative, or you get the error B
integer out of range. This next program is a simple one
illustrating some of these rules...
10 LET a$="abcdef"
20 FOR n=1 TO 6
30 PRINT a$(n TO 6)
40 NEXT n
Type NEW when this program has been run and enter the next
program.
10 LET a$="1234567890"
20 FOR n=1 TO 10
30 PRINT a$(n TO 10),a$((11-n)
TO 10)
40 NEXT n
For string variables, we can not only extract substrings, but also
assign to them. For instance, type...
...and then...
...and...
PRINT a$
Exercise...
the +2A will write the answer 12, ie. the number of characters
(including spaces) in the string Jammy Smears.
4+LEN "Bloggs"
4+6
10
...and...
LEN ("FredBloggs")
LEN "FredBloggs"
10
LET a$="100"
VAL "3.5"
VAL is the reverse of STR$ because if you take any number, apply
STR$ to it, and then apply VAL to it, you get back to the number
you first thought of.
However, if you take a string, apply VAL to it, and then apply
STR$ to it, you do not always get back to your original string.
VAL "2*3"
VAL ("2"+"*3")
LET a$="99"
...and print all of the following: VAL a$, VAL "a$", VAL """a$""",
VAL$ a$, VAL$ "a$" and VAL$ """a$""". Some of these will work, and
some of them won't - try to explain all the answers.
ABS -3.2
...is equal to
ABS 3.2
...which is simply equal to 3.2.
INT 3.9
...is equal to 3.
INT -3.1
SQR calculates the square root of a number, ie. the result that,
when multiplied by itself, gives the argument, for instance...
SQR 4
SQR 0.25
SQR 2
You can also define functions of your own. Possible names for
these are FN followed by a letter (if the result is a number) or
FN followed by a letter followed by $ (if the result is a string).
These functions are much stricter about brackets - the argument
must be enclosed in brackets.
After the = sign comes the actual definition of the function. This
can be any expression, and it can also refer to the argument using
the name you've given it (in this case, x) as though it were an
ordinary variable.
When you have entered this line, you can invoke the function just
like one of the +2A's own functions by typing its name, FN s,
followed by the argument. Remember that when you have defined a
function yourself, the argument must be enclosed in brackets. Try
it out a few times...
PRINT FN s(2)
PRINT FN s(3+4)
PRINT 1+ INT FN s ( LEN "chicken"/2+3)
Once you have put the corresponding DEF statement into the
program, you can use your own functions in expressions just as
freely as you can use the computer's.
INT always rounds down. To round to the nearest integer, add 0.5
first - you could write your own function to do this...
FN r(2.9) is equal to 3
FN r(2.4) is equal to 2
FN r(-2.9) is equal to -3
FN r(-2.4) is equal to -2
Compare these with the answers you will get when you use INT
instead of FN r. Type in and run the following...
10 DEF FN t$(a$)=a$(2 TO ): RE
M TL$
20 DEF FN l$(a$,n)=a$( TO n):
REM LEFT$
Check that these work with strings of length 0 or 1. Note that our
FN l$ has two arguments, one a number and the other a string. A
function can have up to 26 numeric arguments (why 26?) and at the
same time up to 26 string arguments.
Exercise...
Use the function FN s(x)=x*x to test SQR. You should find that...
FN s( SQR x)
SQR FN s(x)
↑
PI, EXP, LN, SIN, COS, TAN, ASN, ACS, ATN
This section deals with the mathematics that the +2A can handle.
Quite possibly you will never have to use any of this at all, so
if you find it too heavy going, don't be afraid of skipping it. It
covers the operation ↑ (raising to a power), the functions EXP and
LN, and the trigonometrical functions SIN, COS, TAN and their
inverses ASN, ACS, and ATN.
↑ and EXP
You can raise one number to the power of another. This means
'multiply the first number by itself the second number of times'.
This is normally shown by writing the second number just above and
to the right of the first number; but obviously this would be
difficult on a computer so we use the symbol ↑ instead. For
example, the powers of 2 are...
2 ↑ 1 equals 2
2 ↑ 2 equals 2x2 equals 4 (2 squared, normally written 22)
2 ↑ 3 equals 2x2x2 equals 8 (2 cubed, normally written 23)
2 ↑ 4 equals 2x2x2x2 equals 16 (2 to the power of four, normally
written 24)
...and so on.
a↑0 equals 1
a↑(-b) equals 1/a↑b
a↑(1/b) equals the bth root of a, which is to say, the number that
you have to multiply by itself b times to get a
...and...
If you have never seen any of this before then don't try to
remember it straight away, just remember that...
...and...
...and maybe when you are familiar with these, the rest will begin
to make sense.
10 INPUT a,b,c
20 PRINT a*(b+c),a↑b*a↑c
30 GO TO 10
...you will see that even starting off from just £10, it all
mounts up quite quickly, and what's more, it gets faster and
faster as time goes on (though you might still find that it
doesn't keep up with inflation).
This sort of behaviour, where after a fixed interval of time some
quantity multiplies itself by a fixed proportion, is called
exponential growth, and it is calculated by raising a fixed number
to the power of the time.
10 DEF FN a(x)=a↑x
PRINT EXP 1
LN
The inverse of an exponential function is a logarithmic function -
the logarithm (to base a) of a number x is the power to which
you'd have to raise a to get the number x, and this is written
logax. Thus by definition, a↑logax is equal to x; and it is also
true that log (a↑x) is equal to x.
You may well already know how to use base 10 logarithms for doing
multiplications; these are called common logarithms. The +2A has a
function LN which calculates logarithms to the base e; these are
called natural logarithms. To calculate logarithms to any other
base, you must divide the natural logarithm by the natural
logarithm of the base, ie. logax is equal to LN x/LN a.
PI
Given any circle, you can find its perimeter (the distance round
its edge - often called its circumference) by multiplying its
diameter (width) by a number called π. π (pronounced pi) is the
Greek equivalent of the English letter p, and it is used because
it stands for perimeter.
Like e, π is an infinite non-recurring decimal - it starts off as
3.1415927. The word PI on the +2A is taken as standing for this
number. Try...
PRINT PI
We have also drawn in two lines called axes through the centre of
the circle. The one through 3 o'clock is called the x-axis, and
the one through 12 o'clock is called the y-axis.
To specify where the point is, you say how far it has moved round
the circle from its 3 o'clock starting position: let us call this
distance a. We know that the circumference of the circle is 2π
(because its radius is 1 and its diameter is thus 2); so when it
has moved a quarter of the way round the circle, a is equal to
π/2; when it has moved halfway round, a is equal to π, and when it
has moved the whole way round, a is equal to 2π.
Given the curved distance round the edge - a, two other distances
you might like to know are how far the point is to the right of
the y-axis, and how far it is above the x-axis. These are called,
respectively, the cosine and sine of a. The functions COS and SIN
on the +2A will calculate these.
Note that if the point goes to the left of the y-axis, then the
cosine becomes negative, and if the point goes below the x-axis,
the sine becomes negative.
Another property is that once a has got up to 2π, the point is
back where it started and the sine and cosine start taking the
same values all over again, ie. SIN (a+2*PI) equals SIN a, and COS
(a+2*PI) equals COS a.
In the diagram of the point moving round the circle, look at the
radius joining the centre to the point. You should be able to see
that the distance we have called a (the distance that the point
has moved round the edge of the circle) is a way of measuring the
angle through which the radius has moved away from the x-axis.
When a is equal to π/2, the angle is 90 degrees; when a is equal
to π the angle is 180 degrees, and so on, round to when a is equal
to 2π, and the angle is 360 degrees. You might just as well forget
about degrees, and measure the angle in terms of a alone; we say
then that we are measuring the angle in radians. Thus π/2 radians
is equal to 90 degrees and so on.
You must always remember that on the +2A, the functions SIN, COS,
etc. use radians and not degrees. To convert degrees to radians,
divide by 180 and multiply by π; to convert back from radians to
degrees, you divide by π and multiply by 180.
Part 11
Random Numbers
Subjects covered...
RANDOMIZE
RND
Each time you use it, its result is a new random number between 0
and 1. (Sometimes it can take the value 0, but never 1.)
Try...
10 PRINT RND
20 GO TO 10
...to see how the answer varies. Can you detect any pattern? You
shouldn't be able to - 'random' means that there is no pattern.
RND gives a random number between 0 and 1, but you can easily get
random numbers in other ranges. For instance, 5*RND is between 0
and 5, and 1.3+0.7*RND is between 1.3 and 2. To get whole numbers,
use INT (remembering that INT always rounds down) as in
1+INT(RND*6), which we shall use in a program to simulate dice.
RND*6 is in the range 0 to 6, but since it never actually reaches
6, INT(RND*6) is 0, 1, 2, 3, 4 or 5.
10 RANDOMIZE 1
20 FOR n=1 TO 5: PRINT RND ,:
NEXT n
30 PRINT : GO TO 10
If you had a program with RND in it and it also had some mistakes
that you had not found, then it would help to use RANDOMIZE like
this so that the program behaved the same way each time you ran
it.
10 RANDOMIZE
20 PRINT RND : GO TO 10
The sequence you get here is not very random, because RANDOMIZE
uses the time since the +2A was switched on. As this has gone up
by the same amount each time that RANDOMIZE is executed, the next
RND does more or less the same. You would get better 'randomness'
by replacing GO TO 10 by GO TO 20.
Here is a program to toss coins and count the numbers of heads and
tails...
(75*(your number+1)-1)/65536
Arrays
DIM
Suppose that you have a list of numbers - for instance, the marks
of ten people in a class. To store them in the +2A you could use
the variables m1, m2, m3...and so on up to m10, but the program to
set up these ten variables would be rather long and tedious to
type in, ie...
10 LET m1=75
20 LET m2=44
30 LET m3=90
40 LET m4=38
50 LET m5=55
60 LET m6=64
70 LET m7=70
80 LET m8=12
90 LET m9=75
100 LET m10=60
Before you can use an array, you must reserve some space for it in
the +2A's memory, and you do this by using the keyword DIM (for
dimension). The statement...
DIM m(10)
10 DIM m(10)
20 FOR n=1 TO 10
30 READ m(n)
40 NEXT n
50 DATA 75,44,90,38,55,64,70,1
2,75,60
10 DIM m(10)
20 FOR n=1 TO 10
30 INPUT m(n)
40 NEXT n
Note that the DIM statement must come before any attempt to access
the array in a program.
If you wish, you may examine the contents of the array using...
10 FOR n=1 TO 10
20 PRINT m(n)
30 NEXT n
PRINT m(1)
PRINT m(2)
PRINT m(3)
...etc...
You can also set up arrays with more than one dimension. In a two
dimensional array you need two numbers to specify an element -
rather like the line and column numbers that specify a character
position on the screen. If you imagine the line and column numbers
(two dimensions) as referring to a printed page, you could then,
for example, have an extra dimension to represent the page
numbers. Think of the elements of a three dimensional array v as
being specified by v (page number,line number,column number).
For example, to set up a two-dimensional array c with dimensions 3
and 6, you use the DIM statement...
DIM c(3,6)
Although you can have a number and an array with the same name,
you cannot have two arrays with the same name, even if they have a
different number of dimensions.
There are also string arrays. The strings in an array differ from
simple strings in that they are of fixed length and assignment to
them is always Procrustean (ie. chopped off or padded with
spaces).
Suppose then, that you want an array a$ of five strings. You must
decide how long these strings are to be - let us suppose that 10
characters for each element is long enough. You then say...
This sets up a 5x10 array of characters, but you can also think of
each row as being a string...
LET a$(2)="1234567890"
...and...
1234567890 7
For the last subscript (the one you can miss out), you can also
have a slicer, so that for instance...
Remember - In a string array, all the strings have the same fixed
length.
The DIM statement has an extra number (the last one) to specify
this length. When you write down a subscripted variable for a
string array, you can put in an extra number (a slicer) to
correspond with the extra number in the DIM statement.
DIM a$(10)
...and you will find that a$ behaves just like a string variable,
except that it always has length 10, and assignment to it is
always Procrustean.
Exercise...
AND, OR
NOT
IF condition THEN...
The conditions there were the relations =, <, >, <=, >= and <>
which compare two numbers or two strings. You can also combine
several of these, using the logical operations: AND, OR and NOT.
The NOT relationship turns things upside down. The NOT relation is
true whenever the relation is false, and false whenever it is
true.
...and also...
Convince yourself that >= and <= are the negations of < and >
respectively. Thus you can always get rid of NOT from in front of
a relation by changing the relation.
Also...
...and...
Using this, you can work NOTs through brackets until eventually
they are all applied to relations, and then you can get rid of
them. Logically speaking, NOT is unnecessary, although you might
still find that using it makes a program clearer.
Try...
(i) =, <, >, <=, >= and <> all give the numeric results: 1 for
true, and 0 for false. Thus, the PRINT command above printed
0 for 1=2, which is false, and 1 for 1<>2, which is true.
IF condition THEN...
...the condition can be actually any numeric expression. If its
value is 0, then it counts as false, and any other value
(including the value of 1 that a true relation gives) counts as
true. Thus the IF statement means exactly the same as...
x, if y is true (non-zero)
x AND y has the value
0 (false), if y is false (zero)
10 INPUT a
20 INPUT b
30 PRINT (a AND a >= b)+(b AND
a< b)
40 GO TO 10
x AND y
...as meaning...
...and of...
x OR y
...as meaning...
You can also make string valued conditional expressions, but only
using AND.
x$ if y is non-zero
x$ AND y has the value
"" if y is zero
Try this program, which inputs two strings and puts them in
alphabetical order.
CODE, CHR$
POKE, PEEK
USR
BIN
There are 256 characters, and each one has a code between 0 and
255 (there is a complete list of them in part 28 of this chapter).
To convert between codes and characters, there are two functions,
CODE and CHR$.
The rest of the characters are not part of ASCII, but are
dedicated to the ZX Spectrum range of computers. First amongst
them are a space and 15 patterns of black and white blobs. These
are called the graphics symbols and can be used for drawing
pictures. You can enter these from the keyboard, using what's
known as graphics mode. Pressing the GRAPH key switches on
graphics mode, after which the keys 1, 2, 3, 4, 5, 6, 7 and 8 will
produce the graphics symbols...
The cursor keys won't work properly while all this is going on as
the +2A interprets them as shifted number keys, and prints
graphics characters accordingly.
128 143
129 142
130 141
131 140
132 139
133 138
134 137
135 136
After the graphics symbols in the character set, you will see what
appears to be another copy of the alphabet from A to S. These are
characters that you can redefine yourself (though when the machine
is first switched on they are set as letters) - they are called
user-defined graphics. You can type these in from the keyboard by
going into graphics mode, and then using the letter keys A to S.
(i) Work out what the character looks like. Each character has an
8 x 8 grid of dots, each of which can appear to be either on
or off. You'd draw a diagram something like this (with black
squares representing the dots which are on)...
When a dot is on, the +2A prints the ink colour; when a dot is
off, the +2A prints the paper colour. (The terms ink and paper are
explained in part 16 of this chapter.)
(If you know about binary numbers, then it should help you to know
that BIN is used to write a number in binary instead of the usual
decimal.) Look at the pattern of binary numbers through half-
closed eyes - you may even be able to see the π character!
10 FOR n=0 TO 7
20 READ row : POKE USR "P"+n, r
ow
30 NEXT n
40 DATA BIN 00000000
50 DATA BIN 00000000
60 DATA BIN 00000010
70 DATA BIN 00111100
80 DATA BIN 01010100
90 DATA BIN 00010100
100 DATA BIN 00010100
110 DATA BIN 00000000
You will have noticed that we have not printed out the first 32
characters (codes 0 to 31) - these are control characters. They
don't produce anything printable, but instead are used to control
the screen display or some other function of the +2A.
The three control characters that the screen display uses are 6, 8
and 13 (these will now be explained). On the whole, CHR$ 8 is the
only one you are likely to find useful.
PRINT 1,2
Obviously this is not a very clear way of using it. A more subtle
way is to say...
Using the codes for the characters we can extend the concept of
'alphanumerical ordering' to cover strings containing any
characters, not just letters. If instead of thinking in terms of
the usual alphabet of 26 letters we use the extended alphabet of
256 characters, in the same order as their codes, then the
principle is exactly the same. For instance, the following strings
are in their 'Spectrum' ASCII alphabetical order. (Notice the
rather odd feature that lower case letters come after all the
capitals; so a comes after Z. Notice also that spaces are
significant.)
Here is the rule for finding out in which order two strings come.
Start by comparing the first two characters. If they are
different, then one of them has its code less than the other, and
the string it comes from is the earlier (lesser) of the two
strings. If they are the same, then go on to compare the next two
characters. If in this process one of the strings runs out before
the other, then that string is the earlier; otherwise they must be
equal.
The relations =, <, >, <=, >=, and <> are used for strings as well
as for numbers: < means 'comes before' and > means 'comes after',
so that...
"AA man"<"AARDVARK"
"AARDVARK">"AA man"
...are both true.
<= and >= work the same way as they do for numbers, so that...
...is false.
Experiment on all this using the program here, which inputs two
strings and puts them in order.
Note (in the above program and also in the program at the end of
part 13) how we have to introduce c$ in line 20 when we swap over
a$ and b$. Can you see why simply using...
The next program sets up user defined graphics for the following
keys to display chess pieces...
B for bishop
K for king
R for rook
Q for queen
P for pawn
N for knight
Chess pieces...
When you have run this program, you may look at the pieces by
pressing GRAPH followed by any of the keys: B, K, R, Q, P or N.
Exercises...
10 INPUT c
20 PRINT CHR$ c;
30 GO TO 10
"EVIL"
"evil"
Part 15
More about PRINT and INPUT
Subjects covered...
CLS
PRINT items
Expressions (numeric or string type)
TAB numeric expression
AT numeric expression
PRINT separators, ; '
INPUT items
Variables (numeric or string type)
LINE string variable
Scrolling
SCREEN$
You have already seen PRINT used quite a lot, so you will have a
rough idea of how it is used. Expressions whose values are printed
are called PRINT items. They may be separated by commas,
semicolons or apostrophes, which are called PRINT separators. A
PRINT item can also be nothing at all, which is a way of
explaining what happens when you use PRINT on its own.
There are two more kinds of PRINT items, which are used to tell
the +2A not what, but where to print. For example, the
instruction...
10 PRINT AT 11,16;"*"
AT line,column
...moves the PRINT position (the place where the next item is to
be printed) to the line and column specified. Lines are numbered
from 0 (at the top) to 21; columns are numbered from 0 (on the
left) to 31.
...will read the asterix printed in the centre of the screen, then
print it at location 0,0 (the top left-hand corner).
The function...
TAB column
...prints enough spaces to move the PRINT position to the column
specified. It stays on the same line, or, if this would involve
backspacing, moves to the next line. Note that the +2A reduces the
column number 'modulo 32' (ie. it divides by 32 and takes the
remainder) - so TAB 33 means the same as TAB 1.
As an example...
...is how you might want to print out the heading on the contents
page (page 1) of a book.
10 FOR n=0 TO 20
20 PRINT TAB 8*n;n;
30 NEXT n
This shows what is meant by the TAB numbers being reduced modulo
32.
(i) TABs and print items are best terminated with semicolons, as
we have done above. You can use commas (or nothing, at the
end of the statement), but this means that after having
carefully set up the PRINT position, you immediately move it
on again - not terribly useful!
(ii) You cannot print on the bottom two lines (22 and 23) on the
screen because they are reserved for commands, INPUT data,
reports, error messages and so on. References to the 'bottom
line' usually mean line 21.
(iii)You can use AT to locate the PRINT position even where there
is already something printed - the new print item will simply
overwrite the old.
The INPUT statement can do much more than we have told you so far.
You have already seen INPUT statements like...
...in which the +2A prints the caption How old are you? at the
bottom of the screen, and then you have to type in your age. In
fact though, an INPUT statement can be made up of items and
separators in exactly the same way as a PRINT statement, so How
old are you? and age are both INPUT items. INPUT items are
generally the same as PRINT items, however, there are some very
important differences:
First, an obvious extra INPUT item is the variable whose value you
require to be typed in - age in our example above. The rule is
that if an INPUT item begins with a letter, then it must be a
variable whose value is to be input.
This would seem to mean that you can't print out the values of
variables as part of a caption. However, you can get round this by
putting brackets around the variable. Any expression that starts
with a letter must be enclosed in brackets if it is to be printed
as part of a caption.
Any kind of PRINT item that is not affected by these rules is also
an INPUT item. Here is an example to illustrate what's going on...
Run the program (just press ENTER each time it stops). When This
is Line 2, is printed, the lower part of the screen moves up to
make room for it; but the numbering moves up as well, so that the
lines of text keep their same numbers.
As the lower part of the screen goes up and up, the upper part
remains undisturbed until the lower part threatens to write on the
same line as the PRINT position. Then the upper part starts
scrolling up to avoid this.
INPUT LINE a$
...then the +2A will not give you the string quotes that it
normally does for a string variable (though it will pretend to
itself that they are there). So if you type in...
bugs
...as the INPUT data, a$ will be given the value bugs. Because the
string quotes do not appear with the string, you cannot delete
them and type in a different sort of string expression for the
INPUT data. Remember that you cannot use LINE for numeric
variables.
10 INPUT numbers
20 PRINT numbers
30 GO TO 10
Input a few numbers, and they'll be printed faithfully onto the
screen. Now press EXTEND MODE followed by the M key. The word PI
appears, and if you press ENTER, then 3.1415927 will appear as if
by magic. However, if you type PI as two letters without the aid
of EXTEND MODE then the +2A will stop with the report 2 Variable
not found, 10:1.
PRINT AT 1, c;
The TAB control character is CHR$ 23 and the two characters after
it combine to give a number between 0 and 65535, specifying the
number you would have in a TAB item. The statement...
You can use POKE to stop the computer asking if you wish to
scroll? by typing...
Exercise...
10 LET m$=""
20 LET a= INT ( RND *12)+1: LE
T b = INT ( RND *12)+1
30 INPUT (m$) ' ' "what is ";(
a);" x ";(b);"?";c
100 IF c=a*b THEN LET m$="Right
.": GO TO 20
110 LET m$="Wrong. Try again.":
GO TO 30
If they are perceptive, they might manage to work out that they do
not have to do the calculation themselves. For instance, if the
+2A asks them to type the answer to 2x3, then all they have to do
is type in 2*3 literally.
Part 16
Colours
Subjects covered...
This shows the eight colours (including white and black) and the
two levels of brightness that the +2A can produce on a colour TV.
(If your TV is black-and-white, then you will see just various
shades of grey.) A quicker way to achieve a similar result is to
RESET the +2A whilst holding down BREAK - but that's a little
drastic. Here is a list of which numbers produce which colours
(for your reference)...
0 - black
1 - blue
2 - red
3 - magenta
4 - green
5 - cyan
6 - yellow
7 - white
The character has two colours associated with it: the ink, or
foreground colour, which is the colour for the black dots in our
square, and the paper, or background colour, which is used for the
white dots. To start off with, every cell has black ink and white
paper so writing appears as black on white.
(ii) Ink and paper colours, each coded into a number between 0 and
7.
Note that since the ink and paper colours cover a whole character
cell, you cannot possibly have more than two colours in a given
block of 64 dots. The same goes for the brightness and flash
numbers - they refer to the whole character cell, not individual
dots within the cell. The colour, brightness and flash number for
a given character cell are called attributes.
When you print something on the screen, you change the dot pattern
for that character cell. It is less obvious, but still true, that
you also change the cell's attributes. To start off with you do
not notice this because everything is printed with black ink on
white paper (at normal brightness and no flashing); however, you
can vary this with the INK, PAPER, BRIGHT and FLASH statements.
Using the edit menu's Screen option, go to the bottom screen, and
try...
PAPER 5
...and then PRINT a few items on the screen - they will appear on
cyan paper, because as they are printed, the paper colour for the
cells they occupy are set to cyan (which has code 5).
The others work the same way, so you may use the settings...
...and any printing will set the corresponding attributes for all
the character cells it subsequently uses.
Try some of these out. You should now be able to see how the
program at the beginning of this section worked (remember that a
space is a character that has its ink and paper the same colour).
There are some more numbers you can use in these statements that
have less direct effects.
PAPER 8
9 can be used only with PAPER and INK, and means 'contrast'. The
colour (ink or paper) that you use it with is made to contrast
with the other by being made white if the other is a dark colour
(black, blue, red or magenta), or being made black if the other is
a light colour (green, cyan, yellow or white).
The ink colour here is always made to contrast with the old paper
colour for each character cell.
Colour TV relies on the fact that the human eye need see only
three colours of light (red, green and blue) in various
combinations and intensities in order to perceive all the colours
of the spectrum. The +2A also displays its spectrum of colours by
using mixtures of red, green and blue. For instance, yellow is
made by mixing red with green - which is why its code, 6, is the
sum of the codes for red and green.
There is a function called ATTR that finds out what the attributes
are at a given position on the screen. It is a fairly complicated
function, so it has been relegated to the end of this section.
There are two more statements, INVERSE and OVER, which control not
the attributes, but the dot pattern that is printed on the screen.
They use the numbers 0 for off, and 1 for on. If you use INVERSE
1, then each character cell's dot pattern will be the inverse of
its usual form, ie. paper dots will be replaced by ink dots and
vice versa. Thus the character cell containing 'a' (shown
previously) would be printed as follows...
If (as at switch on) we have black ink and white paper, then the
'a' will appear as white on black. The statement...
OVER 1
10 OVER 1
20 PRINT "w"; CHR$ 8;"_";
There is another way of using INK, PAPER and so on which you will
probably find more useful than having them as statements. You can
put them as items in a PRINT statement (followed by ;), and they
then do exactly the same as they would have done if they had been
used as statements on their own, except that their effect is only
temporary, lasting as far as the end of the PRINT statement that
contains them. Thus if you type...
PAPER, INK, etc. when used as statements do not affect the colour
in the bottom part of the screen (where INPUT data is typed in and
reports are displayed). The bottom screen uses the colour of the
border for its paper colour, code 9 (for contrast) for its ink
colour, has flashing off, and everything at normal brightness. You
can change the border colour to any of the eight normal colours
(not 8 or 9) using the statement...
BORDER colour
The +2A has a high regard for your sanity - no matter what
combination of effects and colours you manage to produce from a
BASIC program, the editor will always use black ink on white
paper.
On the whole, you would not bother to use these control characters
because you might just as well use the statements PAPER, INK, etc.
However, if you have some old 48K BASIC programs on cassette, you
may find such control characters embedded in the listing. In
general, the editor will actively ignore them, and remove them at
the first opportunity. It is not possible to insert them into
listings as with the old 48K Spectrum.
ATTR (line,column)
Its two arguments are the line and column numbers that you would
use in an AT item, and its result is a number that shows the
colours and so on at the corresponding character position on the
TV screen. You can use this as freely in expressions as you can
any other function.
The number that is the result is the sum of four other numbers as
follows:
Exercises...
l. Try...
Where the / has cut through the B, it has left a white dot. This
is the way that overprinting works on the +2A - two papers or two
inks give a paper, one of each gives an ink. This has the
interesting property that if you overprint with the same thing
twice you end up with what you had at the beginning. If you now
type...
(Never mind how this program works.) The program is changing the
colours of squares on the TV screen and the RND should ensure that
this happens randomly. (The diagonal stripes that you eventually
see are a manifestation of the hidden pattern in RND, ie. pseudo-
random instead of truly random.)
Part 17
Graphics
Subjects covered...
In this section we shall see how to draw pictures on the +2A. The
part of the screen you can use has 22 lines and 32 columns, making
22x32=704 character positions. As you may remember from part 16,
each of these character positions is made up of an 8 x 8 grid of
dots which are called pixels (picture elements).
The statement...
Notice that pixel coordinates are rather different from the line
and column in an AT item. You may find that the diagram in part 15
of this chapter is useful when working out pixel coordinates and
line and column numbers.
To help you with your pictures, the +2A will draw straight lines,
circles and parts of circles for you, using the DRAW and CIRCLE
statements.
The statement DRAW (to draw a straight line) takes the form...
DRAW x,y
The starting place of the line is the pixel where the last PLOT,
DRAW or CIRCLE statement left off (this is called the PLOT
position - RUN, CLEAR, CLS and NEW reset it to the bottom left-
hand corner, at 0,0); the finishing place of the line is x pixels
to the right of that and y pixels up. The DRAW statement on its
own determines the length and direction of the line, but not its
starting point.
You can also plot and draw in colour, although you have to bear in
mind that colours always cover the whole of a character cell and
cannot be specified for individual pixels. When a pixel is
plotted, it is set to show the full ink colour, and the whole of
the character cell containing it is given the current ink colour.
This program demonstrates that point...
The lines seem to get broader as the program goes on, and this is
because a line changes the colours of all the inked-in pixels of
all the character cells that it passes through. Note that you can
embed PAPER, INK, FLASH, BRIGHT, INVERSE and OVER items in a PLOT
or DRAW statement just as you could with PRINT and INPUT. They go
between the keyword and the coordinates, and are terminated by
either semicolons or commas.
An extra frill with DRAW is that you can use it to draw parts of
circles instead of straight lines, by including an extra number to
specify an angle to be turned through. The form is...
DRAW x,y,a
x and y are used to specify the finishing point of the line just
as before, and a is the number of radians that it must turn
through as it goes. If a is positive then it turns to the left; if
a is negative then it turns to the right. Another way of seeing a
is as showing the fraction of a complete circle that will be
drawn, (a complete circle is 2π radians) so if a equals π it will
draw a semicircle, if a equals 0.5xπ a quarter of a circle, and so
on.
Start at (100,100)
Just as with PLOT and DRAW, you can put the various sorts of
colour items in at the beginning of a CIRCLE statement.
Type...
PAPER 7: INK 0
Now try...
This doesn't quite work because the pixels that the line uses on
the way back are not quite the same as the ones that it used on
the way there. You must therefore undraw a line in exactly the
same direction as you drew it.
Exercises...
2. If you have read part 10, see if you can work out how to draw
circles using SIN and COS. Run this program...
You can see that the CIRCLE statement is much quicker, albeit less
accurate.
3. Try...
You can see from this that the CIRCLE statement leaves the PLOT
position at a rather indeterminate place - it is always somewhere
about halfway up the right-hand side of the circle. You will
usually need to follow the CIRCLE statement with a PLOT statement
before you do any more drawing.
Part 18
Timing
Subjects covered...
Quite often you will want to make the program take a specified
length of time, and for this you will find the PAUSE statement
useful.
PAUSE n
The clock will run down after about 55.5 hours because of line 60,
but you can easily make it run longer. Note how the timing is
controlled by line 210. You might expect PAUSE 50 to make it tick
once per second, however, the computing takes a bit of time as
well and has to be allowed for. This is best done by trial and
error, timing the +2A clock against a real one, and adjusting line
210 until they agree. You can't do this very accurately - an
adjustment of one frame per second is equal to 2% (or half an hour
in a day).
There is a much more accurate way of measuring time. This uses the
contents of certain memory locations. The data stored is retrieved
by using PEEK. Part 25 of this chapter explains what we're looking
at in detail. Type in the expression...
This prints the number of seconds since the +2A was turned on (up
to about 3 days and 21 hours, after which it goes back to 0).
The numbers PEEK 23674, PEEK 23673 and PEEK 23672 are held inside
the +2A and used for counting in 50ths of a second. Each is
between 0 and 255 and they gradually increase through all the
numbers from 0 to 255; after 255 they drop straight back to 0.
The one that increases most often is PEEK 23672 - every 1/50
second it increases by 1. When it is at 255, the next increase
'nudges' it to 0, and at the same time it increments PEEK 23673 up
by 1. When (every 256/50 seconds) PEEK 23673 is nudged from 255 to
0, it in turn increments PEEK 23674 up by 1. This should be enough
to explain why the expression above works.
Now, consider this carefully; suppose our three numbers are 0 (for
PEEK 23674), 255 (for PEEK 23673) and 255 (for PEEK 23672). This
means that it is about 21 minutes after switch on. Our expression
ought to yield (65536x0+256x255+255)/50 which is equal to 1310.7.
Now if you look carefully at the previous program, you can see
that it does this implicitly.
You can change the three counter numbers so that they give the
real time instead of the time since the +2A was switched on. For
instance, to set the time at 10.00am, you work out that this is
10x60x60x50 which is equal to 1800000 fiftieths of a second (and
1800000 is equal to 65536x27 + 256x119 + 64x1).
Here line 10 waits for you to lift your finger off the keyboard,
and line 20 waits for you to press a new key.
Unlike INPUT, INKEY$ doesn't wait for you, so you don't have to
press ENTER.
Exercises...
10 PAUSE 0
20 PRINT INKEY$ ;
30 GO TO 10
BEEP, PLAY
As you will have already noticed, the +2A can make a variety of
noises. To get the best quality of sound, it's important to make
sure that your TV is tuned-in properly (see chapter 2). If,
instead of a TV, you are using a VDU monitor (which won't
reproduce the +2A's sound), note that a separate sound signal
(which may be connected to an audio amplifier powering speaker(s)
or headphones) is available from the TAPE/SOUND socket at the back
of the +2A. Headphones may not be plugged into the TAPE/SOUND
socket directly.
To get the most out of the +2A's musical ability, it helps to have
a little knowledge about musical terms.
Type in this command (don't worry about what it means just yet)...
PLAY "ga"
Two notes were played - the second slightly higher than the first.
The difference between the notes is called a tone. Now try...
PLAY "g$a"
Again there were two notes played - the first one was the same as
the previous example, but there was less of a jump to the second.
If you didn't hear the difference, then try the first example
followed by the second again. The second example has half the
difference between notes, and this is called a semitone.
PLAY "gD"
This sort of difference is called a fifth, and occurs quite often
in music of all kinds. With that example ringing in your ears,
type...
PLAY "gG"
There are two ways of making music and sounds with the +2A. The
most elementary is the somewhat spartan BEEP command. This takes
the form...
BEEP duration,pitch
Here is a diagram to show the pitch values of all the notes in one
octave on the piano for BEEP...
Hence, to play the A above middle C for half a second, you would
use...
BEEP 0.5,9
10 FOR f=1 TO 8
20 READ note
30 BEEP 0.5,note
40 NEXT f
50 DATA 0,2,4,5,7,9,11,12
PLAY "a"
PLAY "cdefgabC"
Notice that the last C in the example above is in upper case. This
tells the PLAY command to play it an octave higher than the lower
case c. A scale, by the way, is the term used for a set of notes
spanning an octave. The example above is called the C major scale
because it's the set of notes between two C's. Why major? There
are two main classes of scale, major and minor, and this is just
musical shorthand for describing two different sets.
PLAY "cd$efg$a$bC"
10 LET o$="05"
20 LET n$="DECcg"
30 LET a$=o$+n$
40 PLAY a$
There are a few new things in this program. Firstly, PLAY is just
as happy with a string variable as with a string constant. In
other words, providing that a$ has been set up beforehand, PLAY a$
works just as well as PLAY "05DECcg". In fact, using variables in
PLAY statements has certain distinct advantages, and we shall be
doing this from now on.
Notice also that the string a$ has been 'built up' by combining
the two smaller strings o$ and n$. While this doesn't make much
difference at this sort of level, PLAY can cope with strings many
thousands of notes long, and the only sensible way of creating and
editing those strings from BASIC is to combine lots of smaller
strings in this way.
Now run the above program. Edit line 10 so that "05" becomes "07",
and run it again, or if you want to be a big spaceship make it
"02". If you don't specify an octave number for a particular
string, then the +2A assumes that you want octave 5. Here follows
a diagram of the notes and octave numbers which correspond to the
standard even-tempered musical scale...
PLAY can also handle many different lengths of note. Edit the
program above so that line 10 is now...
10 LET o$="2"
...and run it. Then alter the setting of o$ between "1" and "9".
The note length can be changed anywhere in a string by including a
number between 1 and 9, and this is effective for all subsequent
notes until a new number is encountered. Each of these nine note
lengths has a specific musical name, and looks different when
written down in musical notation. The following table shows which
is which...
PLAY can also cope with triplets, which are three notes played in
the time for two. Unlike simple note lengths, the triplet number
only applies for the three notes immediately following, and then
the previous note length number resumes. The triplet numbers are
as follows...
PLAY is quite happy about being told to 'shut up'! A timed period
during which no notes play is called a rest, and "&" is used to
signify this. The length of rest it produces is the same as the
current note length. To demonstrate, edit lines 10 and 20 to...
10 LET 0$="04"
20 LET n$="DEC&cg"
Two notes played together without a break are called tied notes,
which are signified in a PLAY command by an _ underline, so a
crotchet c and a minim c tied together would be "5_7c". (The
second value is then used as the note length for all subsequent
notes, as before.)
There are occasions when ambiguity creeps in. Say that a piece of
music needs octave 6 and a note length of 2, then...
10 LET o$="062"
...seems a good bet - but no! The computer will find the 0 and try
to read the number following it. When it finds 62, it will stop
with the report n Out of range. In cases like this, there is a
'dummy note' called N that just serves to split things up, so line
10 should be...
10 LET o$="06N2"
Playing more than one channel at a time is very simple; you just
separate lists of notes by commas. Try this new program...
10 LET a$="04cCcCgGgG"
20 LET b$="06CaCe$bd$bD"
30 PLAY a$,b$
5 LET t$="T120"
10 LET a$=t$+"04cCcCgGgG"
20 LET b$="06CaCe$bd$bD"
30 PLAY a$,b$
10 LET a$=t$+"04(cC)(gG)"
PLAY treats it just the same as the old line 10. If you include a
closing bracket (with no matching opening bracket), then the
string up to that point is repeated indefinitely. This is useful
for rhythm effects and bass lines. To demonstrate, try this
(you'll have to press BREAK to stop the sound)...
PLAY "04N2cdefgfed)"
...and...
PLAY "04N2cd(efgf)ed)"
10 LET a$="cegbdfaC"
20 LET b$="04cC)"
30 PLAY a$,b$
10 LET a$="cegbdfaCH"
So far we've only used notes which start and stop at one level of
volume. The +2A can alter the volume of a note while it is
playing, so it can start loud and die away like a piano, or rise
and fall like a dog growling. To turn these effects on, use "W"
(for waveform) followed by a number between 0 and 7, together with
"U" for each channel you want to use the effect on. Any channel
with a volume setting ("V") will not respond to "U". This table
shows graphically how the volume changes for each setting...
This program plays the same note with each effect in turn, so you
can compare them against the diagram above.
10 LET a$="UX1000W0C&W1C&W2C&W
3C&W4C&W5C&W6C&W7C"
20 PLAY a$
The PLAY command isn't limited to pure musical notes. There are
also three 'white noise' generators (white noise is a sound which
is like an un-tuned FM radio or TV), and any of the three channels
can play notes, white noise, or a mixture of both. To select a mix
of noise and note, you may use "M" followed by a number between 1
and 63. You can work out which number to use from this table...
Write down the numbers corresponding to the effects you want, and
then add them together. If you wanted A to be noise, B to be tone,
and C to be both tone and noise, then add 8, 2, 4 and 32 to get 46
(the order of the channels is the order of the strings which
follow the PLAY command). The best effects can be obtained with
the A channel - don't be afraid to experiment.
The PLAY command will simply 'hop over' any comments in the
string.
10 LET a$="Y1T100o2(((1CCg$b))
(($E$E$b$D))((FFC$E))((GGDF
))))"
20 LET b$="Y2o5N&&&&C$bfG)"
30 LET c$="Y3o4((3C&)C&1CCDD(3
$E&)$E&1$E$EEE(3F&)F&1FF$G$
G(3G&)G&1GG$EC))"
40 LET d$="Y4N9&&&&&&&&(9EGF7b
5CD))"
50 PLAY a$,b$,c$,d$
Summary table
Finally, here is a brief list of the parameters that can be used
in the string of a PLAY command together with the values they may
take...
STRING FUNCTION
a...g Specifies the pitch of the note within the current
A...G octave range.
$ Specifies that the note which follows must be flattened.
# Specifies that the note which follows must be sharpened.
0 Specifies the octave number to be used (followed by 0 to
8).
1...12 Specifies the length of notes to be used.
& Specifies that a rest is to be played.
_ Specifies that a tied note is to be played.
N Separates two numbers.
V Specifies the volume to be used (followed by 0 to 15).
W Specifies the volume effect to be used (followed by 0 to
7).
U Specifies that the volume effect is to be used in a
string.
X Specifies duration of volume effect (followed by 0 to
65535).
T Specifies tempo of music (followed by 60 to 240).
() Specifies that enclosed phrase must be repeated.
! ! Specifies that enclosed comment is to be skipped over.
H Specifies that the PLAY command must stop.
M Specifies the channel(s) to be used (followed by 1 to
63).
Y Specifies that MIDI channel is to be used (followed by 1
to 16).
Z Specifies MIDI programming code (followed by code
number).
Part 20
File operations
Subjects covered...
As the processor at the heart of the +2A can only converse with
64K of memory at a time, the extra RAM in the +2A's 128K memory is
used just like another drive. This is called the RAMdisk and is
identified by the letter M: (for memory drive). Drive M: exists on
all +2As (you don't have to add a disk interface to be able to use
it). All the commands (except FORMAT) that you can use on drives
A: and B: can also be used on drive M:. Drive M is much faster
than the mechanical disk drives, but it is very important to
remember that just like the contents of the program memory, the
contents of drive M: are erased if you press the RESET button or
switch off the +2A. BASIC'S NEW command, however, will leave any
files stored on drive M: intact.
If you have connected one disk drive (only) to your +2A, you can
still use the system as if a second drive (drive B:) were present;
if you ask the +2A to perform an operation on drive B: (you'll see
how to do this later), a message will appear asking you to...
...whereupon you should put the disk that you would have used in
drive B: (if it had existed) into drive A: then press any key (for
example ENTER). From then on, the machine will treat your only
disk drive as if it really were drive B:. When the +2A next needs
to perform some operation on the disk that was originally in the
drive (ie. in drive A:), it will ask you to...
Please put the disk for A: into
the drive then press any key
Now that you know which drives are available and what they are
called, let's see what they can be used for. Type in the short
program (which displays coloured squares) that you first met at
the end of part 16, ie...
This is the program that you are going to save onto disk.
If you have not connected a disk drive to the +2A, then the
following information about using the FORMAT command can be
ignored, as the drive you are going to use, drive M:, does not
need to be formatted - in fact it is not possible to use FORMAT on
drive M:.
FORMAT "a:"
If you haven't already put your new disk into the drive, don't
worry - the +2A will just come back with the report Drive not
ready.
In this case, put your new disk into drive A: and re-type the
command.
By the way, while you are using the various disk commands, you may
occasionally see the report Drive A: not ready (possibly followed
by - Retry, Ignore or Cancel?). This invariably means that you
have forgotten to put a disk into the drive.
This is a safety feature that will allow you to abandon the format
before the process gets going, if, by some chance, you inserted
the wrong disk. In this case you should type A (to abandon) and
nothing more will happen. If, however, you really do intend to
reformat the disk and don't mind losing what's on it, then press
any key apart from A (eg. press ENTER).
If you want to recover files from a corrupted disk, you can try to
copy them individually (using COPY) to a known 'good' disk. If one
or more files prove uncopiable, then you have probably lost them
for good. We recommend that you keep at least two copies of
important files (on different disks) - one for day-to-day use, and
one kept in a safe place just in case the unthinkable happens.
Making regular copies of valuable data and programs is known as
backing-up and is an essential habit to get into. Backing-up can
save you untold misery and tears. You can, of course, make back-
ups onto tape (which may prove cheaper in the long run).
If you have not connected a disk drive to the +2A type in the
following command...
SAVE "m:"
SAVE "squares"
The word SQUARES is just a name that you use to 'label' the
program you are going to store on disk. To prevent confusion,
everything stored on disk must be given a name. These names
(called filenames) are a little different from those that you may
use when storing programs on tape.
Filenames
The range of characters that you are allowed to use for disk
filenames is more limited than for tape filenames. The format of
filenames on disks for use with the +2A is the same as that used
by an operating system known as CP/M (Control Program/Monitor) by
Digital Research Inc. The fact that these formats are the same
means that you can take a disk used with a +2A (or +3) and use it
on other computers. In this way, data can be transferred between
the +2A and a CP/M system, and this is most likely to be useful
for people writing machine code programs or moving text from a +2A
word processor to a CP/M program. (It is extremely unlikely that
programs written in BASIC can be usefully converted from one
machine to another using this method.)
You needn't worry about what user number means; if you don't know
already, then it's probably best to remain blissfully ignorant.
However, for anyone who is interested: on CP/M machines with very
large disk capacities (or hard disks) with perhaps more than one
terminal connected, user numbers are used to partition files into
subsections (known as user areas) so that there isn't just one
huge directory with several thousand files. On the +2A, however,
disks cannot have more than 64 files, so the use of user areas is
not really necessary. Nevertheless, user areas can be specified in
filenames used in +2A disk commands. They take the form...
SAVE "5a:squares"
When saving to tape, a user number and drive letter will just be
taken as part of the tape filename (within which 10 characters
(maximum) are permitted). The tape itself will not be partitioned
into separate user areas.
The problem with using user areas on disk is that it's quite easy
to forget which user area you saved a file to, and so finding it
could take a while (as the CAT command can only catalog one user
area at a time).
SAVE "squares"
SAVE "a:squares"
If you have not connected a disk drive to the +2A, then the
default 'drive' is the datacorder and both of the above commands
would save the program to tape (though with differing filenames).
(This is why the message: Press REC & PLAY, then any key appears
each time.) Don't worry that there is no tape in the datacorder
and that nothing has been saved. We can use the RAMdisk drive M:
(the internal memory drive) for the following examples.
There are special forms of SAVE and LOAD which can be used to
change the default drive. When SAVE or LOAD is followed by a
filename that contains nothing but a drive letter and a colon, the
drive identified by the letter is then made the new default drive.
So the commands...
SAVE "m:"
SAVE "squares"
...will change the default drive to drive M: then save the program
onto drive M:. (If the above command had been SAVE "m:squares",
the program would still have been saved to drive M: but the
default drive would not have been changed.)
To switch the default back to drive A:, type the following. (If
you have not connected a disk drive to the +2A, then ignore this
command and leave the default as M:)...
SAVE "a:"
Note that SAVE and LOAD followed by just a drive letter (and
colon) will do nothing other than change the default drive. They
will not actually save or load a program. You must use SAVE or
LOAD followed by a real filename for this.
When using disks (including the RAMdisk drive M:), the name field
of a filename is the only field that you have to specify when
using SAVE, LOAD, etc. The name field can be from 1 to 8
characters long and may contain any of the following:
Letters: a b c d e f g h i j k l m n o p q r s t u
v w x y z (upper or lower case)
Digits: 0 1 2 3 4 5 6 7 8 9
Other characters: " # $ ' @ ↑ _ { } ~ `
Upper and lower case letters are the same in filenames, so EXAMPLE
and example would be identical.
SAVE "squares.bas"
z
squares
m:picture.bin
a:fred
13a:hello
0M:CAPITALS
test.bas
philip
glass.mus
a:a.a
If you have not connected a disk drive to the +2A, then some of
these filenames will produce the error Drive not found. Don't
worry about this, because if you do add a disk drive in the
future, then these filenames will become valid.
Disk catalog
You may have spotted the fact that we have now saved the same
program twice with two different names (SQUARES and SQUARES.BAS).
It would be nice to be able to check what has been saved on a
particular disk, and this is where the CAT command comes in. CAT
displays a catalog of what you have stored on a disk.
CAT
The +2A will take a quick look at the disk (if you have connected
a disk drive and are using drive A: for these examples) and
display a list of the disk's contents on the screen. If the
default drive is still set to M: then a list of the files on the
RAMdisk is displayed. The list is sorted into alphanumerical
order, and each file is followed by an indication of its size to
the nearest number of kilobytes (rounded up). At the end of the
list, the amount of free space on the disk is also displayed.
CAT (on its own) is the simplest form of the command. If you
wanted to list all the files on a different drive (eg. drive B:),
you would use...
CAT "b:"
If you have not connected a disk drive (or drives), don't worry
about the report Drive not found that appears.
LOAD "b:"
CAT
...this will also list all the files on drive B:. This isn't quite
the same as CAT "b:", because now the default drive has been left
as B:. Before continuing, make sure the default is M:, using...
LOAD "m:"
SAVE "squares"
SAVE "squares.bas"
SAVE "fred"
SAVE "fat"
SAVE "santa.bin"
SAVE "trepur.bak"
SAVE "cliff.cjl"
SAVE "sausages.bas"
Wildcards
If a disk has a large number of files, it is often desirable to
selectively list only those of interest. The +2A caters for this.
If, for example, you wished to list only those files that ended in
.BAS you would use...
CAT "*.bas"
The asterix character * is what's known as a wildcard. When a
filename containing a wildcard is specified, CAT will list only
the files that match the 'specification' given. When the *
wildcard is used (in either the name field or the type field) it
means 'any character from here to the end of this field'. So, in
the above command, we want CAT to display files that have any
characters in their name field and the letters BAS in their type
field. If there are no files on the disk that match the
specification, the report No files found will be displayed
(followed by the amount of free space). If you give a file
specification of *.* (ie. CAT "*.*") or no specification at all
(ie. CAT) and the report No files found is displayed, then this
means that the disk is empty. An empty disk in drive A: or B: (if
connected) will have a free space value of 173K (this value may be
different for disks from other types of computer). An empty drive
M: will usually have 62K free (this figure drops to 58K when a
disk drive is connected). If you catalog a disk containing a
commercial program (such as a game), it might appear to have no
files on it but very little space free. This is a protection
measure taken by the software writers to prevent illicit copying,
and shouldn't cause any concern.
If we use...
CAT "s*.bas"
...then all files that begin with the letter S and then have any
characters whatsoever between the S and the end of the name field,
followed by a type field of BAS, will be shown. Files with names
such as SQUARES.BAS, SAUSAGES.BAS, SUPER.BAS would all be listed,
but SQUARES.BIN, TOAST.BAS and SQUARES would not.
CAT "squares.*"
CAT "s*.b*"
When we type CAT (on its own), we want to list all files on a
disk. Therefore, CAT is just a shorthand way of saying...
CAT "*.*"
From the above you will notice that the * wildcard can only be
used as the last character in a field, and it is used to mean 'I
don't care what other characters are present between here and the
end of this field'. Sometimes, however, you may want to specify a
group of files but need to be a little more discerning. This is
when you use the ? question mark wildcard (in either the name
field or the type field). The ? wildcard means 'I don't mind what
character happens to be in this specific position'.
CAT "?at"
...then the files listed would be all those that are three
characters long, ending in AT, but we don't mind what character is
in the first position. Thus files such as CAT, SAT, MAT and FAT
would be listed, but CAR, CATTLE and AT would not. Unlike the *
wildcard, ? wildcards can be used in place of any of the 8
characters of the name field and the 3 characters of the type
field. There is no limit to the number of question marks you can
use (other than the 8 and 3 limits of the filename field lengths).
If you have a printer connected to your +2A you may find it useful
to print-out the files listed by CAT. You can do this by directing
the output from CAT to stream 3 (streams are explained in part 22
of this chapter). The command to do this is...
CAT #3
If you only want some of the files printed-out, you can also
include a file specification in exactly the same way as before.
For example...
CAT #3,"m:*.bas"
Any form of the CAT command may also end with the word EXP, for
example, CAT "m:" EXP. The EXP is short for expanded, and as the
name might suggest, gives you a little more information about the
attributes of the files on a disk. Not only will the expanded
catalog display system files but it will also indicate whether
files are set to write protected mode, archive mode or system
status (these terms are explained in the section ahead entitled
'File attributes').
(There is one other specialist use for the CAT command, and this
will be dealt with in the section ahead entitled 'Tape catalog'.)
NEW
...then select the option +3 BASIC from the opening menu, and type
in...
LOAD "m:squares"
Note that the LOAD command reads in a new program (and variables)
and then deletes any program (and variables) previously in the
memory. (If the program that you specified to load cannot be
found, then any program currently in the memory is not deleted.)
Just like SAVE, the LOAD command must be given a filename whose
name field is at least one character long. If you are familiar
with datacorder operations, then you will know that the command
LOAD "" means 'load the next program on the tape'. The concept of
a 'next program' on disk does not exist, so if you don't specify a
filename, the disk system won't know what to load and will report
an error. If you can't remember what name you saved a file under,
use CAT to check what's on the disk (this is why it's a good idea
to save programs on disk using 'mnemonic' names (names that remind
you what they contain) - eg. it is more obvious what sort of
program a file named TENNIS.BAS contains compared to one simply
named T).
If * can't be found, the +2A will then look for a file called DISK
on drive A: (assuming you have connected a disk drive). This can
be a BASIC program that you've previously written and saved, so if
the Loader option finds a program called DISK, it will load it and
wait for the next operation. (The program will automatically run
if it was saved using a LINE parameter - more about this later
on.)
At this point, pressing ENTER will just load the program again.
If you wish to run or edit the program after it has loaded, first
press the cursor down key ↓ once, then press ENTER. This selects
the +3 BASIC option from the opening menu.
LOAD "m:"
SAVE "m:"
SAVE "dice"
Use NEW to clear the program memory again, then enter and run the
following program...
1 PRINT 1
2 PRINT 2
10 PRINT 10
20 LET x=20
MERGE "dice"
When the program is merged, you will receive the message 0 OK. If
you then LIST the program, you will see that lines 1 and 2 have
survived, but lines 10 and 20 have been overwritten by those from
the 'dice' program. Note that the value of the variable x has also
survived (try PRINT x).
You have now seen simple forms of five of the commands that work
in conjunction with disk:
MERGE Similar to LOAD except that it does not clear the old
program lines and variables unless it has to (because
they are the same as those being loaded in from disk).
If you have not connected a disk drive to your +2A, then skip the
following example.
Type NEW to clear the program memory, select +3 BASIC, then enter
the following...
Again, type NEW to clear the program memory, and when the opening
menu appears, press the ENTER key. This will select the Loader
option which searches for a file called DISK. When it finds the
simple example program you just saved, it will load it, and as it
was saved using a LINE parameter, it will automatically start
running (from line 10).
At this point, pressing ENTER will load and run the program again.
If you wish to edit the program after it has run, press the cursor
down key ↓ once, then press ENTER. This selects the +3 BASIC
option from the opening menu.
Note that if you load a program called DISK which doesn't
automatically run (using the Loader option from the opening menu),
then you will have to select the +3 BASIC option (after the
program has loaded) before you can run it or edit it.
You can save arrays using the keyword DATA in a SAVE statement...
The array name specifies the array you want to save, so it is just
a letter (or a letter followed by $). Remember to put the brackets
() after the array name.
...then SAVE takes the array b() from the computer and stores it
under the name BLOGGS. The command...
You can save character (string) arrays in exactly the same way.
However, note that when you load a character array, it will delete
not only any previous character array with the same name, but also
any simple string variable with the same name.
When dealing with a large amount of data you may find it useful to
use the SAVE...DATA option and the LOAD...DATA option to and from
drive M:. Once saved on drive M: the space previously used by an
array can be re-used. Using drive M: will mean that saving and
loading are very fast.
If you leave out the length parameter, the +2A will read in the
bytes however many there are.
The start parameter specifies the address where the first byte is
to be loaded back to - this can be different from the address it
was saved from; though if they are the same, you can leave out the
start parameter in the LOAD statement.
..or...
SAVE "m:squares"
...to save the program using the filename SQUARES yet again.
If, for example, you wanted to remove a file from drive M: called
FRED.BAS, you would use...
ERASE "m:fred.bas"
If drive M: had already been set as the default drive, then you
wouldn't need to include the M: at the start of the filename. It
doesn't hurt to include the drive letter anyway, and with as
powerful a command as ERASE, you might feel safer if you do. To
erase all the files on drive B: (if it were connected), you would
use...
ERASE "b:*.*"
Before doing this, BASIC, will ask for confirmation...
...and assuming that you really did mean to wipe all the files
from the disk in drive B: you would then type Y.
If you ask to delete a single file (or a group of files using the
wildcards * and ?) and there are no files on the disk that match
the specification, then the report File not found will be
displayed.
Note that ERASE followed by just a drive letter (eg. ERASE "m:")
will erase all files on the specified drive without asking for
confirmation. Be careful, therefore, not to enter this form of the
command unless you really mean to delete everything! (The ERASE
process will stop and report an error if a write protected disk or
file is detected.)
The default drive that you have been using up to now has many
copies of the simple SQUARES program (saved under different names)
on it. This is a waste of space so you might as well erase those
that aren't needed. What you want to do in effect is erase
everything except SQUARES (though there is no simple way to do
this). However, some of the different files have the same letters
in common, so you may be able to use various forms of * and ?
specifications to cut down the amount of typing. See if you can
work out the fewest number of ERASE commands to erase all files
other than SQUARES.
Once a file has been saved, it can be given a new name using the
MOVE command. For example, if there is a file on drive M: called
SQUARES that you would like to call BLOCKS, its name could be
changed as follows (first we make sure there is a file called
SQUARES on drive M:). Type...
SAVE "m:squares"
MOVE "m:squares" TO "m:blocks"
CAT "m:"
Imagine we had saved a file called FRED, and then after working on
it and saving a new version with the same name, realised that we
had made a terrible mistake and would like to recover the last
version. This would be possible using the commands...
ERASE "fred"
MOVE "fred.bak" TO "fred"
MOVE will take into account the current default drive so the
filename doesn't necessarily have to contain a drive letter. Note,
however, that it is not possible to use MOVE to rename files
between different drives. The command...
...(for example) will fail with the error No rename between drives
reported. Instead, you can use the COPY command (explained ahead)
followed by ERASE to achieve the desired result.
File attributes
MOVE has another use besides renaming files. It can also be used
to change the attributes of a disk file. Attributes are bits of
information associated with a file that tell you (and the
computer) a little more about it.
There are three attributes that can be changed. The most useful
attribute is write protection. Once a file's write protection
attribute has been set, it will not be possible to erase it (or
save a file with the same name) until you remove the write
protection. It behaves a little like the write protect hole on a
disk, but works just on individual files. Unlike the write protect
hole, however, it offers no protection against FORMAT, which
erases everything on a disk, regardless of attributes. You can set
a file's write protection attribute to on with a command such
as...
ERASE "squares"
When you are using MOVE to change attributes, the filename can
include the wildcards * and ?. So, to make all the files on drive
M: write protected, you would use...
Bear in mind that you cannot have two files on the same disk with
the same filename, even if you want them to have different system
status attributes; so if you try to create or copy a file onto a
disk where a file of that name already exists (but is hidden from
CAT), the previous file will be deleted or made into a .BAK file.
Copying files
Quite often, a situation will arise when you would like to make a
copy of one of your disk files, (perhaps to generate a second copy
that you can experimentally change without damaging the original
copy). If you have connected a disk drive to your +2A, then the
COPY command can be used to copy files from one drive to another,
or even to make complete copies of disks. The very simplest form
of the COPY command will look something like this...
The above command means, put a copy of the contents of the file
called FRED (which is presently on drive A:) onto drive M:. As no
destination name has been specified (after M:), the new file will
also be called FRED.
The name before the word TO is known as the source filename, and
the name after TO is the destination filename.
The command...
You cannot copy one file to another with the same name and on the
same drive. Trying to do so will result in the error report File
already exists (or possibly File already in use).
The source filename for copying from can include the wildcards *
and ?, however, in this case the destination filename has to be
just a drive letter. So, for example...
...will work (assuming that there are some files on drive A: that
match this specification), and transfers all files on A: with a
.OVR type field onto drive M:. However, the command...
COPY will always list the files it is copying in two columns. This
will allow you to check that any wildcard specification you use
encompasses all the files that you were intending to copy.
After copying, a report will appear to let you know how many files
were copied. (If you were copying a group of files, this may be
useful to check that you have copied the number of files you
intended to.)
If you wish to use the COPY command within a program, but do not
want the text generated by the command to spoil the text or
graphics display produced by the program itself, then you may
disable the listing of copied files to the screen by including the
command...
Even if you have connected only one drive (drive A:) to the +2A,
you can take advantage of the fact that the single mechanism can
be used as if it were drive A: or drive B:. For example, suppose
you have a one-drive system and want to copy a couple of files
(that both end in .BAS) from one disk to another. Put the source
disk in the drive and type...
Once the +2A has read part of the first file that ends in .BAS, it
will ask you to...
Simply follow this instruction. After the +2A has written the
information onto the 'drive B:' disk, it will ask you to...
The command...
...is similar to the above, but this time the contents of the file
will be sent to the printer. In this case, however, control codes
will be sent to the printer. If you have set the print output to
be via the RS232 with tokens unexpanded (using FORMAT LPRINT
"R";"U"), then this command can be used to 'export' files to other
computers. Once again, this command cannot be used for BASIC
programs - it is intended for sending ASCII text files only.
This will produce a new file on the same drive, having the same
name field but with a type field of .HED (short for headed). In
the above example, a new file called GAME.HED will be created, and
it will be written to the default drive (as no drive letter was
specified).
Obviously this command will only be of use for machine code files.
Headed files produced in this way will have the length part in
their header set to the correct value and the type part set to be
a CODE file. However, BASIC cannot know what address the file
should be loaded to, so the load address should be specified when
the LOAD...CODE command is used. For example, if the above program
had been assembled to execute at 7000h (the h denotes a
hexadecimal number) or 28672 decimal, then the headed file could
be loaded with the command...
The RAMdisk
You may have been wondering what point there is in storing
information in the RAMdisk (drive M:) as it will be lost once the
+2A is switched off. Well, perhaps the most obvious use of drive
M: is to store chunks of BASIC program (or routines) which can be
merged (using MERGE M:filename) into a smaller program, in
sequence. This makes it possible to write about 90K of BASIC
program, and hold it in the +2A (though to do this, the program
structure has to be well defined).
If you have connected a disk drive to the +2A, you can keep the
various routines on a floppy disk and use COPY to put them into
drive M: before you run the program. The benefit of doing this is
that drive M: is much quicker to access than the mechanical drives
(A: and B:). The mechanical drives, however, can hold much more
data, so you might like to evolve a system using both disk and
RAMdisk. Careful design and planning will repay itself many times
over in terms of speed and performance.
Tape operations
Much of what has been said in this section about the use of LOAD,
SAVE and MERGE on disk will apply equally on tape. However, the
commands FORMAT, COPY, MOVE, CAT and ERASE do not apply on tape
(although there is a special form of CAT that can be used -
described in the section ahead entitled 'Tape catalog').
If you have connected a disk drive, then you may remember that
when you first switch on the system or reset the +2A the default
drive for all file operations is set to drive A:. This means that
if you use CAT, ERASE, LOAD, SAVE, etc. without specifying a drive
letter, then +3 BASIC will perform the operation on drive A:. You
will also know that the default drive can be changed using
either...
...or...
LOAD "t:"
After LOAD "t:", all subsequent LOAD and MERGE operations are
performed from tape (until changed back to disk by, for example,
LOAD "a:"). Similarly, if you use...
SAVE "t:"
If you have not connected a disk drive to the +2A, then the
default 'drive' for LOAD, SAVE, VERIFY and MERGE is already set to
"t:" (ie, the datacorder) when you first switch on, and so you
needn't ever issue the above commands.
Unlike A:, B: or M:, when you use T: as the drive letter, it will
change only future LOAD and MERGE commands (in the case of LOAD
"t:") or future SAVE commands (in the case of SAVE "t:"). The
default drive used for MOVE, COPY, CAT and ERASE will stay the
same as it was before (as these commands have no relevance to
tape).
SAVE "m:"
... then the default drive for all subsequent operations (except
LOAD and MERGE on a +2A without disk drives) will be set to drive
M:.
LOAD "b:"
...will then set the default drive for all operations to drive B:
(if connected).
SAVE "t:"
...this will perform all future SAVE operations to tape, but all
other commands will still default to drive B:. Using the
command...
LOAD "t:"
...will also perform all future LOAD and MERGE operations to tape;
however, the default drive for all disk-only commands will still
be drive B:.
SAVE "a:"
...will perform all future SAVE operations and all disk operations
(except LOAD and MERGE) to drive A:. LOAD and MERGE will still be
from tape, however.
Let's try to save our simple 'squares' program onto tape. Reset
the +2A, select +3 BASIC, then type in the following program...
If you have a disk drive connected to your +2A, then you can load
the version of this program that you saved earlier, using...
LOAD "squares"
This is the program that you are now going to save onto tape. Any
standard tape should work, although low noise tapes are
preferable.
SAVE "t:"
SAVE "squares"
The above commands will save the program onto tape using the
filename SQUARES. When saving files on tape, you are allowed up to
ten characters in the name. Unlike disk, you can use any
characters you like and the name can include spaces.
You may remember (from chapter 6) that the border changes colour
to indicate that tape saving is taking place.
When the +2A has finished (with the report 0 OK), stop the tape.
Now let us suppose that you have saved the program and
successfully verified it. Loading it back into the memory is just
a matter of typing...
LOAD "squares"
(Since the program verified properly, you should have no problem
loading it.)
If you have connected a disk drive to the +2A, then you may have
some BASIC programs saved on tape that you wish to transfer to
disk. To do this, first
type in...
LOAD "t:"
SAVE "a:"
LOAD ""
...which will load the next file from the tape into the +2A's
program memory. Once loaded, the file can be saved to disk
using...
SAVE filename
MERGE ""
The only file types that may cause difficulty when you want to
transfer them from tape to disk are CODE (and SCREEN$) files. To
be able to transfer a file of this type you need to know at least
two things about it:
CAT "t:"
...the +2A will wait for you to play a tape (the CAT "t:"
operation can be abandoned by pressing BREAK). When the +2A finds
a header on tape it will display the information (in the same form
it was saved). This means that there will be a ten character
filename in quotes. What follows the filename will depend upon the
type of file - if it is a BASIC program, the word (BASIC) will be
displayed. If a LINE parameter was specified when the file was
saved, then this will also be shown. If the file holds data, then
the word DATA followed by the array name will be displayed, and
finally, if the file was saved using CODE (or SCREEN$, which is
really just CODE 16384,6912), the word CODE will be printed
followed by the start address and length that were specified when
the file was saved.
Just like the other forms of CAT, its output can be directed to a
printer using stream 3, ie...
CAT #3,"t:"
From the above it can be seen that if you have loaded (using MERGE
"") a program containing an execution LINE parameter, the CAT "t:"
display will identify that line number for you. You may then wish
to save that program to disk using...
It is the values for the CODE files that you will probably find
most useful from the CAT "t:" display. Either note them down or
print them out, then rewind the tape so it is just before the
header that has been read, and type...
CLEAR start-1
...where start is the value printed for the start address. Now
type...
When the file has loaded into memory and the 0 OK report appears,
the file can be saved to disk using...
There are several reasons why this simple scheme may not work:
Exercise...
Parallel printers
Serial printers
LPRINT, LLIST
FORMAT
COPY
The +2A comes with an 8 bit Centronics parallel port and an RS232
serial port. Both are supported by built-in software enabling you
to use virtually any printer. These features are usable only in +3
BASIC mode.
Make sure you have the correct lead to connect the printer to the
+2A - if in doubt, consult your Sinclair dealer.
Parallel printers
When the +2A is first switched on it will assume that, if a
printer is present, it will be connected to the (parallel) PRINTER
socket. The hardware connection between computer and printer is
relatively straightforward - though you must make sure that you
don't connect the cable the wrong way up at the computer end (if
the cable doesn't have a locating 'key').
LPRINT "hello"
Once you have got your printer to print, you may skip to the
section ahead entitled 'General printing'.
Serial printers
Unlike parallel printers, the connections between the +2A and a
serial (or RS232) printer will vary for different manufacturers'
printers. Make sure that your dealer has provided a lead suitable
for connecting your particular printer to the +2A. A serial
printer must be connected to the +2A's RS232 socket, and details
of connections can be found in chapter 10 (Peripherals for your
+2A).
To get the +2A and the printer communicating with each other, they
must both use the same baud rate. The baud rate is the speed at
which data is transferred between computer and printer. Although
it is possible that your printer can be set to different baud
rates, it'll probably be easier to change the rate at the computer
end. Somewhere in the printer's operating manual, the baud rate
will be specified - find this out and then set the +2A to this
rate, using the command...
For example...
It's important to note that LPRINT and LLIST normally take care to
screen out any embedded colour codes (and their parameters) before
printing or listing anything. Embedded colour codes are a bit of a
hangover from the old 48K Spectrum - when included in a string
they set INK, PAPER and so on. Printers on the whole tend to use
these codes for completely different things like setting italics,
underline, etc., so it would be quite dangerous to send colour
codes to the printer and hope that nothing untoward happens. A
side effect of this is that the +2A will normally not be able to
send escape control sequences to the printer. For example, suppose
your printer expects an escape character (character 27) followed
by "x";CHR$(1) to switch to its NLQ mode; you would normally use
the command...
So, to summarise:
...before printing.
The third BASIC statement used with a printer - COPY, prints out a
copy of the TV screen. To demonstrate, go into the small screen
(by selecting the Screen option from the edit menu) and type in
the following command.
COPY
You can always stop printing at any time by pressing the BREAK
key. Many printers have what's known as a buffer, which stores
text before printing. If your printer has a buffer, then pressing
BREAK will not stop the printer immediately (although the +2A will
register the break at once).
10 FOR b=0 TO 1
20 BRIGHT b
30 FOR i=0 TO 6
40 FOR c=0 TO 31
50 PRINT INK i; i;
60 NEXT c
70 NEXT i
80 NEXT b
...then switch to the bottom part of the screen (using the edit
menu's Screen option). Run the program (which displays twelve
lines of coloured numbers on the screen), then type in...
COPY EXP
The printed output (or dump) from this command is slightly larger
than that from the standard COPY command - (EXP is short for
expanded). The command reproduces the coloured areas of the screen
as different densities of black dots on the printer. (All 24 lines
of the screen are reproduced.) Areas that have been printed with
BRIGHT 1 will appear lighter than areas printed normally (just as
happens on the screen).
Note that INVERSE cannot be used after the simple COPY command -
it only works with COPY EXP.
The dump produced by COPY EXP and COPY EXP INVERSE is designed to
fit a sheet of A4 paper; however, some printers will not print
within about an inch at either end of a sheet. If this problem
occurs, then it is possible to reduce the size of the dump
slightly by using the command...
POKE 23419,8
This sets the number of 216ths inch used as a line feed at the end
of each pass of the print head. It is set to 9 when the +2A is
first switched on. Once set, it will not be changed even if the
NEW command is used. By reducing this value, each pass of the
print head will fractionally overlay the previous pass. As a
consequence, the quality of the dump reproduced will be degraded
slightly.
If you try to use any of the printer commands when there isn't a
printer attached (or if the printer is off line), then the +2A
will stop dead while it patiently waits for the (non-existent)
printer to respond. In such a case, pressing BREAK twice will
bring the +2A back to life.
Try this...
Now change TAB n back to AT 31-n,n still using LPRINT. This time
you will get just a single line of symbols. The reason for the
difference is that the output from LPRINT is not printed straight
away, but is stored in the buffer until either one line's worth of
printer output has accumulated, or something else 'flushes' the
buffer. Hence, printing only takes place:
Number 3 above explains why our program with TAB works the way it
does. As for AT, the line number is ignored, and the LPRINT
position (like the PRINT position) is moved to the column number.
An AT item can never cause a line to be sent to the printer.
Exercises...
Streams
Channels
FORMAT, OPEN, CLOSE
The +2A can 'read' data from the keyboard by using INPUT and
INKEY$, and it can 'write' data onto the TV screen or a printer by
using PRINT and LPRINT. However, these commands are really a form
of shorthand designed to protect the user from some of the
computer's more complex features.
To the BASIC PRINT command, for example, the screen and the
printer are no different. PRINT "Rosanne" really means 'take the
characters which make up the word 'Rosanne' and send them
somewhere else'. It's just convenient to use the screen most of
the time. Likewise, LPRINT usually sends data to the printer. In
fact, what these commands really do is to send data to one of a
number of channels.
OPEN #4,"p"
This might seem over-complicated, and you may well wish to stick
to the standard PRINT and LPRINT commands - that's why they're
there, after all.
PRINT and LPRINT are really the same command. When BASIC is
running, it has three streams normally open. Stream #1 is
connected to the keyboard device (K), and is used by INPUT and
INKEY$. Stream #2 is connected to the screen (S), and is used by
PRINT and LIST. Stream #3 is connected to the printer (P), and is
used by LPRINT and LLIST. All of these commands can be redirected
to use another device by including a # followed by a current
stream number, so...
The +2A can cope with 16 streams. As 3 are used by BASIC, and 1 is
used internally, this leaves you with 12. You can use these by...
If you want to read in data from the RS232 into memory directly,
you can replace line 50 with...
10 CLOSE #2
20 OPEN #2,"p"
IN
OUT
The processor can read from (ROM and RAM) and write to (RAM)
memory by using PEEK and POKE. The processor itself does not
really care whether memory is ROM or RAM - it just thinks that
there are 65536 memory addresses, and it can read a byte from each
one (even if it's nonsense), and write a byte to each one (even if
it gets lost). In a completely analogous way, there are 65536 of
what are called I/O ports (standing for input/output ports). These
are used by the processor for communicating with things like the
keyboard or the printer, and also for controlling the extra memory
and the sound chip. Some of them can be safely controlled from
BASIC by using the IN function and the OUT command, but there are
locations to which you must not write from BASIC as you will
probably cause the system to crash, losing any program and data.
IN address
It has one argument - the port address, and its result is a byte
read from that port.
OUT address,value
...which writes the given value to the port with the given
address. How the address is interpreted depends very much upon the
rest of the computer. Quite often, many different addresses will
mean the same. On the +2A it is most sensible to imagine the
address being written in binary, because the individual bits (each
of which can have the value either 0 or 1) tend to work
independently. There are 16 bits, which we shall refer to (using A
for address) as...
A15, A14, A13, A12, A11, A10, A9, A8, A7, A6, A5, A4, A3,
A2, A1, A0
The byte being written or read has 8 bits, and these are often
referred to (using D for data) as...
There is a set of input addresses that read the keyboard and the
datacorder.
In the byte read in, bits D0 to D4 stand for the five keys in the
given half-row. D0 is for the outside key and D4 is for the one
nearest the middle. The bit is 0 if the key is pressed, 1 if it is
not. D6 is set by the datacorder read data, and is effectively
random if no tape data is present.
Port address 00FEh (254 decimal) in output drives the sound (D4)
and the save signal to the datacorder (D3), and also sets the
border colour (D2, D1 and D0).
Port addresses 00FEh (254), 00F7h (247) and 00EFh (239) are
reserved.
Port address 7FFDh (32765) drives the extra memory. Executing an
OUT to this port from BASIC will nearly always cause the computer
to crash, losing any program and data. There is a fuller
description of this port in part 24 of this chapter (under the
heading 'Memory management'). This port is write only ie. you
cannot determine the current state of the paging by an IN
instruction. This is why the BANKM system variable is always kept
up to date with the last value output to this port.
Port address BFFDh (49149) drives the sound chip's data registers.
Port address FFFDh (65533) in output writes a register address,
and in input reads a register. Judicious use of these two
registers can allow sounds to be generated whilst BASIC gets on
with something else, but you should be aware that they also
control RS232/MIDI and AUX interfaces.
Port address 2FFDh (12285) can be used to read the disk controller
(μPD765A) chip's main status register. This is unlikely to be very
useful without an in-depth knowledge of how the chip operates.
Port address 3FFDh (16381) is the disk controller's data register.
This can be both read from and written to, but once again it is
unlikely to be useful to the BASIC programmer. Random OUTputting
to this port will probably confuse the poor disk controller chip
to such an extent that subsequent disk operations (like LOAD and
SAVE) will be unreliable. It is entirely possible that ill-
informed experiments will corrupt your disks and lose your data -
you have been warned!
...and play around by pressing keys (start with the half-row from
CAPS SHIFT to V). When you have finished with each half-row, press
BREAK and then type...
NEXT n
The control, data and address busses are all exposed at the back
of the +2A on the EXPANSION I/O socket. This means that you can do
almost anything with a +2A that you could with a raw Z80 chip
(although sometimes, the computer's internal workings may get in
the way).
PEEK
POKE
CLEAR
Memory management
All these bytes will probably be quite meaningless to you, but the
processor chip understands them to be instructions telling it what
to do.
POKE address,contents
POKE 31000,57
...then the byte at address 31000 is given the new value 57. Now
type...
...to prove this. (Try poking in other values, to show that there
is no cheating.) The new value must be between -255 and +255; if
it is negative, then 256 is added to it.
The ability to poke gives you immense power over the computer if
you know how to wield it, and immense destructive possibilities if
you don't. It is very easy (by poking the wrong value into the
wrong address) to lose vast programs that took you hours to type
in. Fortunately though, you won't do the computer any permanent
damage.
We shall now take a more detailed look at how the RAM is used.
Don't bother to read this unless you're really interested.
The way that the computer organises its affairs changes slightly
between 48 BASIC and +3 BASIC mode. The area that was the printer
buffer in 48 BASIC mode, is used for extra system variables in +3
BASIC mode in much the same way as it was on the original Spectrum
+2. The variables have changed, though.
The elements with a given first subscript are ordered in the same
way using the second subscript, and so on down to the last.
String:
Array of characters:
The calculator is the part of the BASIC system that deals with
arithmetic, and the numbers on which it is operating are held
mostly in the calculator stack.
The machine stack is the stack used by the Z80 processor to hold
return addresses and so on.
The byte 'pointed to' by RAMTOP has the highest address used by
the BASIC system. Even NEW, which clears the RAM, only does so as
far as this - so it doesn't change the user-defined graphics. You
can change the address RAMTOP by putting a number in a CLEAR
statement, ie...
Using CLEAR in this way, you can either move RAMTOP up to make
more room for the BASIC by overwriting the user-defined graphics,
or you can move it down to make more RAM that is preserved from
NEW. It can also be used to ensure that the machine stack is below
BFE0h (49120) when intending to call +3DOS - this means that the
stack will not have to be subsequently moved within your own
machine code.
If you are in an experimental frame of mind you can also use CLEAR
to explore the extra memory. CLEAR 49151 moves all of BASIC below
the addresses that hold the switchable RAM paging. By using POKE
23388,16+n where n is a number between 0 and 7, you can make the
computer switch in page n of the RAM. You will then be able to use
PEEK and POKE in the normal way to examine and change the page.
Beware - the extra pages are normally used by the system for disk
(if connected) and editor operations, so always reset the +2A
after exploring in this way, before doing anything else.
Type NEW, select +3 BASIC, then enter the command CLEAR 23825 to
get some idea of what happens to the machine when it fills up.
If you then try to make the +2A compute, (for example, type in
PRINT 1+1) you will see the report 4 Out of memory displayed. This
means the computer has no more room for information. If you come
up against this message while entering a large program, you will
have to empty the memory slightly (delete a line or so) in order
to control the computer.
Memory management
We mentioned earlier that there is rather more memory in the
computer than the processor can deal with. While the processor can
indeed address only 64K of memory at once, the extra memory can be
slotted in and out of that 64K at will. Consider a TV set.
Although it (and you) can only deal with one channel at a time,
there are another three channels always there which can be
selected with the right buttons. So, even though there's four
times as much information as you can use at any one time, you can
pick and choose which part is relevant.
It is much the same for the processor. By setting the right bits
in an I/O port it can pick and choose which chunks of the 192K of
memory it wants to use. For the majority of the time in BASIC it
ignores most of the memory, but for games playing, having three
times as much RAM is really rather useful. Look again at the +2A's
memory map (shown at the beginning of this section). RAM pages 2
and 5 are always in the positions shown when BASIC is used, though
there's no reason why they shouldn't be in the banked section
(C000h to FFFFh) - however, it would be difficult to see any use
for this.
The RAM banks are of two types: RAM pages 4 to 7 which are
contended (meaning that they share time with the video circuitry),
and RAM pages 0 to 3 which are uncontended (where the processor
has exclusive use). Any machine code which has critical timing
loops (such as music or communications programs) should keep all
such routines in the uncontended banks. For example, executing
NOPs in contended RAM will give an effective clock frequency of
2.66MHz as opposed to the normal 3.55MHz in uncontended RAM. This
is a reduction in speed of about 25%.
The +2A also uses I/O port 1FFDh for some ROM and RAM switching.
The bit field for this address is is follows:
↑ ↑
Bit 2 1FFDh
(23399)
(system variable:
BANK678) vertical
vertical
↓ ↓
ROM 2 ROM 3
POKE, PEEK
The bytes in memory from 5B00h (23296) to 5CB6h (23734) are set
aside for specific uses by the system. There are a few routines
(used to keep the paging in order), and some locations called
system variables. You can peek these to find out various things
about the system, and some of them can be usefully poked. They are
listed here with their uses.
Although system variables have names, you should not confuse them
with the words and names used in BASIC. The computer will not
recognise the names as referring to system variables; they are
given solely as mnemonics for we humans.
This section is written for those who understand Z80 machine code,
ie. the set of instructions that the Z80 processor chip uses. If
you do not, but would like to, there are plenty of books about it.
You should get one called something along the lines of...'Z80
machine code (or assembly language) for the absolute beginner',
and if it mentions the '+2(+2A)' or other computers in the ZX
Spectrum range, so much the better.
ld bc, 99
ret
...which loads the BC register pair with 99. This translates into
the four machine code bytes 1, 99, 0 (for ld bc, 99) and 201 (for
r e t). (If you look up codes 1 and 201 in part 28 of this
chapter, you will find that 1 corresponds to ld bc, NN - where NN
stands for any two-byte number; and 201 corresponds to ret.)
When you have got your machine code program, the next step is to
get it into the computer - (an assembler would probably do this
automatically). You need to decide whereabouts in memory to locate
it - the best thing is to make extra space for it between the
BASIC area and the user-defined graphics.
If you type...
CLEAR 65267
...this will give you a space of 100 (for good measure) bytes
starting at address 65268.
To put in the machine code program, you would run a BASIC program
something like...
10 LET a=65268
20 READ n: POKE a,n
30 LET a=a+1: GO TO 20
40 DATA 1,99,0,201
(This will stop with the report E Out of DATA when it has filled
in the four bytes you specified.)
To run the machine code, you use the function USR - but this time
with a numeric argument, ie. the starting address. Its result is
the value of the BC register on return from the machine code
program, so if you type...
You can save your machine code program easily enough with (for
example)...
Then you may run the machine code from BASIC using the single
command...
LOAD "Loader"
If BASIC'S CLEAR command has been used so that the BASIC stack is
below BFE0h (49120), then it is not necessary to move the stack.
However, we have done so in the following example to demonstrate
the technique when this is not the case.
org 7000h
demo:
;
; The above will have switched in the DOS ROM and RAM page 7. The
; stack has also been located in a "safe" position for calling DOS
;
; The following is the code to set up and call DOS CATALOG. This
; is where your own code would be placed.
;
;
; If the above worked, then BC holds number of files in catalog,
; the "catbuff" will be filled with the alphanumerically sorted
; catalog and the carry flag bit in "dosret" will be set. This
; will be peeked from BASIC to check if all went well.
;
; Having made the call to DOS, it is now necessary to undo the ROM
; and RAM switch and put BASIC's stack back to where it was on
; entry. The following will achieve this.
;
stardstar:
dosret:
10 LET sum=0
20 FOR i=28672 TO 28758
30 READ n
40 POKE i,n : LET sum=sum+n
50 NEXT i
60 IF sum <> 9387 THEN PRINT "
Error in DATA" : STOP
70 LET x=USR 28672
80 IF INT ( PEEK (28757)/2)= P
EEK (28757)/2 THEN PRINT "D
isk error "; PEEK (28758):
STOP
90 IF x=1 THEN PRINT "No files
found": STOP
100 FOR i=0 TO x-2
110 FOR j=0 TO 10
120 PRINT CHR$ ( PEEK (32781+i*
13+j));
130 NEXT j
140 PRINT
150 NEXT i
160 DATA 243,237,115,0,144,1,25
3,127,58,92,91,203,167,246,
7,50,92,91,237,121,49,255,1
59,251
170 DATA 33,0,128,17,1,128,1,0,
4,54,0,237,176,6,64,14,1,17
,0,128,33,81,112,205,30,1,2
45,225,34,85,112,72,6,0
180 DATA 243,197,1,253,127,58,9
2,91,203,231,230,248,50,92,
91,237,121,193,237,123,0,14
4,201
190 DATA 42,46,42,255,0,0
The addresses picked for the above code and its data areas are
completely arbitrary. However, it is a good idea to keep things in
the central 32K wherever possible so as not to run into the
pitfall of accidentally switching out a vital variable or piece of
code.
The above was a very simple example of calling DOS routines. The
following shows one or two extra techniques that you may find
useful. However, if you are not already familiar with assembler
programming, it might be better to skip this example.
If you have not connected an external disk drive to the +2A, the
above example will still work, producing a catalog of drive M:
(which will be the default drive). The following example, however,
will work only if you have connected a disk drive (and interface)
to the +2A.
;
; Simple example program to write a boot sector to the disk in
; drive A:.
;
; by Cliff Lawson
; copyright (c) AMSTRAD Plc. 1987
;
Erg 0
.phase 07000h
;
; (This allows M80 to generate a .COM file that has addresses
; relative to 7000h. Assemble with "M80 = prog" and link with
; "L80 /p:0,/d:0,prog,prog/n:p/y/e"
; This can be headed with COPY...TO SPECTRUM FORMAT and loaded
; with LOAD...CODE 28672.
;
; A different technique will probably be required for other
; assemblers.)
;
start:
ld a,"A" ;drive A:
ld y,dos_ref_xdpb ;make IX point to XDPB A:
call dodos (necessary for calling DD
;routines)
ld hl,bootsector
ld bc,512 ;going to checksum 512 bytes of
sector
xor a
ld (bootsector+15),a ;reset checksum for starters
ld e,a ;E will hold 8 bit sum
ckloop:
ld a,e
add a,(hl) ;this loop makes 8 bit checksum of
512 byte
ld e,a ;sector in E
inc hl
dec bc
ld a,b
or c
jr nz,ckloop
olstak:
bootsector:
;
; Bootstrap will load into page 3 at address FE00h. The code will
; be entered at FE10h.
;
; Before it is written to track 0, sector 1, the bootstrap has
; byte 15 changed so that it will checksum to 3 mod 256.
;
; Boot will switch the memory so that the 48 BASIC ROM is at the
; bottom. Next up is page 5 - the screen, then page 2, and the top
; will keep page 3, as it would be unwise to switch out the
; bootstrap. BASIC routines can be called with any RAM page
; switched in at the top, but the stack shouldn't be in the
; TSTACK area.
;
bootstart:
;
; The bootstrap sector contains the 16 byte disk specification at
; the start. The following values are for a AMSTRAD PCW range CF-2
; (Spectrum +3) format disk.
;
db 0 ;+3 format
db 0 ;single sided
db 40 ;40 tracks per side
db 9 ;9 sectors per track
ld a,2
call select ;BASIC ROM routine to open stream (A)
ld hl,message
call print ;print a message
eloop:
;
; end with an endless loop changing the border. This is where your
; own code for a game or operating system would go.
;
ld a,r ;a not-very-random random number
out (0feh),a ;switch the border
jr eloop ;and loop
print:
ld a,(hl) ;this just loops printing characters
cp 0FFh ;until it finds FFh
ret z
rst 10h ;with 48K ROM in, this will print char
in A
inc hl
jr print
message:
defb 16,2,17,7,19,0,22,10,1,"Hello, good evening and
welcome", 0ffh
cliff:
ds 512-(cliff-bootstart),0 ;fill to end of sector
with 0s
end
There are one or two things that may be worth noting about this
example. The first is that because BASIC normally has the address
of the ERR NR system variable held in IY (so it can easily
reference its system variables). It is important to store IY and
replace it before returning to the original USR call.
The program uses DOS REF XDPB to make IX point at the relevant
XDPB for drive A:. It then logs in the disk in A: so that it can
be written to. After calculating and modifying the checksum byte
for the information to be written to the boot sector of the disk,
it writes the boot sector using DD WRITE SECTOR.
No checks are made to see that there is even a disk interface, and
possible errors are ignored - the routine isn't designed to be
used by those unfamiliar with possible pitfalls. The routine can
be called with the BASIC command...
USR 28672
The boot sector that is written to the disk has a standard disk
specification in the first 16 bytes. This is followed by the
bootstrap code that will be entered at address FE10h. As will be
described in the interface for DOS BOOT (see part 27 of this
chapter), the memory will initially be set up as 4, 7, 6, 3;
however, the BASIC system variables are still intact and BASIC can
be operated by switching in the correct ROM (3) to the bottom of
memory and making sure that page 5 is in the 4000h...7FFFh area of
memory.
This very simple boot program just uses the BASIC ROM to print a
greeting then enters a tight loop changing the border colour. It
could be modified to load a large binary file and enter it or
perform any other action you desired.
Part 27
Guide to +3DOS
Subjects covered...
ROMs
+3DOS interface
File attributes and headers
Disk format and specification
Tracks and sectors
Disk parameter blocks
CP/M file compatibility
Changing disks
Logical to physical drive mapping
+3DOS messages and requirements
+3DOS routines
Even though the +2A does not incorporate a built-in floppy disk
interface, many of the +3DOS routines may still be called and will
operate on drive M: (the RAMdisk). If you have connected a disk
drive (and interface) to the +2A, then all +3DOS routines are
available for use. See the section ahead entitled 'Using +3DOS
without a floppy disk interface' for further details.
The operating software of the +2A is, in effect, held in four ROMs
(though the information is actually contained in just two ICs).
All four ROMs are addressed between 0000h and 3FFFh, although only
one is switched in at a time.
ROM 0 is the 'editor' ROM and is the one entered when the +2A is
first switched on. This controls the high level 'menuing' and
editing functions.
ROM 1 is the 'syntax' ROM and handles the high level control of +3
BASIC. It contains the code for the BASIC parts of most of the
disk based commands.
ROM 3 is the '48 BASIC' ROM and is virtually identical to the ROM
used in the very first Spectrum. The only real area where it is
different is in the code executed when an interrupt occurs. If non
zero, a 'ticker' variable is decremented every second interrupt,
and when it reaches zero, the disk motor is switched off. This
variable is held in page 7 along with some of the editor and DOS
variables. Page 7 will only be switched in (and this variable
decremented) if bit 4 in the FLAGS system variable is set - this
is used by the software to identify whether it is running 48 BASIC
or +3 BASIC. When 48 BASIC is selected (from the main menu or by
the SPECTRUM command), this bit is reset so that this page-
switching and ticker-decrementing won't happen. However, if bit 4
in the FLAGS system variable is subsequently set by your own
program, this process will start again while interrupt mode 1 is
still selected.
The keypad scanning routines of the Spectrum 128 and the original
+2 have been removed from ROM 3 in the +2A.
A 'bug' in the original 48 BASIC ROM has been fixed in the +2A.
When a non maskable interrupt (NMI) occurs, a jump is made to
location 66h. This now checks the contents of the NMIADD system
variable. If it is zero, a RETN is executed, otherwise a jump is
made to the routine address held in NMIADD. The NMI code in ROM 2
consists of just a RETN.
The fourth ROM (ROM 2) holds +3DOS - the disk operating system.
This is the subject of this section. Unlike the other ROMs, which
are unlikely to be of much use for assembler programmers (except
the 48 BASIC ROM perhaps), the +3DOS ROM has a wealth of routines
that may well be of use in your own programs. We strongly
recommend that any software that uses the disk drives makes use of
these routines as they provide most of the facilities that one
could wish for (more than are currently used by BASIC, in fact).
Furthermore, the routines should only be accessed via the jump
block. This not only makes it easier to write software that can be
adapted to and from the AMSTRAD CPC range of computers, but also
affords upwards compatibility for the future. The entry points for
each routine are held in a jump table at address 0100h (256) in
the ROM. Part 26 of this chapter gave a couple of examples of the
way in which these routines can be called.
+3DOS provides the following facilities:
+3DOS interface
+3DOS's interface is a set of routines accessed via a jump block.
The routines provided fall into three categories:
File attributes
Bit 7 of the name and type field characters are the file
attributes. The top bits of the name field characters are denoted
f1...f8. The top bits of the type field characters are denoted
t1...t3. They have the following meanings:
File headers
Tape files have headers which contain some system information.
+3DOS files may, or may not, have headers. All files created by
BASIC's SAVE command will have headers.
+3DOS files may have a single header in the first 128 bytes of the
file - the header record. These headers are detected by a
'signature' and checksum. If the signature and checksum are as
expected then a header is present; if not, then there is no
header. Thus, it is possible, but unlikely, that a file without a
header could be mistaken for one with a header.
The issue and version numbers are provided for any future
expansion. The issue number must equal the software's issue
number; the version number must be less than or equal to the
software's version number.
Disk formats
+3DOS supports exactly the same disk format as CP/M Plus and
LocoScript on the AMSTRAD PCW range of computer/word processors
(ie. the first format listed below).
Note that the AMSTRAD CPC range's IBM format is not supported.
Other disk formats can be used by patching the XDPB for a drive.
The XDPB is the same as for the first format listed above; it is
not the same as on the CPC range.
1. Alternating sides...
2. Successive sides...
Disk specification
The PCW range disk format (also used by the +3) is, in fact, a
family of formats the precise member of which is defined in the
'disk specification' which is recorded on bytes 0...15 of sector
1, track 0 side 0. The format used on the +3 (and supported by the
+2A) is the same as disk type 0 below. The sector holding this
specification is also that used for a bootstrap program. An
example of how it may be set up is shown in the second example in
part 26 of this chapter.
XDPB structure:
Setting the freeze flag (byte 26) prevents +3DOS from trying to
determine the format of a disk. This should be used when patching
an XDPB for a non-standard format.
0 Single sided
40 Tracks per side
9 Sectors per track
1 First sector number
512 Sector size
42 Gap length (read/write)
82 Gap length (format)
60h(96) MFM mode, skip deleted data address mark
0 Do auto select format
0 Single sided
40 Tracks per side
9 Sectors per track
41h(65) First sector number
512 Sector size
42 Gap length (read/write)
82 Gap length (format)
60h(96) MFM mode, skip deleted data address mark
0 Do auto select format
AMSTRAD CPC range DATA ONLY format (type 2)
36 SPT, records per track
3 BSH, block shift
7 BLM, block mask
0 EXM, extent mask
179 DSM, number of blocks -1
63 DRM, number of directory entries -1
C0h(192) AL0, 2 directory blocks
00h(0) AL1
16 CKS, size of checksum vector
0 OFF, reserved tracks
2 PSH, physical sector shift
3 PHM, physical sector mask
0 Single sided
40 Tracks per side
9 Sectors per track
C1h(193) First sector number
512 Sector size
42 Gap length (read/write)
82 Gap length (format)
60h(96) MFM mode, skip deleted data address mark
0 Do auto select format
The end of file position (EOF) is the lowest byte position that is
greater than all written byte positions. Files without headers can
only record their EOF position to the start of the next 128 byte
record, ie. ceiling (EOF/128). Files with headers have their EOF
position recorded exactly.
Writing a byte after the EOF position will extend the file and
advance the EOF position.
Reading a byte at (or beyond) the EOF position will return an EOF
error.
Changing disks
Under +3DOS, a disk may be changed or removed whenever the drive
is not being accessed (and there are no files open on that drive).
There is no need to log in a disk.
A disk should not be changed while there are files open on it. If,
however, a disk is changed while there are still files open on it,
then as soon as +3DOS detects this, the user will be prompted to
insert the correct disk. +3DOS can only detect this change when it
reads the directory from the disk.
DOS MAP B can also be used to re-map B onto unit 1. If unit 1 does
not exist, then drive B: is disabled.
Non-recoverable errors:
20 Bad filename
21 Bad parameter
22 Drive not found
23 File not found
24 File already exists
25 End of file
26 Disk full
27 Directory full
28 Read-only file
29 File number not open (or open with wrong access)
30 Access denied (file in use already)
31 Cannot rename between drives
32 Extent missing (which should be there)
33 Uncached (software error)
34 File too big (trying to read or write past 8 megabytes)
35 Disk not bootable (boot sector is not acceptable to DOS
BOOT)
36 Drive in use (trying to re-map or remove a drive with
files open)
The report Missing address mark is the error returned when trying
to access a disk that is not formatted (although this is not the
sole reason for the error).
+3DOS Messages
If error messages are enabled (DOS SET MESSAGE) then, in the event
of a recoverable disk error, +3DOS will pass the ALERT routine a
message and the user should be prompted to - Retry, Ignore or
Cancel? If the user replies R, then the disk operation is retried.
If the reply is I, then the error is ignored, and if the reply is
C, then the operation is cancelled and an error condition is
returned to the caller. If error messages are disabled or if the
error is not recoverable, then no message is displayed and an
error condition is returned to the caller.
If, however, while reading data during DOS READ, a bad sector is
found, ALERT will be called to warn the user. This will then offer
the opportunity of retrying (if, for example, the disk was not
properly seated in the drive), ignoring (so that the bad sector
will be ignored allowing as much of the file as possible to be
recovered), or cancelling (perhaps because the problem is
obviously irrecoverable).
(Note that the routine interface for DOS SET MESSAGE has changed
between versions V1.0 and V1.1 of +3DOS. It is important,
therefore, that DOS VERSION is called, and that if the version and
mark are greater than V1.0, the new routine interface is used.
This is the only change between V1.0 and V1.1, and will only be
apparent in non-UK machines.)
+3DOS requirements
When any of the +3DOS routines are called, the following store
configuration is required:
The stack must be below BFE0h (49120) and above 4000h (16384). The
upper value is BFE0h (rather than C000h) because the top 30 bytes
of page 2 are used to implement inter-page block moves. This area
is not reserved by +3DOS; it is merely required that the stack is
not there. The stack must have at least 50 words available.
All +3DOS routines will exit with the same memory configuration as
on entry.
The DOS jump block is located in ROM 2 from address 0100h (256)
onwards. The address and interface for each routine are as
follows:
ENTRY CONDITIONS
None
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DOS VERSION
0103h(259)
Get the DOS issue and version numbers.
ENTRY CONDITIONS
None
EXIT CONDITIONS
D = Issue
E = Version (within issue)
Always:
AF BC HL IX corrupt
All other registers preserved
DOS OPEN
0106h(262)
Create and/or open a file.
Open action
Create action
Files without headers have their EOF position recorded as the byte
at the start of the smallest 128 byte record position greater than
all written record positions.
The header data area is 8 bytes long and may be used by the caller
for any purpose whatsoever. If open action = 1, and the file
exists (and has a header), then the header data is read from the
file, otherwise the header data is zeroised. The header data is
available even if the file does not have a header. Call DOS REF
HEAD to access the header data.
BYTE 0 1 2 3 4 5 6
Program 0 file length 8000h or LINE offset to prog
Numeric array 1 file length xxx name xxx xxx
Character array 2 file length xxx name xxx xxx
CODE or SCREEN$ 3 file length load address xxx xxx
EXIT CONDITIONS
If file newly created:
Carry true
Zero true
A corrupt
If existing file opened:
Carry true
Zero false
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DOS CLOSE
0109h(265)
Close a file.
ENTRY CONDITIONS
B = File number
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DOS ABANDON
010Ch(268)
Abandon a file.
ENTRY CONDITIONS
B = File number
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
The header data area is 8 bytes long and may be used by the caller
for any purpose whatsoever. It is available even if the file does
not have a header; however, only files with a header and opened
with write access will have the header data recorded on disk.
Note that +3 BASIC uses these 8 bytes (see the note under DOS OPEN
which gives the details). If creating a file that will
subsequently be LOADed within BASIC, then these bytes should be
filled with the relevant values.
ENTRY CONDITIONS
B = File number
EXIT CONDITIONS
If OK, but file doesn't have a header:
Carry true
Zero true
A corrupt
IX = Address of header data in page 7
If OK, file has a header:
Carry true
Zero false
A corrupt
IX = Address of header data in page 7
Otherwise:
Carry false
A = Error code
IX corrupt
Always:
BC DE HL corrupt
All other registers preserved
DOS READ
0112h(274)
Read bytes from a file into memory.
ENTRY CONDITIONS
B = File number
C = Page for C000h(49152)...FFFFh(65535)
DE = Number of bytes to read (0 means 64K)
HL = Address for bytes to be read
EXIT CONDITIONS
If OK:
Carry true
A DE corrupt
Otherwise:
Carry false
A = Error code
DE = Number of bytes remaining unread
Always:
BC HL IX corrupt
All other registers preserved
DOS WRITE
0115h(277)
Write bytes to a file from memory.
ENTRY CONDITIONS
B = File number
C = Page for C000h(49152)...FFFFh(65535)
DE = Number of bytes to write (0 means 64K)
HL = Address of bytes to write
EXIT CONDITIONS
If OK:
Carry true
A DE corrupt
Otherwise:
Carry false
A = Error code
DE = Number of bytes remaining unwritten
Always:
BC HL IX corrupt
All other registers preserved
DOS BYTE READ
0118h(280)
Read a byte from a file.
EOF is latched.
ENTRY CONDITIONS
B = File number
EXIT CONDITIONS
If OK - Byte <> 1Ah(26)(soft-EOF):
Carry true
Zero false
A corrupt
C = Byte
If OK - Byte = 1Ah(26)(soft-EOF):
Carry true
Zero true
A corrupt
C = Byte
Otherwise:
Carry false
A = Error code
C corrupt
Always:
B DE HL IX corrupt
All other registers preserved
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DOS CATALOG
011Eh(286)
Fills a buffer with part of the directory (sorted).
Note that +3DOS format disks (which are the same as single sided,
single track AMSTRAD PCW range format disks) may have a maximum of
64 directory entries.
Buffer format:
Entry 0
Entry 1
Entry 2
Entry 3
...to...
Entry n
The file size is the amount of disk space allocated to the file,
not necessarily the same as the amount used by the file.
ENTRY CONDITIONS
B = n + 1, Size of buffer in entries, > = 2
C = Filter
bit 0 = include system files (if set)
bits 1...7 = 0 (reserved)
DE = Address of buffer (first entry initialised)
HL = Address of filename (wildcards permitted)
EXIT CONDITIONS
If OK:
Carry true
A corrupt
B = Number of completed entries in buffer, 0...n.
(If B = n, there may be more to come)
Otherwise:
Carry false
A = Error code
B corrupt
Always:
C DE HL IX corrupt
All other registers preserved
ENTRY CONDITIONS
A = Drive, ASCII 'A'...'P'
EXIT CONDITIONS
If OK:
Carry true
A corrupt
HL = Free space (in kilobytes)
Otherwise:
Carry false
A = Error code
HL corrupt
Always:
BC DE IX corrupt
All other registers preserved
DOS DELETE
0124h(292)
Delete an existing file.
ENTRY CONDITIONS
HL = Address of filename (wildcards permitted)
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DOS RENAME
0127h(295)
Rename an existing file.
File must not be open on any file number. A file with the new
filename must not exist. The new name must specify, or default to,
the same drive as the
old name.
ENTRY CONDITIONS
DE = Address of new filename (no wildcards)
HL = Address of old filename (no wildcards)
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DOS BOOT
012Ah(298)
Boot from disk.
Bootstrap environment:
ENTRY CONDITIONS
None
EXIT CONDITIONS
If OK:
No exit (as the bootstrap will be entered)
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
Does not access the drive, but merely checks that there is a
driver for it (which does not imply that the drive exists).
ENTRY CONDITIONS
A = Drive,ASCII'A'...'P'(FFh(255) = get default drive)
EXIT CONDITIONS
If OK:
Carry true
A = Default drive
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
ENTRY CONDITIONS
A = User 0...15(FFh(255) = get default user)
EXIT CONDITIONS
If OK:
Carry true
A = Default user
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
ENTRY CONDITIONS
B = File number
EXIT CONDITIONS
If OK:
Carry true
A corrupt
E HL = File pointer 000000h...FFFFFFh(0...16777215)
(E holds most significant byte; L holds least
significant byte)
Otherwise:
Carry false
A = Error code
E HL corrupt
Always:
BC D IX corrupt
All other registers preserved
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
ENTRY CONDITIONS
B = File number
EXIT CONDITIONS
If OK:
Carry true
A corrupt
E HL = File pointer 000000h...FFFFFFh(0...16777215)
(E holds most significant byte, L holds least
significant byte)
Otherwise:
Carry false
A = Error code
E HL corrupt
Always:
BC D IX corrupt
All other registers preserved
DOS GET 1346
013Ch(316)
Get the current location of the cache and RAMdisk.
Note that the sizes may be smaller than those specified in DOS SET
1346, as there is an (unpublished) maximum size of cache and a
minimum size of RAMdisk (4 sectors).
ENTRY CONDITIONS
None
EXIT CONDITIONS
D = First buffer for cache
E = Number of cache sector buffers
H = First buffer for RAMdisk
L = Number of RAMdisk sector buffers
Always:
AF BC IX corrupt
All other registers preserved
The location and size of the cache and RAMdisk can be specified
separately; any remaining buffers are unused by DOS and are
available to the caller.
The cache and RAMdisk must not overlap. +3DOS does not check this;
responsibility lies with the caller.
Note that the sizes actually used may be smaller than those
specified as in practice, there is a maximum cache size and a
minimum size of RAMdisk (4 sectors).
A cache size of 0 will still work but will seriously impair the
floppy disk performance.
This routine will fail if there are any files open on drive M:.
ENTRY CONDITIONS
D = First buffer for cache
E = Number of cache sector buffers
H = First buffer for RAMdisk
L = Number of RAMdisk sector buffers
(Note that E + L < = 128)
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DOS FLUSH
0142h(322)
Write any pending headers, data, directory entries for this drive.
ENTRY CONDITIONS
A = Drive,ASCII'A'...'P'
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
ENTRY CONDITIONS
B = File number
C = Access mode required
Bits 0...2 values:
1 = exclusive-read
2 = exclusive-write
3 = exclusive-read-write
5 = shared-read
(all other bit settings reserved)
Bits 3...7 = 0 (reserved)
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
If there are any files open on this drive from other file numbers
with shared-read access, then the disk can only be opened with
shared-read access from this file number.
If there are any files open on this drive from other file numbers
with exclusive access, then the disk cannot be opened from this
file number.
ENTRY CONDITIONS
A = Drive, ASCII'A'...'P'
B = File number
C = Access mode required
Bits 0...2 values:
1 = exclusive-read
2 = exclusive-write
3 = exclusive-read-write
5 = shared-read
(all other bit settings reserved)
Bits 3...7 = 0(reserved)
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL corrupt
All other registers preserved
ENTRY CONDITIONS
A = Enable/disable
FFh(255) = enable
00h(0) = disable
HL = Address of ALERT routine (if enabled)
EXIT CONDITIONS
HL = address of previous ALERT routine (0 if none)
Always:
AF BC DE IX corrupt
All other registers preserved
NOTE
Note that if you are substituting your own ALERT subroutine, the
'entry conditions' are the conditions passed to your subroutine
and the 'exit conditions' are the values that your subroutine must
produce and the registers you are allowed to corrupt.
Note that there are two routine interfaces for ALERT. The first,
(which is used in machines with +3DOS version V1.0) should have
the entry and exit conditions shown ahead.
ENTRY CONDITIONS
DE = Address of reply string (in page 7) terminated by
FFh (255)
HL = Address of error message (in page 7) terminated by
FFh (255)
EXIT CONDITIONS
A = Reply character
Always:
F BC DE HL IX corrupt
All other registers preserved
The second version of ALERT, which allows the user to provide non-
UK error messages and is generally more flexible, is present in
+3DOS versions V1.1 and upwards.
ENTRY CONDITIONS
B = Error number
C = Drive,ASCII'A'...'P'
D = Logical track (if required for message)
E = Logical sector (if required for message)
HL = Address of UK error message (page 7) terminated by
FFh (255)
EXIT CONDITIONS
A = Reply
0 = cancel
1 = retry
2 = ignore
Always:
F BC DE HL IX corrupt
All other registers preserved
ENTRY CONDITIONS
A = Drive,ASCII'A'...'P'
EXIT CONDITIONS
If OK:
Carry true
A corrupt
IX = Address of XDPB
Otherwise:
Carry false
A = Error code
IX corrupt
Always:
BC DE HL corrupt
All other registers preserved
DOS MAP B
0154h(340)
Map drive B: to unit 0 or unit 1. (This routine will fail if drive
B: has files open.)
EXIT CONDITIONS
If OK:
Carry true
A corrupt
HL = Address of previous CHANGE DISK routine (0 if
none)
Otherwise:
Carry false
A HL corrupt
Always:
BC DE IX corrupt
All other registers preserved
NOTE
The definition of the subroutine CHANGE DISK is as shown
ahead. Note that if you are substituting your own CHANGE DISK
subroutine, the 'entry conditions' are the conditions passed
to your subroutine, and the 'exit conditions' are registers
you are allowed to corrupt.
CHANGE DISK
Ask the user to change the disk in unit 0.
ENTRY CONDITIONS
A = Logical drive, ASCII'A'...'P'
HL = Address of message (page 7) terminated by FFh (255)
EXIT CONDITIONS
Always:
AF BC DE HL IX corrupt
All other registers preserved
All routines assume that interrupts are enabled on entry, and will
still be enabled on exit.
DD INTERFACE
0157h(343)
Is the floppy disk drive interface present? (This information is
also held by BASIC in bit 4 of the FLAGS3 system variable.)
ENTRY CONDITIONS
None
EXIT CONDITIONS
If present:
Carry true
Otherwise:
Carry false
Always:
A BC DE HL IX corrupt
All other registers preserved
DD INIT
015Ah(346)
Initialise the disk driver.
ENTRY CONDITIONS
None
EXIT CONDITIONS
Always:
AF BC DE HL IX corrupt
All other registers preserved
DD SETUP
015Dh(349)
Set up disk parameters.
ENTRY CONDITIONS
HL = Address of parameter block
EXIT CONDITIONS
Always:
AF BC DE HL IX corrupt
All other registers preserved
DD SET RETRY
0160h(352)
Set the try and retry count. (A value of 1 will try the operation
once, ie. no retry.)
ENTRY CONDITIONS
A = Try/retry count > = 1
EXIT CONDITIONS
Always:
AF BC DE HL IX corrupt
All other registers preserved
DD READ SECTOR
0163h(355)
Read a sector.
ENTRY CONDITIONS
B = Page for C000h(491S2)...FFFFh(65535)
C = Unit (0/1)
D = Logical track, 0 base
E = Logical sector, 0 base
HL = Address of buffer
IX = Address of XDPB
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DD WRITE SECTOR
0166h(358)
Write a sector.
ENTRY CONDITIONS
B = Page for C000h(49152)...FFFFh(65535)
C = Unit (0/1)
D = Logical track, 0 base
E = Logical sector, 0 base
HL = Address of buffer
IX = Address of XDPB
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DD CHECK SECTOR
0169h(361)
Check a sector. (Uses the μPD765A scan equal command.)
Checks that the sector on disk is the same as the copy in memory.
EXIT CONDITIONS
If OK (equal):
Carry true
Zero true
A corrupt
If OK (not equal):
Carry true
Zero false
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DD FORMAT
016Ch(364)
Format a track. (Uses the μPD765A format track command.)
ENTRY CONDITIONS
B = Page for C000h(49152)...FFFFh(65535)
C = Unit (0/1)
D = Logical track, 0 base
E = Filler byte, usually E5h(229)
HL = Address of format buffer
IX = Address of XDPB
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DD READ ID
016Fh(367)
Read a sector identifier.
ENTRY CONDITIONS
C = Unit (0/1)
D = Logical track, 0 base
IX = Address of XDPB
EXIT CONDITIONS
If OK:
Carry true
A = Sector number from identifier
Otherwise:
Carry false
A = Error code
Always:
HL = Address of result buffer in page 7
BC DE IX corrupt
All other registers preserved
DD TEST UNSUITABLE
0172h(370)
Check that disk is suitable to write to.
A single track disk will not work in a double track drive, and
vice versa.
ENTRY CONDITIONS
C = Unit (0/1)
IX = Address of XDPB
EXIT CONDITIONS
If suitable:
Carry true
A corrupt
Otherwise:
Carry false
A = Error code
Always:
BC DE HL IX corrupt
All other registers preserved
DD LOGIN
0175h(373)
Log in a new disk.
ENTRY CONDITIONS
C = Unit (0/1)
IX = Address of destination XDPB
EXIT CONDITIONS
If OK:
Carry true
A = Disk type
DE = Size of allocation vector
HL = Size of hash table
Otherwise:
Carry false
A = Error code
DE HL corrupt
Always:
BC IX corrupt
All other registers preserved
DD SEL FORMAT
0178h(376)
Initialise an XDPB for a standard format.
EXIT CONDITIONS
If OK:
Carry true
A = Disk type
DE = Size of 2 bit allocation vector
HL = Size of hash table
Otherwise:
Carry false
A = Error code
DE HL corrupt
Always:
BC IX corrupt
All other registers preserved
DD ASK 1
017Bh(379)
Check to see if unit 1 is present. (BASIC holds this information
in bit 5 of the FLAGS3 system variable.)
Note that this routine can be fooled by disks which are almost,
but not quite, inserted in the drive.
This routine assumes that when a disk is not in the drive, then
write-protect is true. This is indeed the case for 3 inch and 8
inch disk drives, but is not the case for 5¼ inch disk drives.
ENTRY CONDITIONS
None
EXIT CONDITIONS
If unit 1 present:
Carry true
Otherwise:
Carry false
Always:
A BC DE HL IX corrupt
All other registers preserved
DD DRIVE STATUS
017Eh(382)
Issue a sense drive status command.
ENTRY CONDITIONS
C = Unit/head
bits 0...1 = unit
bit 2 = head
bits 3...7 = 0
EXIT CONDITIONS
A = ST3 (Status register 3 of μPD765A)
Always:
F BC DE HL IX corrupt
All other registers preserved
DD EQUIPMENT
0181h(385)
Ask what type of drive this is (ie. single/double track,
single/double sided).
Track information can only be determined once a disk has been seen
and had its type identified during logging in.
ENTRY CONDITIONS
C = Unit (0/1)
IX = Address of XDPB
EXIT CONDITIONS
A = Side/track information
bits 0...1 = side information
0 = unknown
1 = single sided
2 = double sided
bits 2...3 = track information
0 = unknown
1 = single track
2 = double track
Always:
F BC DE HL IX corrupt
All other registers preserved
DD ENCODE
0184h(388)
Set the copy protection ENCODE subroutine.
Copy protected disks have some of their track and sector numbers
encoded on disk. Before each disk access, the ENCODE subroutine is
called to encode the physical track and sector numbers.
These encoded track and sector numbers must match those in the
sector identifier.
ENTRY CONDITIONS
A = Enable/disable
00h(0) = disable
FFh(255) = enable
HL = (If enabled) address of ENCODE subroutine
EXIT CONDITIONS
HL = Address of previous ENCODE subroutine (0 if none)
Always:
AF BC DE IX corrupt
All other registers preserved
NOTE
The definition of the subroutine ENCODE is as shown ahead. Note
that if you are substituting your own ENCODE subroutine, the
'entry conditions' are the conditions passed to your subroutine,
and the 'exit conditions' are the values that your subroutine must
produce and the registers you are allowed to corrupt.
ENCODE
ENTRY CONDITIONS
C = Unit/side
bits 0...1 = unit
bit 2 = side
bits 3...7 = 0
D = Physical track
E = Physical sector
IX = Address of DPB
EXIT CONDITIONS
D = Encoded physical track
E = Encoded physical sector
Always:
AF corrupt
All other registers preserved
DD L XDPB
0187h(391)
Initialise an XDPB for a given format.
ENTRY CONDITIONS
IX = Address of destination XDPB
HL = Address of source disk specification
EXIT CONDITIONS
If OK:
Carry true
A = Disk type recorded on disk
DE = Size of allocation vector
HL = Size of hash table
If bad format:
Carry false
A = Error code
DE HL corrupt
Always:
BC IX corrupt
All other registers preserved
DD L DPB
018Ah(394)
Initialise a DPB for a given format.
ENTRY CONDITIONS
IX = Address of destination DPB
HL = Address of source disk specification
EXIT CONDITIONS
If OK:
Carry true
A = Disk type recorded on disk
DE = Size of allocation vector
HL = Size of hash table
If bad format:
Carry false
A = Error report
DE HL corrupt
Always:
BC IX corrupt
All other registers preserved
DD L SEEK
018Dh(397)
Seek to required track.
Retry if fails.
ENTRY CONDITIONS
C = Unit/head
bits 0...l = unit
bit 2 = head
bits 3...7 = 0
D = Track
IX = Address of XDPB
EXIT CONDITIONS
If OK:
Carry true
A corrupt
Otherwise:
Carry false
A = Error report
Always:
BC DE HL IX corrupt
All other registers preserved
DD L READ
0190h(400)
Low level μPD765A read command.
Read data.
Read a track.
Writes commands.
Reads data.
Reads results.
ENTRY CONDITIONS
HL = Address of parameter block
EXIT CONDITIONS
HL = Address of result buffer in page 7
Always:
AF BC DE IX corrupt
All other registers preserved
DD L WRITE
0193h(403)
Low level μPD765A write command.
Write data.
Scan equal.
Writes commands.
Writes data.
Reads results.
ENTRY CONDITIONS
HL = Address of parameter block
EXIT CONDITIONS
HL = Address of result buffer in page 7
Always:
AF BC DE IX corrupt
All other registers preserved
DD L ON MOTOR
0196h(406)
Turn on the motor.
ENTRY CONDITIONS
None
EXIT CONDITIONS
Always:
AF BC DE HL IX corrupt
All other registers preserved
DD L T OFF MOTOR
0199h(409)
Start the motor off time-out.
ENTRY CONDITIONS
None
EXIT CONDITIONS
Always:
AF BC DE HL IX corrupt
All other registers preserved
DD L OFF MOTOR
019Ch(412)
Turn off the motor.
ENTRY CONDITIONS
None
EXIT CONDITIONS
Always:
AF BC DE HL IX corrupt
All other registers preserved
Part 28
Spectrum character set
Subjects covered...
Control codes
Characters
Z80 assembler mnemonics
0 00 nop cb
1 01 ld bc,NN rlc c
2 02 ld(bc),a rlc d
not used
3 03 inc bc rlc e
4 04 inc b rlc h
5 05 dec b rlc l
6 PRINT comma 06 ld b,N rlc (hl)
7 EDIT 07 rlca rlc a
8 cursor left ← 08 ex af,af rrc b
9 cursor right → 09 add hl,bc rrc c
10 cursor down ↓ 0A ld a,(bc) rrc d
11 cursor up ↑ 0B dec bc rrc e
12 DELETE 0C inc c rrc h
13 ENTER 0D dec c rrc l
14 number 0E ld c,N rrc (hl)
15 not used 0F rrca rrc a
16 INK control 10 djnz DIS rl b
17 PAPER control 11 ld de,NN rl c
18 FLASH control 12 ld (de),a rl d
CODE CHARACTER HEX Z80 Assembler -AFTER CBh -AFTER EDh
55 7 37 scf
56 8 38 jr c,DIS srl b
57 9 39 add hl,sp srl c
58 : 3A ld a,(NN) srl d
59 ; 3B dec sp srl e
60 < 3C inc a srl h
61 = 3D dec a srl l
62 > 3E ld a,N srl (hl)
63 ? 3F ccf srl a
64 @ 40 ld b,b bit 0,b in b,(c)
65 A 41 ld b,c bit 0,c out (c),b
66 B 42 ld b,d bit 0,d sbc hl,bc
67 C 43 ld b,e bit 0,e ld (NN),bc
68 D 44 ld b,h bit 0,h neg
69 E 45 ld b,l bit 0,l retn
70 F 46 ld b,(hl) bit 0,(hl) im 0
71 G 47 ld b,a bit 0,a ld i,a
72 H 48 ld c,b bit 1,b in c,(c)
73 I 49 ld c,c bit 1,c out c,(c)
74 J 4A ld c,d bit 1,d adc hl,bc
75 K 4B ld c,e bit 1,e ld bc,(NN)
76 L 4C ld c,h bit 1,h
77 M 4D ld c,l bit 1,l reti
78 N 4E ld c,(hl) bit 1,(hl)
79 O 4F ld c,a bit 1,a ld r,a
80 P 50 ld d,b bit 2,b in d,(c)
81 Q 51 ld d,c bit 2,c out (c),d
82 R 52 ld d,d bit 2,d sbc hl,de
83 S 53 ld d,e bit 2,e ld(NN),de
84 T 54 ld d,h bit 2,h
85 U 55 ld d,l bit 2,l
86 V 56 ld d,(hl) bit 2,(hl) im l
87 W 57 ld d,a bit 2,a ld a,i
88 X 58 ld e,b bit 3,b in e,(c)
89 Y 59 ld e,c bit 3,c out (c),e
90 Z 5A ld e,d bit 3,d adc hl,de
CODE CHARACTER HEX Z80 Assembler -AFTER CBh -AFTER EDh
Reports appear at the bottom of the screen whenever the +2A has
stopped executing some BASIC. They explain why it has stopped - be
it for some natural reason, or because an error has occurred.
Most reports have a code number or letter (so that you can refer
the table ahead), a brief message explaining what happened, and
the line number (and the statement number within the line) where
BASIC stopped. (A command is shown as line 0. Within a line,
statement 1 is at the beginning, statement 2 comes after the first
colon (or THEN), and so on.)
Disk errors marked by RIC (in the left-hand column) will normally
be displayed followed by the options: - Retry, Ignore or Cancel?.
If the cancel option is chosen, then the report shown in the
second column will be displayed.
0 OK Any
Successful completion, or jump to a line
number bigger than any existing. This
report does not change the line and
statement jumped to by CONTINUE.
CODE REPORT/EXPLANATION SITUATION
P FN without DEF FN
User-defined function used without a
corresponding DEF in the program.
Q Parameter error FN
Wrong number of arguments, or one of them
is the wrong type (string instead of
number, or vice versa).
Drive in use
An attempt has been made to re-map a
drive that has files open on it. It is
very unlikely that this error will ever
be seen. CAT, COPY, ERASE,
LOAD, MERGE, MOVE,
Drive not found SAVE
A filename used in a disk command
contains a drive letter specifying a
drive that isn't present. For example,
ERASE "c:fred".
Uncached Unlikely
This is an internal system error and it
is very unlikely that it will ever be
seen.
CODE REPORT/EXPLANATION SITUATION
Hardware
The +2A's memory is divided into 64K ROM and 128K RAM, arranged in
16K pages. The four ROM pages (0-3) can be mapped into the bottom
16K (0000h-3FFFh) of the memory map. The eight RAM pages (0-7) are
usually mapped into the top 16K (C000h-FFFFh) of the memory map.
RAM page 5 is also mapped into the range 4000h-7FFFh, and RAM page
2 is mapped into the range 8000h-BFFFh. There are also several RAM
page combinations that occupy the full 64K address range. These
were given in part 24 of this chapter, under the heading 'Memory
management'.
Physically speaking, the ROMs are two 32K devices (similar to the
27256), which are both treated by the system as two 16K chips. The
RAM is composed of four 64K x 4 bit chips (41464), some of which
(RAM banks 4-7) are time-shared between the circuitry that
produces the screen display, and the Z80A. The others (RAM banks
0-3) are for the exclusive use of the Z80A, as is the ROM.
For the contended RAM (which shares time between the video
circuitry and the processor), during 128 out of every 228 CPU T
states (1 TV line), and during 192 out of every 311 TV lines (1
frame) the CPU is allowed only 1 access to contended RAM in every
8 T states. The CPU is controlled by introducing wait states.
The Uncommitted Logic Array (ULA) handles most of the I/O such as
keyboard, datacorder, part of the printer interface and screen
handling. It converts bytes in memory into patterns and colours on
the screen, and allows the Z80A to scan the keyboard and read and
write data to tape.
The way in which the AY-3-8912 works is quite complex, and the
would-be experimenter is advised to get the manufacturer's data
sheet. The following information should be enough to get things
underway, however:
The tone of a channel is a 12 bit value taken from the sum of D3-
D0 of the coarse register, and D7-D0 of the the fine register. The
basic unit of tone is the clock frequency divided by 16 (ie.
110.83KHz), and with a 12 bit counter range, frequencies from 27Hz
to 110KHz can be generated.
The period of the noise source is taken by counting down the lower
5 bits of the noise register every sound clock period divided by
16.
D7 - Not used
D6 - 1 means input port
- 0 means output port
D4 - Channel B noise
D5 - Channel C noise
D3 - Channel A noise
D2 - Channel C tone
D1 - Channel B tone
D0 - Channel A tone
This register controls both the mixing of noise and tone values
for each channel, and the direction of the 8 bit I/O port. A zero
in a mix bit indicates that the function is enabled.
RD - Envelope control
D3 - Continue
D2 - Attack
D1 - Alternate
D0 - Hold
Number handling
Variables
Strings
Functions
Brief summary of keywords
Mathematical operations
Numeric arrays have names a single letter long, which may be the
same as the name of a simple variable. They may have many
dimensions of arbitrary size. Subscript values start at 1.
Strings are completely flexible in length. The name of a string
consists of a single letter followed by $.
String arrays can have many dimensions of arbitrary size. The name
is a single letter followed by $ but may not be the same as the
name of a simple string variable. All the strings in a given array
have the same fixed length, which is specified as an extra final
dimension in the DIM statement. Subscript values start at 1.
(i) empty
...or...
...or...
...or...
In (a), suppose the string expression has the value s$, then if
the slicer is empty, the result is s$ (considered as a substring
of itself).
If the slicer has the form (iii), then suppose the first numerical
expression has the value m (the default value is 1), and the
second, n (the default value is the length of s$). If 1<=m<=n<=the
length of s$, then the result is the substring of s$ starting with
the mth character and ending with the nth.
Functions
The argument of a function does not need brackets if it is a
constant or a variable (optionally subscripted or sliced).
FN FN followed by a letter
calls up a user-defined
function (see DEF). The
arguments must be enclosed
in brackets - (even if there
are no arguments, the
brackets must still be
present).
OR binary operation,
both operands numbers: a OR b 1 if b<>0
a if b=0
OR has priority 2.
PI none π (3.1415927...).
Error C if x contains a
syntax error or gives a
numerical value.
Other errors possible (as
for VAL).
- number Negation.
OPERATION PRIORITY
Subscripting and slicing 12
All functions except NOT and unary minus 11
↑ (exponentiation) 10
- Unary minus (used to negate) 9
*,/ (multiplication, division) 8
+,- (addition, subtraction) 6
=,>,<,<=,>=,<> (relational operators) 5
NOT 4
AND 3
OR 2
Statements
The following notation is applicable in the remainder of this
section:
All statements except INPUT, DEF FN and DATA can be used either as
commands or in programs (although they may be more sensible in one
than the other). A command or program line can have several
statements, separated by colons. There is no restriction on
whereabouts in a line any particular statement can occur; however,
see IF and REM.
CAT [#n,] [d] [a] EXP Operates as per the CAT command, but
produces an expanded catalog that
includes system files, and displays
those files whose write protection,
system status and archive attributes
have been set. (See MOVE u TO f.)
LLIST LLIST 0
RANDOMIZE RANDOMIZE 0
RESTORE RESTORE 0
RUN RUN 0
Number systems
Bits and bytes
...and so on, and this is made even more systematic with the
numerals that we use. However, the only reason for using ten (the
decimal system) is that we happen to have ten digits on our hands
(fingers and thumbs).
DECIMAL HEX
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 A
DECIMAL HEX
11 B
12 C
13 D
14 E
15 F
16 10
17 11
...continued...
25 19
26 1A
27 1B
...etc...
31 1F
32 20
33 21
...etc...
158 9E
159 9F
160 A0
161 A1
...etc...
255 FF
256 100
...and so on.
If you are using hex notation and you want to make the fact quite
plain, then write 'h' at the end of the number, and say 'hex'. For
instance, for one hundred and fifty eight (decimal), write '9Eh'
and say 'nine E hex'.
Converting between binary and hex is very easy (use the previous
table to help you):
To convert a hex number to binary, change each hex digit into four
bits (again, starting at the right) then put the bits together to
form the complete binary number. For example, to convert F3h to
binary, first convert 3 which corresponds to 0011 binary (remember
- you must use zeros to make the binary number four bits long),
then convert F which corresponds to 1111 binary, put them
together, and you have the complete binary number - 11110011.
The bits inside the computer are mostly grouped into sets of eight
- these are called bytes. A single byte can represent any number
from 0 to 255 decimal (11111111 binary or FFh).
You can only use 0s and 1s for this, so the number must be a non-
negative whole number - for instance, you can not use BIN -11 to
represent -3 decimal, but you can use -BIN 11 instead. The number
must also be no greater than decimal 65535 - ie. it can't have
more than sixteen bits. If you pad out a binary number with
leading zeros, for example, BIN 00000001, the BIN function will
rightly ignore them and treat the number as if it were BIN 1.
Part 33
Example programs
Programs...
Renumber
Clock
Bustout
Telly tennis
Renumber
This short program is an aid to the renumbering facility provided
by the edit menu's Renumber option. If you MERGE this program into
the program you are developing (or wish to renumber), you will be
able to select both the starting line number and the step size
(between successive program lines).
Type RUN 9000 to run the program, enter the start line (in the
range 1...9999), enter the step size (in the range 1...9999), then
press the EDIT key and select the Renumber option from the edit
menu.
Clock
This program sets up the +2A as an analogue (and digital) clock.
Type RUN to start the program, enter the hour (in the range
1...12) and enter the minute (in the range 0...59). The clock will
then start.
Bustout
This program provides a colourful and entertaining little game for
one player against the computer.
To play the game, type RUN, then press any key to start.
Options:
Telly tennis
This program sets up the +2A to play one of the most well-known
and enduring of computer games. For two players, or one player
against the computer.
Type RUN to start the program, then type 1 or 2 (for the number of
players) to play.
Options:
1. The "66"s shown in line 150 are graphics characters. They are
produced by pressing the GRAPH key once (to switch graphics
mode on), typing the characters (using the 6 key), then
pressing the GRAPH key again (to switch graphics mode off).
(Note that these characters will look like black blocks on
the screen.)
2. The "8"s shown in lines 150, 250 and 540 are also graphic
characters. Again, they are produced by pressing GRAPH once,
holding down CAPS SHIFT and pressing the 8 key once, then
pressing GRAPH again. (Again, note that these characters will
look like black blocks on the screen.)
Subjects covered...
To use the calculator, call up the opening menu and select the
Calculator option. (If you don't know how to select a menu option,
refer back to chapter 2.)
When you have selected the Calculator option, the screen will
change to...
6+4
As soon as you press ENTER, the answer 10 will appear. (Note that
you don't key in = as you would on a conventional calculator.)
You will see that the cursor is positioned to right of the answer,
which is a running total (like on a conventional calculator). This
means that you can simply type in the next operation to be carried
out on the running total (without having to type in a whole new
calculation). So, with the cursor still positioned to the right of
the 10 on the screen, type in...
/5
*PI
This produces the result 6.2831853 on the screen. The +2A has used
its built-in pi function - all you had to do was type in PI. This
applies to all the +2A's mathematical functions. To demonstrate,
type in...
*ATN 60
...which gives the result 9.7648943. You may also 'edit' the
contents of the screen. To demonstrate, move the cursor (using the
cursor left key ←) to the beginning of the line and then type in
INT so that the line reads...
INT 9.7648943
1E6
...and back will come the value of that expression. Notice that
before you typed in 1E6, you pressed ENTER on its own - this tells
the +2A that you are about to start a new calculation.
LET x=10
(You must then press ENTER twice for the +2A to accept the
variable assignment.) Now verify that the variable x is being
used, by typing...
x+90
...then...
+x*x
When you have finished using the calculator, press the EDIT key.
The screen will change to...
Select the Exit option to return to the opening menu. If you were
working on a +3 BASIC program before you started using the
calculator, then you may return to the program by selecting the +3
BASIC option. (If you wish to continue using the calculator, then
select the Calculator option.)
Note that if you have set up any user defined functions (using the
DEF FN statement) whilst working on a BASIC program, you will be
able to invoke that function when using the calculator. To
illustrate this point, return to +3 BASIC and type in (for
example)...
FN c(3)
...and the calculator will print the number 27 (ie. the cube of
3).
Chapter 10
Peripherals for your +2A
Subjects covered...
Printer
Joystick(s)
VDU Monitor
Amplifier
Serial devices
MIDI device
Auxiliary interface
Expansion devices
Disk drive(s)
Printer
The +2A may be used with any Centronics compatible parallel
printer. We would particularly recommend the AMSTRAD DMP range of
printers (eg. models DMP2000, DMP3000, DMP3160 or DMP4000) for use
with the +2A.
Connect the end of the lead which is fitted with the flat edge-
connector plug, into the socket marked PRINTER at the back of the
+2A.
PRINTER socket
Note that printers for use with the +2A must generate their line
feeds internally. If you experience problems with printer line
feeds, try adjusting the appropriate 'DIP switch' inside your
printer. (On the AMSTRAD DMP range of printers, DIP switch DS1-4
controls the line feed setting.)
The +2A may also be used with most serial printers conforming to
the RS232 standard. It is recommended that inexperienced users do
not attempt to experiment with serial interface connections. You
should obtain a suitable computer-to-serial printer lead from your
Sinclair dealer, and you should always follow the printer
manufacturer's installation and operation instructions.
A serial printer should be connected to the RS232/MIDI socket at
the back of the +2A.
Joystick(s)
We recommend that you use the Sinclair SJS range of joystick(s)
with the +2A. Other types of joystick (eg. Atari) will not operate
directly, as their connecting plugs are wired differently.
There are two joystick sockets at the left-hand side of the +2A.
In general, games use the JOYSTICK 1 socket.
PIN FUNCTION
1 not used
2 common
3 not used
4 fire
5 up
JOYSTICK 1 and JOYSTICK 2 sockets
6 right
7 left
8 common
9 down
VDU Monitor
The +2A can use an RGB colour VDU monitor (or a French standard
PERITEL TV) instead of (or in addition to) an ordinary TV. If the
monitor that you wish to use isn't quoted as being Spectrum +2A
(or +3) compatible, then the chances are you'll have to buy a lead
for it (contact your Sinclair dealer).
When using a monitor, some provision may have to be made for sound
(if required). If the monitor has an audio input, then then this
should be connected either to pin 3 of the RGB/PERITEL socket or
to the TAPE/SOUND socket at the back of the +2A. If the monitor is
not capable of producing sound, then an external amplifier will
have to be used. See the next paragraph for further details.
Amplifier
The +2A normally reproduces sound through the TV set it is
connected to. However, if a VDU monitor is being used, or if you
would like to record or amplify the sound further, then a sound
signal is available from the TAPE/SOUND socket at the back of the
+2A. This is a 3.5mm stereo jack socket producing 200mV pk-pk at
approximately 5 Kohms impedance. You must not insert a mono jack
plug into the TAPE/SOUND socket - doing so will make all
subsequent datacorder operations fail. When using an amplifier, it
is worth remembering that the datacorder's 'load' and 'save'
signals are also fed to the TAPE/SOUND socket (and therefore the
amplifier's volume control should be turned down when performing
these operations).
Audio output
GND (common)
TAPE/SOUND socket
Serial devices
To connect any serial device to the +2A, you will require a
Spectrum +2A serial lead - available from your Sinclair dealer.
PIN FUNCTION
1 GND
2 TXD
3 RXD RS232 socket
4 DTR
5 CTS
6 +12V
MIDI device
Although the +2A's MIDI (Musical Instrument Digital Interface)
socket shares the same socket as the RS232, you will need a
different lead for it (available from your Sinclair dealer). The
lead should be connected into the 'MIDI IN' socket on your
synthesiser, drum machine, etc. There is no provision for the +2A
to receive MIDI data - it can only act as a source. No setting up
of the MIDI is necessary before use (except the inclusion of the Y
parameter within the PLAY command to turn it on).
Using the MIDI interface will not disturb the RS232's baud rate
setting.
PIN FUNCTION
1 RETURN
2 not used
3 not used MIDI socket
4 not used
5 DATA OUT
6 not used
Auxiliary interface
The AUX (auxiliary interface) socket supports two input lines
(pins 3 and 5) and two output lines (pins 2 and 4). The I/O lines
are driven by 1488 and 1489 line driver chips which are, in turn,
connected to the I/O lines of the AY-3-8912 (see the
manufacturer's data sheet for this device). Basically, register 14
of the AY-3-8912 controls eight I/O lines; the bits are designated
as follows:
BIT SIGNAL
0 AUX pin 2 (out)
1 AUX pin 4 (out)
2 RS232 pin 5 (CTS out)
3 RS232 pin 3 (RXD out)
4 AUX pin 3 (in)
5 AUX pin 5 (in)
6 RS232 pin 4 (DTR in)
7 RS232 pin 5 (TXD in)
Expansion devices
The +2A can connect to a very wide range of peripherals via the
EXPANSION I/O socket at the back of the machine. Although this
socket is much the same as on the old-style Spectrum 48K, there is
no guarantee that a device which ran correctly on a Spectrum 48K
will run on a +2A. You should, therefore, before you purchase any
expansion device or add-on, verify that it will work with the +2A,
and not just with a 48K Spectrum.
Disk drive(s)
If you wish to connect an external disk drive (or drives) to the
+2A, you should use the model AMSTRAD FD-1 drive(s) together with
a suitable interface (the AMSTRAD SI-1 when available, or other
manufacturer's equivalent). The SI-1 interface connects to the
EXPANSION I/O socket at the back of the +2A, and will support up
to two FD-1 drives.
If you have connected two disk drives, switch them both on before
switching on the +2A. The power indicators (green) at the front of
both FD-1s should be illuminated. In addition, the read/write
indicator (red) on the second drive (drive B:) should also be
illuminated.
A
Abandoning loading ..........................................20,25
ABS ........................................................88,283
ACS ....................................................97,271,283
Aerial lead ..................................................9,15
AFC .........................................................14,15
AFT .........................................................14,15
Amplifier .................................................140,320
AMSTRAD computers .....................................215,219,223
AMSTRAD peripherals .................................19,27,317,325
AND .......................................................105,284
Animation .....................................................170
Apostrophe .................................................64,297
Archive status files ..........................................166
Argument ...................................................86,271
Arithmetic operations ..................................92,282,287
Arrays ........................................101,161,192,271,282
ASN ................................................... 97,271,284
Assembler .................................................204,264
AT ............................................116,129,181,271,297
ATN ........................................................97,284
ATTR ..................................................126,128,284
Attributes ........................................124,158,165,218
Auxiliary interface .......................................186,322
AUX socket ................................................279,322
B
Back-ups ................................................8,152,163
Backspace .................................................113,127
BASIC .................................................5,33,55,282
Baud rate .....................................................177
BEEP ..................................................141,289,320
BIN ...................................................111,284,302
Binary ....................................................111,300
Bits ......................................................185,301
Bootstrap .................................................209,217
BORDER ................................................128,273,289
Brackets ...............................................85,105,119
BREAK key ..................................14,25,35,59,66,179,272
BRIGHT ............................................125,273,289,297
Brightness ..................................................14,16
Bytes .............................................161,185,188,302
C
Calculator ....................................................313
CAPS LOCK key ............................................38,51,56
CAPS SHIFT key ...................................38,50,56,109,139
Cassette operations .....................................21,38,170
CAT ............................................46,155,174,274,289
Centronics ............................................176,186,317
Channels ......................................................182
Characters ........................................108,124,154,264
CHR$ ..................................................108,271,284
CIRCLE ....................................................132,289
Circles .................................................94,95,132
CLEAR .........................................175,194,204,273,289
CLOSE .................................................184,272,289
CLS ....................................................68,118,289
C mode .........................................................51
CODE ..............................108,162,169,174,274,284,294,298
Colon ..........................................................65
Colour ..............................................14,16,123,297
Comma ..................................................64,112,297
Commands ................................................37,55,288
Compatibility ...............................................5,215
Connections ................................................10,317
Contents ........................................................1
CONTINUE ............................................64,66,270,290
Contrast ................................................14,16,125
Control codes/characters ......................112,121,128,178,264
Coordinates ...............................................117,130
COPY ..............................................166,179,274,290
Copying files .............................................166,173
COS ........................................................96,284
CP/M ..................................................152,219,225
Cursor .............................................18,34,35,54,58
D
DATA ...........................................77,161,291,294,298
Datacorder operations ...................................21,38,170
Decimal .......................................................300
DEF ....................................................89,291,315
Degrees ........................................................97
Default drive .............................................153,170
Deleting files ................................................163
DELETE key ...............................................35,54,58
Destination file ..............................................166
DIM .................................................. 101,271,291
Disks .........................................8,19,43,150,219,226
Disk drive(s) .................................8,19,27,149,281,325
Disk format ......................................43,45,47,151,219
DOS(+3DOS) ............................................206,214,229
DRAW ......................................................131,291
E
EDIT key .............................................18,34,54,315
Editing ..................................................35,54,60
Edit menu ...................................................18,34
Ejecting a disk .............................................30,31
E mode .........................................................52
ENTER key .........................................17,35,54,58,314
Epson .........................................................176
ERASE .................................................163,274,291
Erasing files .................................................163
Error messages .........................................46,226,267
Escape code ...................................................178
EXP ............................................94,158,179,284,290
EXPANSION I/O socket ..................................187,323,325
Exponents ...................................................81,92
EXTEND MODE key .............................................52,56
External disk drive(s) .....................................19,325
F
Fields .....................................................45,153
Filenames ...............................................39,45,152
FLASH .........................................125,128,273,292,297
FN .................................................89,273,285,315
FOR ....................................................71,270,292
FORMAT .................................45,151,169,177,273,291,292
Functions ..............................................86,283,314
G
G mode .........................................................53
GO SUB .................................................75,271,292
GO TO ............................................63,64,65,271,292
GRAPH key ...............................................53,57,109
Graphics ............................................53,57,109,130
H
Hardware ..........................................176,185,279,322
Headers .......................................................218
Headphones ....................................................140
Hexadecimal ...................................................300
I
IF .....................................................68,105,292
IN ........................................................185,285
INK ...............................................125,273,293,297
INKEY$ ................................................139,182,285
INPUT ..........................................63,119,182,271,293
Inserting disks ............................................ 19,27
Installation ...................................................10
Instructions ............................................37,55,288
INT ........................................................88,285
Interface Two .................................................319
INV VIDEO key .................................................293
INVERSE ...............................126,133,180,273,290,293,297
I/O .......................................................185,195
J
Joysticks .................................................186,319
JOYSTICK sockets ..............................................319
K
Keyboard .............................................54,55,58,186
Keypad ........................................................214
Keywords ................................................37,80,267
K mode ......................................................50,54
L
LEFT$ ..........................................................91
LEN ........................................................86,285
LET ................................................60,271,293,314
LINE ..................................120,160,174,177,273,292,298
Line feed .................................................113,318
Line numbers .............................................34,36,60
LIST ................................................58,62,271,293
Listing .................................................35,58,294
LLIST .................................................178,271,294
L mode .........................................................51
LN .....................................................94,271,285
LOAD ...................................41,156,159,170,205,272,294
Loading a BASIC program ................................42,159,170
Loading software ............................................20,21
LocoScript ....................................................219
Logarithmic function ...........................................94
Logical expressions ...........................................105
Looping ........................................................70
LPRINT ........................................168,176,182,291,295
M
Machine code ..............................................204,264
Mains plug ...............................................10,13,15
Maintenance .....................................................7
Mathematical operations ................................92,282,287
Memory ............................................185,188,228,279
Menus .......................................................16,18
MERGE .........................................159,169,173,272,295
MID$ ...........................................................91
MIDI ..............................................147,187,296,322
MIDI socket ...............................................279,322
Monitor ...................................................140,319
MOVE ..................................................164,274,295
Music .........................................................140
N
Nesting ........................................................72
NEW ................................................63,150,158,295
NEXT ...................................................71,270,295
Noise .....................................................147,280
NOT .......................................................105,285
O
OPEN ..................................................182,272,296
Opening menu ...................................................16
OR ........................................................105,285
OUT .......................................................185,296
OVER ..........................................126,133,273,296,297
Overprinting ..........................................125,127,133
P
PAPER .............................................125,273,296,297
Parabola ......................................................130
PAUSE .................................................136,271,296
PEEK ..............................................111,189,271,285
Peripherals ...................................................317
PI .........................................................94,285
Pixels ....................................................117,130
PLAY ..........................................140,273,296,320,322
PLOT ..................................................130,271,296
POINT .....................................................133,285
POKE ..............................................111,189,271,296
Ports .....................................................176,317
Power indicator lamp ........................................13,15
Power supply unit .......................................8,9,15,18
Precautions .....................................................8
PRINT .......................................60,64,116,182,271,297
Printer .......................................158,174,176,186,317
PRINTER socket ........................................176,182,317
Procrustean assignment .........................................84
Pseudo-random ..............................................98,129
PSU .....................................................8,9,15,18
PSU socket .....................................................10
Q
Quotes ...................................................65,82,88
R
Radians ........................................................97
RAM ...........................................158,185,188,228,279
RAMdisk ...................................................150,169
RAMTOP ........................................................194
RANDOMIZE ..............................................99,271,297
Random numbers .................................................98
READ ...................................................77,271,298
Read/write indicator lamp .............................20,30,31,45
Recursion ......................................................76
Relational operators ...............................69,105,113,287
REM ........................................................63,298
Renaming files ................................................163
Renumbering a program ...................................34,62,303
Reports .......................................46,54,59,63,226,270
RESET button ..........................................14,16,20,24
Resetting the computer ......................................20,24
RESTORE ....................................................78,298
RETURN .................................................75,271,298
RGB/PERITEL socket ............................................320
RIGHT$ .........................................................91
RND ....................................................98,129,286
ROM ...............................................185,188,228,279
Rounding numbers ............................................88,90
RS232 .................................................176,186,321
RS232 socket ..................................177,182,279,318,321
RUN ...........................................36,61,64,65,271,298
S
Safety ....................................................8,10,18
SAVE .......................................39,152,160,171,272,298
Saving a program ...............................38,152,160,171,205
Screen display ............................33,35,54,58,130,162,179
SCREEN$ ...........................116,162,168,174,286,291,294,298
Scrolling ............................................58,64,73,121
Semicolon ..................................................64,297
Serial interface ..................................... 177,186,321
Servicing .......................................................7
Setting up .....................................................10
SGN ........................................................88,286
Sign .......................................................88,286
SIN ....................................................96,130,286
Sine wave .....................................................130
Slicing ................................................83,104,282
Software ..................................................5,20,21
Sound ................................................ 140,279,320
Source file ...................................................166
Speakers ......................................................140
SPECTRUM ...................................50,169,195,214,291,298
Spectrum 48 .............................................23,49,214
SQR ................................................89,130,271,286
Square root ....................................................89
Stack ......................................................75,205
STEP .......................................................71,292
STOP ................................................69,76,271,298
Stopping a program .......................................63,65,66
STR$ .......................................................87,286
Streams ...........................................158,174,182,297
String expressions ............................65,80,82,83,103,282
Subroutines ....................................................75
Subscript .................................................101,271
Substring ..................................................83,282
Switching on/off ..........................................8,13,18
SYMB SHIFT key ...........................................35,50,56
Syntax error ...................................................54
System status files ...........................................165
System variables ..........................................191,198
T
TAB ...................................................118,181,297
TAN ........................................................97,286
Tape operations .........................................21,38,170
TAPE/SOUND socket ........................................ 140,320
Test signal ..............................................13,15,16
THEN ...................................................68,105,292
Timing ....................................................136,279
TL$ ............................................................91
TO ..................................71,83,104,164,274,283,290,295
Tokens .........................................50,108,112,117,178
Transparent ...................................................125
Trigonometrical functions ......................................92
Troubleshooting ................................................15
TRUE VIDEO key ................................................293
Tuning-in TV ................................................13,15
TV ..................................................10,13,126,319
TV socket ......................................................10
U
ULA ...........................................................279
Unpacking .......................................................9
User area/number ..............................................153
User defined function ......................................89,315
User defined graphics ..................................57,110,134
USR ...............................................111,205,271,286
V
VAL ....................................................87,272,287
VAL$ ...................................................88,272,287
Variables ...............................61,80,159,191,271,282,314
VDU .......................................................140,319
VERIFY .................................................41,272,298
Volume ..................................................13,15,320
W
Warnings ................................................8,323,325
Wildcards .................................................156,163
Write protection ........................................28,47,165
X
X-axis .........................................................96
X-coordinate ..............................................117,130
Y
Y-axis .........................................................96
Y-coordinate ..............................................117,130
Z
Z80 micro processor ...........................187,195,204,264,279