MBC2 RAM support was completely broken.

This commit is contained in:
Lior Halphon 2016-09-30 23:34:06 +03:00
parent 17748b980a
commit 4a2bec239a
3 changed files with 16 additions and 10 deletions

View File

@ -624,7 +624,7 @@ static uint32_t rgbEncode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
n_banks = gb.is_cgb ? 2 : 1; n_banks = gb.is_cgb ? 2 : 1;
break; break;
case GBMemoryExternalRAM: case GBMemoryExternalRAM:
n_banks = gb.mbc_ram_size / 0x2000; n_banks = (gb.mbc_ram_size + 0x1FFF) / 0x2000;
break; break;
case GBMemoryRAM: case GBMemoryRAM:
n_banks = gb.is_cgb ? 8 : 1; n_banks = gb.is_cgb ? 8 : 1;

View File

@ -106,9 +106,16 @@ void GB_configure_cart(GB_gameboy_t *gb)
} }
if (gb->cartridge_type->has_ram) { if (gb->cartridge_type->has_ram) {
static const int ram_sizes[256] = {0, 0x800, 0x2000, 0x8000, 0x20000, 0x10000}; if (gb->cartridge_type->mbc_type == GB_MBC2) {
gb->mbc_ram_size = ram_sizes[gb->rom[0x149]]; gb->mbc_ram_size = 0x200;
}
else {
static const int ram_sizes[256] = {0, 0x800, 0x2000, 0x8000, 0x20000, 0x10000};
gb->mbc_ram_size = ram_sizes[gb->rom[0x149]];
}
gb->mbc_ram = malloc(gb->mbc_ram_size); gb->mbc_ram = malloc(gb->mbc_ram_size);
/* Todo: Some games assume unintialized MBC RAM is 0xFF. It this true for all cartridges types?*/
memset(gb->mbc_ram, 0xFF, gb->mbc_ram_size); memset(gb->mbc_ram, 0xFF, gb->mbc_ram_size);
} }

View File

@ -86,7 +86,11 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr)
return 0xFF; return 0xFF;
} }
return gb->mbc_ram[((addr & 0x1FFF) + gb->mbc_ram_bank * 0x2000) & (gb->mbc_ram_size - 1)]; uint8_t ret = gb->mbc_ram[((addr & 0x1FFF) + gb->mbc_ram_bank * 0x2000) & (gb->mbc_ram_size - 1)];
if (gb->cartridge_type->mbc_type == GB_MBC2) {
ret |= 0xF0;
}
return ret;
} }
static uint8_t read_ram(GB_gameboy_t *gb, uint16_t addr) static uint8_t read_ram(GB_gameboy_t *gb, uint16_t addr)
@ -278,8 +282,7 @@ static void write_mbc(GB_gameboy_t *gb, uint16_t addr, uint8_t value)
break; break;
case GB_MBC2: case GB_MBC2:
switch (addr & 0xF000) { switch (addr & 0xF000) {
/* Todo: is this correct? */ case 0x0000: case 0x1000: if (!(addr & 0x100)) gb->mbc_ram_enable = value == 10; break;
case 0x0000: case 0x1000: if (!(addr & 0x100)) gb->mbc_ram_enable = value & 0x1; break;
case 0x2000: case 0x3000: if ( addr & 0x100) gb->mbc2.rom_bank = value; break; case 0x2000: case 0x3000: if ( addr & 0x100) gb->mbc2.rom_bank = value; break;
} }
break; break;
@ -326,10 +329,6 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value)
gb->rtc_latched.data[gb->mbc_ram_bank - 8] = gb->rtc_real.data[gb->mbc_ram_bank - 8] = value; /* Todo: does it really write both? */ gb->rtc_latched.data[gb->mbc_ram_bank - 8] = gb->rtc_real.data[gb->mbc_ram_bank - 8] = value; /* Todo: does it really write both? */
} }
if (gb->cartridge_type->mbc_type == GB_MBC2) {
value &= 0xF;
}
if (!gb->mbc_ram) { if (!gb->mbc_ram) {
return; return;
} }