test_6400_v1_identification_integration.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. """Identification-profile stimulus lifetime and safety integration."""
  2. from __future__ import annotations
  3. import cocotb
  4. from cocotb.triggers import ReadOnly, RisingEdge
  5. from thermal_6400_uart import (
  6. SET_STIMULUS,
  7. SET_STIMULUS_ACK,
  8. HOST_CLKS_PER_BIT,
  9. bring_auto,
  10. encode_frame,
  11. get_snapshot,
  12. host_transaction,
  13. pulse_tick_1s,
  14. send_uart,
  15. send_bmc_snapshot,
  16. wait_for,
  17. )
  18. def array(snapshot: dict[str, object], name: str) -> list[int]:
  19. value = snapshot[name]
  20. assert isinstance(value, list)
  21. return [int(item) for item in value]
  22. async def send_silent_invalid_stimulus(dut, port: str, payload: bytes) -> None:
  23. responses_before = int(dut.host_response_count_mon.value)
  24. active_bank_before = int(dut.u_dut.u_control.stimulus_bank_active.value)
  25. await send_uart(
  26. getattr(dut, f"host_{port}_rx"),
  27. encode_frame(SET_STIMULUS, payload),
  28. HOST_CLKS_PER_BIT,
  29. )
  30. await wait_for(
  31. dut,
  32. lambda: int(dut.host_locked_mon.value) == 0
  33. and int(dut.u_dut.u_command.busy.value) == 0,
  34. cycles=200,
  35. label=f"invalid stimulus silent retirement on {port}",
  36. )
  37. assert int(dut.host_response_count_mon.value) == responses_before
  38. assert int(dut.u_dut.u_control.stimulus_bank_active.value) == active_bank_before
  39. assert int(dut.stimulus_active_mon.value) == 1
  40. assert int(getattr(dut, f"host_{port}_tx").value) == 1
  41. @cocotb.test()
  42. async def test_identification_stimulus_expires_and_safety_preempts(dut):
  43. await bring_auto(dut)
  44. ptc = [20 + (index % 3) for index in range(24)]
  45. front = [20, 30, 40, 50]
  46. rear = [50, 40, 30, 20]
  47. stimulus = bytes([2, *ptc, *front, *rear])
  48. responses_before_repeated_stimulus = int(dut.host_response_count_mon.value)
  49. for transaction_index in (1, 2):
  50. kind, payload = await host_transaction(dut, "rs232", SET_STIMULUS, stimulus)
  51. assert (kind, payload) == (SET_STIMULUS_ACK, b"")
  52. assert int(dut.host_response_count_mon.value) == (
  53. responses_before_repeated_stimulus + transaction_index
  54. )
  55. assert int(dut.stimulus_active_mon.value) == 1
  56. old_control_epoch = int(dut.control_epoch_mon.value)
  57. await send_bmc_snapshot(dut, [60] * 24)
  58. await wait_for(
  59. dut,
  60. lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
  61. cycles=1000,
  62. label="stimulus output commit",
  63. )
  64. active = await get_snapshot(dut, "ttl")
  65. assert array(active, "ptc_requested") == ptc
  66. assert array(active, "ptc_applied") == ptc
  67. assert array(active, "front_requested") == front
  68. assert array(active, "front_applied") == front
  69. assert array(active, "rear_requested") == rear
  70. assert array(active, "rear_applied") == rear
  71. # Duration zero and one out-of-range duty both dirty only the inactive
  72. # staging bank. They are silently rejected: no ACK, no active pointer
  73. # flip, and the already-running A vector remains intact.
  74. await send_silent_invalid_stimulus(
  75. dut, "rs232", bytes([0, *([83] * 32)])
  76. )
  77. invalid_duty = [67] * 32
  78. invalid_duty[15] = 101
  79. await send_silent_invalid_stimulus(
  80. dut, "ttl", bytes([2, *invalid_duty])
  81. )
  82. still_a = await get_snapshot(dut, "rs232")
  83. assert array(still_a, "ptc_applied") == ptc
  84. assert array(still_a, "front_applied") == front
  85. assert array(still_a, "rear_applied") == rear
  86. # Launch a different legal B vector, then admit a new BMC epoch while the
  87. # command is still staging its bytes. The running control pass must lock
  88. # A. The 0x83 ACK is fresh and the pointer flips mid-pass, but every read
  89. # in that pass remains A; the following pass is wholly B.
  90. ptc_b = [41 + (index % 5) for index in range(24)]
  91. front_b = [61, 62, 63, 64]
  92. rear_b = [71, 72, 73, 74]
  93. stimulus_b = bytes([2, *ptc_b, *front_b, *rear_b])
  94. responses_before_b = int(dut.host_response_count_mon.value)
  95. old_active_bank = int(dut.u_dut.u_control.stimulus_bank_active.value)
  96. old_control_epoch = int(dut.control_epoch_mon.value)
  97. b_transaction = cocotb.start_soon(
  98. host_transaction(dut, "rs232", SET_STIMULUS, stimulus_b)
  99. )
  100. await wait_for(
  101. dut,
  102. lambda: int(dut.u_dut.u_command.state.value) == 4
  103. and 4 <= int(dut.u_dut.u_command.index.value) <= 12,
  104. cycles=15000,
  105. label="B stimulus staging window",
  106. )
  107. forced_epoch = (int(dut.bmc_epoch_mon.value) + 1) & 0xFFFF
  108. dut.bmc_epoch_override_value.value = forced_epoch
  109. dut.bmc_epoch_override_enable.value = 1
  110. await wait_for(
  111. dut,
  112. lambda: int(dut.u_dut.u_control.work_state.value) == 2
  113. and int(dut.u_dut.u_control.work_index.value) == 0,
  114. cycles=200,
  115. label="A-locked control pass",
  116. )
  117. assert int(dut.u_dut.u_control.stimulus_pass_bank.value) == old_active_bank
  118. assert int(dut.u_dut.u_control.stimulus_pass_running.value) == 1
  119. vector_a = [*ptc, *front, *rear]
  120. saw_b_commit_mid_pass = False
  121. for _ in range(500):
  122. await RisingEdge(dut.sys_clk)
  123. await ReadOnly()
  124. if int(dut.control_epoch_mon.value) != old_control_epoch:
  125. break
  126. work_state = int(dut.u_dut.u_control.work_state.value)
  127. work_index = int(dut.u_dut.u_control.work_index.value)
  128. assert int(dut.u_dut.u_control.stimulus_pass_bank.value) == old_active_bank
  129. assert int(dut.u_dut.u_control.stimulus_pass_running.value) == 1
  130. if work_state in (1, 2, 3, 4, 5, 6, 8, 10):
  131. assert int(dut.u_dut.u_control.stimulus_pass_data.value) == vector_a[work_index]
  132. if int(dut.u_dut.u_control.stimulus_bank_active.value) != old_active_bank:
  133. saw_b_commit_mid_pass = True
  134. assert work_state != 0
  135. else:
  136. raise AssertionError("A-locked pass did not commit")
  137. assert saw_b_commit_mid_pass
  138. kind, payload = await b_transaction
  139. assert (kind, payload) == (SET_STIMULUS_ACK, b"")
  140. assert int(dut.host_response_count_mon.value) == responses_before_b + 1
  141. first_after_b_commit = await get_snapshot(dut, "ttl")
  142. assert array(first_after_b_commit, "ptc_applied") == ptc
  143. assert array(first_after_b_commit, "front_applied") == front
  144. assert array(first_after_b_commit, "rear_applied") == rear
  145. old_control_epoch = int(dut.control_epoch_mon.value)
  146. forced_epoch = (forced_epoch + 1) & 0xFFFF
  147. dut.bmc_epoch_override_value.value = forced_epoch
  148. await wait_for(
  149. dut,
  150. lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
  151. cycles=1000,
  152. label="first wholly-B control pass",
  153. )
  154. active_b = await get_snapshot(dut, "rs232")
  155. assert array(active_b, "ptc_requested") == ptc_b
  156. assert array(active_b, "ptc_applied") == ptc_b
  157. assert array(active_b, "front_applied") == front_b
  158. assert array(active_b, "rear_applied") == rear_b
  159. ptc, front, rear = ptc_b, front_b, rear_b
  160. release_control_epoch = int(dut.control_epoch_mon.value)
  161. dut.bmc_epoch_override_enable.value = 0
  162. await wait_for(
  163. dut,
  164. lambda: int(dut.control_epoch_mon.value) > release_control_epoch,
  165. cycles=1000,
  166. label="epoch-hook release pass",
  167. )
  168. await pulse_tick_1s(dut)
  169. assert int(dut.stimulus_active_mon.value) == 1
  170. await pulse_tick_1s(dut)
  171. assert int(dut.stimulus_active_mon.value) == 0
  172. old_control_epoch = int(dut.control_epoch_mon.value)
  173. await send_bmc_snapshot(dut, [60] * 24)
  174. await wait_for(
  175. dut,
  176. lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
  177. cycles=1000,
  178. label="autonomous resume after stimulus expiry",
  179. )
  180. resumed = await get_snapshot(dut, "rs232")
  181. assert resumed["control_state"] == 4
  182. assert array(resumed, "ptc_applied") != ptc
  183. # A fresh stimulus is terminated immediately by the 77 C lane override.
  184. stimulus = bytes([3, *([35] * 24), *([25] * 4), *([45] * 4)])
  185. kind, payload = await host_transaction(dut, "ttl", SET_STIMULUS, stimulus)
  186. assert (kind, payload) == (SET_STIMULUS_ACK, b"")
  187. assert int(dut.stimulus_active_mon.value) == 1
  188. # Per-channel thermal protection has priority over an active stimulus.
  189. old_control_epoch = int(dut.control_epoch_mon.value)
  190. await send_bmc_snapshot(dut, [74, *([60] * 23)])
  191. await wait_for(
  192. dut,
  193. lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
  194. cycles=1000,
  195. label="soft-limit stimulus preemption",
  196. )
  197. soft = await get_snapshot(dut, "rs232")
  198. assert int(dut.stimulus_active_mon.value) == 1
  199. assert array(soft, "ptc_applied")[0] < 35
  200. assert array(soft, "ptc_applied")[1:6] == [35] * 5
  201. assert array(soft, "front_applied")[0] == 80
  202. assert array(soft, "rear_applied")[0] == 80
  203. old_control_epoch = int(dut.control_epoch_mon.value)
  204. await send_bmc_snapshot(dut, [76, *([60] * 23)])
  205. await wait_for(
  206. dut,
  207. lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
  208. cycles=1000,
  209. label="pretrip stimulus preemption",
  210. )
  211. pretrip = await get_snapshot(dut, "ttl")
  212. assert int(dut.stimulus_active_mon.value) == 1
  213. assert array(pretrip, "ptc_applied")[0] == 0
  214. assert array(pretrip, "front_applied")[0] == 80
  215. assert array(pretrip, "rear_applied")[0] == 80
  216. await send_bmc_snapshot(dut, [77, *([60] * 23)])
  217. await wait_for(
  218. dut,
  219. lambda: int(dut.stimulus_active_mon.value) == 0
  220. and bool(int(dut.lane_fault_mask_mon.value) & 1),
  221. cycles=1000,
  222. label="hard-limit stimulus preemption",
  223. )
  224. hard = await get_snapshot(dut, "rs232")
  225. assert array(hard, "ptc_applied")[:6] == [0] * 6
  226. assert array(hard, "front_applied")[0] == 100
  227. assert array(hard, "rear_applied")[0] == 100