"""Properties of the DVI TMDS code, checked on the Python model.""" from __future__ import annotations import random from tmds_model import TmdsEncoder, control_code, decode, int_to_bits, stage1, transitions def test_control_codes() -> None: # The value with q_out[0] as bit 0, as tmds_encoder.sv writes it. assert control_code(0, 0) == 0b1101010100 assert control_code(0, 1) == 0b0010101011 assert control_code(1, 0) == 0b0101010100 assert control_code(1, 1) == 0b1010101011 def test_460_unique_data_characters() -> None: """The specification says active data uses 460 of the 1024 characters.""" seen = set() for d in range(256): for cnt in range(-8, 9, 2): # every branch of the flow chart is reached enc = TmdsEncoder() enc.cnt = cnt seen.add(enc.encode(d, 0, 0, 1)) assert len(seen) == 460 controls = {control_code(c1, c0) for c1 in (0, 1) for c0 in (0, 1)} assert not seen & controls, "data characters never look like control characters" def test_stage1_has_at_most_three_transitions() -> None: worst = max(transitions(stage1(d)[:8]) for d in range(256)) assert worst == 3 assert max(transitions(int_to_bits(d, 8)) for d in range(256)) == 7 def test_decode_round_trip_and_bounded_disparity() -> None: rng = random.Random(19) for stream in ("random", 0x00, 0xFF, 0x10): enc = TmdsEncoder() disparity = 0 # ones minus zeros of all 10-bit characters sent worst = 0 for _ in range(200_000 if stream == "random" else 1000): d = rng.randrange(256) if stream == "random" else stream q = enc.encode(d, 0, 0, 1) assert decode(q) == d disparity += 2 * bin(q).count("1") - 10 worst = max(worst, abs(disparity), abs(enc.cnt)) assert worst <= 8, f"{stream}: running disparity reached {worst}"