# SPDX-License-Identifier: MIT """Replay KRIKzz's hardware CDD logs against the drive model, exchange by exchange. Each log line is ``status - command``: what a real drive presented in one 1/75 s exchange and what the host sent in it. The model is given a block time, its status is compared with the log's, and it is sent the log's command. The disc must be the one the logs were made with (Sonic CD, USA). python scripts/replay_cdd_log.py DISC.cue-or-zip [LOG ...] [--show N] [--as-shown] Most logs begin at power-on; six begin mid-session (playing, paused, or with the tray open), which a replay from power-on cannot match. ``--as-shown`` starts the playing and paused ones where their first lines show the drive. """ from __future__ import annotations import argparse import sys from pathlib import Path from segacd_python.cdd import CDD, PAUSED, PLAYING from segacd_python.disc import Disc LOGS = Path("reference/src/mcd-verificator/mcd-verificator/cd-log") def parse(path: Path) -> list[tuple[str, list[int] | None]]: lines = [] for line in path.read_text(errors="replace").splitlines(): parts = line.split() if len(parts) < 3 or parts[1] != "-": continue status = parts[0].replace(".", "") command = parts[2].replace(".", "") nibbles = None if "FF" in command[:2] else [int(c, 16) for c in command] lines.append((status, nibbles)) return lines def _position(status: str) -> int | None: """The LBA a report-0 status shows (absolute time less two seconds), or None.""" if status[1] != "0" or not status[2:8].isdigit(): return None m, s, f = int(status[2:4]), int(status[4:6]), int(status[6:8]) return (m * 60 + s) * 75 + f - 150 def start_as_shown(drive: CDD, lines: list[tuple[str, list[int] | None]]) -> str: """Put the model where a log that begins mid-session shows the drive: playing or paused, the disc read, at the place its first absolute times give (before the first command), showing the report type of its first line. A log that begins at power-on ("0F" or the first exchange's "00"/"80"...), or with the tray open, is left to start from power-on. A measuring aid only (the board is not changed); the paused loop's phase is not recovered.""" first = lines[0][0] state = int(first[0], 16) if state not in (PLAYING, PAUSED) or first[1] == "F": return "power-on" seen = [] for k, (status, command) in enumerate(lines, 1): p = _position(status) if p is not None: seen.append((k, p)) if command is not None and command[0] not in (0, 2): break if not seen: return "power-on" drive.ready_in = 0 drive.report = int(first[1], 16) if int(first[1], 16) <= 2 else drive.report if state == PLAYING: k, p = seen[0] # The exchange of line k shows the sector after the one it played. drive.lba = p - k drive.state = PLAYING return f"playing at {p} (line {k})" end = max(p for _, p in seen) drive.loop_end = drive.target = end drive.lba = end - 8 drive.state = PAUSED return f"paused, loop ending {end}" def replay(disc: Disc, path: Path, show: int, as_shown: bool = False) -> tuple[int, int]: drive = CDD(disc) wrong = 0 lines = parse(path) if as_shown: print(f" starts {start_as_shown(drive, lines)}") for number, (status, command) in enumerate(lines, 1): drive.block() ours = "".join(f"{n:X}" for n in drive.status) if ours != status: wrong += 1 if wrong <= show: print(f" line {number}: hardware {status} model {ours}") if command is not None: drive.command[:] = command drive.send() return wrong, len(lines) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("disc", type=Path) parser.add_argument("logs", nargs="*", type=Path) parser.add_argument("--show", type=int, default=5) parser.add_argument("--cache", type=Path, default=Path("runs/discs")) parser.add_argument("--as-shown", action="store_true", help="start a log that begins mid-session (playing or paused) " "where its first lines show the drive, not from power-on") # fmt: skip args = parser.parse_args() disc = Disc.open(args.disc, args.cache) total = wrong_total = 0 for path in args.logs or sorted(LOGS.rglob("*.txt")): if path.name == "readme.txt": continue print(path.relative_to(LOGS) if path.is_relative_to(LOGS) else path) wrong, count = replay(disc, path, args.show, args.as_shown) print(f" {count - wrong} of {count} exchanges identical") total += count wrong_total += wrong print(f"all: {total - wrong_total} of {total} identical") return 0 if __name__ == "__main__": sys.exit(main())