test_6400_v1_soak.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. """L11 accelerated randomized full-top soak with deterministic coverage."""
  2. from __future__ import annotations
  3. import json
  4. import os
  5. from pathlib import Path
  6. import random
  7. import cocotb
  8. from cocotb.triggers import RisingEdge, Timer, with_timeout
  9. from thermal_6400_uart import (
  10. BMC_CLKS_PER_BIT,
  11. CLOCK_NS,
  12. GET_SNAPSHOT,
  13. HOST_CLKS_PER_BIT,
  14. ACK_NACK,
  15. SET_ALL,
  16. SET_PID_TARGET,
  17. clock_cycles,
  18. encode_frame,
  19. get_snapshot,
  20. host_transaction,
  21. pulse_tick_1s,
  22. send_bmc_snapshot,
  23. send_uart,
  24. wait_for,
  25. )
  26. SEED = int(os.environ.get("PROD2U_SOAK_SEED", "1677779166"))
  27. ITERATIONS = int(os.environ.get("PROD2U_SOAK_ITERATIONS", "96"))
  28. def values(snapshot: dict[str, object], name: str) -> list[int]:
  29. value = snapshot[name]
  30. assert isinstance(value, list)
  31. return [int(item) for item in value]
  32. async def wait_control_epoch(dut, previous: int, label: str) -> None:
  33. await wait_for(
  34. dut,
  35. lambda: int(dut.control_epoch_mon.value) != previous,
  36. cycles=1200,
  37. label=label,
  38. )
  39. async def wait_host_idle(dut) -> None:
  40. await wait_for(
  41. dut,
  42. lambda: int(dut.host_locked_mon.value) == 0,
  43. cycles=6000,
  44. label="Host selector idle unlock",
  45. )
  46. async def initialize_current(dut) -> None:
  47. dut.host_rs232_rx.value = 1
  48. dut.host_ttl_rx.value = 1
  49. dut.bmc_rs232_rx.value = 1
  50. dut.tick_1s_test.value = 0
  51. dut.bmc_epoch_override_enable.value = 0
  52. dut.bmc_epoch_override_value.value = 0
  53. await with_timeout(RisingEdge(dut.rst_n_mon), 1, "ms")
  54. await clock_cycles(dut, 50)
  55. assert int(dut.control_state_mon.value) == 0
  56. async def set_pid_target(dut, port: str, target: int) -> None:
  57. try:
  58. kind, payload = await host_transaction(dut, port, SET_PID_TARGET, bytes([target]))
  59. except BaseException:
  60. cocotb.log.error(
  61. "SET_PID_TARGET_TIMEOUT port=%s target=%d locked=%d selected=%d "
  62. "rx_valid=%d rx_error=%d frame_valid=%d frame_ready=%d command_busy=%d "
  63. "target_commits=%d responses=%d",
  64. port,
  65. target,
  66. int(dut.u_dut.host_locked.value),
  67. int(dut.u_dut.host_port_sel.value),
  68. int(dut.u_dut.host_rx_valid.value),
  69. int(dut.u_dut.host_rx_error.value),
  70. int(dut.u_dut.frame_valid.value),
  71. int(dut.u_dut.frame_ready.value),
  72. int(dut.u_dut.host_command_busy.value),
  73. int(dut.target_commit_count_mon.value),
  74. int(dut.host_response_count_mon.value),
  75. )
  76. raise
  77. assert (kind, payload) == (ACK_NACK, bytes((1, SET_PID_TARGET, 0, 0)))
  78. async def set_all(dut, port: str, vector: bytes) -> None:
  79. kind, payload = await host_transaction(dut, port, SET_ALL, vector)
  80. assert (kind, payload) == (ACK_NACK, bytes((1, SET_ALL, 0, 0)))
  81. async def send_noise(dut, rng: random.Random) -> None:
  82. await wait_host_idle(dut)
  83. before_commit = int(dut.target_commit_count_mon.value)
  84. before_response = int(dut.host_response_count_mon.value)
  85. # A bad-CRC target-shaped frame plus arbitrary bytes must not commit or
  86. # produce a response. Let the bounded idle unlock recover the selected port.
  87. corrupt = bytearray(encode_frame(SET_PID_TARGET, bytes([rng.randint(55, 70)])))
  88. corrupt[-1] ^= 0x80
  89. port = "rs232" if rng.randrange(2) == 0 else "ttl"
  90. await send_uart(getattr(dut, f"host_{port}_rx"), corrupt, HOST_CLKS_PER_BIT)
  91. await send_uart(getattr(dut, f"host_{port}_rx"), bytes(rng.randrange(256) for _ in range(5)), HOST_CLKS_PER_BIT)
  92. await clock_cycles(dut, 1400)
  93. await wait_host_idle(dut)
  94. assert int(dut.target_commit_count_mon.value) == before_commit
  95. assert int(dut.host_response_count_mon.value) == before_response
  96. async def recover_safe(dut) -> None:
  97. for _ in range(4):
  98. previous = int(dut.control_epoch_mon.value)
  99. await send_bmc_snapshot(dut, [60] * 24)
  100. if int(dut.control_state_mon.value) == 2:
  101. await wait_control_epoch(dut, previous, "soak recovery control pass")
  102. await wait_for(
  103. dut,
  104. lambda: int(dut.global_fault_mon.value) == 0
  105. and int(dut.lane_fault_mask_mon.value) == 0
  106. and int(dut.control_state_mon.value) == 2,
  107. cycles=1600,
  108. label="soak recovery PID",
  109. )
  110. async def validate_snapshot(dut, port: str, counters: dict[str, int]) -> dict[str, object]:
  111. await wait_host_idle(dut)
  112. snapshot = await get_snapshot(dut, port)
  113. assert snapshot["control_state"] in range(3)
  114. for name in ("ptc_requested", "ptc_applied", "front_requested", "front_applied", "rear_requested", "rear_applied"):
  115. assert all(0 <= value <= 100 for value in values(snapshot, name))
  116. lane_faults = int(snapshot["lane_fault_mask"])
  117. if lane_faults:
  118. for lane in range(4):
  119. if lane_faults & (1 << lane):
  120. assert values(snapshot, "ptc_applied")[lane * 6 : lane * 6 + 6] == [0] * 6
  121. assert values(snapshot, "front_applied")[lane] == 100
  122. assert values(snapshot, "rear_applied")[lane] == 100
  123. elif snapshot["global_fault"]:
  124. assert values(snapshot, "ptc_applied") == [0] * 24
  125. assert values(snapshot, "front_applied") == [20] * 4
  126. assert values(snapshot, "rear_applied") == [20] * 4
  127. assert int(dut.pwm_deadline_miss_mon.value) == 0
  128. counters["get_snapshot"] += 1
  129. return snapshot
  130. @cocotb.test()
  131. async def test_accelerated_randomized_system_soak(dut):
  132. rng = random.Random(SEED)
  133. counters = {
  134. "bmc_random": 0,
  135. "invalid": 0,
  136. "stale": 0,
  137. "get_snapshot": 0,
  138. "set_pid_target": 0,
  139. "repeated_set": 0,
  140. "set_all": 0,
  141. "recovery": 0,
  142. "epoch_wrap": 0,
  143. "uart_noise": 0,
  144. "reset": 0,
  145. "hard_limit": 0,
  146. "state_transitions": 0,
  147. "virtual_seconds": 0,
  148. }
  149. dut.reset_test.value = 0
  150. previous_state = 0
  151. await initialize_current(dut)
  152. await pulse_tick_1s(dut, 30)
  153. await wait_for(dut, lambda: int(dut.control_state_mon.value) == 2,
  154. cycles=300, label="SAFE watchdog to PID")
  155. for _ in range(4):
  156. await send_bmc_snapshot(dut, [60] * 24)
  157. # The soak-only wrapper starts at 0xfffa, so the first four real BMC
  158. # frames leave the active epoch at 0xfffe. Two more frames cross the wrap.
  159. assert int(dut.bmc_epoch_mon.value) == 0xFFFE
  160. await send_bmc_snapshot(dut, [60] * 24)
  161. assert int(dut.bmc_epoch_mon.value) == 0xFFFF
  162. await send_bmc_snapshot(dut, [60] * 24)
  163. assert int(dut.bmc_epoch_mon.value) == 0
  164. counters["epoch_wrap"] += 1
  165. for iteration in range(ITERATIONS):
  166. action = iteration % 8
  167. cocotb.log.info(
  168. "SOAK_ITERATION iteration=%d action=%d state=%d host_locked=%d bmc_epoch=%d",
  169. iteration,
  170. action,
  171. int(dut.control_state_mon.value),
  172. int(dut.host_locked_mon.value),
  173. int(dut.bmc_epoch_mon.value),
  174. )
  175. if action == 0:
  176. temperatures: list[int | None] = [rng.randint(54, 75) for _ in range(24)]
  177. if iteration % 16 == 0:
  178. temperatures[rng.randrange(24)] = None
  179. counters["invalid"] += 1
  180. previous = int(dut.control_epoch_mon.value)
  181. await send_bmc_snapshot(dut, temperatures)
  182. await wait_control_epoch(dut, previous, "random BMC control pass")
  183. counters["bmc_random"] += 1
  184. elif action == 1:
  185. port = "rs232" if iteration & 1 else "ttl"
  186. target = rng.choice((58, 63, 67, 70))
  187. await wait_host_idle(dut)
  188. await set_pid_target(dut, port, target)
  189. await wait_host_idle(dut)
  190. await set_pid_target(dut, port, target)
  191. counters["set_pid_target"] += 2
  192. counters["repeated_set"] += 1
  193. await recover_safe(dut)
  194. counters["recovery"] += 1
  195. elif action == 2:
  196. await pulse_tick_1s(dut, 3)
  197. counters["virtual_seconds"] += 3
  198. await wait_for(dut, lambda: int(dut.global_fault_mon.value) == 1, cycles=300, label="random stale clamp")
  199. stale = await validate_snapshot(dut, "rs232", counters)
  200. assert values(stale, "ptc_applied") == [0] * 24
  201. counters["stale"] += 1
  202. await recover_safe(dut)
  203. counters["recovery"] += 1
  204. elif action == 3:
  205. await validate_snapshot(dut, "ttl" if iteration & 1 else "rs232", counters)
  206. elif action == 4:
  207. ptc = [rng.randint(0, 45) for _ in range(24)]
  208. front = [rng.randint(0, 100) for _ in range(4)]
  209. rear = [rng.randint(0, 100) for _ in range(4)]
  210. vector = bytes([*ptc, *front, *rear])
  211. await wait_host_idle(dut)
  212. await set_all(dut, "rs232", vector)
  213. assert int(dut.control_state_mon.value) == 1
  214. snap = await validate_snapshot(dut, "ttl", counters)
  215. assert values(snap, "ptc_requested") == ptc
  216. assert values(snap, "front_requested") == front
  217. assert values(snap, "rear_requested") == rear
  218. counters["set_all"] += 1
  219. elif action == 5:
  220. await send_noise(dut, rng)
  221. counters["uart_noise"] += 1
  222. elif action == 6:
  223. lane = rng.randrange(4)
  224. temperatures = [60] * 24
  225. temperatures[lane * 6 + rng.randrange(6)] = 77
  226. previous = int(dut.control_epoch_mon.value)
  227. await send_bmc_snapshot(dut, temperatures)
  228. await wait_control_epoch(dut, previous, "hard-limit control pass")
  229. assert int(dut.lane_fault_mask_mon.value) & (1 << lane)
  230. snap = await validate_snapshot(dut, "ttl", counters)
  231. assert values(snap, "ptc_applied")[lane * 6 : lane * 6 + 6] == [0] * 6
  232. assert values(snap, "front_applied")[lane] == 100
  233. assert values(snap, "rear_applied")[lane] == 100
  234. counters["hard_limit"] += 1
  235. await recover_safe(dut)
  236. counters["recovery"] += 1
  237. else:
  238. # Two deterministic mid-run global resets/reconfigurations.
  239. if iteration in (31, 63):
  240. dut.reset_test.value = 1
  241. await clock_cycles(dut, 4)
  242. dut.reset_test.value = 0
  243. await initialize_current(dut)
  244. await pulse_tick_1s(dut, 30)
  245. for _ in range(4):
  246. await send_bmc_snapshot(dut, [60] * 24)
  247. await wait_for(dut, lambda: int(dut.control_state_mon.value) == 2, cycles=1200, label="PID after reset")
  248. counters["reset"] += 1
  249. else:
  250. await validate_snapshot(dut, "rs232", counters)
  251. state = int(dut.control_state_mon.value)
  252. if state != previous_state:
  253. counters["state_transitions"] += 1
  254. previous_state = state
  255. assert state in range(3)
  256. assert int(dut.pwm_deadline_miss_mon.value) == 0
  257. final_snapshot = await validate_snapshot(dut, "ttl", counters)
  258. assert final_snapshot["control_epoch"] >= 1
  259. required_nonzero = (
  260. "bmc_random", "invalid", "stale", "get_snapshot", "set_pid_target",
  261. "repeated_set", "set_all", "recovery", "epoch_wrap", "uart_noise",
  262. "reset", "hard_limit", "state_transitions", "virtual_seconds",
  263. )
  264. assert all(counters[name] > 0 for name in required_nonzero), counters
  265. result = {
  266. "schema": "prod2u-6400-soak-result-v1",
  267. "seed": SEED,
  268. "iterations": ITERATIONS,
  269. "counters": counters,
  270. "final_control_epoch": int(final_snapshot["control_epoch"]),
  271. "final_bmc_epoch": int(final_snapshot["bmc_epoch"]),
  272. "status": "PASS",
  273. }
  274. result_path = Path(os.environ["PROD2U_SOAK_RESULT"])
  275. result_path.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8")
  276. cocotb.log.info("L11_SOAK_PASS %s", json.dumps(result, sort_keys=True))