#include #include MBC1::MBC1(Cartridge& cart) : cart(cart), main_bankreg(0), external_ram(0x8000) {} u8 MBC1::read8(u16 addr) { if((addr & 0xC000) == 0x0000) return cart[addr]; else if((addr & 0xC000) == 0x4000) { u8 bank = main_bankreg ? main_bankreg : 1; u8 numbanks = cart.size() >> 14; return cart[(bank % numbanks) << 14 | (addr - 0x4000)]; } else if((addr & 0xE000) == 0xA000) { if(!ram_enabled) return 0xFF; return external_ram.read8(ram_bankreg << 13 | (addr - 0xA000)); } else return 0xFF; } void MBC1::write8(u16 addr, u8 data) { if((addr & 0xE000) == 0x0000) ram_enabled = ((data & 0x0F) == 0x0A); else if((addr & 0xE000) == 0x2000) main_bankreg = data & 0x1F; else if((addr & 0xE000) == 0x4000) ram_bankreg = data & 0x03; else if((addr & 0xE000) == 0x6000) panic("Banking mode not implemented"); }