return it->second->store(addr - -it->first, len, bytes);
}
-bus_t::descriptor bus_t::find_device(reg_t addr)
+std::pair<reg_t, abstract_device_t*> bus_t::find_device(reg_t addr)
{
auto it = devices.lower_bound(-addr);
- if (it == devices.end()) {
- bus_t::descriptor desc = {0, 0};
- return desc;
- }
- bus_t::descriptor desc = {-it->first, it->second};
- return desc;
+ if (it == devices.end() || addr < -it->first)
+ return std::make_pair((reg_t)0, (abstract_device_t*)NULL);
+ return std::make_pair(-it->first, it->second);
}
bool store(reg_t addr, size_t len, const uint8_t* bytes);
void add_device(reg_t addr, abstract_device_t* dev);
- struct descriptor { reg_t base; abstract_device_t* device; };
- descriptor find_device(reg_t addr);
+ std::pair<reg_t, abstract_device_t*> find_device(reg_t addr);
private:
std::map<reg_t, abstract_device_t*> devices;
char* sim_t::addr_to_mem(reg_t addr) {
auto desc = bus.find_device(addr);
- if (auto mem = dynamic_cast<mem_t*>(desc.device))
- return mem->contents() + (addr - desc.base);
+ if (auto mem = dynamic_cast<mem_t*>(desc.second))
+ if (addr - desc.first < mem->size())
+ return mem->contents() + (addr - desc.first);
return NULL;
}