"""CPU FIFO reuse is later than memory commit on active H40 lines.""" from dataclasses import replace import nuked_md import pytest from run_fifo_release import TARGETS, cartridge, check_trace from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import System from megadrive_python.vdp import H32, H40, WRITE_CRAM, WRITE_VRAM, WRITE_VSRAM def prepared(target=WRITE_VSRAM, mode=H40, display=True, at=600): v = System(MACHINES["genesis"], Cartridge(bytes(0x10000))).vdp v.rendering = False v.registers[1] = 0x44 if display else 4 v.registers[12] = 0x81 if mode is H40 else 0 v.registers[15] = 2 v.v = 34 v.hmode = mode v._line_events() v._choose_slots() v.code = target v.sync(at) for data in range(4): v._push(data) return v @pytest.mark.parametrize("target", [WRITE_CRAM, WRITE_VSRAM]) @pytest.mark.parametrize("byte", [False, True]) def test_recently_committed_entry_still_delays_cpu_reuse(target, byte): v = prepared(target) v.sync(796) assert not v._fifo_full() assert v.fifo_cpu_ready[0] == 825 write = v.write_byte if byte else v.write_word assert write(0, 0x56, 800) == 25 assert v.mclk == 825 assert v.fifo[0][2] == (0x5656 if byte else 0x56) def test_full_fifo_waits_for_commit_then_acknowledgement_readiness(): v = prepared() assert v.write_word(0, 0x1234, 700) == 125 assert v.mclk == 825 def test_expired_readiness_does_not_add_a_new_delay(): v = prepared() v.sync(796) assert v.write_word(0, 0x1234, 850) == 0 def test_vram_releases_only_after_the_second_byte(): v = prepared(WRITE_VRAM) v.sync(796) assert v.fifo[0][4] assert v.fifo_cpu_ready[0] == 0 assert v.write_word(0, 0x1234, 800) == 153 assert v.fifo_cpu_ready[0] == 953 def test_slow_active_slot_uses_its_measured_release_delay(): v = prepared(at=300) v.sync(H40.offset_of[0x1CE]) assert not v.fifo[0][4] assert v.fifo_cpu_ready[0] == 356 @pytest.mark.parametrize("mode,display", [(H32, True), (H40, False)]) def test_unmeasured_h32_and_idle_paths_keep_the_existing_behavior(mode, display): v = prepared(mode=mode, display=display) v._fifo_write() assert v.fifo_cpu_ready[0] == v.mclk def plasma_row_trace(): arrivals = [ 671, 699, 811, 839, 1161, 1189, 1532, 1560, 1882, 1910, 2253, 2281, 2603, 2631, 2743, 2771, 2883, 2911, 3128, 3415, ] holds = [0] * 17 + [91, 7, 0] return [ nuked_md.Event(1000 + t - 15, 0, 0xC00000, 0x1234, 18 + hold, nuked_md.EV_M68K, 14) for t, hold in zip(arrivals, holds, strict=True) ] SIGNALS = "2000 HC 330 330 34\n2002 FEMPTY 1 330 34\n8840 HC 330 330 35\n" @pytest.mark.parametrize("target", ["cram", "vsram"]) def test_measured_plasma_row_acknowledgements(target): assert check_trace(plasma_row_trace(), SIGNALS, target) == { "writes": 20, "held": 2, "blank_writes": 0, } def test_checker_rejects_a_one_cpu_clock_error(): events = plasma_row_trace() events[17] = replace(events[17], length=events[17].length - 7) with pytest.raises(ValueError, match=r"hold .* expected"): check_trace(events, SIGNALS, "vsram") @pytest.mark.parametrize("signals,error", [ ("2002 FEMPTY 1 330 34\n", "missing physical"), ("2000 HC 330 330 34\n", "missing confirmed empty"), ("2000 HC 330 330 34\n2002 FEMPTY 2 330 34\n", "invalid FEMPTY"), ("2002 HC 330 330 34\n2000 FEMPTY 1 330 34\n", "chronological"), ("bad record\n", "malformed"), (SIGNALS[:SIGNALS.rfind("8840")], "does not cover"), ]) def test_checker_requires_sufficient_signal_evidence(signals, error): with pytest.raises(ValueError, match=error): check_trace(plasma_row_trace(), signals, "vsram") def test_checker_rejects_vacuous_success(): with pytest.raises(ValueError, match="must exercise"): check_trace([], SIGNALS, "vsram") @pytest.mark.parametrize("target", TARGETS) def test_probe_executes_and_writes_the_selected_destination(target): system = System(MACHINES["nuked-md"], Cartridge(cartridge(target))) system.board.fm.enabled = False while system.mclk < 50_000: system.step() assert system.vdp.code & 0x0F == TARGETS[target][0] assert system.vdp.hmode.name == "H40" if target == "vram": assert system.vdp.vram[:2] == b"\x12\x34" elif target == "cram": assert system.vdp.cram[0] == 0x224 else: assert system.vdp.vsram[0] == 0x234