Other port addresses

As seen in the table at the beginning of this chapter and the discussion about decoding, all even addresses refer to ULA functions. You may find yourself in need to read the keyboard directly from the hardware. As mentioned, part of the ULA's function is to return the state of keypresses. The keyboard is divided in 8 half-rows of 5 keys each, each half-row having its own port address5.

Address
Decimal / Hex
BitsAddress
Decimal / Hex
D0D1D2D3D4D4D3D2D1D0
63486 / F7FEh123456789061438 / EFFEh
64510 / FBFEhQWERTYUIOP57342 / DFFEh
65022 / FDFEhASDFGHJKLEnter49150 / BFFEh
65278 / FEFEhCapsZXCVBNMSymSpace32766 / 7FFEh

The diagram above neatly illustrates how the keyboard matrix is separated into half-rows (demarcated by the thick line in the middle). Pay attention to how bits are mirrored going from the outside of the keyboard to the inside.

The address of each half-row in the diagram is calculated as: 254 + 256*(255 - 2ⁿ).

In the formula above, n is the number of half-row which starts with 0 at the bottom right and moves in a counterclockwise manner with each successive half-row increasing by 1.

In the byte read in, bits D0 to D4 stand for each of the five keys in the given half-row – D0 for the outside key and D4 for the one nearest the middle. The bit is 0 if the key is pressed and 1 if it is not.

For example to find the value of the CAPS SHIFT key, you can do:

PRINT %IN 65278 & @1

Writing a value using OUT to the ULA (Port 254 / FEh) controls other hardware as well. You can drive the beeper with D4, the MIC socket with D3, read the EAR socket with D6 and modify the BORDER colour using bits D0,D1 and D2. For example to make the border a nice magenta colour you can:

OUT 254, %@00000011

Port addresses 32765 (7FFDh), 8189 (1FFDh) and 57341 (DFFDh) control the extra memory. Executing an OUT to these ports from NextBASIC without knowing the ramifications will nearly always cause the computer to crash, losing any program and data. These ports are write-only, i.e. 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. Check the last section in this chapter as well as Chapter 23 – The Memory where we examine the banking system in detail.

Writing to port 65533 (FFFDh) will select a particular PSG register (on the AY sound chip) and writing to port 49149 (BFFDh) will send a particular value to that register. Reading from port 65533 (FFFDh) returns the value stored in the selected register. Judicious use of these two registers can allow sounds to be generated while NextBASIC gets on with something else.

The section that follows describes all ZX Spectrum Next – specific hardware ports; addressing them is via OUT and IN commands.

5 Extended keys are combinations of tther keys, so they need to be read as those key combinations. For example EXTEND is CAPS SHIFT + SYMBOL SHIFT.


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.