What's in a ZX81 .P file?
A ZX81 program file is a snapshot of the computer's memory. Here's how to read one, and why you can't just open it in a text editor.
Open a ZX81 .P file in a text editor and you'll see a mess. That's not because it's compressed or encrypted. The file is a raw copy of the machine's memory and not a readable text file. This guide explains what's inside, and what ZX Viewer does to turn it back into a readable listing.
New to $4009-style hex numbers or "byte order"? The glossary explains them from scratch.
A file that is a memory snapshot
When the ZX81 saved a program, it didn't write out "the program". It wrote out a whole region of RAM, from the system variables at address 16393 ($4009) up to the end of the BASIC variables. Loading it back puts everything exactly where it was. That's why a loaded program can carry on with its variables already set.
| Address | Contents |
|---|---|
16393–16508 | System variables: pointers to where the display, variables and editing line start, and so on |
16509 onwards | The BASIC program, line by line |
| then | The display file (the screen) |
| then | BASIC variables, up to the end of the file |
So a .P file's first byte corresponds to address 16393, and the program always starts 116 bytes into the file. To find where the program ends, a viewer reads the system variable that points to the display file. That's the first thing ZX Viewer does.
The ZX81 character set
Sinclair designed its own 64-character set to fit the ZX81's tiny ROM. A few landmarks:
| Code | Character | ASCII would be |
|---|---|---|
$00 | space | $20 |
$0B | " | $22 |
$1C–$25 | 0–9 | $30–$39 |
$26–$3F | A–Z | $41–$5A |
$80–$BF | Inverse (white-on-black) versions of $00–$3F, shown in the chart below | none |
The full character set
All 64 characters, as the ZX81 showed them. The hex code is shown under each one. The first ten after the space are block graphics for drawing pictures.
Normal, codes $00–$3F
- 00
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 0A
- "0B
- £0C
- $0D
- :0E
- ?0F
- (10
- )11
- >12
- <13
- =14
- +15
- -16
- *17
- /18
- ;19
- ,1A
- .1B
- 01C
- 11D
- 21E
- 31F
- 420
- 521
- 622
- 723
- 824
- 925
- A26
- B27
- C28
- D29
- E2A
- F2B
- G2C
- H2D
- I2E
- J2F
- K30
- L31
- M32
- N33
- O34
- P35
- Q36
- R37
- S38
- T39
- U3A
- V3B
- W3C
- X3D
- Y3E
- Z3F
Inverse, codes $80–$BF: the same characters with ink and paper swapped
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 8A
- "8B
- £8C
- $8D
- :8E
- ?8F
- (90
- )91
- >92
- <93
- =94
- +95
- -96
- *97
- /98
- ;99
- ,9A
- .9B
- 09C
- 19D
- 29E
- 39F
- 4A0
- 5A1
- 6A2
- 7A3
- 8A4
- 9A5
- AA6
- BA7
- CA8
- DA9
- EAA
- FAB
- GAC
- HAD
- IAE
- JAF
- KB0
- LB1
- MB2
- NB3
- OB4
- PB5
- QB6
- RB7
- SB8
- TB9
- UBA
- VBB
- WBC
- XBD
- YBE
- ZBF
To get an inverse character, add $80 to the normal code, which sets the byte's top bit. So A is $26 and inverse A is $A6. Programmers used inverse text for titles and highlights. Inverse space, $80, is a solid black square, which made it handy for drawing.
There are no lower-case letters at all. Codes from $C0 upwards are keyword tokens: the ZX81 stored each BASIC keyword as a single byte, and you typed it with a single key press.
How a BASIC line is stored
Each program line has the same layout:
+--------+--------+---------------------------+--------+
| line # | length | tokens and characters | $76 |
| 2 bytes| 2 bytes| ... | NEWLINE|
+--------+--------+---------------------------+--------+
Watch the byte order. The line number is stored high byte first, but the length is low byte first, the normal Z80 order. The length counts everything after itself, including the final $76.
Worked example: 10 PRINT "HI"
00 0A 06 00 F5 0B 2D 2E 0B 76
10 6 PRINT " H I " NEWLINE
$F5 is the PRINT token. $0B is the quote mark. H and I are $2D and $2E, counting up from A at $26.
And here's how that line, plus the 20 GOTO 10 from the next section, looks when you LIST it on a ZX81:
10 PRINT "HI"
20 GOTO 10
Shown in the ZX81 VDU font, which recreates the ZX81's screen characters (see credits).
The hidden numbers
This is the part that surprises people. Whenever a number appears in a BASIC line, the ZX81 stores it twice. First come the digits as you typed them, then a marker byte $7E, then the same value as a 5-byte floating-point number. Here's 20 GOTO 10:
00 14 0A 00 EC 1D 1C 7E 84 20 00 00 00 76
20 10 GOTO 1 0 num 10 as a float NEWLINE
The ZX81 did this for speed. When the program runs, the interpreter skips the slow text-to-number conversion and uses the 5 hidden bytes directly. The listing, though, shows only the digits. So the number you see and the number the computer uses can differ. Some programmers used this trick to hide how a program worked, or as simple copy protection.
When you're reading a listing to understand a program, it's worth knowing that this can happen. If a line seems to do something odd, the hidden value may be the explanation.
Machine code in a REM
The ZX81's BASIC had no easy place to put machine code. So programmers hid it inside a REM statement, which BASIC ignores. If the first line of a program is 1 REM, followed by bytes that look like nonsense, those bytes are almost certainly Z80 code. Because the program always starts at 16509, code in the first REM always starts at 16514. That's why so many ZX81 programs include RAND USR 16514, the command that runs it.
ZX Viewer's Z80 mode disassembles those bytes using the correct start address, so jumps and calls point where they really go.
What about the ZX80?
The ZX80 came a year earlier, in 1980. Its program files (often .o) work in a similar way, but with differences. The snapshot starts at $4000, and its integer-only BASIC has its own token table. It doesn't store hidden floating-point numbers. That's why ZX Viewer has separate ZX80 and ZX81 modes.
Try it yourself
- Open Hitch Hiker in ZX Viewer and read the listing.
- Switch to
DUMPand find the first line at offset 116. Its line number comes high byte first. - Browse the ZX81 Stuff archive for programs with a
1 REMline, and look at them in Z80 mode.
Further reading: the Sinclair BASIC tokenized file entry on the Archive Team wiki, and Ryan Gray's zx81-utils.