"""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"]) self.assertFalse(limits["runtime_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"]) self.assertTrue(startup["limit_stop_during_burst"]) self.assertEqual(startup["stop_semantics"], "finish_current_high_pulse_then_no_next_rising_edge") self.assertEqual(startup["additional_rising_edges_after_synchronized_limit"], 0) self.assertFalse(startup["runtime_limit_use"]) 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) self.assertIn("assign motor_stop_mask = startup_done ? 2'b00 : startup_motor_stop_mask", top) self.assertIn(".motor_stop_mask(startup_motor_stop_mask)", top) self.assertIn(".stop_mask(motor_stop_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"x_limit_sync1\s*<=\s*x_limit_sync0") self.assertRegex(top, r"y_limit_sync0\s*<=\s*y_limit_raw") self.assertRegex(top, r"y_limit_sync1\s*<=\s*y_limit_sync0") self.assertIn("limit_sync_valid", top) self.assertRegex(top, r"limit_sync_valid_cnt\s*<\s*2'd3") def test_top_level_gates_startup_until_sync_valid(self): top = TOP_PATH.read_text(encoding="utf-8") self.assertIn(".limit_sync_valid(limit_sync_valid)", top) def test_startup_module_has_wait_sync_state(self): source = (ROOT / "src" / "modules" / "startup_close_seek.v").read_text(encoding="utf-8") self.assertIn("ST_WAIT_SYNC", source) self.assertIn("limit_sync_valid", source) self.assertIn("if (limit_sync_valid)", source) self.assertIn("reached_mask_next", source) self.assertIn("limit_sync_valid ? limit_sync : AXIS_ZERO", source) self.assertIn("motor_stop_mask", source) self.assertIn("assign motor_stop_mask", source) self.assertIn("AXIS_COUNT", source) 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()