"""A Model 1's Z80 cycle on the cartridge, and the FM's write timing on every machine. The die's cartridge cycle (31 MCLK at least) is short of two Model 1 consoles' by about 4: the probe cart's cartridge-sharing counts (ZBS, ZRO) on an NTSC Model 1 with TMSS and the PCM stream of a PAL console's capture of Overdrive 2 (od2-20k ``flaws/FABLE-HANDOFF.md``, 2026-11-10, second session). The Model 2 keeps the die's: MoD's Genesis 2 waits "2-3". """ from __future__ import annotations import pytest from megadrive_python.arbiter import ( MODEL1_CARTRIDGE_EXTRA_MCLK, MODEL1_CARTRIDGE_RELEASE_MCLK, OWN_CYCLE_CARTRIDGE_MCLK, RELEASE_MCLK, RESUME_MCLK, z80_access_wait, ) from megadrive_python.board import M68K_FM_READ_MCLK, M68K_FM_WRITE_MCLK, Z80_FM_WRITE_MCLK from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import System MODEL1 = ("genesis", "genesis-tmss", "megadrive", "megadrive-tmss", "megadrive-japan") OTHERS = ("genesis2", "genesis3", "megadrive2", "megadrive2-japan", "nuked-md") def test_the_model_1s_have_the_longer_cartridge_cycle_and_no_other_machine() -> None: assert (MODEL1_CARTRIDGE_EXTRA_MCLK, MODEL1_CARTRIDGE_RELEASE_MCLK) == (4, 0) assert set(MODEL1) | set(OTHERS) == set(MACHINES) for name in MODEL1: machine = MACHINES[name] assert (machine.z80_cartridge_extra, machine.z80_cartridge_release) == (4, 0), name for name in OTHERS: machine = MACHINES[name] assert (machine.z80_cartridge_extra, machine.z80_cartridge_release) == (0, 0), name def test_the_late_release_costs_the_z80_a_clock_and_the_68000_nothing() -> None: """With the bus idle the Z80's cycle starts on the 68000's edge at 1,033. The die's 31 ends it at 1,064 and the Model 1's 34 at 1,067, both inside two wait states (released at 1,067); /WAIT let go 1 MCLK after 1,067 misses that clock, so the Z80 waits a third while the 68000 resumes from the cycle's own end as before. No machine has a late release since 2026-11-11 (the Model 1s' went from 1 to 0); the rule stays.""" die = z80_access_wait(1000, [], None, OWN_CYCLE_CARTRIDGE_MCLK) longer = z80_access_wait(1000, [], None, OWN_CYCLE_CARTRIDGE_MCLK + 3) late = z80_access_wait(1000, [], None, OWN_CYCLE_CARTRIDGE_MCLK + 3, 1) resume = 1000 + RELEASE_MCLK + 15 * 2 + RESUME_MCLK assert die == longer == (2, resume, 1033) assert late == (3, resume, 1033) @pytest.mark.parametrize(("machine", "waits"), [("genesis", 3), ("genesis2", 2)]) def test_the_board_arbitrates_a_cartridge_read_by_its_machine(machine: str, waits: int) -> None: """From /MREQ at 999 the cycle starts at 1,033 and two wait states release at 1,066: the die's 31 ends by then (1,064), a Model 1's 34 does not (1,067).""" system = System(MACHINES[machine], Cartridge(bytes(0x10000))) board = system.board board.z80_reset = board.z80_bus_requested = False board.cart_refresh_due = 10_000 board.z80_refresh_taken = -1 board.m68k_accesses = [] board.m68k_step_end = 990 board.m68k_pending_stall = 0 assert board._arbitrate(999, True)[0] == waits assert board._arbitrate(999, False)[0] == 2 # the VDP's cycle is no longer anywhere @pytest.mark.parametrize("machine", sorted(MACHINES)) def test_every_machine_takes_fm_writes_as_the_die_does(machine: str) -> None: """Until 2026-11-10 only ``nuked-md`` did; the consoles took a write at its access's start, and their Overdrive 2 ran 6 to 151 ms ahead of a console's where it now runs 1 to 80.""" board = System(MACHINES[machine], Cartridge(bytes(0x10000))).board assert (board.fm_write_lag, board.fm_read_lag, board.z80_fm_write_lag) == ( M68K_FM_WRITE_MCLK, M68K_FM_READ_MCLK, Z80_FM_WRITE_MCLK ) # fmt: skip # (The withdrawn FM core's timer-control delay was also checked here; the restarted # ym2612-python takes register writes at sample boundaries and has none.)