from __future__ import annotations import importlib.util import json from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[2] SCRIPT = ROOT / "tools/run_6400_td.py" SPEC = importlib.util.spec_from_file_location("run_6400_td", SCRIPT) assert SPEC is not None and SPEC.loader is not None run_6400_td = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(run_6400_td) def test_td_synthesis_uses_source_root_for_generated_includes() -> None: script = (ROOT / "td/syn/prod2u_6400_front_back.tcl").read_text(encoding="utf-8") closure = json.loads( (ROOT / "products/6400-front-rear/source-closure.json").read_text(encoding="utf-8") ) expected_verilog = [path for path in closure["sources"] if path.endswith(".v")] assert "analyze -file [src $relative] -include_dir $source_root" in script assert [f"analyze_src {{{path}}}" for path in expected_verilog] == [ line for line in script.splitlines() if line.startswith("analyze_src {") ] def test_td_source_closure_keeps_fan_adapter_before_production_top() -> None: script = (ROOT / "td/syn/prod2u_6400_front_back.tcl").read_text(encoding="utf-8") closure = json.loads( (ROOT / "products/6400-front-rear/source-closure.json").read_text(encoding="utf-8") ) adapter = "src/actuator/fan_board_output_adapter.v" top = "src/top/prod2u_6400_v1_top.v" assert adapter in closure["sources"] assert closure["sources"].index(adapter) < closure["sources"].index(top) assert script.count(f"analyze_src {{{adapter}}}") == 1 assert script.index(f"analyze_src {{{adapter}}}") < script.index( f"analyze_src {{{top}}}" ) def test_td_hardware_profile_is_single_front_rear_product() -> None: profiles = run_6400_td.HARDWARE_PROFILES assert set(profiles) == {"6400-front-rear"} profile = profiles["6400-front-rear"] assert profile["production"] is True assert profile["stimulus"] is False manifest = run_6400_td.input_manifest("6400-front-rear") assert "src/top/prod2u_6400_rs232_only_top.v" in manifest assert "td/prod2u_6400_front_back.prj" in manifest assert "td/bitgen/prod2u_6400_front_back.tcl" in manifest receipt_fields = run_6400_td.profile_receipt_fields("6400-front-rear") assert receipt_fields == { "profile": "6400-front-rear", "firmware_role": "production", "production": True, "stimulus": False, "simulation_only": False, "artifact_basename": "prod2u_6400_front_back", "hardware_host_interfaces": ["host_rs232"], } bitgen = (ROOT / "td/bitgen/prod2u_6400_front_back.tcl").read_text( encoding="utf-8" ) assert "bitgen -bit prod2u_6400_front_back.bit" in bitgen assert "unconstr_io" not in bitgen def test_td_cli_rejects_sim_debug_hardware_bit() -> None: with pytest.raises(SystemExit) as exc_info: run_6400_td.main(["--profile", "6400-sim-debug"]) assert exc_info.value.code == 2 def test_parse_metrics_uses_routed_slacks_slices_and_lut_fallback(tmp_path: Path) -> None: (tmp_path / "syn").mkdir() (tmp_path / "phy").mkdir() (tmp_path / "bitgen").mkdir() (tmp_path / "syn/prod2u_6400_front_back_gate.area").write_text( "#lut 321\n#reg 222\n#bram 3\n#dsp 1\n", encoding="utf-8", ) (tmp_path / "phy/prod2u_6400_front_back_phy.area").write_text("", encoding="utf-8") (tmp_path / "phy/prod2u_6400_front_back_pr.timing").write_text( """SWNS: 12.500ns, STNS: 0.000ns HWNS: 0.250ns, HTNS: 0.000ns Fmax: 100.000MHz Max: SWNS 12.500ns, 0 Viol Endpoints, 10 Total Endpoints Min: HWNS 0.250ns, 0 Viol Endpoints, 10 Total Endpoints """, encoding="utf-8", ) (tmp_path / "syn/syn.log").write_text("packed: 9 mslices, 11 lslices\n", encoding="utf-8") (tmp_path / "phy/phy.log").write_text("routed: 7 mslices, 8 lslices\n", encoding="utf-8") metrics = run_6400_td.parse_metrics(tmp_path) assert metrics["total_luts"] == 321 assert metrics["total_luts_source"] == "#lut_fallback" assert metrics["mslice"] == 7 assert metrics["lslice"] == 8 assert metrics["packed_slices"] == 15 assert metrics["setup_slack_ns"] == 12.5 assert metrics["hold_slack_ns"] == 0.25 assert metrics["violation_endpoint_counts"] == [0, 0] def _stage_records(log: Path) -> dict[str, dict[str, object]]: return {"syn": {"log": str(log)}} def test_warning_audit_requires_zero_unbound_ports_for_rs232_only_top( tmp_path: Path, ) -> None: log = tmp_path / "syn.log" log.write_text( "RUN-1001 : No hold violation.\n", encoding="utf-8", ) audit = run_6400_td.warning_audit(_stage_records(log)) assert audit["status"] == "PASS" assert audit["observed_unbound_ports"] == [] assert audit["blocking_count"] == 0 def test_warning_audit_rejects_any_unbound_port(tmp_path: Path) -> None: log = tmp_path / "syn.log" log.write_text( "No ADC ports: host_ttl_rx, host_ttl_tx.\n", encoding="utf-8", ) audit = run_6400_td.warning_audit(_stage_records(log)) assert audit["status"] == "FAIL" assert audit["unexpected_unbound_ports"] == ["host_ttl_rx", "host_ttl_tx"] def test_warning_audit_keeps_real_td_errors_blocking(tmp_path: Path) -> None: log = tmp_path / "syn.log" log.write_text( "PDS-9001 ERROR: routing failed\n", encoding="utf-8", ) audit = run_6400_td.warning_audit(_stage_records(log)) assert audit["status"] == "FAIL" assert any("routing failed" in line for line in audit["blocking"])