|
|
@@ -0,0 +1,232 @@
|
|
|
+"""Compile-time motor polarity / endpoint-role config contract tests."""
|
|
|
+
|
|
|
+from __future__ import annotations
|
|
|
+
|
|
|
+import copy
|
|
|
+import json
|
|
|
+import tempfile
|
|
|
+import unittest
|
|
|
+from pathlib import Path
|
|
|
+
|
|
|
+from tools.generate_project import _axis_mask_literal, _render_params, generate_project, load_project
|
|
|
+
|
|
|
+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"
|
|
|
+
|
|
|
+
|
|
|
+class MotorPolarityConfigTests(unittest.TestCase):
|
|
|
+ def test_project_json_has_explicit_default_polarity_arrays(self):
|
|
|
+ project = json.loads(PROJECT_PATH.read_text(encoding="utf-8"))
|
|
|
+ self.assertEqual(project["limits"]["limit_active_high"], [True, True, True])
|
|
|
+ self.assertEqual(project["limits"]["close_limit_is_ls"], [True, True, True])
|
|
|
+ self.assertEqual(project["motors"]["dir_close_level"], [1, 1, 1])
|
|
|
+
|
|
|
+ def test_axis_mask_bit_order_x_is_lsb(self):
|
|
|
+ self.assertEqual(_axis_mask_literal([True, False, True], 3), "3'b101")
|
|
|
+ self.assertEqual(_axis_mask_literal([1, 0, 0], 3), "3'b001")
|
|
|
+ self.assertEqual(_axis_mask_literal([0, 1, 0], 3), "3'b010")
|
|
|
+ self.assertEqual(_axis_mask_literal([0, 0, 1], 3), "3'b100")
|
|
|
+
|
|
|
+ def test_generated_params_emit_default_masks(self):
|
|
|
+ params = PARAMS_PATH.read_text(encoding="utf-8")
|
|
|
+ self.assertIn("`define P4_LIMIT_ACTIVE_HIGH_MASK 3'b111", params)
|
|
|
+ self.assertIn("`define P4_DIR_CLOSE_LEVEL_MASK 3'b111", params)
|
|
|
+ self.assertIn("`define P4_CLOSE_LIMIT_IS_LS_MASK 3'b111", params)
|
|
|
+
|
|
|
+ def test_fresh_generation_matches_checked_in_masks(self):
|
|
|
+ with tempfile.TemporaryDirectory() as directory:
|
|
|
+ out = Path(directory)
|
|
|
+ generate_project(PROJECT_PATH, out)
|
|
|
+ params = (out / "generated" / "prod2u_door_temp_params.vh").read_text(encoding="ascii")
|
|
|
+ checked = PARAMS_PATH.read_text(encoding="utf-8")
|
|
|
+ for define in (
|
|
|
+ "P4_LIMIT_ACTIVE_HIGH_MASK",
|
|
|
+ "P4_DIR_CLOSE_LEVEL_MASK",
|
|
|
+ "P4_CLOSE_LIMIT_IS_LS_MASK",
|
|
|
+ ):
|
|
|
+ self.assertIn(f"`define {define} 3'b111", params)
|
|
|
+ self.assertIn(f"`define {define} 3'b111", checked)
|
|
|
+
|
|
|
+ def test_checked_in_masks_match_project_json(self):
|
|
|
+ """Fail closed when params.vh polarity masks drift from project.json."""
|
|
|
+ project = json.loads(PROJECT_PATH.read_text(encoding="utf-8"))
|
|
|
+ params = PARAMS_PATH.read_text(encoding="utf-8")
|
|
|
+ expected = {
|
|
|
+ "P4_LIMIT_ACTIVE_HIGH_MASK": _axis_mask_literal(
|
|
|
+ project["limits"]["limit_active_high"], 3
|
|
|
+ ),
|
|
|
+ "P4_DIR_CLOSE_LEVEL_MASK": _axis_mask_literal(
|
|
|
+ project["motors"]["dir_close_level"], 3
|
|
|
+ ),
|
|
|
+ "P4_CLOSE_LIMIT_IS_LS_MASK": _axis_mask_literal(
|
|
|
+ project["limits"]["close_limit_is_ls"], 3
|
|
|
+ ),
|
|
|
+ }
|
|
|
+ for name, literal in expected.items():
|
|
|
+ self.assertEqual(_mask_define(params, name), literal, name)
|
|
|
+
|
|
|
+ def test_render_params_respects_alternate_masks(self):
|
|
|
+ project = load_project(PROJECT_PATH)
|
|
|
+ alt = copy.deepcopy(project)
|
|
|
+ # Bypass production lock by rendering from a mutated copy directly.
|
|
|
+ alt["limits"]["limit_active_high"] = [False, False, False]
|
|
|
+ alt["motors"]["dir_close_level"] = [0, 0, 0]
|
|
|
+ alt["limits"]["close_limit_is_ls"] = [False, False, False]
|
|
|
+ params = _render_params(alt)
|
|
|
+ self.assertIn("`define P4_LIMIT_ACTIVE_HIGH_MASK 3'b000", params)
|
|
|
+ self.assertIn("`define P4_DIR_CLOSE_LEVEL_MASK 3'b000", params)
|
|
|
+ self.assertIn("`define P4_CLOSE_LIMIT_IS_LS_MASK 3'b000", params)
|
|
|
+
|
|
|
+ def test_mixed_axis_mask_order(self):
|
|
|
+ project = load_project(PROJECT_PATH)
|
|
|
+ alt = copy.deepcopy(project)
|
|
|
+ # X default, Y active-low + DIR invert, Z LS/HS swap.
|
|
|
+ alt["limits"]["limit_active_high"] = [True, False, True]
|
|
|
+ alt["motors"]["dir_close_level"] = [1, 0, 1]
|
|
|
+ alt["limits"]["close_limit_is_ls"] = [True, True, False]
|
|
|
+ params = _render_params(alt)
|
|
|
+ self.assertIn("`define P4_LIMIT_ACTIVE_HIGH_MASK 3'b101", params)
|
|
|
+ self.assertIn("`define P4_DIR_CLOSE_LEVEL_MASK 3'b101", params)
|
|
|
+ self.assertIn("`define P4_CLOSE_LIMIT_IS_LS_MASK 3'b011", params)
|
|
|
+
|
|
|
+ def test_rejects_wrong_length_dir_close_level(self):
|
|
|
+ project = json.loads(PROJECT_PATH.read_text(encoding="utf-8"))
|
|
|
+ project["motors"]["dir_close_level"] = [1, 1]
|
|
|
+ with tempfile.TemporaryDirectory() as directory:
|
|
|
+ bad = Path(directory) / "project.json"
|
|
|
+ bad.write_text(json.dumps(project), encoding="utf-8")
|
|
|
+ with self.assertRaises(Exception):
|
|
|
+ load_project(bad)
|
|
|
+
|
|
|
+ def test_rejects_non_boolean_limit_active_high(self):
|
|
|
+ project = json.loads(PROJECT_PATH.read_text(encoding="utf-8"))
|
|
|
+ project["limits"]["limit_active_high"] = [1, 1, 1]
|
|
|
+ with tempfile.TemporaryDirectory() as directory:
|
|
|
+ bad = Path(directory) / "project.json"
|
|
|
+ bad.write_text(json.dumps(project), encoding="utf-8")
|
|
|
+ with self.assertRaises(Exception):
|
|
|
+ load_project(bad)
|
|
|
+
|
|
|
+
|
|
|
+def _mask_define(params: str, name: str) -> str:
|
|
|
+ needle = f"`define {name} "
|
|
|
+ for line in params.splitlines():
|
|
|
+ if line.startswith(needle):
|
|
|
+ return line[len(needle) :].strip()
|
|
|
+ raise AssertionError(f"missing {name}")
|
|
|
+
|
|
|
+
|
|
|
+class AlternateConfigMaskMatrixTests(unittest.TestCase):
|
|
|
+ """Thin generator matrix: prove representative alt configs emit correct masks.
|
|
|
+
|
|
|
+ Production load_project locks defaults; these cases mutate a deep copy and
|
|
|
+ call _render_params only (no second firmware / no project.json rewrite).
|
|
|
+ """
|
|
|
+
|
|
|
+ # name -> (limit_active_high, dir_close_level, close_limit_is_ls,
|
|
|
+ # expected_active_mask, expected_dir_mask, expected_close_ls_mask)
|
|
|
+ CASES = (
|
|
|
+ (
|
|
|
+ "all_default",
|
|
|
+ [True, True, True],
|
|
|
+ [1, 1, 1],
|
|
|
+ [True, True, True],
|
|
|
+ "3'b111",
|
|
|
+ "3'b111",
|
|
|
+ "3'b111",
|
|
|
+ ),
|
|
|
+ (
|
|
|
+ "all_active_low",
|
|
|
+ [False, False, False],
|
|
|
+ [1, 1, 1],
|
|
|
+ [True, True, True],
|
|
|
+ "3'b000",
|
|
|
+ "3'b111",
|
|
|
+ "3'b111",
|
|
|
+ ),
|
|
|
+ (
|
|
|
+ "dir_all_invert",
|
|
|
+ [True, True, True],
|
|
|
+ [0, 0, 0],
|
|
|
+ [True, True, True],
|
|
|
+ "3'b111",
|
|
|
+ "3'b000",
|
|
|
+ "3'b111",
|
|
|
+ ),
|
|
|
+ (
|
|
|
+ "ls_hs_all_swap",
|
|
|
+ [True, True, True],
|
|
|
+ [1, 1, 1],
|
|
|
+ [False, False, False],
|
|
|
+ "3'b111",
|
|
|
+ "3'b111",
|
|
|
+ "3'b000",
|
|
|
+ ),
|
|
|
+ (
|
|
|
+ "all_three_inverted",
|
|
|
+ [False, False, False],
|
|
|
+ [0, 0, 0],
|
|
|
+ [False, False, False],
|
|
|
+ "3'b000",
|
|
|
+ "3'b000",
|
|
|
+ "3'b000",
|
|
|
+ ),
|
|
|
+ (
|
|
|
+ "mixed_xyz",
|
|
|
+ # X default / Y active-low + DIR invert / Z LS-HS swap
|
|
|
+ [True, False, True],
|
|
|
+ [1, 0, 1],
|
|
|
+ [True, True, False],
|
|
|
+ "3'b101",
|
|
|
+ "3'b101",
|
|
|
+ "3'b011",
|
|
|
+ ),
|
|
|
+ )
|
|
|
+
|
|
|
+ def test_alternate_config_mask_matrix(self):
|
|
|
+ base = load_project(PROJECT_PATH)
|
|
|
+ for (
|
|
|
+ name,
|
|
|
+ limit_active_high,
|
|
|
+ dir_close_level,
|
|
|
+ close_limit_is_ls,
|
|
|
+ exp_active,
|
|
|
+ exp_dir,
|
|
|
+ exp_close_ls,
|
|
|
+ ) in self.CASES:
|
|
|
+ with self.subTest(name=name):
|
|
|
+ alt = copy.deepcopy(base)
|
|
|
+ alt["limits"]["limit_active_high"] = list(limit_active_high)
|
|
|
+ alt["motors"]["dir_close_level"] = list(dir_close_level)
|
|
|
+ alt["limits"]["close_limit_is_ls"] = list(close_limit_is_ls)
|
|
|
+ params = _render_params(alt)
|
|
|
+ self.assertEqual(
|
|
|
+ _mask_define(params, "P4_LIMIT_ACTIVE_HIGH_MASK"),
|
|
|
+ exp_active,
|
|
|
+ name,
|
|
|
+ )
|
|
|
+ self.assertEqual(
|
|
|
+ _mask_define(params, "P4_DIR_CLOSE_LEVEL_MASK"),
|
|
|
+ exp_dir,
|
|
|
+ name,
|
|
|
+ )
|
|
|
+ self.assertEqual(
|
|
|
+ _mask_define(params, "P4_CLOSE_LIMIT_IS_LS_MASK"),
|
|
|
+ exp_close_ls,
|
|
|
+ name,
|
|
|
+ )
|
|
|
+ # Bit-order cross-check via shared helper (LSB=X).
|
|
|
+ self.assertEqual(
|
|
|
+ _axis_mask_literal(limit_active_high, 3), exp_active, name
|
|
|
+ )
|
|
|
+ self.assertEqual(
|
|
|
+ _axis_mask_literal(dir_close_level, 3), exp_dir, name
|
|
|
+ )
|
|
|
+ self.assertEqual(
|
|
|
+ _axis_mask_literal(close_limit_is_ls, 3), exp_close_ls, name
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ unittest.main()
|