Browse Source

Refactor bus_t::load/store for DRY

pull/1946/head
Andrew Waterman 1 year ago
parent
commit
14029ef4f8
  1. 42
      riscv/devices.cc
  2. 1
      riscv/devices.h

42
riscv/devices.cc

@ -10,53 +10,43 @@ mmio_device_map_t& mmio_device_map()
void bus_t::add_device(reg_t addr, abstract_device_t* dev)
{
// Searching devices via lower_bound/upper_bound
// implicitly relies on the underlying std::map
// container to sort the keys and provide ordered
// iteration over this sort, which it does. (python's
// SortedDict is a good analogy)
devices[addr] = dev;
}
bool bus_t::load(reg_t addr, size_t len, uint8_t* bytes)
{
// Find the device with the base address closest to but
// less than addr (price-is-right search)
auto it = devices.upper_bound(addr);
if (devices.empty() || it == devices.begin()) {
// Either the bus is empty, or there weren't
// any items with a base address <= addr
return false;
}
// Found at least one item with base address <= addr
// The iterator points to the device after this, so
// go back by one item.
it--;
return it->second->load(addr - it->first, len, bytes);
if (auto [base, dev] = find_device(addr, len); dev)
return dev->load(addr - base, len, bytes);
return false;
}
bool bus_t::store(reg_t addr, size_t len, const uint8_t* bytes)
{
// See comments in bus_t::load
auto it = devices.upper_bound(addr);
if (devices.empty() || it == devices.begin()) {
return false;
}
it--;
return it->second->store(addr - it->first, len, bytes);
if (auto [base, dev] = find_device(addr, len); dev)
return dev->store(addr - base, len, bytes);
return false;
}
std::pair<reg_t, abstract_device_t*> bus_t::find_device(reg_t addr)
{
// See comments in bus_t::load
// Obtain iterator to device immediately after the one that might match
auto it = devices.upper_bound(addr);
if (devices.empty() || it == devices.begin()) {
// No devices with base address <= addr
return std::make_pair((reg_t)0, (abstract_device_t*)NULL);
}
// Rewind to device that will match if its size suffices
it--;
return std::make_pair(it->first, it->second);
}
std::pair<reg_t, abstract_device_t*> bus_t::find_device(reg_t addr, size_t len)
{
return find_device(addr);
}
mem_t::mem_t(reg_t size)
: sz(size)
{

1
riscv/devices.h

@ -21,6 +21,7 @@ class bus_t : public abstract_device_t {
void add_device(reg_t addr, abstract_device_t* dev);
std::pair<reg_t, abstract_device_t*> find_device(reg_t addr);
std::pair<reg_t, abstract_device_t*> find_device(reg_t addr, size_t len);
private:
std::map<reg_t, abstract_device_t*> devices;

Loading…
Cancel
Save