The memory map, is divided into eight slots of 8K named MMU0 through MMU7 and the physical memory is broken into 96 8K banks1. Placing a specific 8K bank n into the address range 0 to 8191, we might say that 8K bank n has been written to MMU0.
Since NextBASIC exposes physical memory banks using the Standard scheme's 16K size, we'll concentrate only on this. More information on using the ZX Spectrum Next's MMU system can be found at the end of this chapter, in other sources such as the Spectrum Next Wiki at wiki.specnext.dev and in Volume 2 – Advanced ZX Spectrum Next programming of this manual.
In the normal course of operations, NextZXOS and NextBASIC read and write memory on your behalf. As it has been demonstrated in previous chapters, we sometimes need to examine the memory's contents or directly modify it. For these cases NextBASIC provides a series of commands and functions to examine and modify memory both in the memory map as well as in the whole of the physical memory. These are all variations of two main keywords, namely the PEEK (and PEEK$) functions (to read the contents of memory) and the POKE command (to alter the contents of memory). The full list follows:
| Command | Description |
|---|---|
PEEK addr |
Reads the byte at address addr |
POKE addr,v |
Changes the contents of address addr to the byte value v |
DPEEK addr |
Reads the word stored at addresses starting at addr (addr, addr+1) |
DPOKE addr, v |
Changes the contents of addresses starting at addr (addr, addr+1) to contain the 16 bit value v |
PEEK$ (addr, len/t) |
Reads memory region of length len stored in the addresses beginning with addr and stores it in a string –or– Reads the string terminated with a user specified terminator t beginning with address addr |
POKE addr, s |
Writes a string s in the addresses beginning with addr |
BANK n PEEK o |
Reads the byte at offset o in bank n |
BANK n POKE o, v |
Changes the contents in bank n at offset o to value v |
BANK n DPEEK o |
Reads the word stored in bank n at offset o (o, o+1) |
BANK n DPOKE o, v |
Changes the contents of bank n starting at offset o (o, o+1) to contain the 16 bit value v |
BANK n PEEK$ (o,len/t) |
Reads a region of length len stored in bank n beginning at offset o and stores it in a string –or– Reads the string terminated with a user specified terminator t from bank n beginning at offset o |
BANK n POKE o, s |
Writes a string s in bank n beginning at offset o |
Table 22 – PEEK and POKE variants
As you can see from the table above, NextBASIC provides us with a wealth of options to manipulate the contents of both the 64K memory map and the physical memory as a whole. These, complemented by the extended options provided by the BANK command, which we will examine further below, can cover almost any memory manipulation need that may arise in the course of writing a program.
Before we continue further with examination of PEEK, PEEK$ and POKE, let's first begin with a warning of sorts: Usage of the non BANK variants is extremely discouraged. Instead it's best, if you use their BANK variants at all times. The reason for that is two-fold and goes back to Memory Banking.
Let's explain; as we said earlier NextZXOS and NextBASIC update portions of the memory map like the system variables or the display memory if need be. What this means, is that you can't really be sure a value you POKEd into the memory map will be there when you try to recover it with PEEK unless you take some measures first2.
Furthermore, POKEing into the memory map unless you absolutely know what you're doing, can have unintended consequences which could result in crashing the machine.
1 224 in a fully expanded ZX Spectrum Next 2: Refer to the CLEAR statement further down this chapter
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.