Skip to content

pfile2bas / bas2pfile

Two companions for Sinclair ZX81 .P program files (emulator saves):

  • pfile2bas — converts a .P file into a readable ASCII BASIC listing.
  • bas2pfile — converts such a listing back into a loadable .P file. Together they allow editing ZX81 programs in any text editor and round-tripping them byte-exactly (see FORMAT.md for the exact listing conventions and the few documented, accepted losses).

Derived from zx81pfile.c by Miguel Angel Rodriguez Jodar, 2013 (GPL v3 or later), rewritten 2026 with robust header detection, structural validation and proper command line handling.

Features

  • pfile2bas: converts ZX81 .P saves into readable ASCII BASIC listings — structurally validated (record walk, not trusting D_FILE), robust against truncated or damaged files (warning + partial listing, hard error for non-.P data).
  • bas2pfile: converts listings back into loadable, byte-exact .P files (expanded display file, no autostart — fixed, not configurable).
  • Lossless round trip per FORMAT.md; documented, accepted losses only.
  • ZX81 block graphics map to Unicode Block Elements, exactly like zxdream renders them; inverse text in backtick spans, [NN] hex escapes for REM machine code, "" for embedded quotes.
  • Optional -f/--frames rewrite of the FRAMES-counter idioms to the zxdream FRAMES function — context-aware (string literals, text REMs and [NN] dumps stay untouched).
  • Tape names via -n (1–10 chars, last byte with bit 7 set, like SAVE).
  • C, no dependencies; Unity test suite with ASan/UBSan; Makefile with install/uninstall/zip targets, PREFIX/DESTDIR support.

Build

make            # release build (cc, -O2)
make debug      # debug build with ASan/UBSan (pfile2bas-test)
make test       # run the test suite against the sanitizer build
make clean

Install

Default PREFIX depends on the running user: /usr/local for root, ~/.local otherwise. Both PREFIX and DESTDIR (staged installs) can be overridden:

make install PREFIX=/opt/zx81
make install DESTDIR=/tmp/stage
make uninstall

Usage

pfile2bas [OPTION]... FILE.p [FILE.p...]
  • -o, --output FILE — write the listing to FILE instead of stdout (only valid with a single input file)
  • -f, --frames — rewrite the four FRAMES-counter read idioms (PEEK 16436+PEEK 16437*256 and friends) to the zxdream FRAMES function. Context-aware: identical text inside string literals, text REMs and [NN] machine-code dumps stays untouched. Off by default.
  • -h, --help — show help
  • -V, --version — show version

With several input files the listings are concatenated on stdout. The exit status is 0 only if all files were converted; invalid or damaged inputs are reported on stderr with a non-zero exit status.

pfile2bas game.p                 # listing to stdout
pfile2bas -o game.bas game.p     # listing to a file
pfile2bas examples/*.p           # several files at once

Usage (bas2pfile)

bas2pfile [OPTION]... FILE.bas [FILE.bas...]
  • -o, --output FILE — write the .P image to FILE (default: stdout)
  • -n, --name NAME — prepend a tape name (1–10 characters, last one stored with bit 7 set, as the ZX81 SAVE does)
  • -h, --help / -V, --version

Input is the listing produced by pfile2bas (see FORMAT.md): Unicode block graphics, backtick-delimited inverse text (`HELLO` = inverse HELLO), [NN] hex escapes for REM machine code, and "" for embedded quotation marks. The output always contains an expanded display file and does not autostart (NXTLIN = 0x405D) — these are fixed, not configurable.

bas2pfile -o game.p game.bas       # listing back to a .P file
bas2pfile -n GAME -o game.p game.bas

Notes on the .P format

A .P file is a memory image starting at address 0x4009 (VERSN), optionally preceded by the ZX81 program name (1–10 characters, the last one with bit 7 set). The BASIC program area always starts at image offset 0x74 (= address 0x407D). Each program line is stored as:

  • line number, 2 bytes, big-endian
  • line length, 2 bytes, little-endian (including the trailing ENTER byte 0x76)
  • line body, terminated by 0x76 (ENTER)

The converter walks this structure, so it does not have to trust the D_FILE system pointer. When the walked program end disagrees with D_FILE (e.g. a truncated file), a warning is printed and the partial listing is still produced. Files that are not ZX81 programs at all (plain ASCII text, empty or random data) are rejected with an error.

Characters without an ASCII equivalent are handled as follows: ZX81 block graphics map to Unicode Block Elements (▘▝▀▖▌▞▛▒ and the inverse complements █▟▙▄▜▐▚▗), exactly like zxdream renders them. Note that byte 0x80 (inverse space) is a solid black block — power-pl.p's strings like "ZX█81" really do contain it. The four half-dither characters (black/dither and white/dither mixes) have no Unicode equivalent and are substituted with the gray dithered block ▒. Inverse text characters (0x8B–0xBF) render as their base character inside backtick-delimited spans: a run of inverse text is enclosed in single backticks (`HELLO` = inverse HELLO). Inverse block graphics glyphs may appear inside an open span; normal graphics close it. Example from power-pl.p: "ZX█81" (inverses Z, X, inverse space, inverses 8, 1). Inside a REM that appears to contain machine code, bytes are dumped as [XX] hex — same behaviour as the original tool.

License

GPL v3 or later — see https://www.gnu.org/licenses/gpl-3.0.html. This project is derived from zx81pfile.c by Miguel Angel Rodriguez Jodar (GPL v3 or later), so it cannot be MIT licensed.

Contact

devel ät datenbrei.de

Website & Download