diff --git a/lib/cart.c b/lib/cart.c index a1ffcde..3f0049b 100644 --- a/lib/cart.c +++ b/lib/cart.c @@ -22,7 +22,7 @@ bool cart_mbc3() { } bool cart_mbc5() { - return BETWEEN(ctx.header->type, 25, 30) + return BETWEEN(ctx.header->type, 25, 30); } bool cart_mbc() { @@ -30,7 +30,7 @@ bool cart_mbc() { } bool cart_battery() { - return ctx.header->type == 3 || BETWEEN(ctx.header->type, 15, 16) || ctx.header->type == 19 || ctx.header->type == 6; + return ctx.header->type == 3 || BETWEEN(ctx.header->type, 15, 16) || ctx.header->type == 19 || ctx.header->type == 6 || ctx.header->type == 26 || ctx.header->type == 29; } static const char *ROM_TYPES[] = { @@ -355,7 +355,27 @@ void cart_write(u16 address, u8 value){ } ctx.rom_bank_value = value; ctx.rom_bank_x = ctx.rom_data + (0x4000 * ctx.rom_bank_value); - }else { + } else if(cart_mbc5()) { + if(address < 0x3000){ + switch(ctx.header->rom_size) { + case 0: value &= 0b1; break; + case 1: value &= 0b11; break; + case 2: value &= 0b111; break; + case 3: value &= 0b1111; break; + case 4: value &= 0b11111; break; + case 5: value &= 0b111111; break; + case 6: value &= 0b1111111; break; + default: break; + } + ctx.rom_bank_value = value; + ctx.rom_bank_x = ctx.rom_data + (0x4000 * ctx.rom_bank_value + ctx.rom_bank_value_2); + } else { + if(ctx.header->rom_size == 8) { + ctx.rom_bank_value_2 = (value & 0b1) << 8; + ctx.rom_bank_x = ctx.rom_data + (0x4000 * ctx.rom_bank_value + ctx.rom_bank_value_2); + } + } + } else { //printf("Bank Value: %d\n", value); value &= 0b11111; if(value == 0) { @@ -397,7 +417,9 @@ void cart_write(u16 address, u8 value){ return; } ctx.ram_bank_value = value & 0b11; - if(ctx.ram_banking) { + if(cart_mbc5()) + ctx.ram_bank_value = value & 0xF; + if(ctx.ram_banking || cart_mbc5()) { if(cart_need_save()) { cart_battery_save(); }