# SPDX-License-Identifier: GPL-3.0-or-later """Cold-boot the Book 6 Atlas backup after an interruption between renames.""" import argparse import os from pathlib import Path import shutil import subprocess import tempfile def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("runtime", type=Path) parser.add_argument("core", type=Path) parser.add_argument("roms", type=Path) parser.add_argument("--capture", type=Path, help="save the actual recovery screen as a PNG") args = parser.parse_args() runtime, core, roms = [p.resolve(strict=True) for p in (args.runtime, args.core, args.roms)] fixture = Path(__file__).resolve().parent root = fixture.parents[2] blank = root / "docs/manuals/software-storage-and-peripherals/media/BLANK.DSK" bin_dir = runtime / "bin" tool = bin_dir / "writecocofile" env = dict(os.environ, VCC_CMOC_COMPAT="1", PATH=str(bin_dir) + os.pathsep + os.environ["PATH"]) # Existing visits to Earth and Mars must survive both the save and recovery. names = [b"MERCURY: CRATERS", b"VENUS: CLOUDS", b"EARTH: BLUE SEA", b"MARS: RED RIDGE"] original = b"".join(name.ljust(23, b"\0") + flag for name, flag in zip(names, (b"\0", b"\0", b"V", b"V"))) updated = original[:72] + b"MARS: DUST STORM".ljust(23, b"\0") + original[-1:] note = b"ANOTHER CREW'S FILE\r" with tempfile.TemporaryDirectory(prefix="vcc-atlas-backup-") as tmp: work = Path(tmp) card = work / "card" card.mkdir() disk = card / "VOL.DSK" (card / "startup.cfg").write_text("0=VOL.DSK\n") result = work / "result.dsk" def run(*command, **kwargs): return subprocess.run(command, cwd=work, env=env, check=True, **kwargs) def add(dsk, filename, contents): (work / filename).write_bytes(contents) run(tool, "--binary", dsk, filename) def check_files(expected): for filename in ("ATLAS.DAT", "ATLAS.BAK", "ATLAS.NEW", "CREW.TXT"): extraction = subprocess.run([tool, "--stdout", result, filename], cwd=work, capture_output=True) if filename in expected: if extraction.returncode or extraction.stdout != expected[filename]: raise AssertionError(f"Wrong contents for {filename}: {extraction.stderr!r} {extraction.stdout!r}") elif not extraction.returncode or b"not found on disk image" not in extraction.stdout + extraction.stderr: raise AssertionError(f"Expected absent {filename}: {extraction.stdout!r} {extraction.stderr!r}") def boot(cpu, profile, phase, image): shutil.copyfile(image, disk) result.unlink(missing_ok=True) run("./run", roms, "BACKUP.BIN", cpu, profile, phase) run("xcrun", "clang++", "-std=c++17", "-O2", "-mmacosx-version-min=14.0", "-I", root / "src", fixture / "run.cpp", core, "-o", "run") for opt in ("-O0", "-O2"): run(bin_dir / "cmoc", "--coco", opt, "-Werror", "--org=2800", "-o", "BACKUP.BIN", root / "examples/cmoc/manual/atlasbackup.c", "-lvcccompat", "-ldecbfile") seed = work / "seed.dsk" shutil.copyfile(blank, seed) run(tool, seed, "BACKUP.BIN") add(seed, "CREW.TXT", note) fresh = work / "fresh.dsk" shutil.copyfile(seed, fresh) other_draft = work / "other-draft.dsk" shutil.copyfile(seed, other_draft) add(other_draft, "ATLAS.BAK", original) # Looks like the correct edition, but the Mars visit flag differs. unrelated = updated[:-1] + b"\0" add(other_draft, "ATLAS.NEW", unrelated) malformed = work / "malformed.dsk" shutil.copyfile(seed, malformed) add(malformed, "ATLAS.BAK", original[:-1]) add(malformed, "ATLAS.NEW", updated) add(seed, "ATLAS.DAT", original) for cpu in ("6809", "6309"): for profile in ("disk", "rgb-hd", "sdc"): boot(cpu, profile, "interrupt", seed) check_files({"ATLAS.BAK": original, "ATLAS.NEW": updated, "CREW.TXT": note}) capture = args.capture and (opt, cpu, profile) == ("-O2", "6809", "disk") boot(cpu, profile, "capture" if capture else "recover", result) check_files({"ATLAS.DAT": original, "CREW.TXT": note}) if capture: run("sips", "-s", "format", "png", "recovered.ppm", "--out", args.capture.resolve()) boot(cpu, profile, "save", result) check_files({"ATLAS.DAT": updated, "ATLAS.BAK": original, "CREW.TXT": note}) saved = result.read_bytes() boot(cpu, profile, "repeat", result) if result.read_bytes() != saved: raise AssertionError("Reopening a completed save changed the disk") boot(cpu, profile, "refuse", malformed) if result.read_bytes() != malformed.read_bytes(): raise AssertionError("An invalid backup caused disk changes") boot(cpu, profile, "keep-draft", other_draft) check_files({"ATLAS.DAT": original, "ATLAS.NEW": unrelated, "CREW.TXT": note}) if profile == "disk": boot(cpu, profile, "save", fresh) unvisited = b"".join(name.ljust(24, b"\0") for name in names) check_files({"ATLAS.DAT": unvisited[:72] + b"MARS: DUST STORM".ljust(24, b"\0"), "ATLAS.BAK": unvisited, "CREW.TXT": note}) print(f"PASS {cpu} {opt} {profile}: interrupted, recovered, saved, reopened; " "whole atlas, visit flags and unrelated file preserved", flush=True) if __name__ == "__main__": main()