Merge pull request #156 from p12nGH/noncontiguous_harts
[riscv-isa-sim.git] / riscv / devices.cc
1 #include "devices.h"
2
3 void bus_t::add_device(reg_t addr, abstract_device_t* dev)
4 {
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)
10 devices[addr] = dev;
11 }
12
13 bool bus_t::load(reg_t addr, size_t len, uint8_t* bytes)
14 {
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
21 return false;
22 }
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.
26 it--;
27 return it->second->load(addr - it->first, len, bytes);
28 }
29
30 bool bus_t::store(reg_t addr, size_t len, const uint8_t* bytes)
31 {
32 // See comments in bus_t::load
33 auto it = devices.upper_bound(addr);
34 if (devices.empty() || it == devices.begin()) {
35 return false;
36 }
37 it--;
38 return it->second->store(addr - it->first, len, bytes);
39 }
40
41 std::pair<reg_t, abstract_device_t*> bus_t::find_device(reg_t addr)
42 {
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);
47 }
48 it--;
49 return std::make_pair(it->first, it->second);
50 }