| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #!/usr/bin/env python3
- """Deterministic, simulator-free 6400 manual protocol and watchdog soak."""
- from __future__ import annotations
- import json
- import os
- import random
- from pathlib import Path
- SAFE = bytes([0] * 24 + [20] * 8)
- STATUS_OK, STATUS_UNSUPPORTED, STATUS_BAD_LENGTH, STATUS_BAD_VALUE, STATUS_SAFETY_LOCKED = range(5)
- def crc16(data: bytes) -> int:
- crc = 0xFFFF
- for byte in data:
- crc ^= byte << 8
- for _ in range(8):
- crc = ((crc << 1) ^ 0x1021) & 0xFFFF if crc & 0x8000 else (crc << 1) & 0xFFFF
- return crc
- def frame(kind: int, payload: bytes = b"") -> bytes:
- body = bytes((kind, len(payload))) + payload
- return b"\xA5\x5A" + body + crc16(body).to_bytes(2, "little")
- def decode(raw: bytes) -> tuple[int, bytes]:
- if len(raw) < 6 or raw[:2] != b"\xA5\x5A" or len(raw) != raw[3] + 6:
- raise AssertionError("invalid frame")
- if int.from_bytes(raw[-2:], "little") != crc16(raw[2:-2]):
- raise AssertionError("CRC mismatch")
- return raw[2], raw[4:-2]
- class Model:
- def __init__(self) -> None:
- self.staging = bytearray(32)
- self.mask = 0
- self.active = bytearray(SAFE)
- self.manual = False
- self.watchdog = 0
- self.hard = False
- self.epoch = 0
- def stage(self, index: int, value: int) -> None:
- self.staging[index] = value
- self.mask |= 1 << index
- def command(self, kind: int) -> tuple[int, int]:
- if kind != 0x01:
- return STATUS_UNSUPPORTED, 0
- missing = (~self.mask) & ((1 << 32) - 1)
- if missing:
- return STATUS_BAD_LENGTH, (missing & -missing).bit_length() - 1
- for index, value in enumerate(self.staging):
- if value > 100:
- return STATUS_BAD_VALUE, index
- if self.hard:
- return STATUS_SAFETY_LOCKED, 0
- self.active[:] = self.staging
- self.mask = 0
- self.manual = True
- self.watchdog = 30
- self.epoch += 1
- return STATUS_OK, 0
- def tick(self, seconds: int = 1) -> None:
- self.watchdog = max(0, self.watchdog - seconds)
- if self.watchdog == 0 and self.manual:
- self.active[:] = SAFE
- self.manual = False
- def main() -> int:
- iterations = int(os.environ.get("PROD2U_SOAK_ITERATIONS", "2048"))
- seed = int(os.environ.get("PROD2U_SOAK_SEED", "64006400"))
- rng = random.Random(seed)
- model = Model()
- commits = invalids = timeouts = 0
- for step in range(iterations):
- values = bytes(rng.randrange(101) for _ in range(32))
- raw = frame(0x01, values)
- kind, payload = decode(raw)
- assert kind == 0x01 and payload == values
- for index, value in enumerate(values):
- model.stage(index, value)
- status, detail = model.command(0x01)
- assert (status, detail) == (STATUS_OK, 0)
- assert bytes(model.active) == values and model.manual and model.watchdog == 30
- commits += 1
- if step % 4 == 0:
- old = bytes(model.active)
- for index, value in enumerate(values):
- model.stage(index, 101 if index == 7 else value)
- status, detail = model.command(0x01)
- assert (status, detail) == (STATUS_BAD_VALUE, 7)
- assert bytes(model.active) == old and model.watchdog == 30
- invalids += 1
- for index, value in enumerate(values):
- model.stage(index, value)
- status, detail = model.command(0x01)
- assert (status, detail) == (STATUS_OK, 0)
- assert bytes(model.active) == values and model.watchdog == 30
- commits += 1
- status, detail = model.command(0x04)
- assert (status, detail) == (STATUS_UNSUPPORTED, 0)
- assert model.watchdog == 30
- model.tick(30)
- assert bytes(model.active) == SAFE and not model.manual and model.watchdog == 0
- timeouts += 1
- assert SAFE == bytes([0] * 24 + [20] * 8)
- result = {"status": "PASS", "seed": seed, "iterations": iterations, "commits": commits, "invalid_values": invalids, "timeouts": timeouts, "safe_vector": SAFE.hex()}
- output = os.environ.get("PROD2U_SOAK_RESULT")
- if output:
- Path(output).write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8")
- print("MANUAL_SOAK_PASS", json.dumps(result, sort_keys=True))
- return 0
- if __name__ == "__main__":
- raise SystemExit(main())
|