# SPDX-License-Identifier: MIT """MARIA's DMA against the measured totals ([HF], [SG] App. 3; tier 3), counted exactly on the board, and the seeded faults that must break them. A cartridge that only loops (JMP to itself; NMI and IRQ point at an RTI) is started directly; the display list list and display lists are written into the board's RAM and MARIA's registers set, so the counts are the board's own HALT periods, one per 4 MARIA clocks the CPU was held. Per NTSC frame: 242 DMA lines ([GLD] sheet 17), zones start on the first DMA line, so 16-line zones end 15 times and 8-line zones 30 times among them, and the trailing edge of vertical blank adds its DLL fetch once. """ from __future__ import annotations import pytest from a7800_python.cartridge.connector import Linear from a7800_python.machine import NTSC from a7800_python.system import System def looping_rom() -> bytes: rom = bytearray(0x4000) rom[0:3] = b"\x4c\x00\xc0" # $C000: JMP $C000 rom[3] = 0x40 # $C003: RTI rom[0x3FFA:0x4000] = b"\x03\xc0\x00\xc0\x03\xc0" return bytes(rom) def system_with(zone: int = 16, dli: bool = False, headers: bytes = b"", faults=()) -> System: system = System(Linear(looping_rom()), NTSC, start="direct") board = system.board board.faults |= set(faults) ram = board.ram # index = address - $1800 for k in range(32): # DLL at $1800, every zone's list at $1900 ram[3 * k : 3 * k + 3] = bytes([(0x80 if dli else 0) | (zone - 1), 0x19, 0x00]) ram[0x100 : 0x100 + len(headers)] = headers maria = board.maria maria.write_register(0x0C, 0x18) # DPPH maria.write_register(0x10, 0x00) # DPPL maria.write_register(0x1C, 0x40) # CTRL: DMA on, 160A return system def held_periods_per_frame(system: System) -> int: system.run_frames(2) before = system.board.halted_periods system.run_frames(1) return system.board.halted_periods - before def test_null_lists_16_a_line_24_on_zone_ends_8_a_frame(): assert held_periods_per_frame(system_with(zone=16)) * 4 == 242 * 16 + 15 * 8 + 8 def test_the_zone_end_costs_8_more(): assert held_periods_per_frame(system_with(zone=8)) * 4 == 242 * 16 + 30 * 8 + 8 def test_dlis_cost_no_dma(): assert held_periods_per_frame(system_with(dli=True)) == held_periods_per_frame(system_with()) # Four objects on every line: 4-byte header + 1 byte = 8 + 3; with 2 bytes 8 + 6; # 5-byte header + 1 byte = 10 + 3; indirect 10 + 6; indirect with CWIDTH 10 + 9. H4 = bytes([0x00, 0x20 | 31, 0xE0, 0x10]) H4W2 = bytes([0x00, 0x20 | 30, 0xE0, 0x10]) H5 = bytes([0x00, 0x40, 0xE0, 0x20 | 31, 0x10]) IND = bytes([0x00, 0x60, 0x1A, 0x20 | 31, 0x10]) @pytest.mark.parametrize( ("headers", "per_object", "cwidth"), [(H4, 11, False), (H4W2, 14, False), (H5, 13, False), (IND, 16, False), (IND, 19, True)], ) def test_per_object_costs(headers, per_object, cwidth): system = system_with(headers=headers * 4) if cwidth: system.board.maria.write_register(0x1C, 0x50) per_line = 16 + 4 * per_object # a multiple of 4 in every case: no rounding assert held_periods_per_frame(system) * 4 == 242 * per_line + 15 * 8 + 8 def dlis_per_frame(faults=(), only: int | None = None) -> int: """Zones that fill the 242 DMA lines exactly (15 x 16 + 2), every entry with the DLI bit: entry 0 is read at VBLANK's end and entries 1-16 on zone ends, the last of them on the last DMA line (probes/dliprobe.s phase 5). With ``only``, that entry alone has the bit (phase 2: ``only=16``).""" system = system_with(zone=16, dli=True, faults=faults) system.board.ram[3 * 15] = 0x80 | 1 # entry 15: 2 lines if only is not None: for k in range(32): if k != only: system.board.ram[3 * k] &= 0x7F seen = [] def watch(kind, address, value, clock): if (kind, address) == ("R", 0xFFFA): # the NMI vector fetch: one per DLI taken seen.append(1) system.board.bus_observers.append(watch) system.run_frames(2) seen.clear() system.run_frames(1) return len(seen) def test_dli_from_every_entry_read_including_the_last_dma_line(): # [SG]: "following DMA on the last line of the PREVIOUS zone", no exception # for the frame's last line; C23 (MAME 0.285 and A7800 5.2 take 16). assert dlis_per_frame() == 17 assert dlis_per_frame(faults=["c23-mame"]) == 16 def test_last_line_dli_flag_survives_to_the_vbe_fetch(): # [GLD] sheet 17 (docs/gld-reading.md): the DLI latch is cleared only when a # line DMA starts, so the flag from the last DMA line raises the NMI after # that line's DMA and again after the next VBE fetch. assert dlis_per_frame(only=16) == 2 assert dlis_per_frame(only=16, faults=["dli-clear-on-nmi"]) == 1 # ProSystem NTSC assert dlis_per_frame(only=16, faults=["c23-mame"]) == 0 # MAME, A7800 assert dlis_per_frame(only=5) == 1 # a middle zone: once def test_seeded_fault_last_line_cost_dropped(): system = system_with(zone=16, faults=["last-line-cost-dropped"]) assert held_periods_per_frame(system) * 4 != 242 * 16 + 15 * 8 + 8 def test_end_of_list_is_byte_one_and_5f(): # Ninja Golf and Crossbow end lists with non-zero bytes (C5): $80 in byte 1. terminator = bytes([0x00, 0x80]) assert held_periods_per_frame(system_with(headers=terminator)) * 4 == 242 * 16 + 15 * 8 + 8 faulty = system_with(headers=terminator, faults=["end-test-zero"]) assert held_periods_per_frame(faulty) * 4 != 242 * 16 + 15 * 8 + 8 def nmi_delay(faults=()) -> list[int]: """CPU periods from HALT's release to the DLI's NMI request, for each DLI.""" system = system_with(dli=True, faults=faults) board, cpu = system.board, system.cpu released, delays = [], [] halted = board._halted def watched_halted(): halted() released.append(board.periods) board._halted = watched_halted request = cpu.request_non_maskable_interrupt def watched_request(): delays.append(board.periods - released[-1]) request() cpu.request_non_maskable_interrupt = watched_request system.run_frames(2) return delays def test_dli_one_cycle_after_halt_is_released(): # [1702B] p. 14: "the NMI- pin will be asserted for one cpu cycle, one cycle # after HALT- is released": requested in the second CPU cycle after release. delays = nmi_delay() assert delays and set(delays) == {1} assert set(nmi_delay(faults=["dli-early"])) == {0} def test_line_ram_and_160a_pixels(): # One 4-byte header, 1 byte $55 of palette 1 at position 0: cells 0-3 = colour 1. system = system_with(headers=bytes([0x00, 0x20 | 31, 0xE1, 0x00])) rom_gfx = 0xE100 - 0xC000 system.cartridge.rom = bytearray(system.cartridge.rom) for page in range(16): system.cartridge.rom[rom_gfx + page * 0x100] = 0x55 maria = system.board.maria maria.write_register(0x00, 0x00) # background maria.write_register(0x05, 0x1A) # P1C1 # DMA switched on mid-frame runs the rest of frame 0 from MARIA's power-on # list pointer (whatever the bus holds); a 5-byte header read there can set # write mode 1, which persists. Start clean on the next frame. system.run_frames(1) maria.write_mode = 0 lines = {} maria.on_line = lambda line, pixels, kill: lines.__setitem__(line, bytes(pixels)) system.run_frames(1) row = lines[100] assert row[107:115] == bytes([0x1A]) * 8 # 4 cells x 2 clocks assert row[115] == 0x00