Support debug system bus access.
[riscv-isa-sim.git] / riscv / devices.h
1 #ifndef _RISCV_DEVICES_H
2 #define _RISCV_DEVICES_H
3
4 #include "decode.h"
5 #include <cstdlib>
6 #include <string>
7 #include <map>
8 #include <vector>
9
10 class processor_t;
11
12 class abstract_device_t {
13 public:
14 virtual bool load(reg_t addr, size_t len, uint8_t* bytes) = 0;
15 virtual bool store(reg_t addr, size_t len, const uint8_t* bytes) = 0;
16 virtual ~abstract_device_t() {}
17 };
18
19 class bus_t : public abstract_device_t {
20 public:
21 bool load(reg_t addr, size_t len, uint8_t* bytes);
22 bool store(reg_t addr, size_t len, const uint8_t* bytes);
23 void add_device(reg_t addr, abstract_device_t* dev);
24
25 std::pair<reg_t, abstract_device_t*> find_device(reg_t addr);
26
27 private:
28 std::map<reg_t, abstract_device_t*> devices;
29 };
30
31 class rom_device_t : public abstract_device_t {
32 public:
33 rom_device_t(std::vector<char> data);
34 bool load(reg_t addr, size_t len, uint8_t* bytes);
35 bool store(reg_t addr, size_t len, const uint8_t* bytes);
36 const std::vector<char>& contents() { return data; }
37 private:
38 std::vector<char> data;
39 };
40
41 class mem_t : public abstract_device_t {
42 public:
43 mem_t(size_t size) : len(size) {
44 if (!size)
45 throw std::runtime_error("zero bytes of target memory requested");
46 data = (char*)calloc(1, size);
47 if (!data)
48 throw std::runtime_error("couldn't allocate " + std::to_string(size) + " bytes of target memory");
49 }
50 mem_t(const mem_t& that) = delete;
51 ~mem_t() { free(data); }
52
53 bool load(reg_t addr, size_t len, uint8_t* bytes) { return false; }
54 bool store(reg_t addr, size_t len, const uint8_t* bytes) { return false; }
55 char* contents() { return data; }
56 size_t size() { return len; }
57
58 private:
59 char* data;
60 size_t len;
61 };
62
63 class clint_t : public abstract_device_t {
64 public:
65 clint_t(std::vector<processor_t*>&);
66 bool load(reg_t addr, size_t len, uint8_t* bytes);
67 bool store(reg_t addr, size_t len, const uint8_t* bytes);
68 size_t size() { return CLINT_SIZE; }
69 void increment(reg_t inc);
70 private:
71 typedef uint64_t mtime_t;
72 typedef uint64_t mtimecmp_t;
73 typedef uint32_t msip_t;
74 std::vector<processor_t*>& procs;
75 mtime_t mtime;
76 std::vector<mtimecmp_t> mtimecmp;
77 };
78
79 #endif