#include "bus.h" Bus::Bus() : map() {} std::optional 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, u16 offset) { map.push_back(Bus::MapEntry{ Range(start,end), offset, dev }); } void Bus::write8(u16 addr, u8 data) { auto mapentry = find_entry(addr); if(!mapentry) return; mapentry->dev->write8(addr - mapentry->range.start + mapentry->offset, data); } u8 Bus::read8(u16 addr) { auto mapentry = find_entry(addr); if(!mapentry) return 0xFFu; return mapentry->dev->read8(addr - mapentry->range.start + mapentry->offset); } void Bus::write16(u16 addr, u16 data) { auto mapentry = find_entry(addr); if(!mapentry) return; mapentry->dev->write16(addr - mapentry->range.start + mapentry->offset, data); } u16 Bus::read16(u16 addr) { auto mapentry = find_entry(addr); if(!mapentry) return 0xFFFFu; return mapentry->dev->read16(addr - mapentry->range.start + mapentry->offset); }