manual_6400_soak.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python3
  2. """Deterministic, simulator-free 6400 manual protocol and watchdog soak."""
  3. from __future__ import annotations
  4. import json
  5. import os
  6. import random
  7. from pathlib import Path
  8. SAFE = bytes([0] * 24 + [20] * 8)
  9. STATUS_OK, STATUS_UNSUPPORTED, STATUS_BAD_LENGTH, STATUS_BAD_VALUE, STATUS_SAFETY_LOCKED = range(5)
  10. def crc16(data: bytes) -> int:
  11. crc = 0xFFFF
  12. for byte in data:
  13. crc ^= byte << 8
  14. for _ in range(8):
  15. crc = ((crc << 1) ^ 0x1021) & 0xFFFF if crc & 0x8000 else (crc << 1) & 0xFFFF
  16. return crc
  17. def frame(kind: int, payload: bytes = b"") -> bytes:
  18. body = bytes((kind, len(payload))) + payload
  19. return b"\xA5\x5A" + body + crc16(body).to_bytes(2, "little")
  20. def decode(raw: bytes) -> tuple[int, bytes]:
  21. if len(raw) < 6 or raw[:2] != b"\xA5\x5A" or len(raw) != raw[3] + 6:
  22. raise AssertionError("invalid frame")
  23. if int.from_bytes(raw[-2:], "little") != crc16(raw[2:-2]):
  24. raise AssertionError("CRC mismatch")
  25. return raw[2], raw[4:-2]
  26. class Model:
  27. def __init__(self) -> None:
  28. self.staging = bytearray(32)
  29. self.mask = 0
  30. self.active = bytearray(SAFE)
  31. self.manual = False
  32. self.watchdog = 0
  33. self.hard = False
  34. self.epoch = 0
  35. def stage(self, index: int, value: int) -> None:
  36. self.staging[index] = value
  37. self.mask |= 1 << index
  38. def command(self, kind: int) -> tuple[int, int]:
  39. if kind != 0x01:
  40. return STATUS_UNSUPPORTED, 0
  41. missing = (~self.mask) & ((1 << 32) - 1)
  42. if missing:
  43. return STATUS_BAD_LENGTH, (missing & -missing).bit_length() - 1
  44. for index, value in enumerate(self.staging):
  45. if value > 100:
  46. return STATUS_BAD_VALUE, index
  47. if self.hard:
  48. return STATUS_SAFETY_LOCKED, 0
  49. self.active[:] = self.staging
  50. self.mask = 0
  51. self.manual = True
  52. self.watchdog = 30
  53. self.epoch += 1
  54. return STATUS_OK, 0
  55. def tick(self, seconds: int = 1) -> None:
  56. self.watchdog = max(0, self.watchdog - seconds)
  57. if self.watchdog == 0 and self.manual:
  58. self.active[:] = SAFE
  59. self.manual = False
  60. def main() -> int:
  61. iterations = int(os.environ.get("PROD2U_SOAK_ITERATIONS", "2048"))
  62. seed = int(os.environ.get("PROD2U_SOAK_SEED", "64006400"))
  63. rng = random.Random(seed)
  64. model = Model()
  65. commits = invalids = timeouts = 0
  66. for step in range(iterations):
  67. values = bytes(rng.randrange(101) for _ in range(32))
  68. raw = frame(0x01, values)
  69. kind, payload = decode(raw)
  70. assert kind == 0x01 and payload == values
  71. for index, value in enumerate(values):
  72. model.stage(index, value)
  73. status, detail = model.command(0x01)
  74. assert (status, detail) == (STATUS_OK, 0)
  75. assert bytes(model.active) == values and model.manual and model.watchdog == 30
  76. commits += 1
  77. if step % 4 == 0:
  78. old = bytes(model.active)
  79. for index, value in enumerate(values):
  80. model.stage(index, 101 if index == 7 else value)
  81. status, detail = model.command(0x01)
  82. assert (status, detail) == (STATUS_BAD_VALUE, 7)
  83. assert bytes(model.active) == old and model.watchdog == 30
  84. invalids += 1
  85. for index, value in enumerate(values):
  86. model.stage(index, value)
  87. status, detail = model.command(0x01)
  88. assert (status, detail) == (STATUS_OK, 0)
  89. assert bytes(model.active) == values and model.watchdog == 30
  90. commits += 1
  91. status, detail = model.command(0x04)
  92. assert (status, detail) == (STATUS_UNSUPPORTED, 0)
  93. assert model.watchdog == 30
  94. model.tick(30)
  95. assert bytes(model.active) == SAFE and not model.manual and model.watchdog == 0
  96. timeouts += 1
  97. assert SAFE == bytes([0] * 24 + [20] * 8)
  98. result = {"status": "PASS", "seed": seed, "iterations": iterations, "commits": commits, "invalid_values": invalids, "timeouts": timeouts, "safe_vector": SAFE.hex()}
  99. output = os.environ.get("PROD2U_SOAK_RESULT")
  100. if output:
  101. Path(output).write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8")
  102. print("MANUAL_SOAK_PASS", json.dumps(result, sort_keys=True))
  103. return 0
  104. if __name__ == "__main__":
  105. raise SystemExit(main())