test_6400_td_parser.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. from __future__ import annotations
  2. import importlib.util
  3. import json
  4. from pathlib import Path
  5. import pytest
  6. ROOT = Path(__file__).resolve().parents[2]
  7. SCRIPT = ROOT / "tools/run_6400_td.py"
  8. SPEC = importlib.util.spec_from_file_location("run_6400_td", SCRIPT)
  9. assert SPEC is not None and SPEC.loader is not None
  10. run_6400_td = importlib.util.module_from_spec(SPEC)
  11. SPEC.loader.exec_module(run_6400_td)
  12. def test_td_synthesis_uses_source_root_for_generated_includes() -> None:
  13. script = (ROOT / "td/syn/prod2u_6400_front_back.tcl").read_text(encoding="utf-8")
  14. closure = json.loads(
  15. (ROOT / "products/6400-control/source-closure.json").read_text(encoding="utf-8")
  16. )
  17. expected_verilog = [path for path in closure["sources"] if path.endswith(".v")]
  18. assert "analyze -file [src $relative] -include_dir $source_root" in script
  19. assert [f"analyze_src {{{path}}}" for path in expected_verilog] == [
  20. line for line in script.splitlines() if line.startswith("analyze_src {")
  21. ]
  22. def test_td_source_closure_keeps_fan_adapter_before_production_top() -> None:
  23. script = (ROOT / "td/syn/prod2u_6400_front_back.tcl").read_text(encoding="utf-8")
  24. closure = json.loads(
  25. (ROOT / "products/6400-control/source-closure.json").read_text(encoding="utf-8")
  26. )
  27. adapter = "src/actuator/fan_board_output_adapter.v"
  28. top = "src/top/prod2u_6400_v1_top.v"
  29. assert adapter in closure["sources"]
  30. assert closure["sources"].index(adapter) < closure["sources"].index(top)
  31. assert script.count(f"analyze_src {{{adapter}}}") == 1
  32. assert script.index(f"analyze_src {{{adapter}}}") < script.index(
  33. f"analyze_src {{{top}}}"
  34. )
  35. def test_identification_td_uses_expanded_profile_closure_and_distinct_top() -> None:
  36. script = (ROOT / "td/syn/prod2u_6400_identification.tcl").read_text(
  37. encoding="utf-8"
  38. )
  39. expected_sources, closure_inputs = run_6400_td.source_closure(
  40. "6400-identification"
  41. )
  42. expected_verilog = [path for path in expected_sources if path.endswith(".v")]
  43. assert [f"analyze_src {{{path}}}" for path in expected_verilog] == [
  44. line for line in script.splitlines() if line.startswith("analyze_src {")
  45. ]
  46. assert "elaborate -top {prod2u_6400_identification_top}" in script
  47. assert closure_inputs == [
  48. "products/6400-identification/source-closure.json",
  49. "products/6400-control/source-closure.json",
  50. ]
  51. def test_td_hardware_profiles_keep_roles_and_artifacts_unambiguous() -> None:
  52. profiles = run_6400_td.HARDWARE_PROFILES
  53. assert set(profiles) == {"6400-control", "6400-identification"}
  54. assert "6400-sim-debug" not in profiles
  55. assert profiles["6400-control"]["production"] is True
  56. assert profiles["6400-control"]["stimulus"] is False
  57. assert profiles["6400-identification"]["production"] is False
  58. assert profiles["6400-identification"]["stimulus"] is True
  59. assert profiles["6400-control"]["artifact_basename"] != profiles[
  60. "6400-identification"
  61. ]["artifact_basename"]
  62. manifest = run_6400_td.input_manifest("6400-identification")
  63. assert "src/top/prod2u_6400_identification_top.v" in manifest
  64. assert "td/prod2u_6400_identification.prj" in manifest
  65. assert "td/bitgen/prod2u_6400_identification.tcl" in manifest
  66. receipt_fields = run_6400_td.profile_receipt_fields("6400-identification")
  67. assert receipt_fields == {
  68. "profile": "6400-identification",
  69. "firmware_role": "identification",
  70. "production": False,
  71. "stimulus": True,
  72. "simulation_only": False,
  73. "artifact_basename": "prod2u_6400_identification",
  74. }
  75. bitgen = (ROOT / "td/bitgen/prod2u_6400_identification.tcl").read_text(
  76. encoding="utf-8"
  77. )
  78. assert "bitgen -bit prod2u_6400_identification.bit" in bitgen
  79. assert "prod2u_6400_front_back.bit" not in bitgen
  80. def test_td_cli_rejects_sim_debug_hardware_bit() -> None:
  81. with pytest.raises(SystemExit) as exc_info:
  82. run_6400_td.main(["--profile", "6400-sim-debug"])
  83. assert exc_info.value.code == 2
  84. def test_parse_metrics_uses_routed_slacks_slices_and_lut_fallback(tmp_path: Path) -> None:
  85. (tmp_path / "syn").mkdir()
  86. (tmp_path / "phy").mkdir()
  87. (tmp_path / "bitgen").mkdir()
  88. (tmp_path / "syn/prod2u_6400_front_back_gate.area").write_text(
  89. "#lut 321\n#reg 222\n#bram 3\n#dsp 1\n",
  90. encoding="utf-8",
  91. )
  92. (tmp_path / "phy/prod2u_6400_front_back_phy.area").write_text("", encoding="utf-8")
  93. (tmp_path / "phy/prod2u_6400_front_back_pr.timing").write_text(
  94. """SWNS: 12.500ns, STNS: 0.000ns
  95. HWNS: 0.250ns, HTNS: 0.000ns
  96. Fmax: 100.000MHz
  97. Max: SWNS 12.500ns, 0 Viol Endpoints, 10 Total Endpoints
  98. Min: HWNS 0.250ns, 0 Viol Endpoints, 10 Total Endpoints
  99. """,
  100. encoding="utf-8",
  101. )
  102. (tmp_path / "syn/syn.log").write_text("packed: 9 mslices, 11 lslices\n", encoding="utf-8")
  103. (tmp_path / "phy/phy.log").write_text("routed: 7 mslices, 8 lslices\n", encoding="utf-8")
  104. metrics = run_6400_td.parse_metrics(tmp_path)
  105. assert metrics["total_luts"] == 321
  106. assert metrics["total_luts_source"] == "#lut_fallback"
  107. assert metrics["mslice"] == 7
  108. assert metrics["lslice"] == 8
  109. assert metrics["packed_slices"] == 15
  110. assert metrics["setup_slack_ns"] == 12.5
  111. assert metrics["hold_slack_ns"] == 0.25
  112. assert metrics["violation_endpoint_counts"] == [0, 0]
  113. def _stage_records(log: Path) -> dict[str, dict[str, object]]:
  114. return {"syn": {"log": str(log)}}
  115. def test_warning_audit_allows_only_the_two_frozen_ttl_unknowns(tmp_path: Path) -> None:
  116. log = tmp_path / "syn.log"
  117. log.write_text(
  118. """SYN-6021 CRITICAL-WARNING: 2 out of 43 logical ports has no user assigned specific location constraint.
  119. Bitgen will abort unless unconstr_io is enabled.
  120. No ADC ports: host_ttl_rx, host_ttl_tx.
  121. RUN-1001 : No hold violation.
  122. """,
  123. encoding="utf-8",
  124. )
  125. audit = run_6400_td.warning_audit(_stage_records(log))
  126. assert audit["status"] == "PASS"
  127. assert audit["observed_unbound_ports"] == ["host_ttl_rx", "host_ttl_tx"]
  128. assert audit["blocking_count"] == 0
  129. def test_warning_audit_rejects_any_additional_unbound_port(tmp_path: Path) -> None:
  130. log = tmp_path / "syn.log"
  131. log.write_text(
  132. "No ADC ports: host_ttl_rx, host_ttl_tx, mystery_output.\n",
  133. encoding="utf-8",
  134. )
  135. audit = run_6400_td.warning_audit(_stage_records(log))
  136. assert audit["status"] == "FAIL"
  137. assert audit["unexpected_unbound_ports"] == ["mystery_output"]
  138. def test_warning_audit_keeps_real_td_errors_blocking(tmp_path: Path) -> None:
  139. log = tmp_path / "syn.log"
  140. log.write_text(
  141. """No ADC ports: host_ttl_rx, host_ttl_tx.
  142. PDS-9001 ERROR: routing failed
  143. """,
  144. encoding="utf-8",
  145. )
  146. audit = run_6400_td.warning_audit(_stage_records(log))
  147. assert audit["status"] == "FAIL"
  148. assert any("routing failed" in line for line in audit["blocking"])