"""Keep acknowledgement timing lossless and reject incomplete oracle evidence.""" import nuked_md import pytest from check_z80_ack import check_trace from conftest import ROOT # noqa: F401 def records(*, address=0xC00008, request_flags=21): return [ (1510302, 100, 0x7F08, 0, 120, nuked_md.EV_Z80, request_flags, 0), (1510452, 105, 0, 0, 60, nuked_md.EV_Z80, 21, 0), (1510389, 103, address, 0, 48, nuked_md.EV_M68K, 19, 0), (1510390, 103, address, 0, 0, nuked_md.EV_DTACK, 0, 0), ] def packed(events): return b"".join(nuked_md.EVENT.pack(*event) for event in events) @pytest.mark.parametrize( "address,destination", [ (0xC00008, "VDP"), (0x001000, "cartridge"), (0xFF0000, "work_ram"), ], ) @pytest.mark.parametrize("request_flags", [21, 22]) def test_one_rule_uses_observed_ack_for_every_destination(address, destination, request_flags): result = check_trace(packed(records(address=address, request_flags=request_flags))) assert result == { "accesses": 1, "ack_to_release_half_mclk": 47, "waits": {2: 1}, "ack_latency_half_mclk": {1: 1}, "destinations": {destination: 1}, } def test_half_master_clock_changes_the_required_wait(): events = records() ack = events[-1] events[-1] = (ack[0] + 1, *ack[1:]) with pytest.raises(ValueError, match="wait 2, expected 3"): check_trace(packed(events)) def test_delayed_acknowledgement_adds_a_wait(): events = [list(e) for e in records()] events[0][4] += 30 events[1][0] += 30 events[1][1] += 1 events[2][4] += 30 events[3][0] += 30 assert check_trace(packed(events))["waits"] == {3: 1} @pytest.mark.parametrize( "missing,match", [ (1, "missing next Z80"), (2, "missing shared-bus grant"), (3, "missing device acknowledgement"), ], ) def test_missing_evidence_is_not_a_pass(missing, match): events = records() del events[missing] with pytest.raises(ValueError, match=match): check_trace(packed(events)) @pytest.mark.parametrize( "index,field,value,match", [ (0, 4, 150, "wait/length"), (1, 1, 106, "wait/length"), (1, 0, 1510303, "wait/length"), (2, 4, 49, "shared-bus release"), (3, 2, 0xC0000A, "address differs"), (3, 6, 1, "missing device acknowledgement"), ], ) def test_inconsistent_evidence_is_rejected(index, field, value, match): events = [list(e) for e in records()] events[index][field] = value with pytest.raises(ValueError, match=match): check_trace(packed(events)) def test_boundary_window_keeps_the_next_cycle_as_evidence(): assert check_trace(packed(records()), start=1510302, end=1510303)["accesses"] == 1 with pytest.raises(ValueError, match="no complete shared-bus"): check_trace(packed(records()), start=1510303) def test_trailing_incomplete_request_requires_an_explicit_window(): events = records() tail = (events[0][0] + 1000, 110, *events[0][2:]) events.append(tail) with pytest.raises(ValueError, match="missing next Z80"): check_trace(packed(events)) assert check_trace(packed(events), end=tail[0])["accesses"] == 1 def test_empty_truncated_and_invalid_windows_are_rejected(): with pytest.raises(ValueError, match="no complete shared-bus"): check_trace(b"") with pytest.raises(ValueError, match="truncated"): check_trace(packed(records())[:-1]) for start, end in ((-1, None), (10, 10), (10, 9)): with pytest.raises(ValueError, match="invalid half-MCLK"): check_trace(packed(records()), start=start, end=end)