test_6400_td_parser.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. "hardware_host_interfaces": ["host_rs232"],
  75. }
  76. bitgen = (ROOT / "td/bitgen/prod2u_6400_identification.tcl").read_text(
  77. encoding="utf-8"
  78. )
  79. assert "bitgen -bit prod2u_6400_identification.bit" in bitgen
  80. assert "prod2u_6400_front_back.bit" not in bitgen
  81. assert "unconstr_io" not in bitgen
  82. def test_td_cli_rejects_sim_debug_hardware_bit() -> None:
  83. with pytest.raises(SystemExit) as exc_info:
  84. run_6400_td.main(["--profile", "6400-sim-debug"])
  85. assert exc_info.value.code == 2
  86. def test_parse_metrics_uses_routed_slacks_slices_and_lut_fallback(tmp_path: Path) -> None:
  87. (tmp_path / "syn").mkdir()
  88. (tmp_path / "phy").mkdir()
  89. (tmp_path / "bitgen").mkdir()
  90. (tmp_path / "syn/prod2u_6400_front_back_gate.area").write_text(
  91. "#lut 321\n#reg 222\n#bram 3\n#dsp 1\n",
  92. encoding="utf-8",
  93. )
  94. (tmp_path / "phy/prod2u_6400_front_back_phy.area").write_text("", encoding="utf-8")
  95. (tmp_path / "phy/prod2u_6400_front_back_pr.timing").write_text(
  96. """SWNS: 12.500ns, STNS: 0.000ns
  97. HWNS: 0.250ns, HTNS: 0.000ns
  98. Fmax: 100.000MHz
  99. Max: SWNS 12.500ns, 0 Viol Endpoints, 10 Total Endpoints
  100. Min: HWNS 0.250ns, 0 Viol Endpoints, 10 Total Endpoints
  101. """,
  102. encoding="utf-8",
  103. )
  104. (tmp_path / "syn/syn.log").write_text("packed: 9 mslices, 11 lslices\n", encoding="utf-8")
  105. (tmp_path / "phy/phy.log").write_text("routed: 7 mslices, 8 lslices\n", encoding="utf-8")
  106. metrics = run_6400_td.parse_metrics(tmp_path)
  107. assert metrics["total_luts"] == 321
  108. assert metrics["total_luts_source"] == "#lut_fallback"
  109. assert metrics["mslice"] == 7
  110. assert metrics["lslice"] == 8
  111. assert metrics["packed_slices"] == 15
  112. assert metrics["setup_slack_ns"] == 12.5
  113. assert metrics["hold_slack_ns"] == 0.25
  114. assert metrics["violation_endpoint_counts"] == [0, 0]
  115. def _stage_records(log: Path) -> dict[str, dict[str, object]]:
  116. return {"syn": {"log": str(log)}}
  117. def test_warning_audit_requires_zero_unbound_ports_for_rs232_only_top(
  118. tmp_path: Path,
  119. ) -> None:
  120. log = tmp_path / "syn.log"
  121. log.write_text(
  122. "RUN-1001 : No hold violation.\n",
  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"] == []
  128. assert audit["blocking_count"] == 0
  129. def test_warning_audit_rejects_any_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.\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"] == ["host_ttl_rx", "host_ttl_tx"]
  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. "PDS-9001 ERROR: routing failed\n",
  142. encoding="utf-8",
  143. )
  144. audit = run_6400_td.warning_audit(_stage_records(log))
  145. assert audit["status"] == "FAIL"
  146. assert any("routing failed" in line for line in audit["blocking"])