We'll first give an example of what could go wrong (it's fortunately safe as an example) and then we'll take a detour and explain how the memory map itself is organised from a NextBASIC perspective before returning to PEEK, POKE and their variants. Type:

10 POKE 16384,"ABCabc"
20 CLS:a$=PEEK$ (16384,6)
40 PRINT a$

From what we've talked about thus far, the intention of the program is obvious (for now also never mind what line 10 does; we'll discuss it later). First we put the word ABCabc into address 16384 of the memory map. Then we try to extract it from the same memory location. RUN the program. What do you see? Certainly not ABCabc you were expecting. Now modify lines 20 and 30 and replace 16384 with 20000 in both lines and RUN the program again.

This is perhaps a contrived example but it shows what happens when you try to use memory that is also being used by something else. In this case, address 16384 is where the contents of the display is stored. After placing the string with POKE in address 16384, a CLS is executed which clears the display and the stored string at the same time.

Here is a trickier example:

10 LAYER 1,2
20 POKE 16384,255
30 POKE 24576,255
40 PRINT AT 1,0;"16384 = ";
   PEEK 16384
50 PRINT "24576 = "; PEEK
   24576

This program selects Layer 1,2 (HiRes) and then creates two solid and adjacent character sized lines into the display via the POKE commands in lines 20 and 30. Running the program, the results almost seem correct except the character sized line is only one character wide.

The POKE to 24576 did not go to the display in bank 5 because NextBASIC placed a different memory page in the memory map to cover the last half of bank 5.

Contrast with the following program that does all its PEEKs and POKEs to bank 5 (the BANK commands will be explained in more detail later). As we will see, PEEK and POKE into a 16K bank, is done using an offset into said bank. This means that the "address" range is 0 through 16383; Banks are only 16K long after all. Bank 5, which holds the display is normally placed at address 16384 in the memory map. Performing therefore a POKE into address 16384 is the same as POKE to offset 0 in bank 5. Likewise address 24576 corresponds to offset 8192 in bank 5.

10 LAYER 1,2
20 BANK 5 POKE 0,255:
30 BANK 5 POKE 8192,255
40 PRINT AT 1,0;"16384 = ";%
   BANK 5 PEEK 0
50 PRINT "24576 = ";% BANK 5
   PEEK 8192

This time, the POKE to 24756 (offset 8192) does go to bank 5 and you will see the solid line twice as wide as the first program.


ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.