| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- """Contract tests for the startup close-seek feature.
- These tests verify that project.json, generated parameters, the source manifest,
- and the top-level netlist all agree on the startup configuration. RTL behavior is
- covered by tests/sim/startup_close_seek_tb.v, tests/formal/startup_close_seek.sby,
- and tests/cocotb after toolchains are available.
- """
- from __future__ import annotations
- import json
- import re
- import unittest
- from pathlib import Path
- ROOT = Path(__file__).resolve().parents[2]
- PROJECT_PATH = ROOT / "projects" / "prod2u-door-temp" / "project.json"
- PARAMS_PATH = ROOT / "src" / "generated" / "prod2u_door_temp_params.vh"
- TOP_PATH = ROOT / "src" / "top" / "prod2u_p4_normal_v3_top.v"
- SOURCE_MANIFEST_PATH = ROOT / "src" / "generated" / "td-source-manifest.json"
- class StartupCloseSeekContractTests(unittest.TestCase):
- def test_project_json_has_enabled_close_startup(self):
- project = json.loads(PROJECT_PATH.read_text(encoding="utf-8"))
- limits = project["limits"]
- self.assertTrue(limits["functional_use"])
- startup = limits["startup"]
- self.assertTrue(startup["enabled"])
- self.assertEqual(startup["direction_meaning"], "close")
- self.assertEqual(startup["burst_step_count"], 450)
- self.assertEqual(startup["burst_step_rate_hz"], 1000)
- self.assertEqual(startup["inter_burst_ms"], 50)
- self.assertEqual(startup["max_bursts"], 10)
- self.assertEqual(startup["axis_order"], ["x", "y"])
- def test_generated_params_match_startup_config(self):
- project = json.loads(PROJECT_PATH.read_text(encoding="utf-8"))
- startup = project["limits"]["startup"]
- params = PARAMS_PATH.read_text(encoding="utf-8")
- self.assertIn("`define P4_STARTUP_ENABLED 1", params)
- self.assertIn("`define P4_STARTUP_DIR_MASK 2'b11", params)
- self.assertIn(f"`define P4_STARTUP_BURST_STEPS {startup['burst_step_count']}", params)
- self.assertIn(f"`define P4_STARTUP_MAX_BURSTS {startup['max_bursts']}", params)
- expected_ticks = startup["inter_burst_ms"] * 1000 // project["watchdog"]["tick_us"]
- self.assertIn(f"`define P4_STARTUP_INTER_BURST_TICKS {expected_ticks}", params)
- def test_source_manifest_includes_startup_close_seek(self):
- manifest = json.loads(SOURCE_MANIFEST_PATH.read_text(encoding="utf-8"))
- self.assertIn("src/modules/startup_close_seek.v", manifest["sources"])
- def test_top_level_instantiates_startup_close_seek(self):
- top = TOP_PATH.read_text(encoding="utf-8")
- self.assertIn("startup_close_seek", top)
- self.assertIn("u_startup_close_seek", top)
- def test_top_level_gates_host_motor_and_accept_during_startup(self):
- top = TOP_PATH.read_text(encoding="utf-8")
- self.assertIn("assign frame_accept_enable = host_frame_accept_enable & startup_done", top)
- self.assertIn("assign motor_start = startup_done ? host_motor_start : startup_motor_start", top)
- self.assertIn("assign motor_active_mask = startup_done ? host_motor_active_mask : startup_motor_active_mask", top)
- self.assertIn("assign motor_dir_mask = startup_done ? host_motor_dir_mask : startup_motor_dir_mask", top)
- def test_top_level_synchronizes_limit_inputs(self):
- top = TOP_PATH.read_text(encoding="utf-8")
- self.assertRegex(top, r"x_limit_sync0\s*<=\s*x_limit_raw")
- self.assertRegex(top, r"y_limit_sync0\s*<=\s*y_limit_raw")
- def test_startup_module_has_expected_parameters(self):
- source = (ROOT / "src" / "modules" / "startup_close_seek.v").read_text(encoding="utf-8")
- params = ["FIXED_STEPS", "DIR_SETUP_TICKS", "STEP_HALF_CYCLES", "MAX_BURSTS", "INTER_BURST_TICKS", "DIR_MASK"]
- for param in params:
- self.assertRegex(source, rf"\bparameter\s+((integer|\[[^\]]+\])\s+)?{param}\b")
- if __name__ == "__main__":
- unittest.main()
|