/******************************************************************************\ ** ** This file is part of the Hades GBA Emulator, and is made available under ** the terms of the GNU General Public License version 2. ** ** Copyright (C) 2021-2026 - The Hades Authors ** \******************************************************************************/ #include #include "hades.h" #include "gba/gba.h" #include "gba/core.h" #include "gba/core/helpers.h" #include "gba/memory.h" #include "gba/gpio.h" /* ** Determine the value returned by the BUS during an invalid memory access. ** ** Most of this is taken from GBATek, section "GBA Unpredictable Things". */ uint32_t mem_openbus_read( struct gba const *gba, uint32_t addr ) { uint32_t val; uint32_t shift; shift = addr & 0x3; // On first access, open-bus during DMA transfers returns the last prefetched instruction. // On subsequent transfers it returns the last transferred data. if (gba->memory.was_last_access_from_dma) { return gba->memory.dma_bus >> (8 * shift); } if (gba->core.cpsr.thumb) { uint32_t pc; pc = gba->core.pc; switch (pc >> 24) { case EWRAM_REGION: case PALRAM_REGION: case VRAM_REGION: case CART_0_REGION_1 ... CART_2_REGION_2: { val = gba->core.prefetch[1]; val |= (gba->core.prefetch[1]) << 16; break; }; case BIOS_REGION: case OAM_REGION: { if ((pc & 0x2) == 0) { // 4-byte aligned PC val = gba->core.prefetch[1]; val |= (gba->core.prefetch[1]) << 16; // ??? } else { val = gba->core.prefetch[0]; val |= (gba->core.prefetch[1]) << 16; } break; }; case IWRAM_REGION: { if ((pc & 0x2) == 0) { // 4-byte aligned PC val = gba->core.prefetch[1]; val |= (gba->core.prefetch[0]) << 16; } else { val = gba->core.prefetch[0]; val |= (gba->core.prefetch[1]) << 16; } break; }; default: { hs_panic(HS_MEMORY, "Reading the open bus from an impossible page: %u", pc >> 24); break; }; } } else { val = gba->core.prefetch[1]; } return val >> (8 * shift); } /* ** Read the data of type T located in memory at the given address. ** ** T must be either uint32_t, uint16_t or uint8_t. */ #define template_read(T, gba, unaligned_addr) \ ({ \ T _ret = 0; \ uint32_t _addr; \ \ _addr = align(T, (unaligned_addr)); \ switch (_addr >> 24) { \ case BIOS_REGION: { \ if (_addr <= BIOS_END) { \ uint32_t _shift; \ \ _shift = 8 * (_addr & 0b11); \ if ((gba)->core.pc <= BIOS_END) { \ _addr = align(uint32_t, _addr); \ (gba)->memory.bios_bus = *(uint32_t *)((uint8_t *)((gba)->memory.bios) + _addr); \ } \ _ret = (gba)->memory.bios_bus >> _shift; \ } else { \ hs_dbgln(HS_MEMORY, "Invalid BIOS read of size %zu from 0x%08x", sizeof(T), _addr); \ _ret = mem_openbus_read((gba), _addr); \ } \ break; \ }; \ case EWRAM_REGION: \ _ret = *(T *)((uint8_t *)((gba)->memory.ewram) + (_addr & EWRAM_MASK)); \ break; \ case IWRAM_REGION: \ _ret = *(T *)((uint8_t *)((gba)->memory.iwram) + (_addr & IWRAM_MASK)); \ break; \ case IO_REGION: \ _ret = _Generic(_ret, \ uint32_t: ( \ ((T)mem_io_read8((gba), _addr + 0) << 0) | \ ((T)mem_io_read8((gba), _addr + 1) << 8) | \ ((T)mem_io_read8((gba), _addr + 2) << 16) | \ ((T)mem_io_read8((gba), _addr + 3) << 24) \ ), \ uint16_t: ( \ ((T)mem_io_read8((gba), _addr + 0) << 0) | \ ((T)mem_io_read8((gba), _addr + 1) << 8) \ ), \ default: mem_io_read8((gba), _addr) \ ); \ break; \ case PALRAM_REGION: \ _ret = *(T *)((uint8_t *)((gba)->memory.palram) + (_addr & PALRAM_MASK)); \ break; \ case VRAM_REGION: \ _ret = *(T *)((uint8_t *)((gba)->memory.vram) + (_addr & ((_addr & 0x10000) ? VRAM_MASK_1 : VRAM_MASK_2))); \ break; \ case OAM_REGION: \ _ret = *(T *)((uint8_t *)((gba)->memory.oam) + (_addr & OAM_MASK)); \ break; \ case CART_REGION_START ... CART_REGION_END: { \ if (__hs_unlikely( \ ((gba)->memory.backup_storage.type == BACKUP_EEPROM_4K || (gba)->memory.backup_storage.type == BACKUP_EEPROM_64K) \ && (_addr & (gba)->memory.backup_storage.chip.eeprom.mask) == (gba)->memory.backup_storage.chip.eeprom.range \ )) { \ _ret = mem_eeprom_read8(gba); \ } else if (__hs_unlikely(_addr >= GPIO_REG_START && _addr <= GPIO_REG_END && (gba)->gpio.readable)) { \ _ret = gpio_read_u8((gba), _addr); \ } else if (__hs_unlikely((_addr & (gba)->memory.rom_mask) >= (gba)->memory.rom_size)) { \ _ret = _Generic(_ret, \ uint32_t: ( \ ((_addr >> 1) & 0xFFFF) | \ ((((_addr + 2) >> 1) & 0xFFFF) << 16) \ ), \ uint16_t: ( \ (_addr >> 1) & 0xFFFF \ ), \ default: ((_addr >> (1 + 8 * (_addr & 0b1))) & 0xFF) \ ); \ } else { \ _ret = *(T *)((uint8_t *)((gba)->memory.active_rom) + (_addr & (gba)->memory.rom_mask)); \ } \ break; \ }; \ case SRAM_REGION: \ case SRAM_MIRROR_REGION: { \ _ret = _Generic(_ret, \ uint32_t: ( \ ((T)mem_backup_storage_read8((gba), (unaligned_addr)) * 0x01010101) \ ), \ uint16_t: ( \ ((T)mem_backup_storage_read8((gba), (unaligned_addr)) * 0x0101) \ ), \ default: mem_backup_storage_read8((gba), (unaligned_addr)) \ ); \ break; \ }; \ default: { \ hs_dbgln(HS_MEMORY, "Invalid read of size %zu from 0x%08x", sizeof(T), _addr); \ _ret = mem_openbus_read((gba), _addr); \ break; \ } \ }; \ _ret; \ }) /* ** Write a data of type T to memory at the given address. ** ** T must be either uint32_t, uint16_t or uint8_t. */ #define template_write(T, gba, unaligned_addr, val) \ ({ \ uint32_t _addr; \ \ _addr = align(T, (unaligned_addr)); \ switch (_addr >> 24) { \ case BIOS_REGION: \ /* Ignore writes attempts to the bios memory. */ \ break; \ case EWRAM_REGION: \ *(T *)((uint8_t *)((gba)->memory.ewram) + (_addr & EWRAM_MASK)) = (T)(val); \ break; \ case IWRAM_REGION: \ *(T *)((uint8_t *)((gba)->memory.iwram) + (_addr & IWRAM_MASK)) = (T)(val); \ break; \ case IO_REGION: \ _Generic(val, \ uint32_t: ({ \ mem_io_write8((gba), _addr + 0, (uint8_t)((val) >> 0)); \ mem_io_write8((gba), _addr + 1, (uint8_t)((val) >> 8)); \ mem_io_write8((gba), _addr + 2, (uint8_t)((val) >> 16)); \ mem_io_write8((gba), _addr + 3, (uint8_t)((val) >> 24)); \ }), \ uint16_t: ({ \ mem_io_write8((gba), _addr + 0, (uint8_t)((val) >> 0)); \ mem_io_write8((gba), _addr + 1, (uint8_t)((val) >> 8)); \ }), \ default: ({ \ mem_io_write8((gba), _addr, (val)); \ }) \ ); \ break; \ case PALRAM_REGION: { \ _Generic(val, \ uint32_t: ({ \ *(T *)((uint8_t *)((gba)->memory.palram) + (_addr & PALRAM_MASK)) = (T)(val); \ }), \ uint16_t: ({ \ *(T *)((uint8_t *)((gba)->memory.palram) + (_addr & PALRAM_MASK)) = (T)(val); \ }), \ default: ({ \ /* u8 writes to PALRAM are writting to both the upper/lower bytes */ \ addr &= ~(sizeof(uint16_t) - 1); \ *(T *)((uint8_t *)((gba)->memory.palram) + (_addr & PALRAM_MASK)) = (T)(val); \ *(T *)((uint8_t *)((gba)->memory.palram) + ((_addr + 1) & PALRAM_MASK)) = (T)(val); \ }) \ ); \ break; \ }; \ case VRAM_REGION: { \ _Generic(val, \ uint32_t: ({ \ *(T *)((uint8_t *)((gba)->memory.vram) + (_addr & ((_addr & 0x10000) ? VRAM_MASK_1 : VRAM_MASK_2))) = (T)(val); \ }), \ uint16_t: ({ \ *(T *)((uint8_t *)((gba)->memory.vram) + (_addr & ((_addr & 0x10000) ? VRAM_MASK_1 : VRAM_MASK_2))) = (T)(val); \ }), \ default: ({ \ uint32_t new_addr; \ \ new_addr = _addr & 0x1FFFF; \ /* ** Ignore u8 write attemps to OBJ VRAM memory ** OBJ VRAM size is different depending on the BG mode. */ \ if ( \ ((gba)->io.dispcnt.bg_mode <= 2 && (new_addr) < 0x10000) \ || ((gba)->io.dispcnt.bg_mode >= 3 && (new_addr) < 0x14000) \ ) { \ addr &= ~(sizeof(uint16_t) - 1); \ *(T *)((uint8_t *)((gba)->memory.vram) + (_addr & ((_addr & 0x10000) ? VRAM_MASK_1 : VRAM_MASK_2))) = (T)(val); \ *(T *)((uint8_t *)((gba)->memory.vram) + ((_addr + 1) & (((_addr + 1) & 0x10000) ? VRAM_MASK_1 : VRAM_MASK_2))) = (T)(val); \ } \ }) \ ); \ break; \ }; \ case OAM_REGION: { \ _Generic(val, \ uint32_t: ({ \ *(T *)((uint8_t *)((gba)->memory.oam) + (_addr & OAM_MASK)) = (T)(val); \ }), \ uint16_t: ({ \ *(T *)((uint8_t *)((gba)->memory.oam) + (_addr & OAM_MASK)) = (T)(val); \ }), \ default: ({ \ /* Ignore u8 write attemps to OAM memory */ \ }) \ ); \ break; \ }; \ case CART_REGION_START ... CART_REGION_END: { \ if (((gba)->memory.backup_storage.type == BACKUP_EEPROM_4K || (gba)->memory.backup_storage.type == BACKUP_EEPROM_64K) \ && (_addr & (gba)->memory.backup_storage.chip.eeprom.mask) == (gba)->memory.backup_storage.chip.eeprom.range \ ) { \ mem_eeprom_write8((gba), (val) & 1); \ } else if (_addr >= GPIO_REG_START && _addr <= GPIO_REG_END) { \ gpio_write_u8((gba), _addr, (val)); \ } \ /* Ignore writes attempts to the cartridge memory. */ \ break; \ }; \ case SRAM_REGION: \ case SRAM_MIRROR_REGION: \ /* ** All writes to the backup storage are u8 writes, eventually rotated if the ** address isn't aligned on T. */ \ mem_backup_storage_write8( \ (gba), \ (unaligned_addr), \ ((val) >> (8 * ((unaligned_addr) % sizeof(T)))) \ ); \ break; \ default: { \ hs_dbgln(HS_MEMORY, "Invalid write of size %zu to 0x%08x", sizeof(T), _addr); \ break; \ }; \ }; \ }) uint8_t mem_read8_raw( struct gba *gba, uint32_t addr ) { return template_read(uint8_t, gba, addr); } /* ** Read the byte at the given address. */ uint8_t mem_read8( struct gba *gba, uint32_t addr, enum access_flags access_type ) { #ifdef WITH_DEBUGGER debugger_eval_read_watchpoints(gba, addr, sizeof(uint8_t)); #endif mem_bus_access(gba, addr, sizeof(uint8_t), access_type); return template_read(uint8_t, gba, addr); } uint16_t mem_read16_raw( struct gba *gba, uint32_t addr ) { return template_read(uint16_t, gba, addr); } /* ** Read the half-word at the given address. */ uint16_t mem_read16( struct gba *gba, uint32_t addr, enum access_flags access_type ) { #ifdef WITH_DEBUGGER debugger_eval_read_watchpoints(gba, addr, sizeof(uint16_t)); #endif mem_bus_access(gba, addr, sizeof(uint16_t), access_type); return template_read(uint16_t, gba, addr); } /* ** Read the half-word at the given address and ROR it if the ** address isn't aligned. */ uint32_t mem_read16_ror( struct gba *gba, uint32_t addr, enum access_flags access_type ) { uint32_t rotate; uint32_t value; #ifdef WITH_DEBUGGER debugger_eval_read_watchpoints(gba, addr, sizeof(uint16_t)); #endif mem_bus_access(gba, addr, sizeof(uint16_t), access_type); rotate = (addr & 0b1) * 8; value = template_read(uint16_t, gba, addr); // Unaligned 16-bits loads are supposed to be unpredictable, but in practise the GBA rotates them return ror32(value, rotate); } uint32_t mem_read32_raw( struct gba *gba, uint32_t addr ) { return template_read(uint32_t, gba, addr); } /* ** Read the word at the given address. */ uint32_t mem_read32( struct gba *gba, uint32_t addr, enum access_flags access_type ) { #ifdef WITH_DEBUGGER debugger_eval_read_watchpoints(gba, addr, sizeof(uint32_t)); #endif mem_bus_access(gba, addr, sizeof(uint32_t), access_type); return template_read(uint32_t, gba, addr); } /* ** Read the word at the given address and ROR it if the ** address isn't aligned. */ uint32_t mem_read32_ror( struct gba *gba, uint32_t addr, enum access_flags access_type ) { uint32_t rotate; uint32_t value; #ifdef WITH_DEBUGGER debugger_eval_read_watchpoints(gba, addr, sizeof(uint32_t)); #endif mem_bus_access(gba, addr, sizeof(uint32_t), access_type); rotate = (addr % 4) << 3; value = template_read(uint32_t, gba, addr); return ror32(value, rotate); } void mem_write8_raw( struct gba *gba, uint32_t addr, uint8_t val ) { template_write(uint8_t, gba, addr, val); } /* ** Write a byte at the given address. */ void mem_write8( struct gba *gba, uint32_t addr, uint8_t val, enum access_flags access_type ) { #ifdef WITH_DEBUGGER debugger_eval_write_watchpoints(gba, addr, sizeof(uint8_t), val); #endif mem_bus_access(gba, addr, sizeof(uint8_t), access_type); template_write(uint8_t, gba, addr, val); } void mem_write16_raw( struct gba *gba, uint32_t addr, uint16_t val ) { template_write(uint16_t, gba, addr, val); } /* ** write a half-word at the given address. */ void mem_write16( struct gba *gba, uint32_t addr, uint16_t val, enum access_flags access_type ) { #ifdef WITH_DEBUGGER debugger_eval_write_watchpoints(gba, addr, sizeof(uint16_t), val); #endif mem_bus_access(gba, addr, sizeof(uint16_t), access_type); template_write(uint16_t, gba, addr, val); } void mem_write32_raw( struct gba *gba, uint32_t addr, uint32_t val ) { template_write(uint32_t, gba, addr, val); } /* ** Write a word at the given address. */ void mem_write32( struct gba *gba, uint32_t addr, uint32_t val, enum access_flags access_type ) { #ifdef WITH_DEBUGGER debugger_eval_write_watchpoints(gba, addr, sizeof(uint32_t), val); #endif mem_bus_access(gba, addr, sizeof(uint32_t), access_type); template_write(uint32_t, gba, addr, val); } static void mem_patch_rom32( struct gba *gba, uint32_t addr, uint32_t val ) { *(uint32_t *)(gba->memory.patched_rom + (addr & gba->memory.rom_mask)) = val; } static void mem_patch_rom16( struct gba *gba, uint32_t addr, uint16_t val ) { *(uint16_t *)(gba->memory.patched_rom + (addr & gba->memory.rom_mask)) = val; } static void mem_patch_rom8( struct gba *gba, uint32_t addr, uint8_t val ) { *(gba->memory.patched_rom + (addr & gba->memory.rom_mask)) = val; } static void mem_install_dbg_software_breakpoint( struct gba *gba, uint32_t addr, bool thumb ) { if (!gba->memory.patched_rom) { gba->memory.patched_rom = malloc(CART_SIZE); hs_assert(gba->memory.patched_rom); memcpy(gba->memory.patched_rom, gba->memory.unpatched_rom, CART_SIZE); gba->memory.active_rom = gba->memory.patched_rom; } if (thumb) { mem_patch_rom16(gba, addr, 0xBE00); } else { mem_patch_rom32(gba, addr, 0xEC000000); } } static void mem_install_cheat_rom_patches( struct gba *gba, struct cheat_bin const *bin ) { size_t i; // Early return to avoid allocating memory if there's no ROM patch for this cheat. if (bin->rom_patches.len == 0) { return; } hs_dbgln(HS_CHEAT, "Installing ROM patch for a cheat:"); if (!gba->memory.patched_rom) { gba->memory.patched_rom = malloc(CART_SIZE); hs_assert(gba->memory.patched_rom); memcpy(gba->memory.patched_rom, gba->memory.unpatched_rom, CART_SIZE); gba->memory.active_rom = gba->memory.patched_rom; } for (i = 0; i < bin->rom_patches.len; ++i) { struct cheat_rom_patch *patch; patch = &bin->rom_patches.list[i]; hs_dbgln(HS_CHEAT, " - Patch %zu", i + 1); hs_dbgln(HS_CHEAT, " - Address: %08x", patch->addr); hs_dbgln(HS_CHEAT, " - Width: %i", patch->width); hs_dbgln(HS_CHEAT, " - Val: %0*x", patch->width * 2, patch->value); switch (patch->width) { case 1: mem_patch_rom8(gba, patch->addr, patch->value); break; case 2: mem_patch_rom16(gba, patch->addr, patch->value); break; case 4: mem_patch_rom32(gba, patch->addr, patch->value); break; default: hs_panic(HS_CHEAT, "Invalid rom patch width: %u", patch->width); } } } void mem_refresh_rom_patches( struct gba *gba ) { size_t i; hs_dbgln(HS_CHEAT, "Refreshing ROM patches."); free(gba->memory.patched_rom); gba->memory.patched_rom = NULL; gba->memory.active_rom = gba->memory.unpatched_rom; for (i = 0; i < gba->cheats.len; ++i) { struct cheat_bin *bin; bin = &gba->cheats.list[i]; if (bin->hook.active) { mem_install_dbg_software_breakpoint(gba, bin->hook.bp.ptr, bin->hook.bp.thumb); } mem_install_cheat_rom_patches(gba, &gba->cheats.list[i]); } for (i = 0; i < gba->debugger.sw_breakpoints.len; ++i) { struct sw_breakpoint *swbp; swbp = &gba->debugger.sw_breakpoints.list[i]; mem_install_dbg_software_breakpoint(gba, swbp->ptr, swbp->thumb); } if (gba->core.idle_loop.enabled) { mem_install_dbg_software_breakpoint(gba, gba->core.idle_loop.addr, gba->core.idle_loop.thumb); } }