52 lines
1,004 B
C++
52 lines
1,004 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 (addr >= it->start && addr <= it->end)
|
||
|
return std::make_optional(*it);
|
||
|
}
|
||
|
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
void Bus::map_device(u16 start, u16 end, Mem_device* dev)
|
||
|
{
|
||
|
map.push_back(Bus::MapEntry{ start, end, dev });
|
||
|
}
|
||
|
|
||
|
void Bus::write8(u16 addr, u8 data)
|
||
|
{
|
||
|
auto mapentry = find_entry(addr);
|
||
|
|
||
|
if(!mapentry) return;
|
||
|
|
||
|
mapentry->dev->write8(addr - mapentry->start, data);
|
||
|
}
|
||
|
|
||
|
u8 Bus::read8(u16 addr)
|
||
|
{
|
||
|
auto mapentry = find_entry(addr);
|
||
|
|
||
|
if(!mapentry) return 0xFFu;
|
||
|
|
||
|
return mapentry->dev->read8(addr - mapentry->start);
|
||
|
}
|
||
|
|
||
|
void Bus::write16(u16 addr, u16 data)
|
||
|
{
|
||
|
auto mapentry = find_entry(addr);
|
||
|
if(!mapentry) return;
|
||
|
mapentry->dev->write16(addr - mapentry->start, data);
|
||
|
}
|
||
|
|
||
|
u16 Bus::read16(u16 addr)
|
||
|
{
|
||
|
auto mapentry = find_entry(addr);
|
||
|
if(!mapentry) return 0xFFFFu;
|
||
|
return mapentry->dev->read16(addr - mapentry->start);
|
||
|
}
|