| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- #!/usr/bin/env python3
- """Wire-contract tests for the on-demand CT7428 temperature response."""
- from __future__ import annotations
- import json
- import re
- from pathlib import Path
- import pytest
- from tools import prod2u_p4_protocol as protocol
- ROOT = Path(__file__).resolve().parents[2]
- HOST_CONTRACT = ROOT / "config" / "prod2u_p4_normal_host_contract_v4.json"
- HOST_SCHEMA = ROOT / "schemas" / "prod2u_p4_normal_host_contract_v4.schema.json"
- GOLDEN_REQUEST = bytes.fromhex(
- "A55A020405000000000000000004280000009E0C"
- )
- GOLDEN_RESPONSE = bytes.fromhex(
- "A55A0204101B"
- "000102030405060708090A0B0C0D0E0F101112131415161718191A"
- "1FC9"
- )
- ALL_FF_RESPONSE = bytes.fromhex(
- "A55A0204101B"
- "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
- "1BC4"
- )
- def test_get_temperatures_request_matches_the_frozen_wire_frame() -> None:
- assert protocol.CommandId.GET_TEMPERATURES == 0x28
- assert protocol.build_get_temperatures() == GOLDEN_REQUEST
- decoded = protocol.decode_frame(GOLDEN_REQUEST)
- assert decoded.frame_type == protocol.FrameType.HOST_CMD
- assert decoded.payload == bytes.fromhex("28000000")
- def test_temperature_response_matches_the_frozen_35_byte_frame() -> None:
- values = bytes(range(27))
- response = protocol.build_temperature_response(values)
- assert response == GOLDEN_RESPONSE
- assert len(response) == 35
- assert response[:6] == bytes.fromhex("A55A0204101B")
- assert response[6:33] == values
- assert response[-2:] == protocol.crc16_ccitt(response[2:-2]).to_bytes(
- 2, "little"
- )
- def test_temperature_response_all_ff_frame_matches_the_frozen_crc() -> None:
- response = protocol.build_temperature_response(bytes([0xFF] * 27))
- assert response == ALL_FF_RESPONSE
- assert response[-2:] == bytes.fromhex("1BC4")
- def test_temperature_response_decoder_accepts_only_the_compact_wire_contract() -> None:
- assert protocol.decode_temperature_response(GOLDEN_RESPONSE) == bytes(range(27))
- for invalid in (
- GOLDEN_RESPONSE[:-1],
- GOLDEN_RESPONSE[:4] + b"\x11" + GOLDEN_RESPONSE[5:],
- GOLDEN_RESPONSE[:-1] + bytes([GOLDEN_RESPONSE[-1] ^ 0x01]),
- ):
- with pytest.raises(protocol.P4FrameError):
- protocol.decode_temperature_response(invalid)
- def test_temperature_bus_error_ack_decodes_as_status_0x0a() -> None:
- wire = protocol.encode_frame(
- frame_type=protocol.FrameType.HOST_ACK,
- seq=0,
- timestamp_tick=0,
- payload=bytes.fromhex("00280A0000000000"),
- )
- ack = protocol.decode_host_ack(protocol.decode_frame(wire))
- assert ack.command == protocol.CommandId.GET_TEMPERATURES
- assert ack.status == protocol.StatusCode.TEMPERATURE_BUS_ERROR
- def test_machine_contract_freezes_temperature_request_response_and_failures() -> None:
- contract = json.loads(HOST_CONTRACT.read_text(encoding="utf-8"))
- command = contract["commands"]["get_temperatures"]
- response = contract["responses"]["temperature_short"]
- assert contract["frame_types"]["TEMPERATURE_SHORT"] == 0x10
- assert contract["wire"]["temperature_response_order"] == [
- "TEMPERATURE_SHORT"
- ]
- assert command == {
- "frame_type": 5,
- "command": 0x28,
- "payload_length": 4,
- "request_payload_hex": "28 00 00 00",
- "success_response": "TEMPERATURE_SHORT_ONLY_NO_ACK",
- "powerup_under_one_second": "ALL_FF_NO_I2C",
- "nack_policy": "CURRENT_VALUE_FF_CONTINUE",
- "bus_error_status": "TEMPERATURE_BUS_ERROR",
- "bus_error_status_code": 0x0A,
- "read_order": ["U6", "U24", "U25"],
- "addresses": [0x49, 0x48, 0x4B],
- "registers": list(range(9)),
- }
- assert response["frame_type"] == 0x10
- assert response["payload_length"] == 27
- assert response["full_frame_length"] == 35
- assert response["layout"] == "A5 5A 02 04 10 1B values[27] crc16_le"
- assert response["crc_coverage"] == "02 04 10 1B values[27]"
- assert response["value_order"] == [
- "U6.local",
- "U6.remote1..8",
- "U24.local",
- "U24.remote1..8",
- "U25.local",
- "U25.remote1..8",
- ]
- assert contract["responses"]["host_ack"]["status_codes"][
- "TEMPERATURE_BUS_ERROR"
- ] == 0x0A
- def test_machine_schema_requires_temperature_contract_sections() -> None:
- schema = json.loads(HOST_SCHEMA.read_text(encoding="utf-8"))
- assert "get_temperatures" in schema["properties"]["commands"]["required"]
- assert "temperature_short" in schema["properties"]["responses"]["required"]
- assert schema["properties"]["frame_types"]["const"][
- "TEMPERATURE_SHORT"
- ] == 0x10
- temperature_def = schema["$defs"]["temperatureCommand"]
- assert temperature_def["properties"]["command"]["const"] == 0x28
- assert temperature_def["properties"]["bus_error_status_code"]["const"] == 0x0A
- @pytest.mark.parametrize(
- ("relative", "declaration"),
- [
- ("src/rtl/prod2u_p4_normal_v3_core.v", r"reg\s+\[2:0\]\s+state"),
- ("src/rtl/prod2u_p4_i2c_stop_start_read.v", r"reg\s+\[4:0\]\s+state"),
- ("src/rtl/prod2u_p4_temperature_read.v", r"reg\s+\[1:0\]\s+state"),
- ("src/rtl/prod2u_p4_temperature_short_tx.v", r"reg\s+\[1:0\]\s+state"),
- ("src/rtl/prod2u_p4_normal_v3_tx.v", r"reg\s+\[1:0\]\s+owner"),
- ],
- )
- def test_temperature_path_uses_compact_fsm_encoding(
- relative: str,
- declaration: str,
- ) -> None:
- source = (ROOT / relative).read_text(encoding="ascii")
- pattern = (
- r"\(\*\s*keep\s*=\s*\"true\"\s*,\s*"
- r"syn_encoding\s*=\s*\"sequential\"\s*,\s*"
- r"fsm_encoding\s*=\s*\"none\"\s*\*\)\s*"
- + declaration
- )
- assert re.search(pattern, source)
- def test_temperature_store_is_single_block_ram_candidate() -> None:
- source = (ROOT / "src/rtl/prod2u_p4_temperature_store.v").read_text(
- encoding="ascii"
- )
- assert "EF2_LOGIC_BRAM #(\n" in source
- assert '.IMPLEMENT ("9K")' in source
- assert "// synthesis translate_off" in source
- assert "module prod2u_p4_temperature_bram_sim" in source
- def test_temperature_store_formal_only_constrains_valid_read_data() -> None:
- source = (
- ROOT / "tests/formal/prod2u_p4_temperature_store_contract_formal.v"
- ).read_text(encoding="ascii")
- assert "$past(!rd_req)" not in source
- assert "$stable(rd_data)" not in source
- @pytest.mark.parametrize(
- "values",
- [
- bytes(26),
- bytes(28),
- [0] * 26 + [256],
- [0] * 26 + [-1],
- ],
- )
- def test_temperature_response_rejects_non_27_byte_payloads(values: object) -> None:
- with pytest.raises(ValueError):
- protocol.build_temperature_response(values)
|