test_prod2u_p4_fast_jog_top.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. """RED tests for the independent RX-to-axis JOG fast path."""
  2. from __future__ import annotations
  3. import sys
  4. from pathlib import Path
  5. import cocotb
  6. from cocotb.clock import Clock
  7. from cocotb.triggers import ClockCycles, FallingEdge, ReadOnly, RisingEdge
  8. ROOT = Path(__file__).resolve().parents[2]
  9. sys.path.insert(0, str(ROOT / "tools"))
  10. import prod2u_p4_protocol as protocol # noqa: E402
  11. HOST_ACK = int(protocol.FrameType.HOST_ACK)
  12. GET_STATUS = int(protocol.CommandId.GET_STATUS)
  13. SET_SNAPSHOT = int(protocol.CommandId.SET_OUTPUT_SNAPSHOT)
  14. JOG = int(protocol.MotionOpcode.JOG)
  15. async def start_and_reset(dut) -> None:
  16. cocotb.start_soon(Clock(dut.clk, 10, unit="ns").start())
  17. dut.rst_n.value = 0
  18. dut.rx_valid.value = 0
  19. dut.rx_data.value = 0
  20. dut.frame_ready.value = 0
  21. await ClockCycles(dut.clk, 5)
  22. dut.rst_n.value = 1
  23. await ClockCycles(dut.clk, 5)
  24. async def send_frame_bytes(dut, frame: bytes) -> None:
  25. for byte in frame:
  26. for _ in range(256):
  27. await FallingEdge(dut.clk)
  28. if int(dut.rx_ready.value):
  29. break
  30. else:
  31. raise AssertionError("RX stopped accepting bytes while a fast JOG was pending")
  32. dut.rx_data.value = byte
  33. dut.rx_valid.value = 1
  34. await RisingEdge(dut.clk)
  35. await FallingEdge(dut.clk)
  36. dut.rx_valid.value = 0
  37. async def wait_until(dut, predicate, cycles: int, message: str) -> None:
  38. for _ in range(cycles):
  39. await RisingEdge(dut.clk)
  40. await ReadOnly()
  41. if predicate():
  42. return
  43. raise AssertionError(message)
  44. async def step_monitor(dut, observed: dict[str, int | bool]) -> None:
  45. previous_x = 0
  46. previous_y = 0
  47. while True:
  48. await RisingEdge(dut.clk)
  49. await ReadOnly()
  50. x_step = int(dut.step_out.value) & 1
  51. y_step = (int(dut.step_out.value) >> 1) & 1
  52. if x_step and not previous_x:
  53. observed["x_rises"] = int(observed["x_rises"]) + 1
  54. if y_step and not previous_y:
  55. observed["y_rises"] = int(observed["y_rises"]) + 1
  56. if x_step and y_step:
  57. observed["overlap"] = True
  58. previous_x = x_step
  59. previous_y = y_step
  60. async def ack_monitor(dut, commands: list[int]) -> None:
  61. while True:
  62. await FallingEdge(dut.clk)
  63. await ReadOnly()
  64. if (
  65. int(dut.frame_valid.value)
  66. and int(dut.frame_ready.value)
  67. and int(dut.frame_type.value) == HOST_ACK
  68. ):
  69. commands.append((int(dut.frame_payload.value) >> 8) & 0xFF)
  70. def build_snapshot() -> bytes:
  71. return protocol.build_output_snapshot(
  72. ptc_percent=[10] * 24,
  73. fan_percent=[30] * 4,
  74. solenoid=0,
  75. xyz=(255, 1, 77),
  76. )
  77. def build_jog(axis: int, direction: int, steps: int) -> bytes:
  78. return protocol.build_motor_jog(
  79. axis=axis,
  80. raw_dir=direction,
  81. speed_code=3,
  82. steps=steps,
  83. )
  84. @cocotb.test()
  85. async def fast_jog_runs_while_old_response_is_stalled(dut) -> None:
  86. await start_and_reset(dut)
  87. observed: dict[str, int | bool] = {
  88. "x_rises": 0,
  89. "y_rises": 0,
  90. "overlap": False,
  91. }
  92. acks: list[int] = []
  93. cocotb.start_soon(step_monitor(dut, observed))
  94. cocotb.start_soon(ack_monitor(dut, acks))
  95. await send_frame_bytes(
  96. dut,
  97. protocol.build_host_command(command=protocol.CommandId.GET_STATUS),
  98. )
  99. await wait_until(
  100. dut,
  101. lambda: int(dut.frame_valid.value) == 1,
  102. 64,
  103. "GET_STATUS response did not enter the stalled TX slot",
  104. )
  105. assert int(dut.frame_type.value) == HOST_ACK
  106. assert ((int(dut.frame_payload.value) >> 8) & 0xFF) == GET_STATUS
  107. assert int(dut.request_ready.value) == 0
  108. await send_frame_bytes(
  109. dut,
  110. protocol.build_host_command(command=protocol.CommandId.GET_STATUS),
  111. )
  112. await wait_until(
  113. dut,
  114. lambda: int(dut.request_valid.value) == 1
  115. and int(dut.request_ready.value) == 0,
  116. 32,
  117. "second ordinary request did not remain pending behind the old response",
  118. )
  119. await send_frame_bytes(dut, build_jog(axis=0, direction=1, steps=6))
  120. await wait_until(
  121. dut,
  122. lambda: int(observed["x_rises"]) >= 1,
  123. 32,
  124. "fast X JOG was blocked by command_inflight/TX backpressure",
  125. )
  126. assert ((int(dut.frame_payload.value) >> 8) & 0xFF) == GET_STATUS
  127. await wait_until(
  128. dut,
  129. lambda: int(observed["x_rises"]) == 6,
  130. 256,
  131. "X motion did not finish while TX remained stalled",
  132. )
  133. assert acks == []
  134. await FallingEdge(dut.clk)
  135. dut.frame_ready.value = 1
  136. await wait_until(
  137. dut,
  138. lambda: JOG in acks,
  139. 512,
  140. "pending JOG ACK did not drain after TX resumed",
  141. )
  142. assert acks[0] == GET_STATUS
  143. @cocotb.test()
  144. async def fast_jog_runs_while_core_is_non_idle(dut) -> None:
  145. await start_and_reset(dut)
  146. observed: dict[str, int | bool] = {
  147. "x_rises": 0,
  148. "y_rises": 0,
  149. "overlap": False,
  150. }
  151. cocotb.start_soon(step_monitor(dut, observed))
  152. await send_frame_bytes(dut, build_snapshot())
  153. await wait_until(
  154. dut,
  155. lambda: int(dut.request_ready.value) == 0,
  156. 16,
  157. "snapshot did not make the normal core path non-IDLE",
  158. )
  159. await send_frame_bytes(dut, build_jog(axis=1, direction=0, steps=3))
  160. await wait_until(
  161. dut,
  162. lambda: int(observed["y_rises"]) == 3,
  163. 192,
  164. "fast Y JOG was blocked by the normal snapshot state machine",
  165. )
  166. assert int(observed["x_rises"]) == 0
  167. @cocotb.test()
  168. async def x_ack_then_y_jog_runs_with_axis_overlap(dut) -> None:
  169. await start_and_reset(dut)
  170. dut.frame_ready.value = 1
  171. observed: dict[str, int | bool] = {
  172. "x_rises": 0,
  173. "y_rises": 0,
  174. "overlap": False,
  175. }
  176. acks: list[int] = []
  177. cocotb.start_soon(step_monitor(dut, observed))
  178. cocotb.start_soon(ack_monitor(dut, acks))
  179. await send_frame_bytes(dut, build_jog(axis=0, direction=0, steps=40))
  180. await wait_until(
  181. dut,
  182. lambda: len(acks) >= 1,
  183. 128,
  184. "X JOG ACK did not follow the first X STEP",
  185. )
  186. assert acks[0] == JOG
  187. assert int(observed["x_rises"]) >= 1
  188. await send_frame_bytes(dut, build_jog(axis=1, direction=1, steps=6))
  189. await wait_until(
  190. dut,
  191. lambda: len(acks) >= 2,
  192. 128,
  193. "Y JOG ACK did not follow the first Y STEP",
  194. )
  195. assert acks[1] == JOG
  196. assert int(observed["y_rises"]) >= 1
  197. await wait_until(
  198. dut,
  199. lambda: int(observed["x_rises"]) == 40
  200. and int(observed["y_rises"]) == 6,
  201. 512,
  202. "X/Y independent pulse counts did not complete",
  203. )
  204. assert bool(observed["overlap"]), "X/Y STEP waveforms never overlapped"