diff --git a/Makeconf b/Makeconf index 8f19b57..086f66a 100644 --- a/Makeconf +++ b/Makeconf @@ -3,6 +3,7 @@ modules := memory/device \ memory/ram \ memory/bootrom_overlay \ memory/register \ + memory/bank \ cpu/cpu \ cpu/decoder diff --git a/memory/bank.cpp b/memory/bank.cpp new file mode 100644 index 0000000..b70c728 --- /dev/null +++ b/memory/bank.cpp @@ -0,0 +1,44 @@ +#include + +Bank::Bank() + : banks(), current_bank(nullptr) +{} + +void Bank::addBank(u8 idx, Mem_device* dev) +{ + banks[idx] = dev; +} + +void Bank::setBank(u8 idx) +{ + auto it = banks.find(idx); + if(it == banks.end()) + current_bank = nullptr; + else + current_bank = it->second; +} + + +void Bank::write8(u16 addr, u8 data) +{ + if(current_bank == nullptr) return; + current_bank->write8(addr, data); +} + +u8 Bank::read8(u16 addr) +{ + if(current_bank == nullptr) return 0xFFu; + return current_bank->read8(addr); +} + +void Bank::write16(u16 addr, u16 data) +{ + if(current_bank == nullptr) return; + current_bank->write16(addr, data); +} + +u16 Bank::read16(u16 addr) +{ + if(current_bank == nullptr) return 0xFFu; + return current_bank->read16(addr); +} diff --git a/memory/bank.h b/memory/bank.h new file mode 100644 index 0000000..c6be8bc --- /dev/null +++ b/memory/bank.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +#include + +class Bank : public Mem_device { +private: + Mem_device* current_bank; + std::map banks; +public: + Bank(); + + void addBank(u8 idx, Mem_device* dev); + + void setBank(u8 bank); + + void write8(u16 addr, u8 data); + u8 read8(u16 addr); + + void write16(u16 addr, u16 data); + u16 read16(u16 addr); +};