

MOST first-time programmers (and for that matter a great many programmers unfamiliar with PEEK and POKE) regard these two commands with dark foreboding. Often lack of interest in these useful functions comes about from the “if you don’t want to get burnt, don’t stick your fingers in unfamiliar territory” syndrome. Regarded as very mysterious, the PEEK command, as its name suggests, allows the programmer to look at the contents of a specified memory location. Its opposite, POKE, allows the programmer to place a value in a specified memory location, which will vary depending on available RAM; the greater the RAM the higher the number of memory (POKE) locations. Both commands have fixed formats composed of two variables — the memory address and the contents of that address (or location). The address variable (designated V in the following examples) must be an integer value in the range 0 to 65,535. The contents variable, called C, is also an integer variable and has the numeric range 0 to 255. The commands are used as follows:
LET C = PEEK V
which effectively loads the contents of memory address V into the variable location C and
POKE V, C
which places the value in variable C into memory location V.
On the T/S1000,ZX81 the monitor — the controlling machine code program stored in ROM, which controls the machine’s keyboard, display and permits programs to be written in BASIC — is located in the bottom 8K of memory, i.e. in the bottom 8192 locations. The RAM memory which is used to store user programs and data, starts at location 16384 and, if the machine has been expanded, can go up to 32767. The contents of both types of memory can be looked at with the PEEK command, but only the contents of RAM memory can be changed with the POKE statement.
If you have more than 3.5K of memory available, then the PEEK and POKE commands can be used to great effect in generating video displays. The video display on the T/S1000 being stored as a file of 24 character strings, each 32 characters long and terminated with an ENTER character (marker). This 793 byte long display file is stored in RAM memory at a starting address contained in locations 16396 and 16397 and an end address stored in 16400 and 16401. The following program will print out the first 21 bytes in ROM and their addresses:
10 PRINT "Address: "; TAB 8; " Byte"
20 FOR A = 0 TO 20
30 PRINT A; TAB 8; PEEK A
40 NEXT A
Complementing the previous program, the next will print out all of the ASCII character codes of the current contents of each location on the screen. Such a short utility program is particularly useful for organizing display data, especially when using machine code which is essentially the manipulation of address registers.
10 LET P = PEEK 16396 + 256 * PEEK 16397
20 LET Q = (PEEK 16400 + 256 * PEEK 16401) - 1
30 LET T = Q - P
40 DIM A(T)
50 FOR K = P TO Q
60 LET A(K-P) = PEEK(K)
70 NEXT K
80 FOR K = 1 TO T
90 PRINT A(K)
100 NEXT K
In kind with the above program, the following program permits the user to POKE characters onto the display. Just as the above program will give the ASCII codes of each character stored in the display file, we can use this next program to place characters on the display:
10 LET P 5 PEEK 16396 + 256 * PEEK 16397
20 LET Q = PEEK 16400 + 256 * PEEK 16401
30 FOR K = 1 TO 255
40 IF (K/33) - INT(K/33) = 0 THEN GOTO 60
50 POKE K + P,15
60 NEXT K
Change the value to be POKEd into memory in line 50 and the resulting screen display will change. Line 40 has been included to ‘protect’ the ENTER characters at the end of each line in the display file.
T/S2000
Memory organization on the T/S2000 is not as straightforward as the T/S1000; in fact, the whole memory appears, at first sight, to be a confusion of addresses. The apparent randomness of memory can be infuriating and extremely frustrating when constructing machine code programs. Where a memory-mapped display allows the character in a given screen position to be determined by a simple PEEK into memory, PEEKing into the T/S2000’s memory reveals no useful information as all is completely chaotic. The memory itself is divided into different ‘sections’, handling different functions and for storing varying types of information. These ‘sections’ are large enough only for the information they actually contain. If, by inserting more information in a given area, adding a further program line or variable, no more space is available, the computer ‘makes’ extra space by shifting up everything above that given area. Similarly, if information is deleted, everything is shifted downwards — the directions up and down referring to increasing or decreasing memory addresses.
The display file is that section which handles the television picture, the actual screen output. Each character position on the screen is composed of an 8 x 8 matrix of dots and each dot can be either 0 (paper) or 1 (ink), alternatively on and off. By using binary notation a pattern of dots can be stored as eight bytes, one for each row of dots. Herein lies the complexity of T/S2000 memory because these eight bytes are not stored together. The corresponding rows in the 32 characters of a single line are stored together as a scan of 32 bytes — the electron guns require this when scanning from left to right across the screen. Since the complete picture has 24 lines of eight scans each, you would expect the 172 scan total to be stored in order, one after the other. You would be misled to think so! First come the top scans of lines 0 to 7, then the next scans of lines 0 to 7 and so on to the bottom scans of lines 0 to 7. The same rulings apply to lines 8 to 15 and then for lines 16 to 23.
A little experimentation reveals that the eight bytes forming the character at the top left-hand corner of the screen are held in memory addresses 16384, 16640, 16896, 17152, 17408, 17664, 17920 and 18176, with 16384 starting the display memory area and the others being obtained by adding successive 256s. For example:
Address = 16384 + 256*(n-1) + 32*x + y = 16128 + 256*n + 32*x + y
The address contains the nth byte of the character in line x, column y. But, as mentioned, because of the odd layout, this equation fails after eight screen lines have been looked into with an entirely new block starting 2048 addresses after the first. So:
Address = 18176 + 256*n + 32*(n-8) + y = 17920 + 256*n + 32*x + y
Again, after a total of 16 lines a further new block commences:
Address = 20224 + 256*n + 32*(n-16) + y = 19712 + 256*n + 32*x + y
Obviously therefore, when writing machine code programs or constructing user graphics with PEEK/POKE commands, care has to be taken to determine exactly where characters will be stored so that characters that are to manipulated are, in fact, those and not others elsewhere on the screen.