3 void bus_t::add_device(reg_t addr
, abstract_device_t
* dev
)
5 // Searching devices via lower_bound/upper_bound
6 // implicitly relies on the underlying std::map
7 // container to sort the keys and provide ordered
8 // iteration over this sort, which it does. (python's
9 // SortedDict is a good analogy)
13 bool bus_t::load(reg_t addr
, size_t len
, uint8_t* bytes
)
15 // Find the device with the base address closest to but
16 // less than addr (price-is-right search)
17 auto it
= devices
.upper_bound(addr
);
18 if (devices
.empty() || it
== devices
.begin()) {
19 // Either the bus is empty, or there weren't
20 // any items with a base address <= addr
23 // Found at least one item with base address <= addr
24 // The iterator points to the device after this, so
25 // go back by one item.
27 return it
->second
->load(addr
- it
->first
, len
, bytes
);
30 bool bus_t::store(reg_t addr
, size_t len
, const uint8_t* bytes
)
32 // See comments in bus_t::load
33 auto it
= devices
.upper_bound(addr
);
34 if (devices
.empty() || it
== devices
.begin()) {
38 return it
->second
->store(addr
- it
->first
, len
, bytes
);
41 std::pair
<reg_t
, abstract_device_t
*> bus_t::find_device(reg_t addr
)
43 // See comments in bus_t::load
44 auto it
= devices
.upper_bound(addr
);
45 if (devices
.empty() || it
== devices
.begin()) {
46 return std::make_pair((reg_t
)0, (abstract_device_t
*)NULL
);
49 return std::make_pair(it
->first
, it
->second
);