vgbc/memory/bus.cpp

51 lines
1,022 B
C++

#include "bus.h"
Bus::Bus() : map() {}
std::optional<Bus::MapEntry> Bus::find_entry(u16 addr)
{
for(auto it = map.begin(); it != map.end(); ++it)
{
if (it->range.contains(addr))
return std::make_optional(*it);
}
return {};
}
void Bus::map_device(u16 start, u16 end, Mem_device* dev)
{
map.push_back(Bus::MapEntry{ Range(start,end), dev });
}
void Bus::write8(u16 addr, u8 data)
{
auto mapentry = find_entry(addr);
if(!mapentry) return;
mapentry->dev->write8(addr - mapentry->range.start, data);
}
u8 Bus::read8(u16 addr)
{
auto mapentry = find_entry(addr);
if(!mapentry) return 0xFFu;
return mapentry->dev->read8(addr - mapentry->range.start);
}
void Bus::write16(u16 addr, u16 data)
{
auto mapentry = find_entry(addr);
if(!mapentry) return;
mapentry->dev->write16(addr - mapentry->range.start, data);
}
u16 Bus::read16(u16 addr)
{
auto mapentry = find_entry(addr);
if(!mapentry) return 0xFFFFu;
return mapentry->dev->read16(addr - mapentry->range.start);
}