test_startup_close_seek.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Contract tests for the startup close-seek feature.
  2. These tests verify that project.json, generated parameters, the source manifest,
  3. and the top-level netlist all agree on the startup configuration. RTL behavior is
  4. covered by tests/sim/startup_close_seek_tb.v, tests/formal/startup_close_seek.sby,
  5. and tests/cocotb after toolchains are available.
  6. """
  7. from __future__ import annotations
  8. import json
  9. import re
  10. import unittest
  11. from pathlib import Path
  12. ROOT = Path(__file__).resolve().parents[2]
  13. PROJECT_PATH = ROOT / "projects" / "prod2u-door-temp" / "project.json"
  14. PARAMS_PATH = ROOT / "src" / "generated" / "prod2u_door_temp_params.vh"
  15. TOP_PATH = ROOT / "src" / "top" / "prod2u_p4_normal_v3_top.v"
  16. SOURCE_MANIFEST_PATH = ROOT / "src" / "generated" / "td-source-manifest.json"
  17. class StartupCloseSeekContractTests(unittest.TestCase):
  18. def test_project_json_has_enabled_close_startup(self):
  19. project = json.loads(PROJECT_PATH.read_text(encoding="utf-8"))
  20. limits = project["limits"]
  21. self.assertTrue(limits["functional_use"])
  22. startup = limits["startup"]
  23. self.assertTrue(startup["enabled"])
  24. self.assertEqual(startup["direction_meaning"], "close")
  25. self.assertEqual(startup["burst_step_count"], 450)
  26. self.assertEqual(startup["burst_step_rate_hz"], 1000)
  27. self.assertEqual(startup["inter_burst_ms"], 50)
  28. self.assertEqual(startup["max_bursts"], 10)
  29. self.assertEqual(startup["axis_order"], ["x", "y"])
  30. def test_generated_params_match_startup_config(self):
  31. project = json.loads(PROJECT_PATH.read_text(encoding="utf-8"))
  32. startup = project["limits"]["startup"]
  33. params = PARAMS_PATH.read_text(encoding="utf-8")
  34. self.assertIn("`define P4_STARTUP_ENABLED 1", params)
  35. self.assertIn("`define P4_STARTUP_DIR_MASK 2'b11", params)
  36. self.assertIn(f"`define P4_STARTUP_BURST_STEPS {startup['burst_step_count']}", params)
  37. self.assertIn(f"`define P4_STARTUP_MAX_BURSTS {startup['max_bursts']}", params)
  38. expected_ticks = startup["inter_burst_ms"] * 1000 // project["watchdog"]["tick_us"]
  39. self.assertIn(f"`define P4_STARTUP_INTER_BURST_TICKS {expected_ticks}", params)
  40. def test_source_manifest_includes_startup_close_seek(self):
  41. manifest = json.loads(SOURCE_MANIFEST_PATH.read_text(encoding="utf-8"))
  42. self.assertIn("src/modules/startup_close_seek.v", manifest["sources"])
  43. def test_top_level_instantiates_startup_close_seek(self):
  44. top = TOP_PATH.read_text(encoding="utf-8")
  45. self.assertIn("startup_close_seek", top)
  46. self.assertIn("u_startup_close_seek", top)
  47. def test_top_level_gates_host_motor_and_accept_during_startup(self):
  48. top = TOP_PATH.read_text(encoding="utf-8")
  49. self.assertIn("assign frame_accept_enable = host_frame_accept_enable & startup_done", top)
  50. self.assertIn("assign motor_start = startup_done ? host_motor_start : startup_motor_start", top)
  51. self.assertIn("assign motor_active_mask = startup_done ? host_motor_active_mask : startup_motor_active_mask", top)
  52. self.assertIn("assign motor_dir_mask = startup_done ? host_motor_dir_mask : startup_motor_dir_mask", top)
  53. def test_top_level_synchronizes_limit_inputs(self):
  54. top = TOP_PATH.read_text(encoding="utf-8")
  55. self.assertRegex(top, r"x_limit_sync0\s*<=\s*x_limit_raw")
  56. self.assertRegex(top, r"y_limit_sync0\s*<=\s*y_limit_raw")
  57. def test_startup_module_has_expected_parameters(self):
  58. source = (ROOT / "src" / "modules" / "startup_close_seek.v").read_text(encoding="utf-8")
  59. params = ["FIXED_STEPS", "DIR_SETUP_TICKS", "STEP_HALF_CYCLES", "MAX_BURSTS", "INTER_BURST_TICKS", "DIR_MASK"]
  60. for param in params:
  61. self.assertRegex(source, rf"\bparameter\s+((integer|\[[^\]]+\])\s+)?{param}\b")
  62. if __name__ == "__main__":
  63. unittest.main()