memory/mbc - Partial implementation of MBC1

This commit is contained in:
madmaurice 2023-08-31 23:54:01 +02:00
parent 6e7d06299a
commit 3b4c50cbc5
3 changed files with 62 additions and 0 deletions

View file

@ -4,6 +4,7 @@ modules := memory/device \
memory/bootrom_overlay \ memory/bootrom_overlay \
memory/register \ memory/register \
memory/bank \ memory/bank \
memory/mbc/mbc1 \
cpu/cpu \ cpu/cpu \
cpu/decoder \ cpu/decoder \
cartridge/cartridge cartridge/cartridge

39
memory/mbc/mbc1.cpp Normal file
View file

@ -0,0 +1,39 @@
#include <memory/mbc/mbc1.h>
#include <misc/panic.h>
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");
}

22
memory/mbc/mbc1.h Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include <memory/device.h>
#include <memory/ram.h>
#include <cartridge/cartridge.h>
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);
};