# SPDX-License-Identifier: MIT """The SH-2 side (milestone 2): the shared registers as the SH-2s see them, the interrupt lines, the DREQ FIFO, the frame buffer from an SH-2, the SCI link and the clock. Each test names the quirk (``sega32x_python.machine.Quirks``) or the source it pins. Most tests use stand-in boot ROMs made here (a vector table and a ``BRA`` to itself), so they run without the user's files; the boot test at the end needs them and is slow.""" from __future__ import annotations import pytest from conftest import SYNTHETIC_VECTORS, image, run from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.sockets import Reset from megadrive_python.system import System from sega32x_python import BootROMs, Sega32X from sega32x_python.sh2side import CMD, VINT, VRES def _sh2_rom(size: int) -> bytes: """PC $100, SP in SDRAM, and ``BRA $`` with its slot at $100.""" rom = bytearray(size) rom[0:4] = (0x100).to_bytes(4, "big") rom[4:8] = (0x0603F000).to_bytes(4, "big") rom[0x100:0x104] = bytes.fromhex("affe0009") return bytes(rom) STAND_IN = BootROMs(m68k=SYNTHETIC_VECTORS, master=_sh2_rom(2048), slave=_sh2_rom(1024)) def _system(code: str = "60fe") -> tuple[System, Sega32X]: device = Sega32X(Cartridge(image(code)), boot_roms=STAND_IN) system = System(MACHINES["genesis"], None, slot=device) # type: ignore[arg-type] system.board.fm.enabled = False system.attach_video_timing(device) return system, device def _start(system: System, device: Sega32X) -> None: board = system.board board.write_word(0xA15106, 1) # RV = 1: the 68000's code stays visible board.write_word(0xA15100, 1) # ADEN, RES = 0: held in reset assert not device.sh2.running # type: ignore[union-attr] board.write_word(0xA15100, 3) # ADEN | RES: released assert device.sh2.running # type: ignore[union-attr] def test_sh2s_are_held_without_aden_and_res() -> None: """Quirks.sh2_clock_needs_aden (service manual A406a).""" system, device = _system() run(system, 50) sh2 = device.sh2 assert sh2 is not None and not sh2.running and sh2.instructions == [0, 0] system.board.write_word(0xA15100, 2) # RES alone: still held assert not sh2.running _start(system, device) run(system, 50) assert all(sh2.instructions) assert all(chip.pc in (0x100, 0x102) for chip in sh2.chips) # the BRA or its slot def test_the_clock_is_mclk_times_3_over_7() -> None: """Manual 3.3: SH-2 = MCLK x 3/7; each chip runs to the 68000's clock (socket 5).""" system, device = _system() _start(system, device) run(system, 200) sh2 = device.sh2 target = system.mclk * 3 // 7 assert sh2.time == target # type: ignore[union-attr] for index, chip in enumerate(sh2.chips): # type: ignore[union-attr] now = chip.clock + sh2.skew[index] # type: ignore[union-attr] # One instruction past it at most: from the boot ROM with the cache off, the BRA's # fetch is two 16-bit cycles (Quirks.sh2_area0_width) of the BSC's states (WCR's # power-on long waits here), which may first wait for the other chip's fetch and, # for the slave, for the bus (Quirks.sh2_bus_arbitration). fetch = 2 * chip.bsc.cycle_states[0] assert target <= now < target + 2 * fetch + 2 def test_system_register_view() -> None: """$20004000 per CPU: FM, ADEN, CART (0 = inserted), HEN, the CPU's own masks (Manual 3.2.2; testpico t_32x_sh_defaults reads $0200 / $4001 there).""" system, device = _system() _start(system, device) device.sh2_register_write(0, 0x4000, 0x0082, 0x00FF) # HEN, CMD mask (master) device.sh2_register_write(1, 0x4000, 0x0008, 0x00FF) # V mask (slave); HEN now 0 assert device.sh2_register_read(0, 0x4000) == 0x0202 assert device.sh2_register_read(1, 0x4000) == 0x0208 assert device.sh2_register_read(0, 0x4006) == 0x4001 # EMPTY, RV (Quirks.sh2_dreq_view) device.sh2_register_write(0, 0x4000, 0x8000, 0xFF00) # FM from the SH-2 side assert device.fm and system.board.read_word(0xA15100) == 0x8083 device.sh2_register_write(0, 0x4006, 0x0000, 0xFFFF) # read only here (t_32x_reg_w) assert device.rv def test_comm_ports_are_shared() -> None: system, device = _system() _start(system, device) system.board.write_word(0xA15120, 0x4D5F) assert device.sh2_register_read(1, 0x4020) == 0x4D5F device.sh2_register_write(0, 0x4026, 0x0034, 0x00FF) assert system.board.read_word(0xA15126) == 0x0034 def test_cmd_interrupt() -> None: """Quirks.cmd_interrupt: INTM/INTS read back what is written (the Mars Check Program's test 3) and request CMD while set; the SH-2's clear clears its bit; masked, it waits; FTOA low adds 1 (Quirks.ftoa_to_irl0).""" system, device = _system() _start(system, device) sh2 = device.sh2 master, slave = sh2.chips # type: ignore[union-attr] system.board.write_word(0xA15102, 0xFFFD) assert system.board.read_word(0xA15102) == 1 assert master.irl == 1 # masked: only /IRL0 (FTOA is low after reset) device.sh2_register_write(0, 0x4000, 0x0002, 0x00FF) # CMD mask assert master.irl == 9 and slave.irl == 1 system.board.write_word(0xA15102, 0x0002) # INTS only: INTM's request goes assert system.board.read_word(0xA15102) == 2 and master.irl == 1 system.board.write_word(0xA15102, 0x0001) assert master.irl == 9 device.sh2_register_write(0, 0x401A, 0, 0xFFFF) # the master clears its CMD assert master.irl == 1 and system.board.read_word(0xA15102) == 0 sh2.ftoa[0] = 1 # type: ignore[union-attr] sh2._update_irl(0) # type: ignore[union-attr] assert master.irl == 0 def test_vres_on_the_reset_button() -> None: """Manual 3.2.2: VRES, level 14, no mask; cleared by $20004014 (socket 9's BUTTON).""" system, device = _system() _start(system, device) run(system, 10) device.reset(Reset.BUTTON, system.mclk) sh2 = device.sh2 assert sh2.pending == [VRES, VRES] # type: ignore[union-attr] assert sh2.chips[0].irl == 15 # type: ignore[union-attr] device.sh2_register_write(0, 0x4014, 0, 0xFFFF) assert sh2.pending == [0, VRES] # type: ignore[union-attr] def test_vint_is_asserted_for_vblank() -> None: """Quirks.vint_latch (from testpico t_32x_irq_vint): VINT asserts on both CPUs at VBlank whatever their masks, a clear takes it from one CPU, and VBlank's end takes it from the other.""" system, device = _system() _start(system, device) sh2 = device.sh2 device.sh2_register_write(1, 0x4000, 0x0008, 0x00FF) # V mask on the slave only for _ in range(100_000): system.step() if sh2.pending[0] & VINT: # type: ignore[union-attr] break assert sh2.pending == [VINT, VINT] # type: ignore[union-attr] assert sh2.chips[1].irl & ~1 == 12 and sh2.chips[0].irl & ~1 == 0 # type: ignore[union-attr] device.sh2_register_write(1, 0x4016, 0, 0xFFFF) assert sh2.pending == [VINT, 0] # type: ignore[union-attr] for _ in range(100_000): system.step() if not sh2.pending[0] & VINT: # type: ignore[union-attr] break assert not device.vdp.vblank and not device.vdp.status(system.mclk)[0] def test_dreq_fifo() -> None: """Quirks.dreq_fifo: 68S on, words queued (FULL at 8), DREQ0 while queued; the SH-2 side's reads take them and the length counts down to 68S's end.""" system, device = _system() _start(system, device) board = system.board board.write_word(0xA15110, 4) board.write_word(0xA15106, 0x0005) # 68S | RV for word in range(8): board.write_word(0xA15112, 0x1000 + word) assert board.read_word(0xA15106) & 0x80 # FULL master = device.sh2.chips[0] # type: ignore[union-attr] assert master.dreq0 == 1 reads = [device.sh2_register_read(0, 0x4012) for _ in range(4)] assert reads == [0x1000, 0x1001, 0x1002, 0x1003] assert device.dreq_length == 0 and not device.dreq_control & 4 board.write_word(0xA15106, 0x0001) # 68S off: the rest dropped assert master.dreq0 == 0 and device.fifo == [] def test_sh2_framebuffer_writes() -> None: """testpico t_32x_sh_fb's sequence: a $00 byte dropped, a $0000 word written, the overwrite image dropping every $00 byte; FM = 0 ignores the SH-2 (Quirks. sh2_vdp_without_fm).""" system, device = _system() _start(system, device) board = system.board board.write_word(0x840000, 0x1234) board.write_word(0x840002, 0x5678) board.write_word(0x840004, 0x89AB) board.write_word(0x840006, 0xCDEF) device.sh2_framebuffer_write(0x04000000, 8, 0) # FM = 0: ignored anyway board.write_byte(0xA15100, 0x80) # FM = 1 device.sh2_framebuffer_write(0x04000000, 8, 0) device.sh2_framebuffer_write(0x04020001, 8, 0) device.sh2_framebuffer_write(0x04000002, 16, 0) device.sh2_framebuffer_write(0x04020000, 16, 0) device.sh2_framebuffer_write(0x04020004, 32, 0x5A0000A5) board.write_byte(0xA15100, 0x00) assert (board.read_word(0x840000), board.read_word(0x840002)) == (0x1234, 0x0000) assert (board.read_word(0x840004), board.read_word(0x840006)) == (0x5AAB, 0xCDA5) assert device.sh2_register_read(0, 0x4100) == 0 # FM = 0: the VDP reads 0 here def test_sci_link_crosses_the_cpus() -> None: """Quirks.sci_link: the master's TxD is the slave's RxD.""" system, device = _system() _start(system, device) master, slave = device.sh2.chips # type: ignore[union-attr] slave.sci.write8(0xFFFFFE02, 0x10) # RE master.on_txd(0x5A, master.clock) assert slave.sci.rdr == 0x5A def test_cartridge_access_with_rv_holds_the_chip() -> None: """Quirks.rv_stall: an SH-2 cartridge access while RV = 1 marks the chip held; it stays held (time skewed) while RV stays 1 and is released when RV = 0.""" system, device = _system() _start(system, device) sh2 = device.sh2 read = sh2.chips[0].read # type: ignore[union-attr] assert read(0x02000100, 16, 0) == int.from_bytes(b"SE", "big") assert sh2.rv_blocked[0] # type: ignore[union-attr] skew = sh2.skew[0] # type: ignore[union-attr] run(system, 20) assert sh2.skew[0] > skew # type: ignore[union-attr] system.board.write_word(0xA15106, 0) system.step() assert not sh2.rv_blocked[0] # type: ignore[union-attr] @pytest.mark.slow def test_the_users_boot_roms_reach_m_ok_and_s_ok(user_boot_roms: BootROMs) -> None: """Manual 5.1-5.2: the master writes "M_OK" to comm0, the slave "S_OK" to comm4, with testpico's cartridge (its $3C0 header and Sega's $3F0 initial program).""" from pathlib import Path from stubs import enable_stub root = Path(__file__).resolve().parents[1] rom = root / "reference/build/testpico/hardware/testpico-30a5783.bin" if not rom.exists(): pytest.skip("testpico is not built (scripts/build_referees.py)") device = Sega32X(Cartridge.from_image(enable_stub(rom.read_bytes())), boot_roms=user_boot_roms) system = System(MACHINES["genesis"], None, slot=device) # type: ignore[arg-type] system.board.fm.enabled = False system.attach_video_timing(device) for _ in range(12): system.run_frame() assert device.comm[0:4] == [0x4D5F, 0x4F4B, 0x535F, 0x4F4B] assert not device.sh2.unmapped # type: ignore[union-attr] assert CMD == 2 # the constants the handlers' clear table indexes def test_pwm_fifos_drain_while_the_counter_runs() -> None: """Quirks.pwm_registers (the Mars Check Program's tests 13-15): with cycle $800 and the left channel on, a full FIFO drains a word every cycle - 1 SH-2 states, and every TM periods the PWM interrupt is raised on both SH-2s.""" system, device = _system() _start(system, device) board = system.board device.sh2_register_write(0, 0x4000, 0x0001, 0x00FF) # PWM mask, master device.sh2_register_write(1, 0x4000, 0x0001, 0x00FF) # and slave board.write_word(0xA15132, 0x800) board.write_word(0xA15130, 0x0001) # L on (TM = 0: every 16 periods) for _ in range(3): board.write_word(0xA15134, 0) assert board.read_word(0xA15134) & 0x8000 # FULL start = system.mclk while system.mclk - start < 3 * 0x7FF * 7 // 3 + 100: system.step() assert board.read_word(0xA15134) & 0x4000 # EMPTY while system.mclk - start < 17 * 0x7FF * 7 // 3: system.step() assert device.sh2.pending == [1, 1] # type: ignore[union-attr] # PWM on both def test_the_debugger_hook_sees_every_step_of_both_chips() -> None: """``SH2Side.on_step``: once before each step, with the chip about to run it.""" system, device = _system() _start(system, device) sh2 = device.sh2 assert sh2 is not None seen: list[tuple[int, int]] = [] sh2.on_step = lambda index, chip: seen.append((index, chip.pc)) run(system, 100) assert len(seen) == sum(sh2.instructions) assert {index for index, _pc in seen} == {0, 1} assert all(pc in (0x100, 0x102) for _index, pc in seen)