all repos — mgba @ 93a839f52a1748c7768c80968571a9d42ef8fbec

mGBA Game Boy Advance Emulator

GB Memory: Clean up MBC1
Jeffrey Pfau jeffrey@endrift.com
Sat, 27 Feb 2016 01:12:23 -0800
commit

93a839f52a1748c7768c80968571a9d42ef8fbec

parent

878c6b8a2ce54bd7b8c5b2d390e6ac5e0393cdac

2 files changed, 23 insertions(+), 2 deletions(-)

jump to
M src/gb/memory.csrc/gb/memory.c

@@ -438,8 +438,8 @@ static void _switchBank(struct GBMemory* memory, int bank) {

size_t bankStart = bank * GB_SIZE_CART_BANK0; if (bankStart + GB_SIZE_CART_BANK0 > memory->romSize) { mLOG(GB_MBC, GAME_ERROR, "Attempting to switch to an invalid ROM bank: %0X", bank); - bankStart &= (GB_SIZE_CART_BANK0 - 1); - bank /= GB_SIZE_CART_BANK0; + bankStart &= (memory->romSize - 1); + bank = bankStart / GB_SIZE_CART_BANK0; } memory->romBank = &memory->rom[bankStart]; memory->currentBank = bank;

@@ -495,6 +495,22 @@ if (!bank) {

++bank; } _switchBank(memory, bank | (memory->currentBank & 0x60)); + break; + case 0x2: + bank &= 3; + if (!memory->mbcState.mbc1.mode) { + _switchBank(memory, (bank << 5) | (memory->currentBank & 0x1F)); + } else { + _switchSramBank(memory, bank); + } + break; + case 0x3: + memory->mbcState.mbc1.mode = value & 1; + if (memory->mbcState.mbc1.mode) { + _switchBank(memory, memory->currentBank & 0x1F); + } else { + _switchSramBank(memory, 0); + } break; default: // TODO
M src/gb/memory.hsrc/gb/memory.h

@@ -90,6 +90,10 @@ GBMBC7_STATE_READ = 7,

GBMBC7_STATE_WRITE = 8, }; +struct GBMBC1State { + int mode; +}; + struct GBMBC7State { enum GBMBC7MachineState state; uint32_t sr;

@@ -101,6 +105,7 @@ GBMBC7Field field;

}; union GBMBCState { + struct GBMBC1State mbc1; struct GBMBC7State mbc7; };