# SPDX-License-Identifier: MIT """The memory map, mirrors, narrow writes, open bus and BIOS protection (docs/memory-map.md; GBATEK §GBA Memory Map, §Memory Mirrors, §GBA Unpredictable Things; DOC).""" from __future__ import annotations from programs import board_for def board(): return board_for(b"\x00" * 0x100) def cpu_write(b, address, width, value): b.bus.write_region(address >> 24, address, width, value, b.bus.now) def cpu_read(b, address, width): return b.bus.read_region(address >> 24 if address < 0x10000000 else 16, address, width, 0) def test_mirrors(): b = board() cpu_write(b, 0x02000010, 32, 0x11223344) assert cpu_read(b, 0x02040010, 32) == 0x11223344 # EWRAM every 256 KB cpu_write(b, 0x03000020, 16, 0xBEEF) assert cpu_read(b, 0x03FFFFE0 & ~0x7FFF | 0x20, 16) == 0xBEEF # IWRAM every 32 KB cpu_write(b, 0x05000002, 16, 0x7C1F) assert cpu_read(b, 0x05000402, 16) == 0x7C1F # palette every 1 KB cpu_write(b, 0x06010000, 16, 0x1234) assert cpu_read(b, 0x06018000, 16) == 0x1234 # VRAM's OBJ 32 KB repeats at 18000 assert cpu_read(b, 0x06030000, 16) == 0x1234 # and the 128 KB pattern repeats cpu_write(b, 0x07000004, 16, 0x5555) assert cpu_read(b, 0x07000404, 16) == 0x5555 # OAM every 1 KB def test_bitmap_mode_hides_vram_18000(): b = board() b.ppu.write16(0x000, 0x0003, 0xFFFF, 0) # mode 3 cpu_write(b, 0x06018000, 16, 0x1234) assert cpu_read(b, 0x06018000, 16) == 0 # hw-test ppu/vram-mirror "M3 18000h: 0x0" def test_byte_writes_to_video_memory(): b = board() cpu_write(b, 0x05000011, 8, 0xAB) assert cpu_read(b, 0x05000010, 16) == 0xABAB # palette: both halves cpu_write(b, 0x06000021, 8, 0xCD) assert cpu_read(b, 0x06000020, 16) == 0xCDCD # BG VRAM: both halves cpu_write(b, 0x06010000, 8, 0xEE) assert cpu_read(b, 0x06010000, 16) == 0 # OBJ VRAM: ignored cpu_write(b, 0x07000000, 8, 0xEE) assert cpu_read(b, 0x07000000, 16) == 0 # OAM: ignored def test_rom_beyond_the_chip_reads_the_address(): b = board() assert cpu_read(b, 0x09000000, 16) == (0x01000000 >> 1) & 0xFFFF assert b.rom.halfword(0x123456) == 0x3456 def test_unused_memory_reads_the_cpu_latch(): b = board() b.bus.open_bus = 0xA1B2C3D4 assert cpu_read(b, 0x00004000, 32) == 0xA1B2C3D4 assert cpu_read(b, 0x10000002, 16) == 0xA1B2 assert cpu_read(b, 0x01000001, 8) == 0xC3 def test_bios_protection_returns_the_latch(): b = board_for(b"\x00" * 0x100, bios=bytes(range(256)) * 64) b.bus.fetch_in_bios = True assert b.bus.read_region(0, 0x10, 32, 1) == 0x13121110 # a fetch latches the word b.bus.fetch_in_bios = False assert b.bus.read_region(0, 0x80, 32, 0) == 0x13121110 assert b.bus.read_region(0, 0x82, 16, 0) == 0x1312 def test_io_open_bus_rules(): b = board() b.bus.open_bus = 0x12345678 # BG0HOFS/BG0VOFS: the whole 32-bit fragment is write-only: open bus. assert b.bus.read_io(0x04000010, 16) == 0x5678 # DMA0CNT_L is write-only but DMA0CNT_H is readable: zero. assert b.bus.read_io(0x040000B8, 16) == 0 # The undocumented word at 04000800 repeats every 64 KB. assert b.bus.read_io(0x04010800, 32) == 0x0D000020 def test_sram_wide_accesses(): b = board_for(b"SRAM_V113" + bytes(0x100)) cpu_write(b, 0x0E000000, 8, 0x5A) assert cpu_read(b, 0x0E000000, 16) == 0x5A5A assert cpu_read(b, 0x0E000000, 32) == 0x5A5A5A5A cpu_write(b, 0x0E000011, 32, 0x11223344) # stores (data ROR 8) & FF assert cpu_read(b, 0x0E000011, 8) == 0x33 assert cpu_read(b, 0x0E008011, 8) == 0x33 # 32 KB twice in the window