test_6400_v1_soak.py 11 KB

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