test_6400_v1_host_protocol_integration.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """Focused full-UART test for the destructive 6400 Host protocol migration."""
  2. from __future__ import annotations
  3. import cocotb
  4. from cocotb.triggers import RisingEdge, Timer, with_timeout
  5. from thermal_6400_uart import (
  6. ACK_NACK, CLOCK_NS, GET_SNAPSHOT, HOST_CLKS_PER_BIT, SET_ALL,
  7. SET_PID_TARGET, SET_STIMULUS, SNAPSHOT_DATA, clock_cycles, encode_frame,
  8. receive_frame, send_uart, wait_for,
  9. )
  10. async def transact(dut, kind: int, payload: bytes) -> tuple[int, bytes]:
  11. response = cocotb.start_soon(receive_frame(dut.host_rs232_tx))
  12. await send_uart(dut.host_rs232_rx, encode_frame(kind, payload), HOST_CLKS_PER_BIT)
  13. result = await with_timeout(response, 25, "ms")
  14. await Timer(HOST_CLKS_PER_BIT * CLOCK_NS * 2, units="ns")
  15. await wait_for(dut, lambda: int(dut.host_locked_mon.value) == 0, cycles=1500, label="unlock")
  16. return result
  17. def ack(payload: bytes, ok: int, request: int, status: int, detail: int = 0) -> None:
  18. assert payload == bytes((ok, request, status, detail))
  19. @cocotb.test()
  20. async def test_host_ack_nack_crc_silence_atomic_set_all_and_get_order(dut):
  21. dut.host_rs232_rx.value = 1
  22. dut.host_ttl_rx.value = 1
  23. dut.bmc_rs232_rx.value = 1
  24. dut.tick_1s_test.value = 0
  25. await with_timeout(RisingEdge(dut.rst_n_mon), 1, "ms")
  26. await clock_cycles(dut, 40)
  27. # Complete CRC-correct requests always get one unified response.
  28. kind, payload = await transact(dut, SET_ALL, bytes(31))
  29. assert kind == ACK_NACK; ack(payload, 0, SET_ALL, 2, 32)
  30. kind, payload = await transact(dut, SET_STIMULUS, bytes((1,)) + bytes(32))
  31. assert kind == ACK_NACK; ack(payload, 0, SET_STIMULUS, 1)
  32. kind, payload = await transact(dut, 0x55, b"")
  33. assert kind == ACK_NACK; ack(payload, 0, 0x55, 1)
  34. # Oversized requests are bounded streaming discards, not resync points.
  35. # The whole payload (including embedded A5 5A) and CRC must retire before
  36. # one length/unsupported response is emitted; no ghost command is formed.
  37. embedded = bytearray(34)
  38. embedded[7:9] = b"\xA5\x5A"
  39. before = int(dut.host_response_count_mon.value)
  40. kind, payload = await transact(dut, SET_ALL, bytes(embedded))
  41. assert kind == ACK_NACK; ack(payload, 0, SET_ALL, 2, 32)
  42. assert int(dut.host_response_count_mon.value) == before + 1
  43. oversized = bytearray((index * 17) & 0xFF for index in range(255))
  44. oversized[120:122] = b"\xA5\x5A"
  45. before = int(dut.host_response_count_mon.value)
  46. kind, payload = await transact(dut, 0x55, bytes(oversized))
  47. assert kind == ACK_NACK; ack(payload, 0, 0x55, 1)
  48. assert int(dut.host_response_count_mon.value) == before + 1
  49. # First invalid index is reported and nothing enters Manual.
  50. bad = bytearray([0] * 24 + [20] * 8)
  51. bad[7] = 101
  52. commits = int(dut.target_commit_count_mon.value)
  53. kind, payload = await transact(dut, SET_ALL, bytes(bad))
  54. assert kind == ACK_NACK; ack(payload, 0, SET_ALL, 3, 7)
  55. assert int(dut.target_commit_count_mon.value) == commits
  56. assert int(dut.control_state_mon.value) == 0
  57. # Legal and identical SET_ALL both atomically commit/feed and ACK.
  58. vector = bytes([50] * 24 + [0, 20, 50, 100, 0, 20, 50, 100])
  59. for repeat in range(2):
  60. before = int(dut.target_commit_count_mon.value)
  61. kind, payload = await transact(dut, SET_ALL, vector)
  62. assert kind == ACK_NACK; ack(payload, 1, SET_ALL, 0)
  63. assert int(dut.target_commit_count_mon.value) == before + 1
  64. assert int(dut.control_state_mon.value) == 1
  65. # PID target ACKs but does not exit Manual or feed via a SET_ALL commit.
  66. before = int(dut.target_commit_count_mon.value)
  67. kind, payload = await transact(dut, SET_PID_TARGET, b"\x46")
  68. assert kind == ACK_NACK; ack(payload, 1, SET_PID_TARGET, 0)
  69. assert int(dut.current_target_mon.value) == 70
  70. assert int(dut.control_state_mon.value) == 1
  71. assert int(dut.target_commit_count_mon.value) == before
  72. # GET locks one snapshot and produces exactly 06 then 82.
  73. ack_task = cocotb.start_soon(receive_frame(dut.host_rs232_tx))
  74. await send_uart(dut.host_rs232_rx, encode_frame(GET_SNAPSHOT), HOST_CLKS_PER_BIT)
  75. kind, payload = await with_timeout(ack_task, 25, "ms")
  76. assert kind == ACK_NACK; ack(payload, 1, GET_SNAPSHOT, 0)
  77. assert int(dut.host_locked_mon.value) == 1
  78. data_kind, data = await with_timeout(receive_frame(dut.host_rs232_tx), 25, "ms")
  79. assert data_kind == SNAPSHOT_DATA and len(data) == 201
  80. await wait_for(dut, lambda: int(dut.host_locked_mon.value) == 0, cycles=1500, label="GET unlock")
  81. # CRC damage and partial timeout are silent; the next legal request resyncs.
  82. responses = int(dut.host_response_count_mon.value)
  83. damaged = bytearray(encode_frame(SET_PID_TARGET, b"\x41")); damaged[-1] ^= 1
  84. await send_uart(dut.host_rs232_rx, damaged, HOST_CLKS_PER_BIT)
  85. await clock_cycles(dut, 1200)
  86. assert int(dut.host_response_count_mon.value) == responses
  87. await send_uart(dut.host_rs232_rx, b"\xA5\x5A\x04\x01", HOST_CLKS_PER_BIT)
  88. await clock_cycles(dut, 1200)
  89. assert int(dut.host_response_count_mon.value) == responses
  90. kind, payload = await transact(dut, SET_PID_TARGET, b"\x41")
  91. assert kind == ACK_NACK; ack(payload, 1, SET_PID_TARGET, 0)
  92. assert int(dut.current_target_mon.value) == 65