# SPDX-License-Identifier: MIT """The GBA's bus: region decode, access pricing, the latches, and the board's clock. Every CPU bus cycle the core reports (one ``read``/``write``/``idle`` callback per cycle; arm7tdmi-python ``ARM7TDMI`` docstring) is priced here and advances the board's one clock, ``now``, counted in 16.78 MHz cycles (docs/timing.md "The clock"; DOC). DMA uses the same pricing through ``dma_read``/``dma_write``. The order of record (docs/timing.md "The order of record"; HYPOTHESIS, confirmed or fitted by rung 1's judges as recorded there): for each CPU callback at time ``now``, 1. every scheduled event due at or before ``now`` runs (timers' overflows, the picture processor's line events, serial completion, sound sequencer), in time order; 2. a DMA whose start time has come takes the bus: it runs to the end (or until it is paused by a higher channel's own run) before the CPU's memory access; a CPU internal cycle runs beside it instead (alyosha-tas Interactions/ReadMe.txt; TEST ROM); 3. the access is performed: the value is read or written at ``now``; 4. ``now`` advances by the access's cost. The IRQ line is updated by the board between instructions (gba_python.irq). Regions, widths and costs: docs/memory-map.md "Regions" and docs/timing.md "Access time by region" (GBATEK §GBA Memory Map, §GBA System Control; DOC). """ from __future__ import annotations from arm7tdmi_python import CODE, SEQ, gba_access # Region numbers are address bits 24-27; 16 stands for "outside 00000000-0FFFFFFF". BIOS, EWRAM, IWRAM, IO, PALETTE, VRAM, OAM = 0, 2, 3, 4, 5, 6, 7 ROM_FIRST, ROM_LAST, SRAM_LO, SRAM_HI, UNUSED = 8, 13, 14, 15, 16 # WAITCNT's wait-state choices (GBATEK §GBA System Control; DOC; CowBite REG_WSCNT agrees). FIRST_WAITS = (4, 3, 2, 8) # SRAM and WS0-2 first access SECOND_WAITS = ((2, 1), (4, 1), (8, 1)) # WS0, WS1, WS2 second access, by the bit # How a CPU read leaves the CPU's data-bus latch (open bus), per region. GBATEK's table # (§GBA Unpredictable Things, "Reading from Unused Memory"; DOC) follows if a 16-bit bus # drives both halves of the CPU's 32-bit bus, the BIOS and OAM drive the whole aligned word # even for a halfword, and IWRAM drives only the halfword it serves (HYPOTHESIS for the # mechanism: docs/memory-map.md "Open bus"). LATCH_BOTH_HALVES, LATCH_WORD, LATCH_HALF, LATCH_NONE = 0, 1, 2, 3 LATCH_RULE = [LATCH_NONE] * 17 for _r in (EWRAM, PALETTE, VRAM, 8, 9, 10, 11, 12, 13): LATCH_RULE[_r] = LATCH_BOTH_HALVES LATCH_RULE[BIOS] = LATCH_WORD LATCH_RULE[OAM] = LATCH_WORD # I/O: HYPOTHESIS that it behaves as IWRAM (an on-chip 32-bit bus driving only the halfword # asked for); no permitted document says. The backup bus: HYPOTHESIS, the byte on all lanes. LATCH_RULE[IWRAM] = LATCH_HALF LATCH_RULE[IO] = LATCH_HALF def region_of(address: int) -> int: return address >> 24 if address < 0x10000000 else UNUSED class Bus: """Memories, latches, access pricing and the clock.""" def __init__(self, board, bios: bytes, rom, backup) -> None: self.board = board self.now = 0 # cycles since power-on self.next_event = 0 # the earliest scheduled event (gba_python.scheduler) self.bios = bytes(bios) self.ewram = bytearray(0x40000) self.iwram = bytearray(0x8000) self.palette = bytearray(0x400) self.vram = bytearray(0x18000) self.oam = bytearray(0x400) self.rom = rom self.backup = backup self.eeprom = backup if backup.kind == "eeprom" else None self.gpio = None # the cartridge's GPIO port (cartridge/rtc.py), set by the board # The CPU's data-bus latch: what unused memory returns (memory-map.md "Open bus"). self.open_bus = 0 # The BIOS latch: the last opcode word fetched from the BIOS (memory-map.md "BIOS read # protection"; DOC), and whether the fetch in progress is in the BIOS (U-MEM-1). self.bios_latch = 0 self.fetch_in_bios = True # The cartridge's address counter, a halfword index (timing.md "The ROM's own address # counter"; GBATEK §GBA Cartridge ROM; DOC; U-TIM-2). self.cart_latch = 0 # The last cartridge halfword ended a 128 KB block (the 128 KB rule, cart_access). self.cart_wrapped = False # In a DMA run the first cartridge access is N and the rest S, whichever side they are # on (FITTED: hw-test bus/128kb-boundary "DMA INC 07FFFFF8", whose first ROM unit # follows two OAM units, against dma/burst-into-tears, whose ROM read follows the same # run's ROM write and is S; the mGBA suite's ROM-to-ROM DMA rows, whose first write is # S; docs/timing.md "The 128 KB rule"). self.dma_cart_fresh = True # Whether the previous callback was idle() (for gba_access), and whether another bus # master used the bus since the CPU's last access (then the CPU's next access is N: # hw-test dma/force-nseq-access; TEST ROM). self.after_idle = False self.after_write = False self.cpu_seq_broken = True # CPU internal cycles that overlap a DMA already run on the bus (order of record 2). self.stall_credit = 0 # The bitmap modes hide 06018000-0601BFFF (hw-test ppu/vram-mirror; TEST ROM). self.bitmap_mode = False # WAITCNT and the internal memory control (04000800). self.waitcnt = 0 self.memcnt = 0x0D000020 # reset value "by hardware" (GBATEK §GBA System Control; DOC) self.n16 = [1] * 17 self.s16 = [1] * 17 self.n32 = [1] * 17 self.s32 = [1] * 17 self.update_waits() # Filled in by the board once the devices exist. self.io = None self.dma = None self.prefetch = None self.dots = None # the picture processor's dot model (gba_python.ppu.dots) # ---------------------------------------------------------------------------------- # Access pricing def update_waits(self) -> None: """Rebuild the cost tables from WAITCNT and 04000800 (timing.md "Access time by region"; GBATEK §GBA System Control; DOC). Each access is 1 cycle plus its wait states; a 32-bit access on a 16-bit bus is two halfword accesses, the second always sequential.""" n16, s16, n32, s32 = self.n16, self.s16, self.n32, self.s32 for region in range(17): n16[region] = s16[region] = n32[region] = s32[region] = 1 # Palette and VRAM are on 16-bit buses: 32-bit accesses take two cycles (DOC). for region in (PALETTE, VRAM): n32[region] = s32[region] = 2 # EWRAM: 15 - n wait states from 04000800 bits 24-27 (default n = 13: 3/3/6; DOC). control = (self.memcnt >> 24) & 0xF ewram_waits = 15 - control if control < 15 else 15 # 15 locks up: HYPOTHESIS, priced n16[EWRAM] = s16[EWRAM] = 1 + ewram_waits n32[EWRAM] = s32[EWRAM] = 2 * (1 + ewram_waits) waitcnt = self.waitcnt for ws in range(3): first = FIRST_WAITS[(waitcnt >> (2 + 3 * ws)) & 3] second = SECOND_WAITS[ws][(waitcnt >> (4 + 3 * ws)) & 1] # 32-bit accesses are priced per halfword in cart_access, not from n32/s32. for region in (8 + 2 * ws, 9 + 2 * ws): n16[region] = 1 + first s16[region] = 1 + second sram = 1 + FIRST_WAITS[waitcnt & 3] for region in (SRAM_LO, SRAM_HI): n16[region] = s16[region] = n32[region] = s32[region] = sram def write_waitcnt(self, value: int) -> None: # Bit 15 (Game Pak type) is read-only, 0 for a GBA cartridge; bit 13 unused (DOC). self.waitcnt = value & 0x5FFF self.update_waits() if self.prefetch is not None: self.prefetch.waitcnt_changed(self.now) def write_memcnt(self, value: int) -> None: self.memcnt = value & 0xFFFFFFFF self.update_waits() # ---------------------------------------------------------------------------------- # The CPU's callbacks def read(self, address: int, width: int, access: int) -> int: if self.now >= self.next_event: self.board.run_events() if self.board.dma_due <= self.now: self.dma.run() if self.board.irq.halted: self.board.halt_during_access() # For the prefetch buffer, an opcode fetch continues the stream when the core says S # (the merged IS cycle included) or when it follows a write (STR's next fetch is N # on the pins but sequential in the program); a branch's refill follows a fetch or an # internal cycle (gba_python.prefetch). raw_seq = (access & SEQ or self.after_write) and not self.cpu_seq_broken self.after_write = False access = gba_access(access, self.after_idle) self.after_idle = False self.stall_credit = 0 region = address >> 24 if address < 0x10000000 else UNUSED seq = access & SEQ and not self.cpu_seq_broken self.cpu_seq_broken = False if ROM_FIRST <= region <= ROM_LAST: prefetch = self.prefetch if access & CODE: self.fetch_in_bios = False if prefetch.enabled: value = prefetch.code_fetch(address, width, raw_seq, seq) self.update_latch(region, address, width, value) return value elif prefetch.enabled: prefetch.data_access(self.now) value = self.cart_read(address, width, seq) prefetch.after_cart(self.now) self.update_latch(region, address, width, value) return value value = self.cart_read(address, width, seq) self.update_latch(region, address, width, value) return value if access & CODE: in_bios = region == BIOS and address < 0x4000 self.fetch_in_bios = in_bios cost = (self.s32 if seq else self.n32) if width == 32 else (self.s16 if seq else self.n16) if PALETTE <= region <= OAM: self.video_wait(region, address, width) value = self.read_region(region, address, width, access & CODE) self.now += cost[region] self.update_latch(region, address, width, value) return value def write(self, address: int, width: int, value: int, access: int) -> None: if self.now >= self.next_event: self.board.run_events() if self.board.dma_due <= self.now: self.dma.run() if self.board.irq.halted: self.board.halt_during_access() access = gba_access(access, self.after_idle) self.after_idle = False self.after_write = True self.stall_credit = 0 region = address >> 24 if address < 0x10000000 else UNUSED seq = access & SEQ and not self.cpu_seq_broken self.cpu_seq_broken = False if ROM_FIRST <= region <= ROM_LAST: prefetch = self.prefetch if prefetch.enabled: prefetch.data_access(self.now) self.cart_write(address, width, value, seq) prefetch.after_cart(self.now) return self.cart_write(address, width, value, seq) return cost = (self.s32 if seq else self.n32) if width == 32 else (self.s16 if seq else self.n16) if PALETTE <= region <= OAM: self.video_wait(region, address, width) # The write lands at the access's first cycle (order of record 3), then time moves. start = self.now self.now += cost[region] self.write_region(region, address, width, value, start) def idle(self, address: int) -> None: if self.now >= self.next_event: self.board.run_events() if self.board.dma_due <= self.now: # A DMA starting now runs beside this internal cycle (order of record 2). before = self.now self.dma.run() self.stall_credit += self.now - before self.after_idle = True self.after_write = False if self.stall_credit > 0: self.stall_credit -= 1 return self.now += 1 def update_latch(self, region: int, address: int, width: int, value: int) -> None: """The CPU data-bus latch after a CPU read (docs/memory-map.md "Open bus").""" if width == 32: if region != UNUSED and not (region == 1 or (region == BIOS and address >= 0x4000)): self.open_bus = value return rule = LATCH_RULE[region] if rule == LATCH_NONE or (region == BIOS and address >= 0x4000): # Unused memory does not update the latch (alyosha-tas Bus/ReadMe.txt; TEST ROM). return if width == 8: value = value << ((address & 1) << 3) # the byte in its lane of the halfword if rule == LATCH_BOTH_HALVES: half = value & 0xFFFF self.open_bus = half | half << 16 elif rule == LATCH_WORD: self.open_bus = self.read_word_silent(region, address) else: if address & 2: self.open_bus = (self.open_bus & 0xFFFF) | (value & 0xFFFF) << 16 else: self.open_bus = (self.open_bus & 0xFFFF0000) | (value & 0xFFFF) def read_word_silent(self, region: int, address: int) -> int: """The aligned word a 32-bit memory drives for a narrower read (BIOS, OAM).""" if region == BIOS: if not self.fetch_in_bios: return self.bios_latch o = address & 0x3FFC b = self.bios return b[o] | b[o + 1] << 8 | b[o + 2] << 16 | b[o + 3] << 24 o = address & 0x3FC m = self.oam return m[o] | m[o + 1] << 8 | m[o + 2] << 16 | m[o + 3] << 24 # ---------------------------------------------------------------------------------- # Values, without timing def unused_value(self, address: int, width: int) -> int: """Open bus: the CPU latch's byte, halfword or word at the address's alignment.""" value = self.open_bus if width == 32: return value if width == 16: return (value >> ((address & 2) << 3)) & 0xFFFF return (value >> ((address & 3) << 3)) & 0xFF def read_region(self, region: int, address: int, width: int, code: int) -> int: if region == IWRAM: if not self.memcnt & 1: return self.read_mem(self.iwram, address & 0x7FFF, width) return self.unused_value(address, width) if region == EWRAM: if self.memcnt & 1: return self.unused_value(address, width) if not self.memcnt & 0x20: # Bit 5 off: "the 32K WRAM mirrored there" (GBATEK §GBA System Control; DOC). return self.read_mem(self.iwram, address & 0x7FFF, width) return self.read_mem(self.ewram, address & 0x3FFFF, width) if region == IO: return self.read_io(address, width) if region == BIOS: if address >= 0x4000: return self.unused_value(address, width) if code: o = address & 0x3FFC b = self.bios self.bios_latch = b[o] | b[o + 1] << 8 | b[o + 2] << 16 | b[o + 3] << 24 return self.read_mem(self.bios, address, width) if self.fetch_in_bios: return self.read_mem(self.bios, address, width) # Protected: the last opcode fetched from the BIOS (GBATEK §GBA Unpredictable # Things; DOC; U-MEM-1). value = self.bios_latch if width == 32: return value if width == 16: return (value >> ((address & 2) << 3)) & 0xFFFF return (value >> ((address & 3) << 3)) & 0xFF if region == PALETTE: return self.read_mem(self.palette, address & 0x3FF, width) if region == VRAM: offset = self.vram_offset(address) if offset < 0: return 0 return self.read_mem(self.vram, offset, width) if region == OAM: return self.read_mem(self.oam, address & 0x3FF, width) if region == SRAM_LO or region == SRAM_HI: byte = self.backup.read8(address & 0xFFFF) # 16/32-bit reads return the byte times 0101h / 01010101h (GBATEK §GBA # Unpredictable Things, "Accessing SRAM Area by 16bit/32bit"; DOC). if width == 32: return byte * 0x01010101 if width == 16: return byte * 0x0101 return byte return self.unused_value(address, width) def vram_offset(self, address: int) -> int: """VRAM's 128 KB pattern: 64 KB BG, 32 KB OBJ, the OBJ 32 KB again (GBATEK §Memory Mirrors; DOC). In bitmap modes 06018000-0601BFFF does not work: -1 (hw-test ppu/vram-mirror; TEST ROM).""" offset = address & 0x1FFFF if offset >= 0x18000: if self.bitmap_mode and offset < 0x1C000: return -1 offset -= 0x8000 return offset @staticmethod def read_mem(memory, offset: int, width: int) -> int: if width == 32: o = offset & ~3 return memory[o] | memory[o + 1] << 8 | memory[o + 2] << 16 | memory[o + 3] << 24 if width == 16: o = offset & ~1 return memory[o] | memory[o + 1] << 8 return memory[offset] @staticmethod def write_mem(memory, offset: int, width: int, value: int) -> None: if width == 32: o = offset & ~3 memory[o] = value & 0xFF memory[o + 1] = (value >> 8) & 0xFF memory[o + 2] = (value >> 16) & 0xFF memory[o + 3] = (value >> 24) & 0xFF elif width == 16: o = offset & ~1 memory[o] = value & 0xFF memory[o + 1] = (value >> 8) & 0xFF else: memory[offset] = value & 0xFF def write_region(self, region: int, address: int, width: int, value: int, time: int) -> None: if region == IWRAM: if not self.memcnt & 1: self.write_mem(self.iwram, address & 0x7FFF, width, value) elif region == EWRAM: if self.memcnt & 1: return if not self.memcnt & 0x20: self.write_mem(self.iwram, address & 0x7FFF, width, value) else: self.write_mem(self.ewram, address & 0x3FFFF, width, value) elif region == IO: self.write_io(address, width, value, time) elif region == PALETTE: self.dots.note_write(time) if width == 8: # 8-bit writes to palette RAM write the byte to both halves (GBATEK §GBA # Unpredictable Things, "Writing 8bit Data to Video Memory"; DOC). self.write_mem(self.palette, address & 0x3FF, 16, (value & 0xFF) * 0x101) else: self.write_mem(self.palette, address & 0x3FF, width, value) elif region == VRAM: offset = self.vram_offset(address) if offset < 0: return self.dots.note_write(time) if width == 8: # BG VRAM (below 64 KB, or 80 KB in bitmap modes) takes the byte in both # halves; OBJ VRAM ignores 8-bit writes (same section; DOC). if offset < (0x14000 if self.bitmap_mode else 0x10000): self.write_mem(self.vram, offset, 16, (value & 0xFF) * 0x101) else: self.write_mem(self.vram, offset, width, value) elif region == OAM: self.dots.note_write(time) if width != 8: # 8-bit writes to OAM are ignored (same section; DOC) self.write_mem(self.oam, address & 0x3FF, width, value) elif region == SRAM_LO or region == SRAM_HI: # A wider write stores only the byte (data ROR (address*8)) & FF (GBATEK §GBA # Unpredictable Things; DOC). if width != 8: rotate = (address & 3) << 3 if width == 16: value = value | value << 16 value = ((value >> rotate) | (value << (32 - rotate))) & 0xFF self.backup.write8(address & 0xFFFF, value) # BIOS, unused: ignored (DOC: the BIOS is ROM; HYPOTHESIS for unused space). # ---------------------------------------------------------------------------------- # I/O def read_io(self, address: int, width: int) -> int: offset = address & 0x00FFFFFF if offset >= 0x400: # 04000800 repeats every 64 KB (GBATEK §Memory Mirrors; DOC); the rest is unused. if (offset & 0xFFFF) in (0x800, 0x802): value = self.memcnt if width == 32: return value if width == 16: return (value >> ((address & 2) << 3)) & 0xFFFF return (value >> ((address & 3) << 3)) & 0xFF return self.unused_value(address, width) io = self.io if width == 32: low = io.read16(offset & 0x3FC) high = io.read16((offset & 0x3FC) | 2) if low is None and high is None: return self.open_bus return (low or 0) | (high or 0) << 16 half = io.read16(offset & 0x3FE) if half is None: # Unreadable: zero when the other half of the 32-bit fragment is readable, open # bus otherwise (GBATEK §GBA Unpredictable Things; DOC; mGBA suite io-read.c). if io.read16((offset & 0x3FE) ^ 2) is not None: half = 0 else: half = (self.open_bus >> ((address & 2) << 3)) & 0xFFFF if width == 16: return half return (half >> ((address & 1) << 3)) & 0xFF def write_io(self, address: int, width: int, value: int, time: int) -> None: offset = address & 0x00FFFFFF if offset >= 0x400: if (offset & 0xFFFF) in (0x800, 0x802): if width == 32: self.write_memcnt(value) else: shift = (address & 2 if width == 16 else address & 3) << 3 mask = (0xFFFF if width == 16 else 0xFF) << shift self.write_memcnt((self.memcnt & ~mask) | ((value << shift) & mask)) return io = self.io if width == 32: io.write16(offset & 0x3FC, value & 0xFFFF, 0xFFFF, time) io.write16((offset & 0x3FC) | 2, (value >> 16) & 0xFFFF, 0xFFFF, time) elif width == 16: io.write16(offset & 0x3FE, value & 0xFFFF, 0xFFFF, time) elif offset & 1: io.write16(offset & 0x3FE, (value & 0xFF) << 8, 0xFF00, time) else: io.write16(offset, value & 0xFF, 0x00FF, time) # ---------------------------------------------------------------------------------- # The cartridge bus def in_eeprom(self, index: int) -> bool: """The EEPROM is at 0D000000-0DFFFFFF on carts of 16 MB or less, only at 0DFFFF00-0DFFFFFF on 32 MB carts (GBATEK §GBA Cart Backup EEPROM; DOC). ``index`` is a halfword index in the 32 MB space, so 0D000000 is 0x800000.""" if index < 0x800000: return False if self.rom.size > 0x1000000: return index >= 0xFFFF80 return True def cart_access(self, address: int, width: int, seq, value: int | None = None) -> int: """One CPU or DMA access to the Game Pak ROM area, as halfwords on the cartridge bus. - Each halfword costs 1 + the window's N or S wait states; a 32-bit access is two, the second always S (GBATEK §GBA System Control; DOC). - N loads the chip's address counter from the address, S uses the counter; either way it then counts on (GBATEK §GBA Cartridge ROM; DOC; U-TIM-2). So an S access returns the halfword after the last one served, whatever address is driven: hw-test dma/burst-into-tears reads 08000002 and 08000004 where it drives 08000000 and 08000002 (TEST ROM). A write moves the counter like a read (same test; HYPOTHESIS for the mechanism). - The 128 KB rule: "The GBA forcefully uses non-sequential timing at the beginning of each 128K-block" (GBATEK; DOC); alyosha-tas DMA/readme.txt: "non-sequential accesses are decided based on if the current internal addresses is adjacent to a 0x20000 boundary ... the NEXT access will use non-sequential timing, even if in decrement or fixed mode" (TEST ROM). Here: an access that follows a cartridge halfword driven at the last halfword of a 128 KB block (where a 16-bit counter wraps) is N, whatever its own address (``cart_wrapped``). FITTED: it gives all 18 lines of hw-test bus/128kb-boundary (the decrement lines included, where the plain "access at a block start" rule is 2 cycles off on five lines) and alyosha-tas DMA/DMA_ROM_Fixed; docs/timing.md "The 128 KB rule". """ region = address >> 24 seq = bool(seq) and not self.cart_wrapped index = (address & 0x1FFFFFF) >> 1 halves = 1 if width == 32: index &= ~1 halves = 2 result = 0 eeprom = self.eeprom gpio = self.gpio for half in range(halves): sequential = seq or half == 1 self.now += self.s16[region] if sequential else self.n16[region] if not sequential: self.cart_latch = (index + half) & 0xFFFFFF at = self.cart_latch self.cart_latch = (at + 1) & 0xFFFFFF # The driven address, not the chip's counter, decides (alyosha-tas: "the current # internal address"; the decrement lines need it). self.cart_wrapped = ((index + half) & 0xFFFF) == 0xFFFF if eeprom is not None and self.in_eeprom(at): # The access ends at self.now; the chip answers then (HYPOTHESIS). if value is None: result |= eeprom.read_bit(self.now) << (16 * half) else: eeprom.write_bit((value >> (16 * half)) & 1, not sequential, self.now) elif gpio is not None and 0x62 <= at <= 0x64: # The ROM chip's GPIO port at 080000C4-080000C8 (cartridge/rtc.py; DOC). if value is None: port = gpio.read(at, self.now) result |= (self.rom.halfword(at) if port is None else port) << (16 * half) else: gpio.write(at, (value >> (16 * half)) & 0xFFFF, self.now) elif value is None: result |= self.rom.halfword(at) << (16 * half) if width == 8: return (result >> ((address & 1) << 3)) & 0xFF return result def cart_read(self, address: int, width: int, seq) -> int: """A CPU access: GBATEK's 128 KB rule as written also applies (an S access at the start of a block is N): hw-test bus/128kb-boundary "LDM 07FFFFF8", whose third word at 08000000 follows two OAM words (TEST ROM; DOC).""" return self.cart_access(address, width, seq and (address & 0x1FFFF) != 0) def cart_write(self, address: int, width: int, value: int, seq) -> None: """A plain ROM ignores the data; the EEPROM takes data bit 0 and the GPIO port its register (docs/cartridge.md). A byte store drives its byte on both halves of the bus and reaches the port too (HYPOTHESIS, U-CART-4: GBATEK asks whether STRB is ignored "only in DS mode?").""" self.cart_access(address, width, seq and (address & 0x1FFFF) != 0, value) # ---------------------------------------------------------------------------------- # DMA's accesses (gba_python.dma): the same memories and prices, no CPU latch rules def dma_read(self, address: int, width: int, seq: bool) -> int: """A DMA read; the caller has checked that the address is readable for DMA.""" if self.now >= self.next_event: self.board.run_events() region = address >> 24 if address < 0x10000000 else UNUSED if ROM_FIRST <= region <= ROM_LAST: prefetch = self.prefetch if prefetch.enabled: prefetch.data_access(self.now) value = self.cart_access(address, width, not self.dma_cart_fresh) self.dma_cart_fresh = False if prefetch.enabled: prefetch.after_cart(self.now) return value cost = (self.s32 if seq else self.n32) if width == 32 else (self.s16 if seq else self.n16) if PALETTE <= region <= OAM: self.video_wait(region, address, width) value = self.read_region(region, address, width, 0) self.now += cost[region] return value def dma_write(self, address: int, width: int, value: int, seq: bool) -> None: if self.now >= self.next_event: self.board.run_events() region = address >> 24 if address < 0x10000000 else UNUSED if ROM_FIRST <= region <= ROM_LAST: prefetch = self.prefetch if prefetch.enabled: prefetch.data_access(self.now) self.cart_access(address, width, not self.dma_cart_fresh, value) self.dma_cart_fresh = False if prefetch.enabled: prefetch.after_cart(self.now) return cost = (self.s32 if seq else self.n32) if width == 32 else (self.s16 if seq else self.n16) if PALETTE <= region <= OAM: self.video_wait(region, address, width) start = self.now self.now += cost[region] self.write_region(region, address, width, value, start) def video_wait(self, region: int, address: int, width: int) -> None: """An access to palette RAM, VRAM or OAM waits one cycle for each of its cycles in which the picture processor uses that memory ("a waitstate (one clock cycle) being inserted automatically in case that the display controller was accessing memory simultaneously": GBATEK §GBA Memory Map; DOC): one cycle, then the access goes ahead whatever the picture processor does next (HYPOTHESIS from "one clock cycle"; waiting until it is free fails alyosha-tas ppu/Sprite_Last_VRAM_Access_Free by one cycle at every fetch start that passes otherwise). Which cycles those are: gba_python.ppu.dots ``busy`` (U-VID-11). A 32-bit access to the 16-bit palette or VRAM is two cycles, each checked (HYPOTHESIS for the second).""" dots = self.dots if dots.busy(region, address, self.now): self.now += 1 if width == 32 and region != OAM and dots.busy(region, address, self.now + 1): self.now += 1 def dma_idle(self, cycles: int) -> None: """DMA's internal cycles (GBATEK §GBA DMA Transfers, "Transfer Rate/Timing"; DOC).""" if self.now >= self.next_event: self.board.run_events() self.now += cycles