| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- """RED tests for the independent RX-to-axis JOG fast path."""
- from __future__ import annotations
- import sys
- from pathlib import Path
- import cocotb
- from cocotb.clock import Clock
- from cocotb.triggers import ClockCycles, FallingEdge, ReadOnly, RisingEdge
- ROOT = Path(__file__).resolve().parents[2]
- sys.path.insert(0, str(ROOT / "tools"))
- import prod2u_p4_protocol as protocol # noqa: E402
- HOST_ACK = int(protocol.FrameType.HOST_ACK)
- GET_STATUS = int(protocol.CommandId.GET_STATUS)
- SET_SNAPSHOT = int(protocol.CommandId.SET_OUTPUT_SNAPSHOT)
- JOG = int(protocol.MotionOpcode.JOG)
- async def start_and_reset(dut) -> None:
- cocotb.start_soon(Clock(dut.clk, 10, unit="ns").start())
- dut.rst_n.value = 0
- dut.rx_valid.value = 0
- dut.rx_data.value = 0
- dut.frame_ready.value = 0
- await ClockCycles(dut.clk, 5)
- dut.rst_n.value = 1
- await ClockCycles(dut.clk, 5)
- async def send_frame_bytes(dut, frame: bytes) -> None:
- for byte in frame:
- for _ in range(256):
- await FallingEdge(dut.clk)
- if int(dut.rx_ready.value):
- break
- else:
- raise AssertionError("RX stopped accepting bytes while a fast JOG was pending")
- dut.rx_data.value = byte
- dut.rx_valid.value = 1
- await RisingEdge(dut.clk)
- await FallingEdge(dut.clk)
- dut.rx_valid.value = 0
- async def wait_until(dut, predicate, cycles: int, message: str) -> None:
- for _ in range(cycles):
- await RisingEdge(dut.clk)
- await ReadOnly()
- if predicate():
- return
- raise AssertionError(message)
- async def step_monitor(dut, observed: dict[str, int | bool]) -> None:
- previous_x = 0
- previous_y = 0
- while True:
- await RisingEdge(dut.clk)
- await ReadOnly()
- x_step = int(dut.step_out.value) & 1
- y_step = (int(dut.step_out.value) >> 1) & 1
- if x_step and not previous_x:
- observed["x_rises"] = int(observed["x_rises"]) + 1
- if y_step and not previous_y:
- observed["y_rises"] = int(observed["y_rises"]) + 1
- if x_step and y_step:
- observed["overlap"] = True
- previous_x = x_step
- previous_y = y_step
- async def ack_monitor(dut, commands: list[int]) -> None:
- while True:
- await FallingEdge(dut.clk)
- await ReadOnly()
- if (
- int(dut.frame_valid.value)
- and int(dut.frame_ready.value)
- and int(dut.frame_type.value) == HOST_ACK
- ):
- commands.append((int(dut.frame_payload.value) >> 8) & 0xFF)
- def build_snapshot() -> bytes:
- return protocol.build_output_snapshot(
- ptc_percent=[10] * 24,
- fan_percent=[30] * 4,
- solenoid=0,
- xyz=(255, 1, 77),
- )
- def build_jog(axis: int, direction: int, steps: int) -> bytes:
- return protocol.build_motor_jog(
- axis=axis,
- raw_dir=direction,
- speed_code=3,
- steps=steps,
- )
- @cocotb.test()
- async def fast_jog_runs_while_old_response_is_stalled(dut) -> None:
- await start_and_reset(dut)
- observed: dict[str, int | bool] = {
- "x_rises": 0,
- "y_rises": 0,
- "overlap": False,
- }
- acks: list[int] = []
- cocotb.start_soon(step_monitor(dut, observed))
- cocotb.start_soon(ack_monitor(dut, acks))
- await send_frame_bytes(
- dut,
- protocol.build_host_command(command=protocol.CommandId.GET_STATUS),
- )
- await wait_until(
- dut,
- lambda: int(dut.frame_valid.value) == 1,
- 64,
- "GET_STATUS response did not enter the stalled TX slot",
- )
- assert int(dut.frame_type.value) == HOST_ACK
- assert ((int(dut.frame_payload.value) >> 8) & 0xFF) == GET_STATUS
- assert int(dut.request_ready.value) == 0
- await send_frame_bytes(
- dut,
- protocol.build_host_command(command=protocol.CommandId.GET_STATUS),
- )
- await wait_until(
- dut,
- lambda: int(dut.request_valid.value) == 1
- and int(dut.request_ready.value) == 0,
- 32,
- "second ordinary request did not remain pending behind the old response",
- )
- await send_frame_bytes(dut, build_jog(axis=0, direction=1, steps=6))
- await wait_until(
- dut,
- lambda: int(observed["x_rises"]) >= 1,
- 32,
- "fast X JOG was blocked by command_inflight/TX backpressure",
- )
- assert ((int(dut.frame_payload.value) >> 8) & 0xFF) == GET_STATUS
- await wait_until(
- dut,
- lambda: int(observed["x_rises"]) == 6,
- 256,
- "X motion did not finish while TX remained stalled",
- )
- assert acks == []
- await FallingEdge(dut.clk)
- dut.frame_ready.value = 1
- await wait_until(
- dut,
- lambda: JOG in acks,
- 512,
- "pending JOG ACK did not drain after TX resumed",
- )
- assert acks[0] == GET_STATUS
- @cocotb.test()
- async def fast_jog_runs_while_core_is_non_idle(dut) -> None:
- await start_and_reset(dut)
- observed: dict[str, int | bool] = {
- "x_rises": 0,
- "y_rises": 0,
- "overlap": False,
- }
- cocotb.start_soon(step_monitor(dut, observed))
- await send_frame_bytes(dut, build_snapshot())
- await wait_until(
- dut,
- lambda: int(dut.request_ready.value) == 0,
- 16,
- "snapshot did not make the normal core path non-IDLE",
- )
- await send_frame_bytes(dut, build_jog(axis=1, direction=0, steps=3))
- await wait_until(
- dut,
- lambda: int(observed["y_rises"]) == 3,
- 192,
- "fast Y JOG was blocked by the normal snapshot state machine",
- )
- assert int(observed["x_rises"]) == 0
- @cocotb.test()
- async def x_ack_then_y_jog_runs_with_axis_overlap(dut) -> None:
- await start_and_reset(dut)
- dut.frame_ready.value = 1
- observed: dict[str, int | bool] = {
- "x_rises": 0,
- "y_rises": 0,
- "overlap": False,
- }
- acks: list[int] = []
- cocotb.start_soon(step_monitor(dut, observed))
- cocotb.start_soon(ack_monitor(dut, acks))
- await send_frame_bytes(dut, build_jog(axis=0, direction=0, steps=40))
- await wait_until(
- dut,
- lambda: len(acks) >= 1,
- 128,
- "X JOG ACK did not follow the first X STEP",
- )
- assert acks[0] == JOG
- assert int(observed["x_rises"]) >= 1
- await send_frame_bytes(dut, build_jog(axis=1, direction=1, steps=6))
- await wait_until(
- dut,
- lambda: len(acks) >= 2,
- 128,
- "Y JOG ACK did not follow the first Y STEP",
- )
- assert acks[1] == JOG
- assert int(observed["y_rises"]) >= 1
- await wait_until(
- dut,
- lambda: int(observed["x_rises"]) == 40
- and int(observed["y_rises"]) == 6,
- 512,
- "X/Y independent pulse counts did not complete",
- )
- assert bool(observed["overlap"]), "X/Y STEP waveforms never overlapped"
|