2016-01-14 07:02:50 +00:00
|
|
|
/* Copyright (c) 2013-2016 Jeffrey Pfau
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2016-12-31 01:00:22 +00:00
|
|
|
#include <mgba/internal/gb/memory.h>
|
2016-01-14 07:02:50 +00:00
|
|
|
|
2016-12-31 01:00:22 +00:00
|
|
|
#include <mgba/core/interface.h>
|
|
|
|
#include <mgba/internal/gb/gb.h>
|
|
|
|
#include <mgba/internal/gb/io.h>
|
|
|
|
#include <mgba/internal/gb/mbc.h>
|
|
|
|
#include <mgba/internal/gb/serialize.h>
|
2020-01-01 19:35:00 +00:00
|
|
|
#include <mgba/internal/sm83/sm83.h>
|
2016-01-14 07:02:50 +00:00
|
|
|
|
2016-12-31 01:00:22 +00:00
|
|
|
#include <mgba-util/memory.h>
|
2016-01-14 07:02:50 +00:00
|
|
|
|
2017-03-05 23:58:00 +00:00
|
|
|
mLOG_DEFINE_CATEGORY(GB_MEM, "GB Memory", "gb.memory");
|
2016-01-26 06:17:01 +00:00
|
|
|
|
2019-09-08 01:32:37 +00:00
|
|
|
static const uint8_t _yankBuffer[] = { 0xFF };
|
|
|
|
|
2018-03-21 16:11:24 +00:00
|
|
|
enum GBBus {
|
|
|
|
GB_BUS_CPU,
|
|
|
|
GB_BUS_MAIN,
|
|
|
|
GB_BUS_VRAM,
|
|
|
|
GB_BUS_RAM
|
2017-05-23 02:49:48 +00:00
|
|
|
};
|
|
|
|
|
2018-03-21 16:11:24 +00:00
|
|
|
static const enum GBBus _oamBlockDMG[] = {
|
|
|
|
GB_BUS_MAIN, // 0x0000
|
|
|
|
GB_BUS_MAIN, // 0x2000
|
|
|
|
GB_BUS_MAIN, // 0x4000
|
|
|
|
GB_BUS_MAIN, // 0x6000
|
|
|
|
GB_BUS_VRAM, // 0x8000
|
|
|
|
GB_BUS_MAIN, // 0xA000
|
|
|
|
GB_BUS_MAIN, // 0xC000
|
|
|
|
GB_BUS_CPU, // 0xE000
|
2017-05-23 02:49:48 +00:00
|
|
|
};
|
|
|
|
|
2018-03-21 16:11:24 +00:00
|
|
|
static const enum GBBus _oamBlockCGB[] = {
|
|
|
|
GB_BUS_MAIN, // 0x0000
|
|
|
|
GB_BUS_MAIN, // 0x2000
|
|
|
|
GB_BUS_MAIN, // 0x4000
|
|
|
|
GB_BUS_MAIN, // 0x6000
|
|
|
|
GB_BUS_VRAM, // 0x8000
|
|
|
|
GB_BUS_MAIN, // 0xA000
|
|
|
|
GB_BUS_RAM, // 0xC000
|
|
|
|
GB_BUS_CPU // 0xE000
|
2017-05-23 02:49:48 +00:00
|
|
|
};
|
|
|
|
|
2018-12-06 03:39:29 +00:00
|
|
|
static const uint8_t _blockedRegion[1] = { 0xFF };
|
|
|
|
|
2016-05-09 05:44:56 +00:00
|
|
|
static void _pristineCow(struct GB* gba);
|
|
|
|
|
2020-01-01 19:35:00 +00:00
|
|
|
static uint8_t GBFastLoad8(struct SM83Core* cpu, uint16_t address) {
|
2017-01-11 09:35:06 +00:00
|
|
|
if (UNLIKELY(address >= cpu->memory.activeRegionEnd)) {
|
2016-02-23 05:39:05 +00:00
|
|
|
cpu->memory.setActiveRegion(cpu, address);
|
|
|
|
return cpu->memory.cpuLoad8(cpu, address);
|
|
|
|
}
|
|
|
|
return cpu->memory.activeRegion[address & cpu->memory.activeMask];
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:35:00 +00:00
|
|
|
static void GBSetActiveRegion(struct SM83Core* cpu, uint16_t address) {
|
2016-02-23 05:39:05 +00:00
|
|
|
struct GB* gb = (struct GB*) cpu->master;
|
|
|
|
struct GBMemory* memory = &gb->memory;
|
|
|
|
switch (address >> 12) {
|
|
|
|
case GB_REGION_CART_BANK0:
|
|
|
|
case GB_REGION_CART_BANK0 + 1:
|
|
|
|
case GB_REGION_CART_BANK0 + 2:
|
|
|
|
case GB_REGION_CART_BANK0 + 3:
|
|
|
|
cpu->memory.cpuLoad8 = GBFastLoad8;
|
2016-05-20 05:31:13 +00:00
|
|
|
cpu->memory.activeRegion = memory->romBase;
|
2016-02-23 05:39:05 +00:00
|
|
|
cpu->memory.activeRegionEnd = GB_BASE_CART_BANK1;
|
|
|
|
cpu->memory.activeMask = GB_SIZE_CART_BANK0 - 1;
|
2019-09-08 01:32:37 +00:00
|
|
|
if (gb->memory.romSize < GB_SIZE_CART_BANK0) {
|
|
|
|
if (address >= gb->memory.romSize) {
|
|
|
|
cpu->memory.activeRegion = _yankBuffer;
|
|
|
|
cpu->memory.activeMask = 0;
|
|
|
|
} else {
|
|
|
|
cpu->memory.activeRegionEnd = gb->memory.romSize;
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 05:39:05 +00:00
|
|
|
break;
|
|
|
|
case GB_REGION_CART_BANK1:
|
|
|
|
case GB_REGION_CART_BANK1 + 1:
|
|
|
|
case GB_REGION_CART_BANK1 + 2:
|
|
|
|
case GB_REGION_CART_BANK1 + 3:
|
2020-08-16 23:27:11 +00:00
|
|
|
if ((gb->memory.mbcType & GB_UNL_BBD) == GB_UNL_BBD) {
|
|
|
|
cpu->memory.cpuLoad8 = GBLoad8;
|
|
|
|
break;
|
|
|
|
}
|
2016-02-23 05:39:05 +00:00
|
|
|
cpu->memory.cpuLoad8 = GBFastLoad8;
|
2018-04-18 05:44:37 +00:00
|
|
|
if (gb->memory.mbcType != GB_MBC6) {
|
|
|
|
cpu->memory.activeRegion = memory->romBank;
|
|
|
|
cpu->memory.activeRegionEnd = GB_BASE_VRAM;
|
|
|
|
cpu->memory.activeMask = GB_SIZE_CART_BANK0 - 1;
|
|
|
|
} else {
|
|
|
|
cpu->memory.activeMask = GB_SIZE_CART_HALFBANK - 1;
|
|
|
|
if (address & 0x2000) {
|
|
|
|
cpu->memory.activeRegion = memory->mbcState.mbc6.romBank1;
|
|
|
|
cpu->memory.activeRegionEnd = GB_BASE_VRAM;
|
|
|
|
} else {
|
|
|
|
cpu->memory.activeRegion = memory->romBank;
|
|
|
|
cpu->memory.activeRegionEnd = GB_BASE_CART_BANK1 + 0x2000;
|
|
|
|
}
|
|
|
|
}
|
2019-09-08 01:32:37 +00:00
|
|
|
if (gb->memory.romSize < GB_SIZE_CART_BANK0 * 2) {
|
|
|
|
if (address >= gb->memory.romSize) {
|
|
|
|
cpu->memory.activeRegion = _yankBuffer;
|
|
|
|
cpu->memory.activeMask = 0;
|
|
|
|
} else {
|
|
|
|
cpu->memory.activeRegionEnd = gb->memory.romSize;
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 05:39:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cpu->memory.cpuLoad8 = GBLoad8;
|
|
|
|
break;
|
|
|
|
}
|
2018-12-06 03:39:29 +00:00
|
|
|
if (gb->memory.dmaRemaining) {
|
|
|
|
const enum GBBus* block = gb->model < GB_MODEL_CGB ? _oamBlockDMG : _oamBlockCGB;
|
|
|
|
enum GBBus dmaBus = block[memory->dmaSource >> 13];
|
|
|
|
enum GBBus accessBus = block[address >> 13];
|
|
|
|
if ((dmaBus != GB_BUS_CPU && dmaBus == accessBus) || (address >= GB_BASE_OAM && address < GB_BASE_UNUSABLE)) {
|
|
|
|
cpu->memory.activeRegion = _blockedRegion;
|
|
|
|
cpu->memory.activeMask = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 07:02:50 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 01:56:46 +00:00
|
|
|
static void _GBMemoryDMAService(struct mTiming* timing, void* context, uint32_t cyclesLate);
|
|
|
|
static void _GBMemoryHDMAService(struct mTiming* timing, void* context, uint32_t cyclesLate);
|
2016-01-22 03:28:56 +00:00
|
|
|
|
2016-01-14 07:02:50 +00:00
|
|
|
void GBMemoryInit(struct GB* gb) {
|
2020-01-01 19:35:00 +00:00
|
|
|
struct SM83Core* cpu = gb->cpu;
|
2016-01-27 04:56:03 +00:00
|
|
|
cpu->memory.cpuLoad8 = GBLoad8;
|
2016-01-14 07:02:50 +00:00
|
|
|
cpu->memory.load8 = GBLoad8;
|
|
|
|
cpu->memory.store8 = GBStore8;
|
2017-05-23 02:39:27 +00:00
|
|
|
cpu->memory.currentSegment = GBCurrentSegment;
|
2016-01-14 07:02:50 +00:00
|
|
|
cpu->memory.setActiveRegion = GBSetActiveRegion;
|
|
|
|
|
|
|
|
gb->memory.wram = 0;
|
|
|
|
gb->memory.wramBank = 0;
|
|
|
|
gb->memory.rom = 0;
|
|
|
|
gb->memory.romBank = 0;
|
|
|
|
gb->memory.romSize = 0;
|
2016-02-15 07:07:44 +00:00
|
|
|
gb->memory.sram = 0;
|
2016-09-09 23:29:52 +00:00
|
|
|
gb->memory.mbcType = GB_MBC_AUTODETECT;
|
2017-06-08 06:06:28 +00:00
|
|
|
gb->memory.mbcRead = NULL;
|
|
|
|
gb->memory.mbcWrite = NULL;
|
2016-01-15 04:50:43 +00:00
|
|
|
|
2016-01-30 07:49:25 +00:00
|
|
|
gb->memory.rtc = NULL;
|
2017-07-26 01:34:39 +00:00
|
|
|
gb->memory.rotation = NULL;
|
|
|
|
gb->memory.rumble = NULL;
|
2017-07-26 17:57:57 +00:00
|
|
|
gb->memory.cam = NULL;
|
2016-01-30 07:49:25 +00:00
|
|
|
|
2016-01-15 04:50:43 +00:00
|
|
|
GBIOInit(gb);
|
2016-01-14 07:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBMemoryDeinit(struct GB* gb) {
|
|
|
|
mappedMemoryFree(gb->memory.wram, GB_SIZE_WORKING_RAM);
|
|
|
|
if (gb->memory.rom) {
|
|
|
|
mappedMemoryFree(gb->memory.rom, gb->memory.romSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBMemoryReset(struct GB* gb) {
|
|
|
|
if (gb->memory.wram) {
|
|
|
|
mappedMemoryFree(gb->memory.wram, GB_SIZE_WORKING_RAM);
|
|
|
|
}
|
|
|
|
gb->memory.wram = anonymousMemoryMap(GB_SIZE_WORKING_RAM);
|
2016-09-22 13:49:32 +00:00
|
|
|
if (gb->model >= GB_MODEL_CGB) {
|
|
|
|
uint32_t* base = (uint32_t*) gb->memory.wram;
|
|
|
|
size_t i;
|
|
|
|
uint32_t pattern = 0;
|
|
|
|
for (i = 0; i < GB_SIZE_WORKING_RAM / 4; i += 4) {
|
|
|
|
if ((i & 0x1FF) == 0) {
|
|
|
|
pattern = ~pattern;
|
|
|
|
}
|
|
|
|
base[i + 0] = pattern;
|
|
|
|
base[i + 1] = pattern;
|
|
|
|
base[i + 2] = ~pattern;
|
|
|
|
base[i + 3] = ~pattern;
|
|
|
|
}
|
|
|
|
}
|
2016-02-16 04:13:32 +00:00
|
|
|
GBMemorySwitchWramBank(&gb->memory, 1);
|
2016-02-20 06:59:36 +00:00
|
|
|
gb->memory.ime = false;
|
|
|
|
gb->memory.ie = 0;
|
|
|
|
|
|
|
|
gb->memory.dmaRemaining = 0;
|
|
|
|
gb->memory.dmaSource = 0;
|
|
|
|
gb->memory.dmaDest = 0;
|
|
|
|
gb->memory.hdmaRemaining = 0;
|
|
|
|
gb->memory.hdmaSource = 0;
|
|
|
|
gb->memory.hdmaDest = 0;
|
|
|
|
gb->memory.isHdma = false;
|
|
|
|
|
2016-09-18 01:56:46 +00:00
|
|
|
|
|
|
|
gb->memory.dmaEvent.context = gb;
|
|
|
|
gb->memory.dmaEvent.name = "GB DMA";
|
|
|
|
gb->memory.dmaEvent.callback = _GBMemoryDMAService;
|
2016-12-18 19:46:45 +00:00
|
|
|
gb->memory.dmaEvent.priority = 0x40;
|
2016-09-18 01:56:46 +00:00
|
|
|
gb->memory.hdmaEvent.context = gb;
|
|
|
|
gb->memory.hdmaEvent.name = "GB HDMA";
|
|
|
|
gb->memory.hdmaEvent.callback = _GBMemoryHDMAService;
|
2016-12-18 19:46:45 +00:00
|
|
|
gb->memory.hdmaEvent.priority = 0x41;
|
2016-09-18 01:56:46 +00:00
|
|
|
|
2016-02-20 06:59:36 +00:00
|
|
|
memset(&gb->memory.hram, 0, sizeof(gb->memory.hram));
|
2018-07-28 08:04:36 +00:00
|
|
|
|
2021-02-25 04:18:13 +00:00
|
|
|
GBMBCReset(gb);
|
2016-01-14 07:02:50 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 04:13:32 +00:00
|
|
|
void GBMemorySwitchWramBank(struct GBMemory* memory, int bank) {
|
|
|
|
bank &= 7;
|
|
|
|
if (!bank) {
|
|
|
|
bank = 1;
|
|
|
|
}
|
|
|
|
memory->wramBank = &memory->wram[GB_SIZE_WORKING_RAM_BANK0 * bank];
|
|
|
|
memory->wramCurrentBank = bank;
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:35:00 +00:00
|
|
|
uint8_t GBLoad8(struct SM83Core* cpu, uint16_t address) {
|
2016-01-14 07:02:50 +00:00
|
|
|
struct GB* gb = (struct GB*) cpu->master;
|
|
|
|
struct GBMemory* memory = &gb->memory;
|
2017-05-23 02:49:48 +00:00
|
|
|
if (gb->memory.dmaRemaining) {
|
2018-03-21 16:11:24 +00:00
|
|
|
const enum GBBus* block = gb->model < GB_MODEL_CGB ? _oamBlockDMG : _oamBlockCGB;
|
|
|
|
enum GBBus dmaBus = block[memory->dmaSource >> 13];
|
|
|
|
enum GBBus accessBus = block[address >> 13];
|
|
|
|
if (dmaBus != GB_BUS_CPU && dmaBus == accessBus) {
|
2017-05-23 02:49:48 +00:00
|
|
|
return 0xFF;
|
|
|
|
}
|
2019-07-06 23:13:39 +00:00
|
|
|
if (address >= GB_BASE_OAM && address < GB_BASE_IO) {
|
2017-05-23 02:49:48 +00:00
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 07:02:50 +00:00
|
|
|
switch (address >> 12) {
|
|
|
|
case GB_REGION_CART_BANK0:
|
|
|
|
case GB_REGION_CART_BANK0 + 1:
|
|
|
|
case GB_REGION_CART_BANK0 + 2:
|
|
|
|
case GB_REGION_CART_BANK0 + 3:
|
2019-09-08 01:32:37 +00:00
|
|
|
if (address >= memory->romSize) {
|
|
|
|
return 0xFF;
|
|
|
|
}
|
2016-05-20 05:31:13 +00:00
|
|
|
return memory->romBase[address & (GB_SIZE_CART_BANK0 - 1)];
|
2016-01-14 07:02:50 +00:00
|
|
|
case GB_REGION_CART_BANK1 + 2:
|
|
|
|
case GB_REGION_CART_BANK1 + 3:
|
2017-08-12 01:19:31 +00:00
|
|
|
if (memory->mbcType == GB_MBC6) {
|
|
|
|
return memory->mbcState.mbc6.romBank1[address & (GB_SIZE_CART_HALFBANK - 1)];
|
|
|
|
}
|
|
|
|
// Fall through
|
|
|
|
case GB_REGION_CART_BANK1:
|
|
|
|
case GB_REGION_CART_BANK1 + 1:
|
2019-09-08 01:32:37 +00:00
|
|
|
if (address >= memory->romSize) {
|
|
|
|
return 0xFF;
|
|
|
|
}
|
2020-08-16 23:27:11 +00:00
|
|
|
if ((memory->mbcType & GB_UNL_BBD) == GB_UNL_BBD) {
|
|
|
|
return memory->mbcRead(memory, address);
|
|
|
|
}
|
2016-01-14 07:02:50 +00:00
|
|
|
return memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)];
|
|
|
|
case GB_REGION_VRAM:
|
|
|
|
case GB_REGION_VRAM + 1:
|
2018-06-24 23:11:37 +00:00
|
|
|
if (gb->video.mode != 3) {
|
|
|
|
return gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)];
|
|
|
|
}
|
|
|
|
return 0xFF;
|
2016-01-14 07:02:50 +00:00
|
|
|
case GB_REGION_EXTERNAL_RAM:
|
|
|
|
case GB_REGION_EXTERNAL_RAM + 1:
|
2016-01-30 07:49:25 +00:00
|
|
|
if (memory->rtcAccess) {
|
2016-08-23 20:14:14 +00:00
|
|
|
return memory->rtcRegs[memory->activeRtcReg];
|
2017-06-08 06:06:28 +00:00
|
|
|
} else if (memory->mbcRead) {
|
|
|
|
return memory->mbcRead(memory, address);
|
2017-07-29 22:31:29 +00:00
|
|
|
} else if (memory->sramAccess && memory->sram) {
|
2016-08-23 20:14:14 +00:00
|
|
|
return memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM - 1)];
|
2016-06-01 09:56:53 +00:00
|
|
|
} else if (memory->mbcType == GB_HuC3) {
|
|
|
|
return 0x01; // TODO: Is this supposed to be the current SRAM bank?
|
2016-01-22 03:30:51 +00:00
|
|
|
}
|
|
|
|
return 0xFF;
|
2016-01-14 07:02:50 +00:00
|
|
|
case GB_REGION_WORKING_RAM_BANK0:
|
|
|
|
case GB_REGION_WORKING_RAM_BANK0 + 2:
|
|
|
|
return memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
|
|
|
|
case GB_REGION_WORKING_RAM_BANK1:
|
|
|
|
return memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
|
|
|
|
default:
|
2016-01-15 04:50:43 +00:00
|
|
|
if (address < GB_BASE_OAM) {
|
|
|
|
return memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
|
|
|
|
}
|
2016-01-22 03:30:51 +00:00
|
|
|
if (address < GB_BASE_UNUSABLE) {
|
|
|
|
if (gb->video.mode < 2) {
|
|
|
|
return gb->video.oam.raw[address & 0xFF];
|
|
|
|
}
|
|
|
|
return 0xFF;
|
|
|
|
}
|
2016-01-15 04:50:43 +00:00
|
|
|
if (address < GB_BASE_IO) {
|
2016-01-26 06:17:01 +00:00
|
|
|
mLOG(GB_MEM, GAME_ERROR, "Attempt to read from unusable memory: %04X", address);
|
2016-01-22 03:30:51 +00:00
|
|
|
return 0xFF;
|
2016-01-15 04:50:43 +00:00
|
|
|
}
|
|
|
|
if (address < GB_BASE_HRAM) {
|
|
|
|
return GBIORead(gb, address & (GB_SIZE_IO - 1));
|
|
|
|
}
|
|
|
|
if (address < GB_BASE_IE) {
|
|
|
|
return memory->hram[address & GB_SIZE_HRAM];
|
|
|
|
}
|
2020-10-22 03:57:06 +00:00
|
|
|
return GBIORead(gb, GB_REG_IE);
|
2016-01-14 07:02:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:35:00 +00:00
|
|
|
void GBStore8(struct SM83Core* cpu, uint16_t address, int8_t value) {
|
2016-01-14 07:02:50 +00:00
|
|
|
struct GB* gb = (struct GB*) cpu->master;
|
|
|
|
struct GBMemory* memory = &gb->memory;
|
2017-05-23 02:49:48 +00:00
|
|
|
if (gb->memory.dmaRemaining) {
|
2018-03-21 16:11:24 +00:00
|
|
|
const enum GBBus* block = gb->model < GB_MODEL_CGB ? _oamBlockDMG : _oamBlockCGB;
|
|
|
|
enum GBBus dmaBus = block[memory->dmaSource >> 13];
|
|
|
|
enum GBBus accessBus = block[address >> 13];
|
|
|
|
if (dmaBus != GB_BUS_CPU && dmaBus == accessBus) {
|
2017-05-23 02:49:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (address >= GB_BASE_OAM && address < GB_BASE_UNUSABLE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 07:02:50 +00:00
|
|
|
switch (address >> 12) {
|
|
|
|
case GB_REGION_CART_BANK0:
|
|
|
|
case GB_REGION_CART_BANK0 + 1:
|
|
|
|
case GB_REGION_CART_BANK0 + 2:
|
|
|
|
case GB_REGION_CART_BANK0 + 3:
|
|
|
|
case GB_REGION_CART_BANK1:
|
|
|
|
case GB_REGION_CART_BANK1 + 1:
|
|
|
|
case GB_REGION_CART_BANK1 + 2:
|
|
|
|
case GB_REGION_CART_BANK1 + 3:
|
2017-06-08 06:06:28 +00:00
|
|
|
memory->mbcWrite(gb, address, value);
|
2016-02-23 05:39:05 +00:00
|
|
|
cpu->memory.setActiveRegion(cpu, cpu->pc);
|
2016-01-14 07:02:50 +00:00
|
|
|
return;
|
|
|
|
case GB_REGION_VRAM:
|
|
|
|
case GB_REGION_VRAM + 1:
|
2018-06-24 23:11:37 +00:00
|
|
|
if (gb->video.mode != 3) {
|
|
|
|
gb->video.renderer->writeVRAM(gb->video.renderer, (address & (GB_SIZE_VRAM_BANK0 - 1)) | (GB_SIZE_VRAM_BANK0 * gb->video.vramCurrentBank));
|
|
|
|
gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)] = value;
|
|
|
|
}
|
2016-01-14 07:02:50 +00:00
|
|
|
return;
|
|
|
|
case GB_REGION_EXTERNAL_RAM:
|
|
|
|
case GB_REGION_EXTERNAL_RAM + 1:
|
2016-01-30 07:49:25 +00:00
|
|
|
if (memory->rtcAccess) {
|
2016-08-23 20:14:14 +00:00
|
|
|
memory->rtcRegs[memory->activeRtcReg] = value;
|
2020-07-08 01:11:18 +00:00
|
|
|
} else if (memory->sramAccess && memory->sram && memory->directSramAccess) {
|
2016-08-23 20:14:14 +00:00
|
|
|
memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM - 1)] = value;
|
2017-07-24 17:26:29 +00:00
|
|
|
} else {
|
|
|
|
memory->mbcWrite(gb, address, value);
|
2016-01-22 03:30:51 +00:00
|
|
|
}
|
2016-09-16 01:11:12 +00:00
|
|
|
gb->sramDirty |= GB_SRAM_DIRT_NEW;
|
2016-01-14 07:02:50 +00:00
|
|
|
return;
|
|
|
|
case GB_REGION_WORKING_RAM_BANK0:
|
|
|
|
case GB_REGION_WORKING_RAM_BANK0 + 2:
|
|
|
|
memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
|
|
|
|
return;
|
|
|
|
case GB_REGION_WORKING_RAM_BANK1:
|
|
|
|
memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
|
|
|
|
return;
|
|
|
|
default:
|
2016-01-15 04:50:43 +00:00
|
|
|
if (address < GB_BASE_OAM) {
|
|
|
|
memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
|
2016-01-22 03:30:51 +00:00
|
|
|
} else if (address < GB_BASE_UNUSABLE) {
|
|
|
|
if (gb->video.mode < 2) {
|
|
|
|
gb->video.oam.raw[address & 0xFF] = value;
|
2017-04-18 08:55:32 +00:00
|
|
|
gb->video.renderer->writeOAM(gb->video.renderer, address & 0xFF);
|
2016-01-22 03:30:51 +00:00
|
|
|
}
|
2016-01-15 04:50:43 +00:00
|
|
|
} else if (address < GB_BASE_IO) {
|
2016-01-26 06:17:01 +00:00
|
|
|
mLOG(GB_MEM, GAME_ERROR, "Attempt to write to unusable memory: %04X:%02X", address, value);
|
2016-01-15 04:50:43 +00:00
|
|
|
} else if (address < GB_BASE_HRAM) {
|
|
|
|
GBIOWrite(gb, address & (GB_SIZE_IO - 1), value);
|
|
|
|
} else if (address < GB_BASE_IE) {
|
|
|
|
memory->hram[address & GB_SIZE_HRAM] = value;
|
|
|
|
} else {
|
2020-10-22 03:57:06 +00:00
|
|
|
GBIOWrite(gb, GB_REG_IE, value);
|
2016-01-15 04:50:43 +00:00
|
|
|
}
|
2016-01-14 07:02:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 02:39:27 +00:00
|
|
|
|
2020-01-01 19:35:00 +00:00
|
|
|
int GBCurrentSegment(struct SM83Core* cpu, uint16_t address) {
|
2017-05-23 02:39:27 +00:00
|
|
|
struct GB* gb = (struct GB*) cpu->master;
|
|
|
|
struct GBMemory* memory = &gb->memory;
|
|
|
|
switch (address >> 12) {
|
|
|
|
case GB_REGION_CART_BANK0:
|
|
|
|
case GB_REGION_CART_BANK0 + 1:
|
|
|
|
case GB_REGION_CART_BANK0 + 2:
|
|
|
|
case GB_REGION_CART_BANK0 + 3:
|
|
|
|
return 0;
|
|
|
|
case GB_REGION_CART_BANK1:
|
|
|
|
case GB_REGION_CART_BANK1 + 1:
|
|
|
|
case GB_REGION_CART_BANK1 + 2:
|
|
|
|
case GB_REGION_CART_BANK1 + 3:
|
|
|
|
return memory->currentBank;
|
|
|
|
case GB_REGION_VRAM:
|
|
|
|
case GB_REGION_VRAM + 1:
|
|
|
|
return gb->video.vramCurrentBank;
|
|
|
|
case GB_REGION_EXTERNAL_RAM:
|
|
|
|
case GB_REGION_EXTERNAL_RAM + 1:
|
|
|
|
return memory->sramCurrentBank;
|
|
|
|
case GB_REGION_WORKING_RAM_BANK0:
|
|
|
|
case GB_REGION_WORKING_RAM_BANK0 + 2:
|
|
|
|
return 0;
|
|
|
|
case GB_REGION_WORKING_RAM_BANK1:
|
|
|
|
return memory->wramCurrentBank;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:35:00 +00:00
|
|
|
uint8_t GBView8(struct SM83Core* cpu, uint16_t address, int segment) {
|
2016-08-23 20:14:14 +00:00
|
|
|
struct GB* gb = (struct GB*) cpu->master;
|
|
|
|
struct GBMemory* memory = &gb->memory;
|
|
|
|
switch (address >> 12) {
|
|
|
|
case GB_REGION_CART_BANK0:
|
|
|
|
case GB_REGION_CART_BANK0 + 1:
|
|
|
|
case GB_REGION_CART_BANK0 + 2:
|
|
|
|
case GB_REGION_CART_BANK0 + 3:
|
|
|
|
return memory->romBase[address & (GB_SIZE_CART_BANK0 - 1)];
|
|
|
|
case GB_REGION_CART_BANK1:
|
|
|
|
case GB_REGION_CART_BANK1 + 1:
|
|
|
|
case GB_REGION_CART_BANK1 + 2:
|
|
|
|
case GB_REGION_CART_BANK1 + 3:
|
|
|
|
if (segment < 0) {
|
|
|
|
return memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)];
|
2016-09-19 16:25:28 +00:00
|
|
|
} else if ((size_t) segment * GB_SIZE_CART_BANK0 < memory->romSize) {
|
2016-08-23 20:14:14 +00:00
|
|
|
return memory->rom[(address & (GB_SIZE_CART_BANK0 - 1)) + segment * GB_SIZE_CART_BANK0];
|
2016-09-19 16:25:28 +00:00
|
|
|
} else {
|
|
|
|
return 0xFF;
|
2016-08-23 20:14:14 +00:00
|
|
|
}
|
|
|
|
case GB_REGION_VRAM:
|
|
|
|
case GB_REGION_VRAM + 1:
|
|
|
|
if (segment < 0) {
|
|
|
|
return gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)];
|
2016-09-19 16:25:28 +00:00
|
|
|
} else if (segment < 2) {
|
2016-08-23 20:14:14 +00:00
|
|
|
return gb->video.vram[(address & (GB_SIZE_VRAM_BANK0 - 1)) + segment *GB_SIZE_VRAM_BANK0];
|
2016-09-19 16:25:28 +00:00
|
|
|
} else {
|
|
|
|
return 0xFF;
|
2016-08-23 20:14:14 +00:00
|
|
|
}
|
|
|
|
case GB_REGION_EXTERNAL_RAM:
|
|
|
|
case GB_REGION_EXTERNAL_RAM + 1:
|
|
|
|
if (memory->rtcAccess) {
|
|
|
|
return memory->rtcRegs[memory->activeRtcReg];
|
|
|
|
} else if (memory->sramAccess) {
|
2017-07-29 22:31:29 +00:00
|
|
|
if (segment < 0 && memory->sram) {
|
2016-08-23 20:14:14 +00:00
|
|
|
return memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM - 1)];
|
2016-09-19 16:25:28 +00:00
|
|
|
} else if ((size_t) segment * GB_SIZE_EXTERNAL_RAM < gb->sramSize) {
|
2016-08-23 20:14:14 +00:00
|
|
|
return memory->sram[(address & (GB_SIZE_EXTERNAL_RAM - 1)) + segment *GB_SIZE_EXTERNAL_RAM];
|
2016-09-19 16:25:28 +00:00
|
|
|
} else {
|
|
|
|
return 0xFF;
|
2016-08-23 20:14:14 +00:00
|
|
|
}
|
2017-06-08 06:06:28 +00:00
|
|
|
} else if (memory->mbcRead) {
|
|
|
|
return memory->mbcRead(memory, address);
|
2016-08-23 20:14:14 +00:00
|
|
|
} else if (memory->mbcType == GB_HuC3) {
|
|
|
|
return 0x01; // TODO: Is this supposed to be the current SRAM bank?
|
|
|
|
}
|
|
|
|
return 0xFF;
|
|
|
|
case GB_REGION_WORKING_RAM_BANK0:
|
|
|
|
case GB_REGION_WORKING_RAM_BANK0 + 2:
|
|
|
|
return memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
|
|
|
|
case GB_REGION_WORKING_RAM_BANK1:
|
|
|
|
if (segment < 0) {
|
|
|
|
return memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
|
2016-09-19 16:25:28 +00:00
|
|
|
} else if (segment < 8) {
|
2016-08-23 20:14:14 +00:00
|
|
|
return memory->wram[(address & (GB_SIZE_WORKING_RAM_BANK0 - 1)) + segment *GB_SIZE_WORKING_RAM_BANK0];
|
2016-09-19 16:25:28 +00:00
|
|
|
} else {
|
|
|
|
return 0xFF;
|
2016-08-23 20:14:14 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if (address < GB_BASE_OAM) {
|
|
|
|
return memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
|
|
|
|
}
|
|
|
|
if (address < GB_BASE_UNUSABLE) {
|
|
|
|
if (gb->video.mode < 2) {
|
|
|
|
return gb->video.oam.raw[address & 0xFF];
|
|
|
|
}
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
if (address < GB_BASE_IO) {
|
|
|
|
mLOG(GB_MEM, GAME_ERROR, "Attempt to read from unusable memory: %04X", address);
|
2019-07-06 23:13:39 +00:00
|
|
|
if (gb->video.mode < 2) {
|
|
|
|
switch (gb->model) {
|
|
|
|
case GB_MODEL_AGB:
|
|
|
|
return (address & 0xF0) | ((address >> 4) & 0xF);
|
|
|
|
case GB_MODEL_CGB:
|
|
|
|
// TODO: R/W behavior
|
|
|
|
return 0x00;
|
|
|
|
default:
|
|
|
|
return 0x00;
|
|
|
|
}
|
|
|
|
}
|
2016-08-23 20:14:14 +00:00
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
if (address < GB_BASE_HRAM) {
|
|
|
|
return GBIORead(gb, address & (GB_SIZE_IO - 1));
|
|
|
|
}
|
|
|
|
if (address < GB_BASE_IE) {
|
|
|
|
return memory->hram[address & GB_SIZE_HRAM];
|
|
|
|
}
|
2020-10-22 03:57:06 +00:00
|
|
|
return GBIORead(gb, GB_REG_IE);
|
2016-08-23 20:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-14 07:02:50 +00:00
|
|
|
|
2016-01-22 03:28:56 +00:00
|
|
|
void GBMemoryDMA(struct GB* gb, uint16_t base) {
|
2020-06-17 06:06:32 +00:00
|
|
|
if (base >= 0xE000) {
|
|
|
|
base &= 0xDFFF;
|
2016-01-22 03:28:56 +00:00
|
|
|
}
|
2017-06-19 05:07:39 +00:00
|
|
|
mTimingDeschedule(&gb->timing, &gb->memory.dmaEvent);
|
2020-12-28 11:32:43 +00:00
|
|
|
mTimingSchedule(&gb->timing, &gb->memory.dmaEvent, 8 * (2 - gb->doubleSpeed));
|
2016-01-22 03:28:56 +00:00
|
|
|
gb->memory.dmaSource = base;
|
2016-01-28 07:25:36 +00:00
|
|
|
gb->memory.dmaDest = 0;
|
2016-01-22 03:28:56 +00:00
|
|
|
gb->memory.dmaRemaining = 0xA0;
|
|
|
|
}
|
|
|
|
|
2017-11-06 05:46:10 +00:00
|
|
|
uint8_t GBMemoryWriteHDMA5(struct GB* gb, uint8_t value) {
|
2020-10-22 03:57:06 +00:00
|
|
|
gb->memory.hdmaSource = gb->memory.io[GB_REG_HDMA1] << 8;
|
|
|
|
gb->memory.hdmaSource |= gb->memory.io[GB_REG_HDMA2];
|
|
|
|
gb->memory.hdmaDest = gb->memory.io[GB_REG_HDMA3] << 8;
|
|
|
|
gb->memory.hdmaDest |= gb->memory.io[GB_REG_HDMA4];
|
2016-02-17 07:00:24 +00:00
|
|
|
gb->memory.hdmaSource &= 0xFFF0;
|
|
|
|
if (gb->memory.hdmaSource >= 0x8000 && gb->memory.hdmaSource < 0xA000) {
|
|
|
|
mLOG(GB_MEM, GAME_ERROR, "Invalid HDMA source: %04X", gb->memory.hdmaSource);
|
2017-11-06 05:46:10 +00:00
|
|
|
return value | 0x80;
|
2016-02-17 07:00:24 +00:00
|
|
|
}
|
|
|
|
gb->memory.hdmaDest &= 0x1FF0;
|
|
|
|
gb->memory.hdmaDest |= 0x8000;
|
2016-02-20 21:57:16 +00:00
|
|
|
bool wasHdma = gb->memory.isHdma;
|
2016-02-17 07:00:24 +00:00
|
|
|
gb->memory.isHdma = value & 0x80;
|
2020-10-22 03:57:06 +00:00
|
|
|
if ((!wasHdma && !gb->memory.isHdma) || (GBRegisterLCDCIsEnable(gb->memory.io[GB_REG_LCDC]) && gb->video.mode == 0)) {
|
2017-08-12 21:12:49 +00:00
|
|
|
if (gb->memory.isHdma) {
|
|
|
|
gb->memory.hdmaRemaining = 0x10;
|
|
|
|
} else {
|
|
|
|
gb->memory.hdmaRemaining = ((value & 0x7F) + 1) * 0x10;
|
|
|
|
}
|
2016-11-11 04:05:33 +00:00
|
|
|
gb->cpuBlocked = true;
|
2016-09-18 01:56:46 +00:00
|
|
|
mTimingSchedule(&gb->timing, &gb->memory.hdmaEvent, 0);
|
2020-10-22 03:57:06 +00:00
|
|
|
} else if (gb->memory.isHdma && !GBRegisterLCDCIsEnable(gb->memory.io[GB_REG_LCDC])) {
|
2017-11-06 05:46:10 +00:00
|
|
|
return 0x80 | ((value + 1) & 0x7F);
|
2016-02-17 07:00:24 +00:00
|
|
|
}
|
2017-11-06 05:46:10 +00:00
|
|
|
return value & 0x7F;
|
2016-02-17 07:00:24 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 01:56:46 +00:00
|
|
|
void _GBMemoryDMAService(struct mTiming* timing, void* context, uint32_t cyclesLate) {
|
|
|
|
struct GB* gb = context;
|
2017-05-23 02:49:48 +00:00
|
|
|
int dmaRemaining = gb->memory.dmaRemaining;
|
|
|
|
gb->memory.dmaRemaining = 0;
|
2016-01-22 03:28:56 +00:00
|
|
|
uint8_t b = GBLoad8(gb->cpu, gb->memory.dmaSource);
|
2016-01-28 07:25:36 +00:00
|
|
|
// TODO: Can DMA write OAM during modes 2-3?
|
|
|
|
gb->video.oam.raw[gb->memory.dmaDest] = b;
|
2017-04-18 08:55:32 +00:00
|
|
|
gb->video.renderer->writeOAM(gb->video.renderer, gb->memory.dmaDest);
|
2016-01-22 03:28:56 +00:00
|
|
|
++gb->memory.dmaSource;
|
|
|
|
++gb->memory.dmaDest;
|
2017-05-23 02:49:48 +00:00
|
|
|
gb->memory.dmaRemaining = dmaRemaining - 1;
|
2016-01-22 03:28:56 +00:00
|
|
|
if (gb->memory.dmaRemaining) {
|
2020-12-28 11:32:43 +00:00
|
|
|
mTimingSchedule(timing, &gb->memory.dmaEvent, 4 * (2 - gb->doubleSpeed) - cyclesLate);
|
2016-01-22 03:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 01:56:46 +00:00
|
|
|
void _GBMemoryHDMAService(struct mTiming* timing, void* context, uint32_t cyclesLate) {
|
|
|
|
struct GB* gb = context;
|
2016-11-11 04:05:33 +00:00
|
|
|
gb->cpuBlocked = true;
|
2016-02-17 07:00:24 +00:00
|
|
|
uint8_t b = gb->cpu->memory.load8(gb->cpu, gb->memory.hdmaSource);
|
|
|
|
gb->cpu->memory.store8(gb->cpu, gb->memory.hdmaDest, b);
|
|
|
|
++gb->memory.hdmaSource;
|
|
|
|
++gb->memory.hdmaDest;
|
|
|
|
--gb->memory.hdmaRemaining;
|
|
|
|
if (gb->memory.hdmaRemaining) {
|
2016-11-11 04:05:33 +00:00
|
|
|
mTimingDeschedule(timing, &gb->memory.hdmaEvent);
|
2020-12-28 11:32:43 +00:00
|
|
|
mTimingSchedule(timing, &gb->memory.hdmaEvent, 4 - cyclesLate);
|
2016-02-17 07:00:24 +00:00
|
|
|
} else {
|
2016-11-11 04:05:33 +00:00
|
|
|
gb->cpuBlocked = false;
|
2020-10-22 03:57:06 +00:00
|
|
|
gb->memory.io[GB_REG_HDMA1] = gb->memory.hdmaSource >> 8;
|
|
|
|
gb->memory.io[GB_REG_HDMA2] = gb->memory.hdmaSource;
|
|
|
|
gb->memory.io[GB_REG_HDMA3] = gb->memory.hdmaDest >> 8;
|
|
|
|
gb->memory.io[GB_REG_HDMA4] = gb->memory.hdmaDest;
|
2016-02-18 04:00:23 +00:00
|
|
|
if (gb->memory.isHdma) {
|
2020-10-22 03:57:06 +00:00
|
|
|
--gb->memory.io[GB_REG_HDMA5];
|
|
|
|
if (gb->memory.io[GB_REG_HDMA5] == 0xFF) {
|
2016-02-21 10:48:34 +00:00
|
|
|
gb->memory.isHdma = false;
|
|
|
|
}
|
2016-02-18 04:00:23 +00:00
|
|
|
} else {
|
2020-10-22 03:57:06 +00:00
|
|
|
gb->memory.io[GB_REG_HDMA5] = 0xFF;
|
2016-02-18 04:00:23 +00:00
|
|
|
}
|
2016-02-17 07:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:35:00 +00:00
|
|
|
void GBPatch8(struct SM83Core* cpu, uint16_t address, int8_t value, int8_t* old, int segment) {
|
2016-05-09 05:44:56 +00:00
|
|
|
struct GB* gb = (struct GB*) cpu->master;
|
|
|
|
struct GBMemory* memory = &gb->memory;
|
|
|
|
int8_t oldValue = -1;
|
|
|
|
|
|
|
|
switch (address >> 12) {
|
|
|
|
case GB_REGION_CART_BANK0:
|
|
|
|
case GB_REGION_CART_BANK0 + 1:
|
|
|
|
case GB_REGION_CART_BANK0 + 2:
|
|
|
|
case GB_REGION_CART_BANK0 + 3:
|
|
|
|
_pristineCow(gb);
|
2016-10-24 18:49:06 +00:00
|
|
|
oldValue = memory->romBase[address & (GB_SIZE_CART_BANK0 - 1)];
|
|
|
|
memory->romBase[address & (GB_SIZE_CART_BANK0 - 1)] = value;
|
2016-05-09 05:44:56 +00:00
|
|
|
break;
|
|
|
|
case GB_REGION_CART_BANK1:
|
|
|
|
case GB_REGION_CART_BANK1 + 1:
|
|
|
|
case GB_REGION_CART_BANK1 + 2:
|
|
|
|
case GB_REGION_CART_BANK1 + 3:
|
|
|
|
_pristineCow(gb);
|
2016-09-17 01:17:29 +00:00
|
|
|
if (segment < 0) {
|
|
|
|
oldValue = memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)];
|
|
|
|
memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)] = value;
|
2016-09-19 16:25:28 +00:00
|
|
|
} else if ((size_t) segment * GB_SIZE_CART_BANK0 < memory->romSize) {
|
2016-09-17 01:17:29 +00:00
|
|
|
oldValue = memory->rom[(address & (GB_SIZE_CART_BANK0 - 1)) + segment * GB_SIZE_CART_BANK0];
|
|
|
|
memory->rom[(address & (GB_SIZE_CART_BANK0 - 1)) + segment * GB_SIZE_CART_BANK0] = value;
|
2016-09-19 16:25:28 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2016-09-17 01:17:29 +00:00
|
|
|
}
|
2016-05-09 05:44:56 +00:00
|
|
|
break;
|
|
|
|
case GB_REGION_VRAM:
|
|
|
|
case GB_REGION_VRAM + 1:
|
2016-09-17 01:17:29 +00:00
|
|
|
if (segment < 0) {
|
|
|
|
oldValue = gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)];
|
|
|
|
gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)] = value;
|
2016-10-17 18:59:31 +00:00
|
|
|
gb->video.renderer->writeVRAM(gb->video.renderer, (address & (GB_SIZE_VRAM_BANK0 - 1)) + GB_SIZE_VRAM_BANK0 * gb->video.vramCurrentBank);
|
2016-09-19 16:25:28 +00:00
|
|
|
} else if (segment < 2) {
|
2016-09-17 01:17:29 +00:00
|
|
|
oldValue = gb->video.vram[(address & (GB_SIZE_VRAM_BANK0 - 1)) + segment * GB_SIZE_VRAM_BANK0];
|
|
|
|
gb->video.vramBank[(address & (GB_SIZE_VRAM_BANK0 - 1)) + segment * GB_SIZE_VRAM_BANK0] = value;
|
2016-10-17 18:59:31 +00:00
|
|
|
gb->video.renderer->writeVRAM(gb->video.renderer, (address & (GB_SIZE_VRAM_BANK0 - 1)) + segment * GB_SIZE_VRAM_BANK0);
|
2016-09-19 16:25:28 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2016-09-17 01:17:29 +00:00
|
|
|
}
|
2016-05-09 05:44:56 +00:00
|
|
|
break;
|
|
|
|
case GB_REGION_EXTERNAL_RAM:
|
|
|
|
case GB_REGION_EXTERNAL_RAM + 1:
|
2019-11-30 19:37:36 +00:00
|
|
|
if (memory->rtcAccess) {
|
|
|
|
memory->rtcRegs[memory->activeRtcReg] = value;
|
|
|
|
} else if (memory->sramAccess && memory->sram && memory->mbcType != GB_MBC2) {
|
|
|
|
// TODO: Remove sramAccess check?
|
|
|
|
memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM - 1)] = value;
|
|
|
|
} else {
|
|
|
|
memory->mbcWrite(gb, address, value);
|
|
|
|
}
|
|
|
|
gb->sramDirty |= GB_SRAM_DIRT_NEW;
|
2016-05-09 05:44:56 +00:00
|
|
|
return;
|
|
|
|
case GB_REGION_WORKING_RAM_BANK0:
|
|
|
|
case GB_REGION_WORKING_RAM_BANK0 + 2:
|
|
|
|
oldValue = memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
|
|
|
|
memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
|
|
|
|
break;
|
|
|
|
case GB_REGION_WORKING_RAM_BANK1:
|
2016-09-17 01:17:29 +00:00
|
|
|
if (segment < 0) {
|
|
|
|
oldValue = memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
|
|
|
|
memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
|
2016-09-19 16:25:28 +00:00
|
|
|
} else if (segment < 8) {
|
2016-09-17 01:17:29 +00:00
|
|
|
oldValue = memory->wram[(address & (GB_SIZE_WORKING_RAM_BANK0 - 1)) + segment * GB_SIZE_WORKING_RAM_BANK0];
|
|
|
|
memory->wram[(address & (GB_SIZE_WORKING_RAM_BANK0 - 1)) + segment * GB_SIZE_WORKING_RAM_BANK0] = value;
|
2016-09-19 16:25:28 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2016-09-17 01:17:29 +00:00
|
|
|
}
|
2016-05-09 05:44:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (address < GB_BASE_OAM) {
|
|
|
|
oldValue = memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
|
|
|
|
memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
|
|
|
|
} else if (address < GB_BASE_UNUSABLE) {
|
|
|
|
oldValue = gb->video.oam.raw[address & 0xFF];
|
|
|
|
gb->video.oam.raw[address & 0xFF] = value;
|
2017-04-18 08:55:32 +00:00
|
|
|
gb->video.renderer->writeOAM(gb->video.renderer, address & 0xFF);
|
2016-05-09 05:44:56 +00:00
|
|
|
} else if (address < GB_BASE_HRAM) {
|
|
|
|
mLOG(GB_MEM, STUB, "Unimplemented memory Patch8: 0x%08X", address);
|
|
|
|
return;
|
|
|
|
} else if (address < GB_BASE_IE) {
|
|
|
|
oldValue = memory->hram[address & GB_SIZE_HRAM];
|
|
|
|
memory->hram[address & GB_SIZE_HRAM] = value;
|
|
|
|
} else {
|
|
|
|
mLOG(GB_MEM, STUB, "Unimplemented memory Patch8: 0x%08X", address);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (old) {
|
|
|
|
*old = oldValue;
|
|
|
|
}
|
|
|
|
}
|
2016-01-17 05:29:09 +00:00
|
|
|
|
2016-09-06 18:15:27 +00:00
|
|
|
void GBMemorySerialize(const struct GB* gb, struct GBSerializedState* state) {
|
|
|
|
const struct GBMemory* memory = &gb->memory;
|
2016-05-30 22:03:20 +00:00
|
|
|
memcpy(state->wram, memory->wram, GB_SIZE_WORKING_RAM);
|
|
|
|
memcpy(state->hram, memory->hram, GB_SIZE_HRAM);
|
|
|
|
STORE_16LE(memory->currentBank, 0, &state->memory.currentBank);
|
|
|
|
state->memory.wramCurrentBank = memory->wramCurrentBank;
|
|
|
|
state->memory.sramCurrentBank = memory->sramCurrentBank;
|
|
|
|
|
|
|
|
STORE_16LE(memory->dmaSource, 0, &state->memory.dmaSource);
|
|
|
|
STORE_16LE(memory->dmaDest, 0, &state->memory.dmaDest);
|
|
|
|
|
|
|
|
STORE_16LE(memory->hdmaSource, 0, &state->memory.hdmaSource);
|
|
|
|
STORE_16LE(memory->hdmaDest, 0, &state->memory.hdmaDest);
|
|
|
|
|
|
|
|
STORE_16LE(memory->hdmaRemaining, 0, &state->memory.hdmaRemaining);
|
|
|
|
state->memory.dmaRemaining = memory->dmaRemaining;
|
|
|
|
memcpy(state->memory.rtcRegs, memory->rtcRegs, sizeof(state->memory.rtcRegs));
|
|
|
|
|
2016-12-20 03:40:16 +00:00
|
|
|
STORE_32LE(memory->dmaEvent.when - mTimingCurrentTime(&gb->timing), 0, &state->memory.dmaNext);
|
|
|
|
STORE_32LE(memory->hdmaEvent.when - mTimingCurrentTime(&gb->timing), 0, &state->memory.hdmaNext);
|
|
|
|
|
2016-06-01 06:24:20 +00:00
|
|
|
GBSerializedMemoryFlags flags = 0;
|
|
|
|
flags = GBSerializedMemoryFlagsSetSramAccess(flags, memory->sramAccess);
|
|
|
|
flags = GBSerializedMemoryFlagsSetRtcAccess(flags, memory->rtcAccess);
|
|
|
|
flags = GBSerializedMemoryFlagsSetRtcLatched(flags, memory->rtcLatched);
|
|
|
|
flags = GBSerializedMemoryFlagsSetIme(flags, memory->ime);
|
|
|
|
flags = GBSerializedMemoryFlagsSetIsHdma(flags, memory->isHdma);
|
|
|
|
flags = GBSerializedMemoryFlagsSetActiveRtcReg(flags, memory->activeRtcReg);
|
|
|
|
STORE_16LE(flags, 0, &state->memory.flags);
|
2017-07-08 23:27:49 +00:00
|
|
|
|
|
|
|
switch (memory->mbcType) {
|
|
|
|
case GB_MBC1:
|
|
|
|
state->memory.mbc1.mode = memory->mbcState.mbc1.mode;
|
|
|
|
state->memory.mbc1.multicartStride = memory->mbcState.mbc1.multicartStride;
|
2020-06-17 03:00:44 +00:00
|
|
|
state->memory.mbc1.bankLo = memory->mbcState.mbc1.bankLo;
|
|
|
|
state->memory.mbc1.bankHi = memory->mbcState.mbc1.bankHi;
|
2017-07-08 23:27:49 +00:00
|
|
|
break;
|
|
|
|
case GB_MBC3_RTC:
|
|
|
|
STORE_64LE(gb->memory.rtcLastLatch, 0, &state->memory.rtc.lastLatch);
|
|
|
|
break;
|
|
|
|
case GB_MBC7:
|
|
|
|
state->memory.mbc7.state = memory->mbcState.mbc7.state;
|
|
|
|
state->memory.mbc7.eeprom = memory->mbcState.mbc7.eeprom;
|
|
|
|
state->memory.mbc7.address = memory->mbcState.mbc7.address;
|
|
|
|
state->memory.mbc7.access = memory->mbcState.mbc7.access;
|
|
|
|
state->memory.mbc7.latch = memory->mbcState.mbc7.latch;
|
|
|
|
state->memory.mbc7.srBits = memory->mbcState.mbc7.srBits;
|
|
|
|
STORE_16LE(memory->mbcState.mbc7.sr, 0, &state->memory.mbc7.sr);
|
|
|
|
STORE_32LE(memory->mbcState.mbc7.writable, 0, &state->memory.mbc7.writable);
|
|
|
|
break;
|
2018-07-28 08:04:36 +00:00
|
|
|
case GB_MMM01:
|
|
|
|
state->memory.mmm01.locked = memory->mbcState.mmm01.locked;
|
|
|
|
state->memory.mmm01.bank0 = memory->mbcState.mmm01.currentBank0;
|
|
|
|
break;
|
2020-08-16 23:27:11 +00:00
|
|
|
case GB_UNL_BBD:
|
|
|
|
case GB_UNL_HITEK:
|
|
|
|
state->memory.bbd.dataSwapMode = memory->mbcState.bbd.dataSwapMode;
|
|
|
|
state->memory.bbd.bankSwapMode = memory->mbcState.bbd.bankSwapMode;
|
|
|
|
break;
|
2017-07-08 23:27:49 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-05-30 22:03:20 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 18:15:27 +00:00
|
|
|
void GBMemoryDeserialize(struct GB* gb, const struct GBSerializedState* state) {
|
|
|
|
struct GBMemory* memory = &gb->memory;
|
2016-05-30 22:03:20 +00:00
|
|
|
memcpy(memory->wram, state->wram, GB_SIZE_WORKING_RAM);
|
|
|
|
memcpy(memory->hram, state->hram, GB_SIZE_HRAM);
|
|
|
|
LOAD_16LE(memory->currentBank, 0, &state->memory.currentBank);
|
|
|
|
memory->wramCurrentBank = state->memory.wramCurrentBank;
|
|
|
|
memory->sramCurrentBank = state->memory.sramCurrentBank;
|
|
|
|
|
2017-02-01 21:38:11 +00:00
|
|
|
GBMBCSwitchBank(gb, memory->currentBank);
|
2016-05-30 22:03:20 +00:00
|
|
|
GBMemorySwitchWramBank(memory, memory->wramCurrentBank);
|
2016-09-06 18:15:27 +00:00
|
|
|
GBMBCSwitchSramBank(gb, memory->sramCurrentBank);
|
2016-05-30 22:03:20 +00:00
|
|
|
|
|
|
|
LOAD_16LE(memory->dmaSource, 0, &state->memory.dmaSource);
|
|
|
|
LOAD_16LE(memory->dmaDest, 0, &state->memory.dmaDest);
|
|
|
|
|
|
|
|
LOAD_16LE(memory->hdmaSource, 0, &state->memory.hdmaSource);
|
|
|
|
LOAD_16LE(memory->hdmaDest, 0, &state->memory.hdmaDest);
|
|
|
|
|
|
|
|
LOAD_16LE(memory->hdmaRemaining, 0, &state->memory.hdmaRemaining);
|
|
|
|
memory->dmaRemaining = state->memory.dmaRemaining;
|
|
|
|
memcpy(memory->rtcRegs, state->memory.rtcRegs, sizeof(state->memory.rtcRegs));
|
|
|
|
|
2016-12-20 03:40:16 +00:00
|
|
|
uint32_t when;
|
|
|
|
LOAD_32LE(when, 0, &state->memory.dmaNext);
|
|
|
|
if (memory->dmaRemaining) {
|
|
|
|
mTimingSchedule(&gb->timing, &memory->dmaEvent, when);
|
2020-08-11 00:24:50 +00:00
|
|
|
} else {
|
|
|
|
memory->dmaEvent.when = when + mTimingCurrentTime(&gb->timing);
|
2016-12-20 03:40:16 +00:00
|
|
|
}
|
|
|
|
LOAD_32LE(when, 0, &state->memory.hdmaNext);
|
|
|
|
if (memory->hdmaRemaining) {
|
|
|
|
mTimingSchedule(&gb->timing, &memory->hdmaEvent, when);
|
2020-08-11 00:24:50 +00:00
|
|
|
} else {
|
|
|
|
memory->hdmaEvent.when = when + mTimingCurrentTime(&gb->timing);
|
2016-12-20 03:40:16 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 06:24:20 +00:00
|
|
|
GBSerializedMemoryFlags flags;
|
|
|
|
LOAD_16LE(flags, 0, &state->memory.flags);
|
|
|
|
memory->sramAccess = GBSerializedMemoryFlagsGetSramAccess(flags);
|
|
|
|
memory->rtcAccess = GBSerializedMemoryFlagsGetRtcAccess(flags);
|
|
|
|
memory->rtcLatched = GBSerializedMemoryFlagsGetRtcLatched(flags);
|
|
|
|
memory->ime = GBSerializedMemoryFlagsGetIme(flags);
|
|
|
|
memory->isHdma = GBSerializedMemoryFlagsGetIsHdma(flags);
|
|
|
|
memory->activeRtcReg = GBSerializedMemoryFlagsGetActiveRtcReg(flags);
|
2017-07-08 23:27:49 +00:00
|
|
|
|
|
|
|
switch (memory->mbcType) {
|
|
|
|
case GB_MBC1:
|
|
|
|
memory->mbcState.mbc1.mode = state->memory.mbc1.mode;
|
|
|
|
memory->mbcState.mbc1.multicartStride = state->memory.mbc1.multicartStride;
|
2020-06-17 03:00:44 +00:00
|
|
|
memory->mbcState.mbc1.bankLo = state->memory.mbc1.bankLo;
|
|
|
|
memory->mbcState.mbc1.bankHi = state->memory.mbc1.bankHi;
|
|
|
|
if (!(memory->mbcState.mbc1.bankLo || memory->mbcState.mbc1.bankHi)) {
|
|
|
|
// Backwards compat
|
|
|
|
memory->mbcState.mbc1.bankLo = memory->currentBank & ((1 << memory->mbcState.mbc1.multicartStride) - 1);
|
|
|
|
memory->mbcState.mbc1.bankHi = memory->currentBank >> memory->mbcState.mbc1.multicartStride;
|
|
|
|
}
|
2017-07-08 23:27:49 +00:00
|
|
|
if (memory->mbcState.mbc1.mode) {
|
2020-06-17 03:00:44 +00:00
|
|
|
GBMBCSwitchBank0(gb, memory->mbcState.mbc1.bankHi);
|
2017-07-08 23:27:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GB_MBC3_RTC:
|
2017-08-12 21:13:24 +00:00
|
|
|
LOAD_64LE(gb->memory.rtcLastLatch, 0, &state->memory.rtc.lastLatch);
|
2017-07-08 23:27:49 +00:00
|
|
|
break;
|
|
|
|
case GB_MBC7:
|
|
|
|
memory->mbcState.mbc7.state = state->memory.mbc7.state;
|
|
|
|
memory->mbcState.mbc7.eeprom = state->memory.mbc7.eeprom;
|
|
|
|
memory->mbcState.mbc7.address = state->memory.mbc7.address & 0x7F;
|
|
|
|
memory->mbcState.mbc7.access = state->memory.mbc7.access;
|
|
|
|
memory->mbcState.mbc7.latch = state->memory.mbc7.latch;
|
|
|
|
memory->mbcState.mbc7.srBits = state->memory.mbc7.srBits;
|
|
|
|
LOAD_16LE(memory->mbcState.mbc7.sr, 0, &state->memory.mbc7.sr);
|
|
|
|
LOAD_32LE(memory->mbcState.mbc7.writable, 0, &state->memory.mbc7.writable);
|
|
|
|
break;
|
2018-07-28 08:04:36 +00:00
|
|
|
case GB_MMM01:
|
|
|
|
memory->mbcState.mmm01.locked = state->memory.mmm01.locked;
|
|
|
|
memory->mbcState.mmm01.currentBank0 = state->memory.mmm01.bank0;
|
|
|
|
if (memory->mbcState.mmm01.locked) {
|
|
|
|
GBMBCSwitchBank0(gb, memory->mbcState.mmm01.currentBank0);
|
|
|
|
} else {
|
|
|
|
GBMBCSwitchBank0(gb, gb->memory.romSize / GB_SIZE_CART_BANK0 - 2);
|
|
|
|
}
|
|
|
|
break;
|
2020-08-16 23:27:11 +00:00
|
|
|
case GB_UNL_BBD:
|
|
|
|
case GB_UNL_HITEK:
|
|
|
|
memory->mbcState.bbd.dataSwapMode = state->memory.bbd.dataSwapMode & 0x7;
|
|
|
|
memory->mbcState.bbd.bankSwapMode = state->memory.bbd.bankSwapMode & 0x7;
|
|
|
|
break;
|
2017-07-08 23:27:49 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-05-30 22:03:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 05:44:56 +00:00
|
|
|
void _pristineCow(struct GB* gb) {
|
2017-02-01 21:38:11 +00:00
|
|
|
if (!gb->isPristine) {
|
2016-05-09 05:44:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-02-01 21:38:11 +00:00
|
|
|
void* newRom = anonymousMemoryMap(GB_SIZE_CART_MAX);
|
|
|
|
memcpy(newRom, gb->memory.rom, gb->memory.romSize);
|
|
|
|
memset(((uint8_t*) newRom) + gb->memory.romSize, 0xFF, GB_SIZE_CART_MAX - gb->memory.romSize);
|
|
|
|
if (gb->memory.rom == gb->memory.romBase) {
|
|
|
|
gb->memory.romBase = newRom;
|
2016-10-24 18:49:06 +00:00
|
|
|
}
|
2017-02-01 21:38:11 +00:00
|
|
|
gb->memory.rom = newRom;
|
|
|
|
GBMBCSwitchBank(gb, gb->memory.currentBank);
|
2017-05-31 05:30:11 +00:00
|
|
|
gb->isPristine = false;
|
2016-05-09 05:44:56 +00:00
|
|
|
}
|