# SPDX-License-Identifier: GPL-3.0-or-later """Run the C book's field log exercise, inspect its flushed disk and try its append edit.""" import argparse import os from pathlib import Path 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="write a PNG of the completed first survey") 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] example = root / "examples/cmoc/manual/fieldlog.c" blank = root / "docs/manuals/software-storage-and-peripherals/media/BLANK.DSK" env = dict(os.environ, VCC_CMOC_COMPAT="1", PATH=str(runtime / "bin") + os.pathsep + os.environ["PATH"]) with tempfile.TemporaryDirectory(prefix="vcc-field-log-") as tmp: work = Path(tmp) def run(*command, **kwargs): return subprocess.run(command, cwd=work, env=env, check=True, **kwargs) run("xcrun", "clang++", "-std=c++17", "-O2", "-mmacosx-version-min=14.0", "-I", root / "src", fixture / "run.cpp", core, "-o", "run") original = example.read_text() edit = 'fopen(LOG_NAME, "w")' assert original.count(edit) == 1, "The lesson's append edit no longer matches the source" (work / "append.c").write_text(original.replace(edit, 'fopen(LOG_NAME, "a")')) for opt in ("-O0", "-O2"): for src, binary in ((example, "LOG.BIN"), ("append.c", "APPEND.BIN")): run(runtime / "bin/cmoc", "--coco", opt, "-Werror", "--org=2800", "-o", binary, src, "-lvcccompat", "-ldecbfile") capture = args.capture and opt == "-O2" run("./run", roms, blank, "LOG.BIN", "APPEND.BIN", "capture" if capture else "check") for cpu in ("6809", "6309"): first, second = b"EARTH: BLUE SEA\r", b"MARS: RED RIDGE\r" for mode, prior in (("fresh", b""), ("append", first + second)): for stage, expected in (("checkpoint", prior + first), ("closed", prior + first + second)): disk = f"{cpu}-{mode}-{stage}.dsk" actual = run(runtime / "bin/writecocofile", "--stdout", disk, "FIELD.LOG", stdout=subprocess.PIPE).stdout if actual != expected: raise RuntimeError(f"{disk}: expected {expected!r}, got {actual!r}") print(f"PASS {opt}: fresh/append, independent disk read before/after close, protection, BASIC return", flush=True) if capture: run("sips", "-s", "format", "png", "log.ppm", "--out", args.capture.resolve()) if __name__ == "__main__": main()