Debug runs and persistent media

Use a command file to step through a FORTRAN program, inspect variables and media, and save tapes and drums for a later run. The FORTRAN AST and GW-BASIC interpreter implement the same scripted line monitor, with native inspectors. The v1 contract defines commands, stops and inspection checkpoints. GUI views and restorable execution checkpoints remain future work in the joint plan.

Run the example

Run from the repository root with an unused trace.txt path. Change that argument if a previous run left a log; the debug log is create-only:

./fortran704 --debug-script examples/debug-media/commands.txt --debug-output trace.txt --save-media state.f704m --export-text 1 exchange.txt examples/debug-media/write.f
./fortran704 --load-media state.f704m examples/debug-media/read.f

The first run writes tapes and a drum, stops before STOP, prints watches and media views to trace.txt, saves media and exports two ASCII text records. The second run prints 7, 1.25, 1.25 from the saved binary tape/drum. The media and text output paths are atomically replaced on a successful STOP. Debug output never goes to the line printer.

Optionally, with the GW-BASIC build in the sibling repository, read the exported text records:

../gw-basic-2026/build/gwbasic examples/debug-media/read.bas

BASIC prints HELLO and FORTRAN 7.

A failed compilation or load may leave a debugger log. Use a new log path for the corrected run. Path preflight briefly reserves absent output names with exclusive empty files to detect filesystem aliases, including case folding and dangling links; it removes those reservations before execution. Existing files are never opened for this check. Errors writing the final STOP notice on stderr are ignored after media is saved. Earlier diagnostics, including PAUSE, retain the process’s inherited SIGPIPE handling. Printer output on stdout must succeed before media is saved.

Native FORTRAN inspection

watch A, inspect I and inspect A(2,3) read the stopped unit’s bindings. Values show the real octal physical address, undefined status, raw 12-digit octal word and labelled real/integer/BCD interpretations. The latter are views of the same bits, not type conversions performed on guest storage. A ? in the BCD view means no printable mapping was found. COMMON, EQUIVALENCE and dummy arguments report the same address when they refer to the same cell.

After inspect, the lowercase words flags, card, media, tape and drum select the built-in inspectors. Use watch FLAGS or inspect FLAGS to inspect a variable with a reserved name. Longer names such as TAPES remain variable references.

Additional inspectors:

Command

View

inspect flags

Arithmetic flags, sense lights and switches, without clearing them

inspect card

Compiled source card, up to 80 columns with a ruler

inspect media

Tape record counts/cursors and drum geometry

inspect tape UNIT RECORD

Zero-based record, kind, length and cursor; at most 64 BCD bytes or 16 binary words

inspect drum UNIT ADDRESS COUNT

Decimal address and 1–64 octal words, bounded to addresses 0–2047

Card views retain the text captured by the parser, so editing the source during a pause does not change the view. They show text, not fabricated punch holes. No mechanical timing, drum rotation or tape speed is simulated. Inspectors never move a head, read a guest input record, allocate a symbol or change arithmetic flags.

Media lifecycle and text exchange

--load-media FILE loads all ten tapes and eight drums at run start. --save-media FILE saves them on successful STOP, including tape positions, empty BCD/binary records, marks and all 2048 words per drum. Existing dialect rules, including FORTRAN 0 drum boundaries and later dialect wrap, still apply. Without these options media starts empty and lives only for the process. This is media persistence, not a program resume: variables and execution frames start afresh. Loading and saving the same image is supported.

The loader checks the entire image, checksum, bounds, kinds and 36-bit packing before installing it. Corrupt/truncated images fail before program execution. Failures before the commit stage preserve both destinations. STOP flushes and checks printer output before saving media. Output is written to a same-directory temporary file, flushed and fsynced, then renamed. Each output is atomic independently; text and media together are not a multi-file transaction. The text export commits first, so a later bundle rename failure can leave the new text file in place. New/replaced outputs have owner-only permissions (0600). File fsync is used; crash-durable directory publication is not promised. Conflicting source/script/debug/media/text paths are rejected before opening the debug log, including BSS input decks. Preflight compares current path identities; concurrent retargeting of directories/symlinks is outside this local single-writer contract.

--export-text UNIT FILE exports the tape from record zero, without moving its cursor. Each BCD record becomes one LF-terminated line; printable ASCII bytes 0x20–0x7e are preserved exactly. No trimming or floating conversion occurs. Empty records become empty lines. One final tape mark may terminate the file; it is omitted from the text. Binary records, embedded/multiple marks, records after a mark, nonprintable bytes and records longer than BASIC’s 255-byte string capacity are rejected. The native FORMAT writer normally limits BCD records to 120 characters. See the checked-in examples/debug-media pair. This is text interchange with this BASIC implementation, not a cassette image or proof of binary numeric compatibility with Microsoft GW-BASIC.

Format choice

The existing SIMH standard format carries byte records and marks, with zero record length denoting a mark. Its live cursor is drive state. Extended SIMH allows private classes, but preserving our typed empty records and all device state would require private metadata or a sidecar. These facts come from the SIMH tape specification and Open SIMH’s representation guide. P7B embeds six data bits, parity and start-of-record in bytes, with an EOF code; see the SIMH definitions.

The first persistence format is therefore an explicitly private interpreter bundle, with a documented wire layout. SIMH/P7B adapters can follow separately; a bundle must not be renamed .tap or presented as an emulator interchange image. This keeps cursor, type and drum semantics exact without pretending that a BASIC byte file is an IBM device.

F704M01 wire layout

All integers are unsigned big-endian. A word uses five bytes, whose high four bits must be zero. There is no C-structure dump or host floating representation.

  1. Eight-byte magic: ASCII F704M01 followed by LF.

  2. For units 1–10: 32-bit record count, 32-bit cursor (0..count), then records.

  3. Each record: 8-bit kind (1 BCD, 2 binary, 3 mark), 32-bit length, payload. BCD length counts preserved host bytes; binary length counts five-byte words. A mark has length zero. Empty BCD and binary records remain distinct.

  4. Drums 1–8 in order: 2048 five-byte words each, ascending address.

  5. 32-bit CRC-32/ISO-HDLC of every preceding byte (the algorithm used by Python zlib.crc32). No trailing bytes.

Limits: 64 MiB including header/checksum; 100,000 tape records in total. CRC is an accidental-corruption check, not authentication. Arbitrary BCD bytes survive native round-trip; the stricter printable-ASCII rule applies to text export. The current FORTRAN executable and atomic writer target POSIX hosts. DOS16/32 validation applies to the separate BASIC monitor, not this media implementation.

Validation

make test includes tests/test_debug_media.py, which checks:

  • Independent wire images, round trips, cursors, marks and empty records.

  • Corrupt or truncated images and preservation of existing files on failure.

  • Separate debugger and program I/O, calls, aliases and repeated checkpoints.

  • Unchanged dialect behaviour, devices and flags during inspection.

  • Text exchange through the companion BASIC runtime’s LINE INPUT#.

Set GWBASIC_BINARY when the BASIC executable is elsewhere. The cross-runtime test explicitly skips when that binary is absent; the other tests remain local.