test_prod2u_p4_temperature_contract.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/env python3
  2. """Wire-contract tests for the on-demand CT7428 temperature response."""
  3. from __future__ import annotations
  4. import json
  5. import re
  6. from pathlib import Path
  7. import pytest
  8. from tools import prod2u_p4_protocol as protocol
  9. ROOT = Path(__file__).resolve().parents[2]
  10. HOST_CONTRACT = ROOT / "config" / "prod2u_p4_normal_host_contract_v4.json"
  11. HOST_SCHEMA = ROOT / "schemas" / "prod2u_p4_normal_host_contract_v4.schema.json"
  12. GOLDEN_REQUEST = bytes.fromhex(
  13. "A55A020405000000000000000004280000009E0C"
  14. )
  15. GOLDEN_RESPONSE = bytes.fromhex(
  16. "A55A0204101B"
  17. "000102030405060708090A0B0C0D0E0F101112131415161718191A"
  18. "1FC9"
  19. )
  20. ALL_FF_RESPONSE = bytes.fromhex(
  21. "A55A0204101B"
  22. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  23. "1BC4"
  24. )
  25. def test_get_temperatures_request_matches_the_frozen_wire_frame() -> None:
  26. assert protocol.CommandId.GET_TEMPERATURES == 0x28
  27. assert protocol.build_get_temperatures() == GOLDEN_REQUEST
  28. decoded = protocol.decode_frame(GOLDEN_REQUEST)
  29. assert decoded.frame_type == protocol.FrameType.HOST_CMD
  30. assert decoded.payload == bytes.fromhex("28000000")
  31. def test_temperature_response_matches_the_frozen_35_byte_frame() -> None:
  32. values = bytes(range(27))
  33. response = protocol.build_temperature_response(values)
  34. assert response == GOLDEN_RESPONSE
  35. assert len(response) == 35
  36. assert response[:6] == bytes.fromhex("A55A0204101B")
  37. assert response[6:33] == values
  38. assert response[-2:] == protocol.crc16_ccitt(response[2:-2]).to_bytes(
  39. 2, "little"
  40. )
  41. def test_temperature_response_all_ff_frame_matches_the_frozen_crc() -> None:
  42. response = protocol.build_temperature_response(bytes([0xFF] * 27))
  43. assert response == ALL_FF_RESPONSE
  44. assert response[-2:] == bytes.fromhex("1BC4")
  45. def test_temperature_response_decoder_accepts_only_the_compact_wire_contract() -> None:
  46. assert protocol.decode_temperature_response(GOLDEN_RESPONSE) == bytes(range(27))
  47. for invalid in (
  48. GOLDEN_RESPONSE[:-1],
  49. GOLDEN_RESPONSE[:4] + b"\x11" + GOLDEN_RESPONSE[5:],
  50. GOLDEN_RESPONSE[:-1] + bytes([GOLDEN_RESPONSE[-1] ^ 0x01]),
  51. ):
  52. with pytest.raises(protocol.P4FrameError):
  53. protocol.decode_temperature_response(invalid)
  54. def test_temperature_bus_error_ack_decodes_as_status_0x0a() -> None:
  55. wire = protocol.encode_frame(
  56. frame_type=protocol.FrameType.HOST_ACK,
  57. seq=0,
  58. timestamp_tick=0,
  59. payload=bytes.fromhex("00280A0000000000"),
  60. )
  61. ack = protocol.decode_host_ack(protocol.decode_frame(wire))
  62. assert ack.command == protocol.CommandId.GET_TEMPERATURES
  63. assert ack.status == protocol.StatusCode.TEMPERATURE_BUS_ERROR
  64. def test_machine_contract_freezes_temperature_request_response_and_failures() -> None:
  65. contract = json.loads(HOST_CONTRACT.read_text(encoding="utf-8"))
  66. command = contract["commands"]["get_temperatures"]
  67. response = contract["responses"]["temperature_short"]
  68. assert contract["frame_types"]["TEMPERATURE_SHORT"] == 0x10
  69. assert contract["wire"]["temperature_response_order"] == [
  70. "TEMPERATURE_SHORT"
  71. ]
  72. assert command == {
  73. "frame_type": 5,
  74. "command": 0x28,
  75. "payload_length": 4,
  76. "request_payload_hex": "28 00 00 00",
  77. "success_response": "TEMPERATURE_SHORT_ONLY_NO_ACK",
  78. "powerup_under_one_second": "ALL_FF_NO_I2C",
  79. "nack_policy": "CURRENT_VALUE_FF_CONTINUE",
  80. "bus_error_status": "TEMPERATURE_BUS_ERROR",
  81. "bus_error_status_code": 0x0A,
  82. "read_order": ["U6", "U24", "U25"],
  83. "addresses": [0x49, 0x48, 0x4B],
  84. "registers": list(range(9)),
  85. }
  86. assert response["frame_type"] == 0x10
  87. assert response["payload_length"] == 27
  88. assert response["full_frame_length"] == 35
  89. assert response["layout"] == "A5 5A 02 04 10 1B values[27] crc16_le"
  90. assert response["crc_coverage"] == "02 04 10 1B values[27]"
  91. assert response["value_order"] == [
  92. "U6.local",
  93. "U6.remote1..8",
  94. "U24.local",
  95. "U24.remote1..8",
  96. "U25.local",
  97. "U25.remote1..8",
  98. ]
  99. assert contract["responses"]["host_ack"]["status_codes"][
  100. "TEMPERATURE_BUS_ERROR"
  101. ] == 0x0A
  102. def test_machine_schema_requires_temperature_contract_sections() -> None:
  103. schema = json.loads(HOST_SCHEMA.read_text(encoding="utf-8"))
  104. assert "get_temperatures" in schema["properties"]["commands"]["required"]
  105. assert "temperature_short" in schema["properties"]["responses"]["required"]
  106. assert schema["properties"]["frame_types"]["const"][
  107. "TEMPERATURE_SHORT"
  108. ] == 0x10
  109. temperature_def = schema["$defs"]["temperatureCommand"]
  110. assert temperature_def["properties"]["command"]["const"] == 0x28
  111. assert temperature_def["properties"]["bus_error_status_code"]["const"] == 0x0A
  112. @pytest.mark.parametrize(
  113. ("relative", "declaration"),
  114. [
  115. ("src/rtl/prod2u_p4_normal_v3_core.v", r"reg\s+\[2:0\]\s+state"),
  116. ("src/rtl/prod2u_p4_i2c_stop_start_read.v", r"reg\s+\[4:0\]\s+state"),
  117. ("src/rtl/prod2u_p4_temperature_read.v", r"reg\s+\[1:0\]\s+state"),
  118. ("src/rtl/prod2u_p4_temperature_short_tx.v", r"reg\s+\[1:0\]\s+state"),
  119. ("src/rtl/prod2u_p4_normal_v3_tx.v", r"reg\s+\[1:0\]\s+owner"),
  120. ],
  121. )
  122. def test_temperature_path_uses_compact_fsm_encoding(
  123. relative: str,
  124. declaration: str,
  125. ) -> None:
  126. source = (ROOT / relative).read_text(encoding="ascii")
  127. pattern = (
  128. r"\(\*\s*keep\s*=\s*\"true\"\s*,\s*"
  129. r"syn_encoding\s*=\s*\"sequential\"\s*,\s*"
  130. r"fsm_encoding\s*=\s*\"none\"\s*\*\)\s*"
  131. + declaration
  132. )
  133. assert re.search(pattern, source)
  134. def test_temperature_store_is_single_block_ram_candidate() -> None:
  135. source = (ROOT / "src/rtl/prod2u_p4_temperature_store.v").read_text(
  136. encoding="ascii"
  137. )
  138. assert "EF2_LOGIC_BRAM #(\n" in source
  139. assert '.IMPLEMENT ("9K")' in source
  140. assert "// synthesis translate_off" in source
  141. assert "module prod2u_p4_temperature_bram_sim" in source
  142. def test_temperature_store_formal_only_constrains_valid_read_data() -> None:
  143. source = (
  144. ROOT / "tests/formal/prod2u_p4_temperature_store_contract_formal.v"
  145. ).read_text(encoding="ascii")
  146. assert "$past(!rd_req)" not in source
  147. assert "$stable(rd_data)" not in source
  148. @pytest.mark.parametrize(
  149. "values",
  150. [
  151. bytes(26),
  152. bytes(28),
  153. [0] * 26 + [256],
  154. [0] * 26 + [-1],
  155. ],
  156. )
  157. def test_temperature_response_rejects_non_27_byte_payloads(values: object) -> None:
  158. with pytest.raises(ValueError):
  159. protocol.build_temperature_response(values)