| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 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-control/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-control/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_identification_td_uses_expanded_profile_closure_and_distinct_top() -> None:
- script = (ROOT / "td/syn/prod2u_6400_identification.tcl").read_text(
- encoding="utf-8"
- )
- expected_sources, closure_inputs = run_6400_td.source_closure(
- "6400-identification"
- )
- expected_verilog = [path for path in expected_sources if path.endswith(".v")]
- assert [f"analyze_src {{{path}}}" for path in expected_verilog] == [
- line for line in script.splitlines() if line.startswith("analyze_src {")
- ]
- assert "elaborate -top {prod2u_6400_identification_top}" in script
- assert closure_inputs == [
- "products/6400-identification/source-closure.json",
- "products/6400-control/source-closure.json",
- ]
- def test_td_hardware_profiles_keep_roles_and_artifacts_unambiguous() -> None:
- profiles = run_6400_td.HARDWARE_PROFILES
- assert set(profiles) == {"6400-control", "6400-identification"}
- assert "6400-sim-debug" not in profiles
- assert profiles["6400-control"]["production"] is True
- assert profiles["6400-control"]["stimulus"] is False
- assert profiles["6400-identification"]["production"] is False
- assert profiles["6400-identification"]["stimulus"] is True
- assert profiles["6400-control"]["artifact_basename"] != profiles[
- "6400-identification"
- ]["artifact_basename"]
- manifest = run_6400_td.input_manifest("6400-identification")
- assert "src/top/prod2u_6400_identification_top.v" in manifest
- assert "td/prod2u_6400_identification.prj" in manifest
- assert "td/bitgen/prod2u_6400_identification.tcl" in manifest
- receipt_fields = run_6400_td.profile_receipt_fields("6400-identification")
- assert receipt_fields == {
- "profile": "6400-identification",
- "firmware_role": "identification",
- "production": False,
- "stimulus": True,
- "simulation_only": False,
- "artifact_basename": "prod2u_6400_identification",
- "hardware_host_interfaces": ["host_rs232"],
- }
- bitgen = (ROOT / "td/bitgen/prod2u_6400_identification.tcl").read_text(
- encoding="utf-8"
- )
- assert "bitgen -bit prod2u_6400_identification.bit" in bitgen
- assert "prod2u_6400_front_back.bit" not 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"])
|