|IN and OUT|
The processor can read from and (at least with RAM) write to memory by using PEEK and POKE. The processor itself does not really care whether memory is ROM, RAM or even nothing at all; it just knows 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 they can be controlled from the BASIC by using the IN function and the OUT statement.
IN is a function like PEEK.
It has one argument, the port address, and its result is a byte read from that port.
OUT is a statement like POKE.
OUT address, value
writes the given value to the port with the given address. How the address is interpreted depends very much on the rest of the computer; quite often, many different addresses will mean the same. On the Spectrum it is most sensible to imagine the address being written in binary, because the individual bits tend to work independently. There are 16 bits, which we shall call (using A for address)
A15, A14, A13, A12, . . . . . . , A2, Al, A0
Here A0 is the 1s bit, Al the 2s bit, A2 the 4s bit and so on. Bits A0, Al, A2, A3 and A4 are the important ones. They are normally 1, but if any one of them is 0 this tells the computer to do something specific. The computer cannot cope with more than one thing at a time, so no more than one of these five bits should be 0. Bits A6 and A7 are ignored, so if you are a wizard with electronics you can use them yourself. The best addresses to use are those that are 1 less than a multiple of 32, so that A0,...A4 are all 1. Bits A8, A9 and so on are sometimes used to give extra information.
The byte read or written has 8 bits, and these are often referred to (using D for data) as D7, D6,...., Dl, D0. Here is a list of the port addresses used.
There is a set of input addresses that read the keyboard and also the EAR socket.
The keyboard is dividecl up into 8 half rows of 5 keys each.
IN 65278 reads the half row CAPS SHIFT to V
IN 65022 reads the half row A to G
IN 64510 reads the half row Q to T
IN 63486 reads the half row 1 to 5
IN 61438 reads the half row O to 6
IN 57342 reads the half row P to 7
IN 49150 reads the half row ENTER to H
IN 32766 reads the half row SPACE to B
(These addresses are 254+256*(255-21 n) as n goes from 0 to 7.)
In the byte read in, bits D0 to D4 stand for the five keys in the given half row - D0 for the outside key, D4 for the one nearest the middle. The bit is 0 if the key is pressed, 1 if it is not. D6 is the value at the EAR socket.
Port address 254 in output drives the loudspeaker (C4) and the MIC socket (D3), and also sets the border colour (D2, D1 and D0).
Port address 251 runs the printer, both in reading and writing: reading finds out whether the printer is ready for more, and writing sends out dots to be printed.
Port addresses 254, 247 and 239 are used for the extra devices mentioned in Chapter 22.
Run this program
10 FOR n=0 TO 7: REM half-row number
20 LET a=254+256*(255-2 n)
30 PRINT AT 0,0; IN a: GO TO 30
and play around by pressing keys. When you get bored with each half-row, press BREAK and then type
The control, data and address busses are all exposed at the back of the Spectrum, so you can do almost anything with a Spectrum that you can with a Z80. Sometimes, though, the Spectrum hardware might get in the way. Here is a diagram of the exposed connections at the back: