diff --git a/Makeconf b/Makeconf index d17ba72..ac8fd84 100644 --- a/Makeconf +++ b/Makeconf @@ -4,6 +4,7 @@ modules := memory/device \ memory/bootrom_overlay \ memory/register \ memory/bank \ + memory/mbc/mbc1 \ cpu/cpu \ cpu/decoder \ cartridge/cartridge diff --git a/memory/mbc/mbc1.cpp b/memory/mbc/mbc1.cpp new file mode 100644 index 0000000..9941764 --- /dev/null +++ b/memory/mbc/mbc1.cpp @@ -0,0 +1,39 @@ +#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"); +} diff --git a/memory/mbc/mbc1.h b/memory/mbc/mbc1.h new file mode 100644 index 0000000..9c745a6 --- /dev/null +++ b/memory/mbc/mbc1.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include + +class MBC1 : public Mem_device { +private: + Cartridge& cart; + u8 main_bankreg; + u8 ram_bankreg; + + bool ram_enabled; + + RAM external_ram; + +public: + MBC1(Cartridge& cart); + + virtual void write8(u16 addr, u8 data); + virtual u8 read8(u16 addr); +};