test_6400_v1_soak.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 send_noise(dut, rng: random.Random) -> None:
  43. before_commit = int(dut.target_commit_count_mon.value)
  44. before_response = int(dut.host_response_count_mon.value)
  45. # A bad-CRC target-shaped frame plus arbitrary bytes must not commit or
  46. # produce a response. Let the bounded idle unlock recover the selected port.
  47. corrupt = bytearray(encode_frame(SET_TARGET, bytes([rng.randint(55, 75)])))
  48. corrupt[-1] ^= 0x80
  49. port = "rs232" if rng.randrange(2) == 0 else "ttl"
  50. await send_uart(getattr(dut, f"host_{port}_rx"), corrupt, HOST_CLKS_PER_BIT)
  51. await send_uart(getattr(dut, f"host_{port}_rx"), bytes(rng.randrange(256) for _ in range(5)), HOST_CLKS_PER_BIT)
  52. await clock_cycles(dut, 1400)
  53. assert int(dut.target_commit_count_mon.value) == before_commit
  54. assert int(dut.host_response_count_mon.value) == before_response
  55. async def recover_safe(dut) -> None:
  56. for _ in range(4):
  57. previous = int(dut.control_epoch_mon.value)
  58. await send_bmc_snapshot(dut, [60] * 24)
  59. if int(dut.control_state_mon.value) in (4, 5):
  60. await wait_control_epoch(dut, previous, "soak recovery control pass")
  61. await wait_for(
  62. dut,
  63. lambda: int(dut.global_fault_mon.value) == 0
  64. and int(dut.lane_fault_mask_mon.value) == 0
  65. and int(dut.control_state_mon.value) == 4,
  66. cycles=1600,
  67. label="soak recovery AUTO",
  68. )
  69. async def validate_snapshot(dut, port: str, counters: dict[str, int]) -> dict[str, object]:
  70. snapshot = await get_snapshot(dut, port)
  71. assert snapshot["control_state"] in range(7)
  72. for name in ("ptc_requested", "ptc_applied", "front_requested", "front_applied", "rear_requested", "rear_applied"):
  73. assert all(0 <= value <= 100 for value in values(snapshot, name))
  74. if snapshot["global_fault"]:
  75. assert values(snapshot, "ptc_applied") == [0] * 24
  76. assert values(snapshot, "front_applied") == [100] * 4
  77. assert values(snapshot, "rear_applied") == [100] * 4
  78. assert int(dut.pwm_deadline_miss_mon.value) == 0
  79. counters["get_snapshot"] += 1
  80. return snapshot
  81. @cocotb.test()
  82. async def test_accelerated_randomized_system_soak(dut):
  83. rng = random.Random(SEED)
  84. counters = {
  85. "bmc_random": 0,
  86. "invalid": 0,
  87. "stale": 0,
  88. "get_snapshot": 0,
  89. "set_target": 0,
  90. "repeated_set": 0,
  91. "stimulus": 0,
  92. "recovery": 0,
  93. "epoch_wrap": 0,
  94. "uart_noise": 0,
  95. "reset": 0,
  96. "hard_limit": 0,
  97. "state_transitions": 0,
  98. "virtual_seconds": 0,
  99. }
  100. dut.reset_test.value = 0
  101. previous_state = 0
  102. await bring_auto(dut)
  103. # Exercise the 16-bit acquisition epoch rollover with real BMC UART frames.
  104. dut.u_dut.u_bmc_snapshot.epoch.value = 0xFFFE
  105. await send_bmc_snapshot(dut, [60] * 24)
  106. assert int(dut.bmc_epoch_mon.value) == 0xFFFF
  107. await send_bmc_snapshot(dut, [60] * 24)
  108. assert int(dut.bmc_epoch_mon.value) == 0
  109. counters["epoch_wrap"] += 1
  110. for iteration in range(ITERATIONS):
  111. action = iteration % 8
  112. if action == 0:
  113. temperatures: list[int | None] = [rng.randint(54, 75) for _ in range(24)]
  114. if iteration % 16 == 0:
  115. temperatures[rng.randrange(24)] = None
  116. counters["invalid"] += 1
  117. previous = int(dut.control_epoch_mon.value)
  118. await send_bmc_snapshot(dut, temperatures)
  119. await wait_control_epoch(dut, previous, "random BMC control pass")
  120. counters["bmc_random"] += 1
  121. elif action == 1:
  122. port = "rs232" if iteration & 1 else "ttl"
  123. target = rng.choice((58, 63, 70, 75))
  124. kind, payload = await host_transaction(dut, port, SET_TARGET, bytes([target]))
  125. assert (kind, payload) == (SET_TARGET_ACK, b"")
  126. kind, payload = await host_transaction(dut, port, SET_TARGET, bytes([target]))
  127. assert (kind, payload) == (SET_TARGET_ACK, b"")
  128. counters["set_target"] += 2
  129. counters["repeated_set"] += 1
  130. await recover_safe(dut)
  131. counters["recovery"] += 1
  132. elif action == 2:
  133. await pulse_tick_1s(dut, 3)
  134. counters["virtual_seconds"] += 3
  135. await wait_for(dut, lambda: int(dut.global_fault_mon.value) == 1, cycles=300, label="random stale clamp")
  136. stale = await validate_snapshot(dut, "rs232", counters)
  137. assert values(stale, "ptc_applied") == [0] * 24
  138. counters["stale"] += 1
  139. await recover_safe(dut)
  140. counters["recovery"] += 1
  141. elif action == 3:
  142. await validate_snapshot(dut, "ttl" if iteration & 1 else "rs232", counters)
  143. elif action == 4:
  144. ptc = [rng.randint(0, 45) for _ in range(24)]
  145. front = [rng.randint(20, 70) for _ in range(4)]
  146. rear = [rng.randint(20, 70) for _ in range(4)]
  147. duration = rng.randint(1, 3)
  148. kind, payload = await host_transaction(
  149. dut, "rs232", SET_STIMULUS, bytes([duration, *ptc, *front, *rear])
  150. )
  151. assert (kind, payload) == (SET_STIMULUS_ACK, b"")
  152. previous = int(dut.control_epoch_mon.value)
  153. await send_bmc_snapshot(dut, [60] * 24)
  154. await wait_control_epoch(dut, previous, "stimulus commit")
  155. await pulse_tick_1s(dut, duration)
  156. counters["virtual_seconds"] += duration
  157. assert int(dut.stimulus_active_mon.value) == 0
  158. counters["stimulus"] += 1
  159. elif action == 5:
  160. await send_noise(dut, rng)
  161. counters["uart_noise"] += 1
  162. elif action == 6:
  163. lane = rng.randrange(4)
  164. temperatures = [60] * 24
  165. temperatures[lane * 6 + rng.randrange(6)] = 77
  166. previous = int(dut.control_epoch_mon.value)
  167. await send_bmc_snapshot(dut, temperatures)
  168. await wait_control_epoch(dut, previous, "hard-limit control pass")
  169. assert int(dut.lane_fault_mask_mon.value) & (1 << lane)
  170. snap = await validate_snapshot(dut, "ttl", counters)
  171. assert values(snap, "ptc_applied")[lane * 6 : lane * 6 + 6] == [0] * 6
  172. assert values(snap, "front_applied")[lane] == 100
  173. assert values(snap, "rear_applied")[lane] == 100
  174. counters["hard_limit"] += 1
  175. await recover_safe(dut)
  176. counters["recovery"] += 1
  177. else:
  178. # Two deterministic mid-run global resets/reconfigurations.
  179. if iteration in (31, 63):
  180. dut.reset_test.value = 1
  181. await clock_cycles(dut, 4)
  182. dut.reset_test.value = 0
  183. await initialize(dut)
  184. for _ in range(4):
  185. await send_bmc_snapshot(dut, [60] * 24)
  186. await wait_for(dut, lambda: int(dut.control_state_mon.value) == 4, cycles=1200, label="AUTO after reset")
  187. counters["reset"] += 1
  188. else:
  189. await validate_snapshot(dut, "rs232", counters)
  190. state = int(dut.control_state_mon.value)
  191. if state != previous_state:
  192. counters["state_transitions"] += 1
  193. previous_state = state
  194. assert state in range(7)
  195. assert int(dut.pwm_deadline_miss_mon.value) == 0
  196. final_snapshot = await validate_snapshot(dut, "ttl", counters)
  197. assert final_snapshot["control_epoch"] >= 1
  198. required_nonzero = (
  199. "bmc_random", "invalid", "stale", "get_snapshot", "set_target",
  200. "repeated_set", "stimulus", "recovery", "epoch_wrap", "uart_noise",
  201. "reset", "hard_limit", "state_transitions", "virtual_seconds",
  202. )
  203. assert all(counters[name] > 0 for name in required_nonzero), counters
  204. result = {
  205. "schema": "prod2u-6400-soak-result-v1",
  206. "seed": SEED,
  207. "iterations": ITERATIONS,
  208. "counters": counters,
  209. "final_control_epoch": int(final_snapshot["control_epoch"]),
  210. "final_bmc_epoch": int(final_snapshot["bmc_epoch"]),
  211. "status": "PASS",
  212. }
  213. result_path = Path(os.environ["PROD2U_SOAK_RESULT"])
  214. result_path.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8")
  215. cocotb.log.info("L11_SOAK_PASS %s", json.dumps(result, sort_keys=True))