2023-08-26 19:04:02 +02:00
|
|
|
#include "bus.h"
|
|
|
|
|
|
|
|
Bus::Bus() : map() {}
|
|
|
|
|
|
|
|
std::optional<Bus::MapEntry> Bus::find_entry(u16 addr)
|
|
|
|
{
|
|
|
|
for(auto it = map.begin(); it != map.end(); ++it)
|
|
|
|
{
|
2023-08-30 13:02:20 +02:00
|
|
|
if (it->range.contains(addr))
|
2023-08-26 19:04:02 +02:00
|
|
|
return std::make_optional(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-09-01 23:19:02 +02:00
|
|
|
void Bus::map_device(u16 start, u16 end, Mem_device* dev, u16 offset)
|
2023-08-26 19:04:02 +02:00
|
|
|
{
|
2023-09-01 23:19:02 +02:00
|
|
|
map.push_back(Bus::MapEntry{ Range(start,end), offset, dev });
|
2023-08-26 19:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bus::write8(u16 addr, u8 data)
|
|
|
|
{
|
|
|
|
auto mapentry = find_entry(addr);
|
|
|
|
|
|
|
|
if(!mapentry) return;
|
|
|
|
|
2023-09-01 23:19:02 +02:00
|
|
|
mapentry->dev->write8(addr - mapentry->range.start + mapentry->offset, data);
|
2023-08-26 19:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u8 Bus::read8(u16 addr)
|
|
|
|
{
|
|
|
|
auto mapentry = find_entry(addr);
|
|
|
|
|
|
|
|
if(!mapentry) return 0xFFu;
|
|
|
|
|
2023-09-01 23:19:02 +02:00
|
|
|
return mapentry->dev->read8(addr - mapentry->range.start + mapentry->offset);
|
2023-08-26 19:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bus::write16(u16 addr, u16 data)
|
|
|
|
{
|
|
|
|
auto mapentry = find_entry(addr);
|
|
|
|
if(!mapentry) return;
|
2023-09-01 23:19:02 +02:00
|
|
|
mapentry->dev->write16(addr - mapentry->range.start + mapentry->offset, data);
|
2023-08-26 19:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u16 Bus::read16(u16 addr)
|
|
|
|
{
|
|
|
|
auto mapentry = find_entry(addr);
|
|
|
|
if(!mapentry) return 0xFFFFu;
|
2023-09-01 23:19:02 +02:00
|
|
|
return mapentry->dev->read16(addr - mapentry->range.start + mapentry->offset);
|
2023-08-26 19:04:02 +02:00
|
|
|
}
|