|
@@ -0,0 +1,238 @@
|
|
|
|
|
+#!/usr/bin/env python3
|
|
|
|
|
+"""Run clean, source-bound TD synthesis, place/route, and bit generation."""
|
|
|
|
|
+
|
|
|
|
|
+from __future__ import annotations
|
|
|
|
|
+
|
|
|
|
|
+import argparse
|
|
|
|
|
+from datetime import datetime, timezone
|
|
|
|
|
+import hashlib
|
|
|
|
|
+import json
|
|
|
|
|
+import os
|
|
|
|
|
+from pathlib import Path
|
|
|
|
|
+import re
|
|
|
|
|
+import shutil
|
|
|
|
|
+import subprocess
|
|
|
|
|
+import sys
|
|
|
|
|
+import time
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
+PROJECT = ROOT / "products/6400-control/project.json"
|
|
|
|
|
+CLOSURE = ROOT / "products/6400-control/source-closure.json"
|
|
|
|
|
+TD_PROJECT = "td/prod2u_6400_front_back.prj"
|
|
|
|
|
+TD_FILES = (
|
|
|
|
|
+ "td/syn/prod2u_6400_front_back.tcl",
|
|
|
|
|
+ "td/phy/prod2u_6400_front_back.tcl",
|
|
|
|
|
+ "td/bitgen/prod2u_6400_front_back.tcl",
|
|
|
|
|
+)
|
|
|
|
|
+CONSTRAINT_FILES = (
|
|
|
|
|
+ "constraints/prod2u_6400_front_back.adc",
|
|
|
|
|
+ "constraints/prod2u_6400_front_back.sdc",
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def sha256(path: Path) -> str:
|
|
|
|
|
+ digest = hashlib.sha256()
|
|
|
|
|
+ with path.open("rb") as stream:
|
|
|
|
|
+ for chunk in iter(lambda: stream.read(1024 * 1024), b""):
|
|
|
|
|
+ digest.update(chunk)
|
|
|
|
|
+ return digest.hexdigest()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def git(*args: str) -> str:
|
|
|
|
|
+ return subprocess.check_output(["git", *args], cwd=ROOT, text=True).strip()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def input_manifest() -> dict[str, str]:
|
|
|
|
|
+ closure = json.loads(CLOSURE.read_text(encoding="utf-8"))
|
|
|
|
|
+ paths = [*closure["sources"], *CONSTRAINT_FILES, TD_PROJECT, *TD_FILES, str(PROJECT.relative_to(ROOT)), str(CLOSURE.relative_to(ROOT))]
|
|
|
|
|
+ missing = [item for item in paths if not (ROOT / item).is_file()]
|
|
|
|
|
+ if missing:
|
|
|
|
|
+ raise RuntimeError(f"source closure contains missing inputs: {missing}")
|
|
|
|
|
+ return {item: sha256(ROOT / item) for item in paths}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def run_stage(name: str, command: list[str], cwd: Path, env: dict[str, str]) -> dict[str, object]:
|
|
|
|
|
+ log_path = cwd / f"{name}.log"
|
|
|
|
|
+ started = time.monotonic()
|
|
|
|
|
+ with log_path.open("wb") as log:
|
|
|
|
|
+ completed = subprocess.run(command, cwd=cwd, env=env, stdout=log, stderr=subprocess.STDOUT, check=False)
|
|
|
|
|
+ duration = round(time.monotonic() - started, 3)
|
|
|
|
|
+ return {
|
|
|
|
|
+ "command": command,
|
|
|
|
|
+ "cwd": str(cwd),
|
|
|
|
|
+ "exit_code": completed.returncode,
|
|
|
|
|
+ "duration_seconds": duration,
|
|
|
|
|
+ "log": str(log_path),
|
|
|
|
|
+ "log_sha256": sha256(log_path),
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def first_int(text: str, patterns: tuple[str, ...]) -> int | None:
|
|
|
|
|
+ for pattern in patterns:
|
|
|
|
|
+ match = re.search(pattern, text, re.IGNORECASE | re.MULTILINE)
|
|
|
|
|
+ if match:
|
|
|
|
|
+ return int(match.group(1))
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def first_float(text: str, patterns: tuple[str, ...]) -> float | None:
|
|
|
|
|
+ for pattern in patterns:
|
|
|
|
|
+ match = re.search(pattern, text, re.IGNORECASE | re.MULTILINE)
|
|
|
|
|
+ if match:
|
|
|
|
|
+ return float(match.group(1))
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def parse_metrics(run_root: Path) -> dict[str, object]:
|
|
|
|
|
+ area_paths = [
|
|
|
|
|
+ run_root / "phy/prod2u_6400_front_back_phy.area",
|
|
|
|
|
+ run_root / "syn/prod2u_6400_front_back_gate.area",
|
|
|
|
|
+ ]
|
|
|
|
|
+ area = "\n".join(path.read_text(encoding="utf-8", errors="replace") for path in area_paths if path.is_file())
|
|
|
|
|
+ timing_path = run_root / "phy/prod2u_6400_front_back_pr.timing"
|
|
|
|
|
+ timing = timing_path.read_text(encoding="utf-8", errors="replace") if timing_path.is_file() else ""
|
|
|
|
|
+ return {
|
|
|
|
|
+ "total_luts": first_int(area, (r"^#Total_luts\s+(\d+)",)),
|
|
|
|
|
+ "lut": first_int(area, (r"^#lut\s+(\d+)",)),
|
|
|
|
|
+ "registers": first_int(area, (r"^#reg\s+(\d+)",)),
|
|
|
|
|
+ "dsp": first_int(area, (r"^#dsp\s+(\d+)",)),
|
|
|
|
|
+ "bram": first_int(area, (r"^#bram\s+(\d+)",)),
|
|
|
|
|
+ "mslice": first_int(area, (r"^#MSLICE\s+(\d+)", r"MSLICE\s*[:=]\s*(\d+)")),
|
|
|
|
|
+ "lslice": first_int(area, (r"^#LSLICE\s+(\d+)", r"LSLICE\s*[:=]\s*(\d+)")),
|
|
|
|
|
+ "packed_slices": first_int(area, (r"Packed(?:/Total)?\s+Slice\w*\s*[:=]\s*(\d+)",)),
|
|
|
|
|
+ "fmax_mhz": first_float(timing, (r"Fmax[^\n]*?([0-9]+(?:\.[0-9]+)?)\s*MHz", r"([0-9]+(?:\.[0-9]+)?)\s*MHz")),
|
|
|
|
|
+ "setup_slack_ns": first_float(timing, (r"(?:Setup|WNS)[^\n]*?(-?[0-9]+(?:\.[0-9]+)?)\s*ns",)),
|
|
|
|
|
+ "hold_slack_ns": first_float(timing, (r"Hold[^\n]*?(-?[0-9]+(?:\.[0-9]+)?)\s*ns",)),
|
|
|
|
|
+ "area_reports": [str(path) for path in area_paths if path.is_file()],
|
|
|
|
|
+ "timing_report": str(timing_path) if timing_path.is_file() else None,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def warning_audit(stage_records: dict[str, dict[str, object]]) -> dict[str, object]:
|
|
|
|
|
+ lines: list[str] = []
|
|
|
|
|
+ blocking: list[str] = []
|
|
|
|
|
+ blocker = re.compile(r"\b(error|fatal|timing violation|unrouted|not routed|setup failed|hold failed)\b", re.IGNORECASE)
|
|
|
|
|
+ noteworthy = re.compile(r"\b(warn(?:ing)?|error|fatal|unconstrained|unassigned|latch|loop)\b", re.IGNORECASE)
|
|
|
|
|
+ for record in stage_records.values():
|
|
|
|
|
+ log = Path(str(record["log"]))
|
|
|
|
|
+ for line in log.read_text(encoding="utf-8", errors="replace").splitlines():
|
|
|
|
|
+ if noteworthy.search(line):
|
|
|
|
|
+ compact = line.strip()
|
|
|
|
|
+ if compact and compact not in lines:
|
|
|
|
|
+ lines.append(compact)
|
|
|
|
|
+ if blocker.search(compact):
|
|
|
|
|
+ blocking.append(compact)
|
|
|
|
|
+ return {
|
|
|
|
|
+ "status": "FAIL" if blocking else "PASS",
|
|
|
|
|
+ "noteworthy_count": len(lines),
|
|
|
|
|
+ "blocking_count": len(blocking),
|
|
|
|
|
+ "blocking": blocking[:100],
|
|
|
|
|
+ "noteworthy": lines[:300],
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def main(argv: list[str] | None = None) -> int:
|
|
|
|
|
+ parser = argparse.ArgumentParser()
|
|
|
|
|
+ parser.add_argument("--run-root", type=Path)
|
|
|
|
|
+ args = parser.parse_args(argv)
|
|
|
|
|
+
|
|
|
|
|
+ td = shutil.which("td2026")
|
|
|
|
|
+ if td is None:
|
|
|
|
|
+ print("TD_MISSING: td2026 is not on PATH", file=sys.stderr)
|
|
|
|
|
+ return 4
|
|
|
|
|
+ dirty_before = git("status", "--porcelain")
|
|
|
|
|
+ if dirty_before:
|
|
|
|
|
+ print("TD_BLOCKED: source worktree must be clean", file=sys.stderr)
|
|
|
|
|
+ print(dirty_before, file=sys.stderr)
|
|
|
|
|
+ return 3
|
|
|
|
|
+
|
|
|
|
|
+ commit = git("rev-parse", "HEAD")
|
|
|
|
|
+ tree = git("rev-parse", "HEAD^{tree}")
|
|
|
|
|
+ stamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
|
|
|
|
|
+ run_id = f"6400-td-{stamp}-{commit[:8]}"
|
|
|
|
|
+ run_root = args.run_root or Path(os.environ.get("PROD2U_TD_RUN_ROOT", "/data/prod2u/cpld-runs")) / run_id
|
|
|
|
|
+ for stage in ("syn", "phy", "bitgen"):
|
|
|
|
|
+ (run_root / stage).mkdir(parents=True, exist_ok=False)
|
|
|
|
|
+
|
|
|
|
|
+ inputs = input_manifest()
|
|
|
|
|
+ manifest_bytes = json.dumps(inputs, sort_keys=True, separators=(",", ":")).encode()
|
|
|
|
|
+ version_probe = subprocess.run([td, "-version"], capture_output=True, text=True, check=False)
|
|
|
|
|
+ version_text = (version_probe.stdout + version_probe.stderr).strip()
|
|
|
|
|
+ env = os.environ.copy()
|
|
|
|
|
+ env["PROD2U_SOURCE_ROOT"] = str(ROOT)
|
|
|
|
|
+ env["PATH"] = str(ROOT / "tools") + os.pathsep + env.get("PATH", "")
|
|
|
|
|
+ stages: dict[str, dict[str, object]] = {}
|
|
|
|
|
+ tcl = {
|
|
|
|
|
+ "syn": ROOT / TD_FILES[0],
|
|
|
|
|
+ "phy": ROOT / TD_FILES[1],
|
|
|
|
|
+ "bitgen": ROOT / TD_FILES[2],
|
|
|
|
|
+ }
|
|
|
|
|
+ status = "PASS"
|
|
|
|
|
+ for name in ("syn", "phy", "bitgen"):
|
|
|
|
|
+ stages[name] = run_stage(name, [td, str(tcl[name])], run_root / name, env)
|
|
|
|
|
+ print(f"{name}: exit={stages[name]['exit_code']} duration_s={stages[name]['duration_seconds']}", flush=True)
|
|
|
|
|
+ if stages[name]["exit_code"] != 0:
|
|
|
|
|
+ status = "FAIL"
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ expected = {
|
|
|
|
|
+ "syn": run_root / "syn/prod2u_6400_front_back_gate.db",
|
|
|
|
|
+ "phy": run_root / "phy/prod2u_6400_front_back_pr.db",
|
|
|
|
|
+ "bitgen": run_root / "bitgen/prod2u_6400_front_back.bit",
|
|
|
|
|
+ }
|
|
|
|
|
+ missing = [name for name, path in expected.items() if name in stages and stages[name]["exit_code"] == 0 and not path.is_file()]
|
|
|
|
|
+ if missing:
|
|
|
|
|
+ status = "FAIL"
|
|
|
|
|
+ audit = warning_audit(stages)
|
|
|
|
|
+ if audit["status"] != "PASS":
|
|
|
|
|
+ status = "FAIL"
|
|
|
|
|
+ bitstream = expected["bitgen"]
|
|
|
|
|
+ receipt = {
|
|
|
|
|
+ "schema": "prod2u-6400-td-receipt-v1",
|
|
|
|
|
+ "run_id": run_id,
|
|
|
|
|
+ "status": status,
|
|
|
|
|
+ "generated_at": datetime.now(timezone.utc).isoformat(),
|
|
|
|
|
+ "commit": commit,
|
|
|
|
|
+ "tree": tree,
|
|
|
|
|
+ "branch": git("branch", "--show-current"),
|
|
|
|
|
+ "clean_before": not bool(dirty_before),
|
|
|
|
|
+ "clean_after": not bool(git("status", "--porcelain")),
|
|
|
|
|
+ "project": str(PROJECT.relative_to(ROOT)),
|
|
|
|
|
+ "project_sha256": sha256(PROJECT),
|
|
|
|
|
+ "source_closure": str(CLOSURE.relative_to(ROOT)),
|
|
|
|
|
+ "source_closure_sha256": sha256(CLOSURE),
|
|
|
|
|
+ "source_manifest_sha256": hashlib.sha256(manifest_bytes).hexdigest(),
|
|
|
|
|
+ "inputs": inputs,
|
|
|
|
|
+ "tool": {
|
|
|
|
|
+ "command": "td2026",
|
|
|
|
|
+ "resolved": td,
|
|
|
|
|
+ "version_probe_exit": version_probe.returncode,
|
|
|
|
|
+ "version": version_text,
|
|
|
|
|
+ "container_image_id": os.environ.get("PROD2U_CONTAINER_IMAGE_ID"),
|
|
|
|
|
+ "container_digest": os.environ.get("PROD2U_CONTAINER_DIGEST"),
|
|
|
|
|
+ },
|
|
|
|
|
+ "safety": {"opens_serial": False, "programs_hardware": False, "jtag": False},
|
|
|
|
|
+ "stages": stages,
|
|
|
|
|
+ "missing_outputs": missing,
|
|
|
|
|
+ "warning_audit": audit,
|
|
|
|
|
+ "metrics": parse_metrics(run_root),
|
|
|
|
|
+ "bitstream": str(bitstream) if bitstream.is_file() else None,
|
|
|
|
|
+ "bitstream_sha256": sha256(bitstream) if bitstream.is_file() else None,
|
|
|
|
|
+ }
|
|
|
|
|
+ receipt_path = run_root / "td-receipt.json"
|
|
|
|
|
+ receipt_path.write_text(json.dumps(receipt, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
|
|
|
+ print(receipt_path)
|
|
|
|
|
+ if status != "PASS":
|
|
|
|
|
+ for name, record in stages.items():
|
|
|
|
|
+ if record["exit_code"] != 0:
|
|
|
|
|
+ log = Path(str(record["log"]))
|
|
|
|
|
+ print(f"--- {name} log tail ---", file=sys.stderr)
|
|
|
|
|
+ print("\n".join(log.read_text(encoding="utf-8", errors="replace").splitlines()[-120:]), file=sys.stderr)
|
|
|
|
|
+ return 1
|
|
|
|
|
+ print(f"TD_PASS bitstream_sha256={receipt['bitstream_sha256']}")
|
|
|
|
|
+ return 0
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ raise SystemExit(main())
|