# SPDX-License-Identifier: MIT AND ISC """The S-CPU's eight DMA/HDMA channels: registers $43x0-$43xF and transfers. DOC: fullsnes "SNES DMA Transfer" and "SNES HDMA Transfer" (registers, patterns, the A-bus exclusions, count 0 = 65536, the HDMA table formats); anomie timing "DMA" and "HDMA" (the 8-clock grid, overheads). Where those leave the order of operations open, this follows ares 9408cb4 ``sfc/cpu/dma.cpp`` (ISC; notice in NOTICE and in board.py), and says so. """ from __future__ import annotations from typing import TYPE_CHECKING if TYPE_CHECKING: from snes_python.board import SNES # DOC: fullsnes "43x0h - DMAPx" bits 0-2: bytes per unit for HDMA, by mode # (0:1, 1:2, 2:2 (same register twice), 3:4, 4:4, 5:4 (as 1, twice), # 6:2 (as 2), 7:4 (as 3)). HDMA_LENGTHS = (1, 2, 2, 4, 4, 4, 2, 4) def valid_a(address: int) -> bool: """Whether the A bus of a DMA may reach ``address`` (DOC: fullsnes "SNES DMA Transfer": not $2100-$21FF, $4000-$41FF, $4200-$421F, $4300-$437F of the system banks; the same four ranges in ares Channel::validA).""" if address & 0x400000: return True offset = address & 0xFFFF if offset & 0xFF00 == 0x2100: return False if offset & 0xFE00 == 0x4000: return False if offset & 0xFFE0 == 0x4200: return False return offset & 65408 != 17152 class Channel: """One channel's registers and HDMA state.""" __slots__ = ( "a_address", "a_bank", "b_address", "count", "direction", "dma_enable", "fixed", "hdma_completed", "hdma_do_transfer", "hdma_enable", "id", "indirect", "indirect_bank", "line_counter", "mode", "reverse", "table_address", "unknown", "unused", ) def __init__(self, index: int) -> None: self.id = index self.dma_enable = False self.hdma_enable = False # DOC: fullsnes "43x0h-43xBh": "Default: FFh" for every register # after power-on (ares Channel defaults agree). self.mode = 7 self.fixed = True self.reverse = True self.unused = True self.indirect = True self.direction = True self.b_address = 0xFF self.a_address = 0xFFFF self.a_bank = 0xFF # DASxL/H, which is also the HDMA indirect address. self.count = 0xFFFF self.indirect_bank = 0xFF self.table_address = 0xFFFF self.line_counter = 0xFF self.unknown = 0xFF self.hdma_completed = False self.hdma_do_transfer = False def hdma_active(self) -> bool: return self.hdma_enable and not self.hdma_completed # -- registers ------------------------------------------------------------ def read(self, r: int, mdr: int) -> int: """$43xR, R = 0-15; unused R read open bus (DOC: fullsnes; ares readDMA).""" if r == 0: return ( self.mode | self.fixed << 3 | self.reverse << 4 | self.unused << 5 | self.indirect << 6 | self.direction << 7 ) if r == 1: return self.b_address if r == 2: return self.a_address & 0xFF if r == 3: return self.a_address >> 8 if r == 4: return self.a_bank if r == 5: return self.count & 0xFF if r == 6: return self.count >> 8 if r == 7: return self.indirect_bank if r == 8: return self.table_address & 0xFF if r == 9: return self.table_address >> 8 if r == 10: return self.line_counter if r == 11 or r == 15: # DOC: fullsnes "43xBh/43xFh - UNUSEDx": a read/write byte, mirrored. return self.unknown return mdr def write(self, r: int, value: int) -> None: if r == 0: self.mode = value & 7 self.fixed = bool(value & 0x08) self.reverse = bool(value & 0x10) self.unused = bool(value & 0x20) self.indirect = bool(value & 0x40) self.direction = bool(value & 0x80) elif r == 1: self.b_address = value elif r == 2: self.a_address = self.a_address & 0xFF00 | value elif r == 3: self.a_address = value << 8 | self.a_address & 0xFF elif r == 4: self.a_bank = value elif r == 5: self.count = self.count & 0xFF00 | value elif r == 6: self.count = value << 8 | self.count & 0xFF elif r == 7: self.indirect_bank = value elif r == 8: self.table_address = self.table_address & 0xFF00 | value elif r == 9: self.table_address = value << 8 | self.table_address & 0xFF elif r == 10: self.line_counter = value elif r == 11 or r == 15: self.unknown = value class DMAController: """The transfers. Time passes through ``board.dma_step`` (8 clocks a byte).""" def __init__(self, board: SNES) -> None: self.board = board self.channels = [Channel(i) for i in range(8)] def dma_enabled(self) -> bool: for channel in self.channels: if channel.dma_enable: return True return False def hdma_enabled(self) -> bool: for channel in self.channels: if channel.hdma_enable: return True return False def hdma_any_active(self) -> bool: for channel in self.channels: if channel.hdma_enable and not channel.hdma_completed: return True return False # -- one byte ------------------------------------------------------------ def _read_a(self, address: int) -> int: board = self.board board.dma_step(4) board.mdr = board.bus_read(address, board.mdr) if valid_a(address) else 0 board.dma_step(4) return board.mdr def _transfer(self, channel: Channel, address_a: int, index: int) -> None: """One byte between the A bus and $21xx (DOC: fullsnes "43x0h" bits 0-2 transfer patterns).""" board = self.board mode = channel.mode b = channel.b_address if mode == 1 or mode == 5: b = (b + (index & 1)) & 0xFF elif mode == 3 or mode == 7: b = (b + ((index >> 1) & 1)) & 0xFF elif mode == 4: b = (b + index) & 0xFF # DOC: fullsnes "WRAM-to-WRAM DMA isn't possible"; MEASURED (console, # probe suite WDMA, 2026-10-25): A to B the write is dropped; B to A # the A-bus target gets the open-bus byte. (ares writes $00 there: # the console overrules it.) wram_a = (address_a & 0xFE0000) == 0x7E0000 or (address_a & 0x40E000) == 0 valid = b != 0x80 or not wram_a if not channel.direction: data = self._read_a(address_a) if valid: board.bus_write(0x2100 | b, data) else: board.dma_step(4) if valid: board.mdr = board.bus_read(0x2100 | b, board.mdr) board.dma_step(4) if valid_a(address_a): board.bus_write(address_a, board.mdr) # -- general-purpose DMA --------------------------------------------------- def run_dma(self) -> None: """Every enabled channel, lowest first (DOC: fullsnes "420Bh - MDMAEN"; anomie timing "DMA": 8 clocks of overhead, then 8 per channel; the order of steps and HDMA checks OBSERVED from ares CPU::dmaRun and Channel::dmaRun).""" board = self.board board.dma_step(8) board.dma_edge() for channel in self.channels: if not channel.dma_enable: continue board.dma_step(8) board.dma_edge() index = 0 while True: self._transfer(channel, channel.a_bank << 16 | channel.a_address, index) index = (index + 1) & 3 if not channel.fixed: step = -1 if channel.reverse else 1 channel.a_address = (channel.a_address + step) & 0xFFFF board.dma_edge() if not channel.dma_enable: break channel.count = (channel.count - 1) & 0xFFFF if channel.count == 0: break channel.dma_enable = False # -- HDMA -------------------------------------------------------------------- def hdma_reset(self) -> None: for channel in self.channels: channel.hdma_completed = False channel.hdma_do_transfer = False def _hdma_finished(self, after: int) -> bool: for channel in self.channels[after + 1:]: if channel.hdma_enable and not channel.hdma_completed: return False return True def _hdma_reload(self, channel: Channel) -> None: """Fetch a line-count byte (and an indirect address) when the count runs out (DOC: fullsnes "SNES HDMA Transfer"; order OBSERVED from ares Channel::hdmaReload, including its early stop when the table ends and no later channel is active).""" data = self._read_a(channel.a_bank << 16 | channel.table_address) if channel.line_counter & 0x7F == 0: channel.line_counter = data channel.table_address = (channel.table_address + 1) & 0xFFFF channel.hdma_completed = channel.line_counter == 0 channel.hdma_do_transfer = not channel.hdma_completed if channel.indirect: data = self._read_a(channel.a_bank << 16 | channel.table_address) channel.table_address = (channel.table_address + 1) & 0xFFFF channel.count = data << 8 if channel.hdma_completed and self._hdma_finished(channel.id): return data = self._read_a(channel.a_bank << 16 | channel.table_address) channel.table_address = (channel.table_address + 1) & 0xFFFF channel.count = data << 8 | channel.count >> 8 def hdma_setup(self) -> None: """The frame's HDMA initialisation (DOC: anomie timing "HDMA").""" board = self.board board.dma_step(8) for channel in self.channels: # OBSERVED (ares hdmaSetup): every channel's do-transfer flag is # set, enabled or not. channel.hdma_do_transfer = True if not channel.hdma_enable: continue # DOC: fullsnes: HDMA stops a DMA running on the same channel. channel.dma_enable = False channel.table_address = channel.a_address channel.line_counter = 0 self._hdma_reload(channel) def hdma_run(self) -> None: """One line's HDMA (DOC: anomie timing "HDMA", fullsnes).""" board = self.board board.dma_step(8) for channel in self.channels: if not (channel.hdma_enable and not channel.hdma_completed): continue channel.dma_enable = False if not channel.hdma_do_transfer: continue for index in range(HDMA_LENGTHS[channel.mode]): if channel.indirect: address = channel.indirect_bank << 16 | channel.count channel.count = (channel.count + 1) & 0xFFFF else: address = channel.a_bank << 16 | channel.table_address channel.table_address = (channel.table_address + 1) & 0xFFFF self._transfer(channel, address, index) for channel in self.channels: if not (channel.hdma_enable and not channel.hdma_completed): continue channel.line_counter = (channel.line_counter - 1) & 0xFF channel.hdma_do_transfer = bool(channel.line_counter & 0x80) self._hdma_reload(channel) __all__ = ["HDMA_LENGTHS", "Channel", "DMAController", "valid_a"]