# SPDX-License-Identifier: MIT """The SH-2 bus's timing (milestone 5b): the WAIT states the board drives, the frame buffer's write FIFO, the two SH-2s' arbitration and the cartridge between an SH-2 and the 68000. Each test calls a chip's bus callback the way sh2-python does (``chip.clock`` at the cycle's start, ``chip.wait`` 0 on entry) and reads what the board added; each names the quirk (``sega32x_python.machine.Quirks``) it pins. The BSC is set as the user's boot ROMs leave it (read off the board's chips after boot): BCR1 $0001 (CS3 SDRAM), BCR2 $00A8 (areas 1-3 16 bits), WCR $0055 (one programmed wait per area, WAIT sampled), so an ordinary cycle is 3 states.""" from __future__ import annotations from conftest import SYNTHETIC_VECTORS, image from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import System from sh2_python.bus import FILL, FIRST from sega32x_python import BootROMs, Sega32X from sega32x_python.machine import Quirks from sega32x_python.sh2side import SH2Side ROM = bytearray(2048) ROM[0:4] = (0x100).to_bytes(4, "big") ROM[4:8] = (0x0603F000).to_bytes(4, "big") ROM[0x100:0x104] = bytes.fromhex("affe0009") STAND_IN = BootROMs(m68k=SYNTHETIC_VECTORS, master=bytes(ROM), slave=bytes(ROM[:1024])) SYSTEM_REGISTER, VDP_REGISTER, PALETTE = 0x4020, 0x4100, 0x4200 CARTRIDGE, FRAME_BUFFER, SDRAM, BOOT_ROM = 0x02000100, 0x04000000, 0x06000000, 0x0100 STATUS = 0x4006 def _bus(wcr: int = 0x0055) -> tuple[System, Sega32X, SH2Side]: device = Sega32X(Cartridge(image("60fe")), boot_roms=STAND_IN) system = System(MACHINES["genesis"], None, slot=device) # type: ignore[arg-type] system.board.fm.enabled = False board = system.board board.write_word(0xA15100, 3) # ADEN | RES: the SH-2s run (RV = 0) sh2 = device.sh2 assert sh2 is not None and sh2.running for chip in sh2.chips: chip.bsc.write(0xFFFFFFE0, 32, 0xA55A0001) chip.bsc.write(0xFFFFFFE4, 32, 0xA55A00A8) chip.bsc.write(0xFFFFFFE8, 32, 0xA55A0000 | wcr) return system, device, sh2 def _cycle( sh2: SH2Side, index: int, at: int, address: int, *, width: int = 16, kind: int = 0, write: bool = False, ) -> int: # fmt: skip """One bus cycle of chip ``index`` starting at board state ``at``; the WAIT states the board added. The chip's clock is moved to the cycle's end, as the core does.""" chip = sh2.chips[index] chip.clock = at - sh2.skew[index] chip.wait = 0 if write: chip.write(address, width, 0x1234 if width == 16 else 0x12, kind) else: chip.read(address, width, kind) area = (address >> 25) & 3 chip.clock += chip.bsc.cycle_states[area] + chip.wait return chip.wait def test_the_board_drives_what_wcr_does_not_cover() -> None: """Quirks.sh2_waits with Quirks.sh2_wait_overlap: Manual 4.1's waits (a cycle of 2 + n) less WCR's one programmed wait (HM 7.4.2: WAIT is sampled only at the last Tw).""" _, _, sh2 = _bus() waits = Quirks().sh2_waits.value assert waits["system_register"] == 1 and waits["vdp"] == 5 and waits["cartridge"] == 6 t = 10_000 assert _cycle(sh2, 0, t, BOOT_ROM) == 0 # 1 wait: WCR's own assert _cycle(sh2, 0, t + 100, SYSTEM_REGISTER) == 0 assert _cycle(sh2, 0, t + 200, VDP_REGISTER) == 4 assert _cycle(sh2, 0, t + 300, CARTRIDGE) == 5 assert _cycle(sh2, 0, t + 400, FRAME_BUFFER) == 4 # a read, the FIFO empty assert _cycle(sh2, 0, t + 500, FRAME_BUFFER, write=True) == 0 # 3 clocks, room # Two programmed waits leave one less to drive; none (W = 00) disables WAIT. _, _, two = _bus(wcr=0x00AA) assert _cycle(two, 0, t, VDP_REGISTER) == 3 _, _, none = _bus(wcr=0x0000) assert _cycle(none, 0, t, VDP_REGISTER) == 0 def test_sdram_cycles_are_the_cores() -> None: """Quirks.sh2_sdram: WAIT does not reach the SDRAM (HM 7.2.3); the board adds nothing, reads or writes.""" _, _, sh2 = _bus() assert _cycle(sh2, 0, 1000, SDRAM) == 0 assert _cycle(sh2, 0, 1100, SDRAM, write=True) == 0 def test_area0_is_16_bits() -> None: """Quirks.sh2_area0_width: CS0 (boot ROM, registers) 16 bits wide.""" _, _, sh2 = _bus() assert Quirks().sh2_area0_width.value == 16 assert all(chip.bsc.widths[0] == 16 for chip in sh2.chips) def test_frame_buffer_writes_fill_the_fifo_then_take_five() -> None: """Quirks.sh2_framebuffer_fifo: back-to-back writes take 3 clocks while the 4-word FIFO has room and 5 once it is full (Manual 4.1); FULL and EMPTY follow it.""" _, device, sh2 = _bus() chip = sh2.chips[0] t = 20_000 assert sh2.fifo_status(t) == (False, True) lengths = [] for _ in range(16): start = t _cycle(sh2, 0, t, FRAME_BUFFER, write=True) t = chip.clock + sh2.skew[0] lengths.append(t - start) assert lengths[:4] == [3, 3, 3, 3] assert lengths[-4:] == [5, 5, 5, 5] assert sh2.fifo_status(t) == (True, False) # $20004006 reads them (bits 15 and 14) at the access's time. chip.clock = t - sh2.skew[0] chip.wait = 0 assert device.sh2_register_read(0, 0x20004006) & 0xC000 == 0x8000 # Four words leave 5 clocks apart: 20 clocks later it is empty. assert sh2.fifo_status(t + 20) == (False, True) # An idle gap shortens the next write (never below the 3-clock cycle). assert _cycle(sh2, 0, t + 20, FRAME_BUFFER, write=True) == 0 def test_a_frame_buffer_read_waits_for_the_word_leaving() -> None: """Quirks.sh2_framebuffer_fifo: a read waits for the word leaving the FIFO (not the whole FIFO), then its 5 waits; the next word leaves after the read.""" _, _, sh2 = _bus() t = 30_000 for i in range(3): _cycle(sh2, 0, t + 3 * i, FRAME_BUFFER, write=True) # The words leave the FIFO at t + 8, t + 13 and t + 18. At t + 9 the first has left # and the second is leaving: 4 states for it, then the read's own 4. assert _cycle(sh2, 0, t + 9, FRAME_BUFFER) == 4 + 4 fifo = list(sh2.fb_fifo) read_end = sh2.chips[0].clock + sh2.skew[0] assert fifo[0] >= read_end + Quirks().sh2_framebuffer_fifo.value["drain"] def test_the_slave_asks_for_the_bus_each_access() -> None: """Quirks.sh2_bus_arbitration: the slave pays slave_request per access; an access's second port cycle and a line fill's later cycles do not ask again (HM 7.10).""" _, _, sh2 = _bus() t = 40_000 assert _cycle(sh2, 1, t, SDRAM) == 2 end = sh2.chips[1].clock + sh2.skew[1] assert _cycle(sh2, 1, end, SDRAM + 2) == 0 # a longword's second half t += 100 assert _cycle(sh2, 1, t, SDRAM + 0x10, width=16, kind=FILL | FIRST) == 2 for offset in (0x12, 0x14, 0x16): end = sh2.chips[1].clock + sh2.skew[1] kind = FILL | FIRST if offset == 0x12 else FILL assert _cycle(sh2, 1, end, SDRAM + offset, kind=kind) == 0 # The master, which holds the bus, asks for nothing when the bus is free. assert _cycle(sh2, 0, t + 200, SDRAM) == 0 def test_a_cycle_waits_for_the_other_chips_access() -> None: """Quirks.sh2_bus_arbitration: the master waits for a slave access under way and then master_return to take the bus back; the slave waits for the master's.""" _, _, sh2 = _bus() t = 50_000 _cycle(sh2, 1, t, CARTRIDGE) # starts at t + 2, 3 + 5 states: ends at t + 10 assert _cycle(sh2, 0, t + 4, SDRAM) == (t + 10 + 1) - (t + 4) master_end = sh2.chips[0].clock + sh2.skew[0] assert _cycle(sh2, 1, master_end - 1, SDRAM) == 2 # its request covers the rest # A master access long after the slave's is not held. assert _cycle(sh2, 0, t + 500, SDRAM) == 0 def test_the_cartridge_between_an_sh2_and_the_68000() -> None: """Quirks.cartridge_contention (Manual 3.4): the second to arrive waits for the first. An SH-2 cycle under way holds a 68000 window read; a 68000 access under way holds an SH-2 cartridge cycle.""" system, device, sh2 = _bus() board = system.board mclk = system.mclk + 7_000 device.catch_up(mclk) now = mclk * 3 // 7 _cycle(sh2, 0, now - 2, CARTRIDGE) # an SH-2 cycle of 8 states, ends at now + 6 stall = board.stall device.read_word(0x880100, mclk) held = board.stall - stall assert held > 0 and (now + 6) * 7 // 3 - mclk <= held # Now the 68000 is on the cartridge: an SH-2 cycle starting within it waits. m68k_start, m68k_end = sh2.cart_m68k assert m68k_end > m68k_start waits = _cycle(sh2, 0, m68k_start + 1, CARTRIDGE) assert waits == (m68k_end - m68k_start - 1) + 5