"""The Z80 on the 68000's bus, as the boot walk of 2026-11-07 settled it. Six rules found by walking the board's 68000 bus stream against Nuked-MD's from the Z80's start (``scripts/od2_boot_drift.py``), each first step dissected on both sides of the bus and in the referee's Z80-bus and FM-signal logs; od2-20k ``flaws/FABLE-HANDOFF.md``, "The boot walk: five mechanisms" and "A sixth". The numbers below are the events themselves, anchor-relative to the Z80's start (die 42,570,940 MCLK, board 42,330,663) where they came from the demo. The YM2612 rule (a timer-flag reset in an overflow's cycle loses the overflow) belongs to ym2612-python and is not here. """ from __future__ import annotations from megadrive_python.arbiter import ( AS_LOW_MCLK, OWN_CYCLE_CARTRIDGE_MCLK, OWN_CYCLE_MCLK, OWN_CYCLE_VDP_MCLK, REFRESH_MCLK, REFRESH_SELF_MCLK, RELEASE_MCLK, z80_access_wait, ) from megadrive_python.board import CART_REFRESH_MCLK, Z80_FM_WRITE_MCLK from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import Z80_BUSACK_AFTER_MCLK, Z80_BUSREQ_LAG_MCLK, System def test_the_z80s_own_cycle_depends_on_what_it_reads() -> None: """Frame 1501 of Overdrive 2: a cartridge read's cycle on the 68000's bus is never shorter than 31 MCLK on the die, a VDP read's never shorter than 24; one of the 64 reads a frame that 29 ended a wait state early (a boot-frame arbitration, the board's own log: request 15,956,354, three 68000 cycles just before it).""" assert (OWN_CYCLE_MCLK, OWN_CYCLE_CARTRIDGE_MCLK, OWN_CYCLE_VDP_MCLK) == (29, 31, 24) request = 15_956_354 grid = [[15_956_329, AS_LOW_MCLK], [15_956_357, AS_LOW_MCLK], [15_956_385, AS_LOW_MCLK]] wait, _, own = z80_access_wait(request, grid) assert (wait, request + RELEASE_MCLK + 15 * wait - own) == (2, 29) assert z80_access_wait(request, grid, None, OWN_CYCLE_CARTRIDGE_MCLK)[0] == 3 assert z80_access_wait(request, grid, None, OWN_CYCLE_VDP_MCLK)[0] == 2 def _board(): system = System(MACHINES["genesis"], Cartridge(bytes(0x10000))) board = system.board board.z80_reset = board.z80_bus_requested = False return system, board def test_a_refresh_tick_a_68000_cycle_has_passed_is_not_owed_to_the_z80() -> None: """Boot +543,403: the tick fell due at 543,312 and a 68000 cycle passed it (the due moved on), so the die's Z80 cycle is 35 MCLK, unstretched; the board rebuilt the tick as due - 896 and stretched the cycle 14, a wait state more.""" _, board = _board() board.cart_refresh_due = 543_312 + CART_REFRESH_MCLK board.z80_refresh_taken = -1 board.m68k_accesses = [[543_396, AS_LOW_MCLK]] board.m68k_step_end = 543_414 board.m68k_pending_stall = 0 wait, _ = board._arbitrate(543_371, True) assert wait == 2 assert board.cart_refresh_due == 543_312 + CART_REFRESH_MCLK # untouched def test_a_refresh_tick_before_the_z80s_own_cycle_is_used_up_by_it() -> None: """Boot +1,113,168: the tick fell between the request's lookahead (``t`` + 50) and the Z80's own cycle start, so the Z80's VDP read used it up and the die's next cartridge read by the 68000 is not held; the board had held it 14.""" _, board = _board() board.cart_refresh_due = 1_113_168 board.z80_refresh_taken = -1 board.m68k_accesses = [[1_113_105, AS_LOW_MCLK], [1_113_133, AS_LOW_MCLK], [1_113_161, AS_LOW_MCLK]] # fmt: skip board.m68k_step_end = 1_113_189 board.m68k_pending_stall = 0 wait, _ = board._arbitrate(1_113_116, False) assert wait == 3 # the die's: a 30-MCLK VDP cycle, the tick not stretched into it assert board.z80_refresh_taken == 1_113_168 assert board.cart_refresh_due == 1_113_168 + CART_REFRESH_MCLK def test_the_z80_lets_go_at_the_end_of_the_sampled_machine_cycle() -> None: """Zilog UM0080: BUSREQ is sampled on the last clock of *any* machine cycle. `JR d` is M1 4T, read 3T, internal 5T; a request sampled in the read lets go at 7 T-states, not at the instruction's end (the die at boot +1,255,198, the driver's `JR NC`).""" system, board = _board() board.z80_ram[0:2] = bytes((0x18, 0x10)) # JR +$10 system.z80.pc = 0 system.z80_mclk = 1_000_000 mclk = 1_000_082 - Z80_BUSREQ_LAG_MCLK # sampled 22 MCLK into the operand read system.catch_up_z80(mclk, stopping=True) assert system._z80_partial == (1_000_000, 1_000_000 + 7 * 15) def test_an_instruction_at_the_68000s_clock_is_recorded_as_partial_at_the_grant() -> None: """The instruction that starts exactly at the 68000's clock is never reached by the catch-up's loop; the grant still computes its let-go and must record it, or the whole instruction runs again after the release (the die at boot +1,376,291: `LD A,L` at $05E2 completes its M1 before letting go, the next fetch follows the release).""" system, board = _board() board.z80_ram[0] = 0x7D # LD A,L: one M1 cycle, 4T system.z80.pc = 0 system.z80_mclk = 1_000_000 ack = system.z80_busack(1_000_000) assert system._z80_partial == (1_000_000, 1_000_060) assert ack == 1_000_060 + Z80_BUSACK_AFTER_MCLK def test_the_bus_request_is_sampled_32_mclk_after_the_write() -> None: """The edge that coincides with the /BUSREQ fall counts: a machine cycle ending exactly 32 MCLK after the 68000's write is the one the Z80 lets go after (boot +8,748,369, the frame handshake; 33 took the next cycle and the board's Z80 led the die's by 105 MCLK from then on).""" assert Z80_BUSREQ_LAG_MCLK == 32 system, board = _board() board.z80_ram[0:2] = bytes((0x18, 0x10)) # JR +$10: cycles end at 60, 105, 180 system.z80.pc = 0 system.z80_mclk = 1_000_000 ack = system.z80_busack(1_000_060 - Z80_BUSREQ_LAG_MCLK) assert ack == 1_000_060 + Z80_BUSACK_AFTER_MCLK def test_the_z80s_fm_write_lands_23_mclk_after_its_access_starts() -> None: """The chip takes a write in the internal cycle under way if it lands before that cycle ends. Boot +9,150,686: the die did not take the driver's register $27 write 22.5 MCLK before a cycle's end (its flag cleared a cycle later than the board's, and the overflow due in that cycle was lost); frames 2500-2700: it takes writes 24.5 before (the clear lands 108.5..147.5 after the write). So a cycle ending 22 MCLK after the access's start runs before the write is presented; one ending 24 after does not.""" assert Z80_FM_WRITE_MCLK == 23 board = System(MACHINES["nuked-md"], Cartridge(bytes(0x10000))).board # die figures assert board.z80_fm_write_lag == Z80_FM_WRITE_MCLK # The rest of this test drove the withdrawn FM core's internal cycle (42 MCLK) to # show a write landing inside or after it. The restarted ym2612-python steps once a # sample and takes writes at sample boundaries, so that grid is not there to test; # the lag stays a board constant, applied before the chip is run. def test_a_refresh_left_alone_on_a_free_bus_runs_by_itself() -> None: # The die (alewman-probe's ZBS3): a Z80 cartridge read on a free bus is stretched by a # refresh that fell due up to 112 MCLK before its own cycle began, and not by one # that has already run by itself. request = 1000 _, _, start = z80_access_wait(request, [], None, OWN_CYCLE_CARTRIDGE_MCLK) plain = z80_access_wait(request, [], None, OWN_CYCLE_CARTRIDGE_MCLK)[0] for late, stretched in ((0, True), (REFRESH_SELF_MCLK - 7, True), (REFRESH_SELF_MCLK + REFRESH_MCLK, False), (300, False)): # fmt: skip wait = z80_access_wait(request, [], start - late, OWN_CYCLE_CARTRIDGE_MCLK)[0] assert wait == (plain + 1 if stretched else plain), late