#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Seek and play times in KRIKzz's real-drive logs, beside the drive model's. Each log line is one 1/75 s exchange: the status the drive presented and the command the host sent (``cd-log/``, "cdd communication logs from real hardware"; docs/ validation.md). For every seek (command 4) and play (command 3) this finds where the pickup was (the last absolute-time report, report 0, before the command), where it was sent, and how many exchanges passed until the status first showed the state the command asks for (4 paused, 1 playing), with what the drive reported on the way. The model's count for the same move is printed beside it (``--model``): the board's drive played through the same log from power-on, so it starts where the model is, not where the drive was; the from/to columns are the hardware's. python scripts/cdd_seek_times.py [LOG ...] [--model DISC.cue] [--trace] """ from __future__ import annotations import argparse import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) from replay_cdd_log import LOGS, parse # noqa: E402 PREGAP = 150 def lba_of(nibbles: str) -> int: """Six BCD nibbles mm ss ff as an LBA (absolute time less two seconds).""" minutes = int(nibbles[0:2]) seconds = int(nibbles[2:4]) frames = int(nibbles[4:6]) return (minutes * 60 + seconds) * 75 + frames - PREGAP def events(lines: list[tuple[str, list[int] | None]]) -> list[dict]: found = [] position = None for i, (status, command) in enumerate(lines): if status[1] == "0" and status[0] in "01234" and status[2:8].isdigit(): position = lba_of(status[2:8]) if command is None or command[0] not in (3, 4): continue target = lba_of("".join(str(n) for n in command[2:8])) want = "1" if command[0] == 3 else "4" seen = [] arrived = None for j in range(i + 1, len(lines)): later = lines[j][0] seen.append(later) if later[0] == want: arrived = j - i break nxt = lines[j][1] if nxt is not None and nxt[0] in (1, 3, 4, 6, 7): break # another command takes over before it settles found.append({"line": i + 1, "code": command[0], "from": position, "to": target, "arrived": arrived, "seen": seen}) # fmt: skip return found def model_counts(disc_path: Path, lines: list[tuple[str, list[int] | None]]) -> dict[int, int]: """The model's exchanges from each seek or play command to its state, replayed as scripts/replay_cdd_log.py replays (from power-on, sending the log's commands).""" from segacd_python.cdd import CDD, PAUSED, PLAYING from segacd_python.disc import Disc drive = CDD(Disc.open(disc_path, ROOT / "runs/discs")) counts: dict[int, int] = {} waiting: tuple[int, int, int] | None = None # line, state wanted, exchanges so far for number, (_status, command) in enumerate(lines, 1): drive.block() if waiting is not None: line, want, so_far = waiting so_far += 1 if drive.status[0] == want: counts[line] = so_far waiting = None else: waiting = (line, want, so_far) if command is not None: drive.command[:] = command drive.send() if command[0] in (3, 4): waiting = (number, PLAYING if command[0] == 3 else PAUSED, 0) return counts def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("logs", nargs="*", type=Path) parser.add_argument("--model", type=Path, help="a disc for the model (Sonic CD (USA))") parser.add_argument("--trace", action="store_true", help="print the statuses on the way") args = parser.parse_args() paths = args.logs or sorted(p for p in LOGS.rglob("*.txt") if p.name != "readme.txt") print(f"{'log':42} {'line':>5} {'cmd':>4} {'from':>7} {'to':>7} {'hardware':>8} {'model':>6}") for path in paths: lines = parse(path) model = model_counts(args.model, lines) if args.model else {} name = str(path.relative_to(LOGS) if path.is_relative_to(LOGS) else path) for event in events(lines): print(f"{name[:42]:42} {event['line']:5d} {'play' if event['code'] == 3 else 'seek':>4} " f"{event['from'] if event['from'] is not None else '-':>7} {event['to']:7d} " f"{event['arrived'] if event['arrived'] is not None else '-':>8} " f"{model.get(event['line'], '-'):>6}") # fmt: skip if args.trace: print(" " + " ".join(event["seen"][:60])) return 0 if __name__ == "__main__": sys.exit(main())