aboutsummaryrefslogtreecommitdiff
path: root/riscv/abstract_device.h
blob: f2a90fa64fddd3a0dfae3cc34464098d599f01bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef _RISCV_ABSTRACT_DEVICE_H
#define _RISCV_ABSTRACT_DEVICE_H

#include "decode.h"
#include "common.h"
#include <cstdint>
#include <cstddef>
#include <string>
#include <map>
#include <stdexcept>

class sim_t;

class abstract_device_t {
 public:
  virtual bool load(reg_t addr, size_t len, uint8_t* bytes) = 0;
  virtual bool store(reg_t addr, size_t len, const uint8_t* bytes) = 0;
  virtual ~abstract_device_t() {}
  virtual void tick(reg_t UNUSED rtc_ticks) {}
};

// factory for devices which should show up in the DTS, and can be
// parameterized by parsing the DTS
class device_factory_t {
public:
  virtual abstract_device_t* parse_from_fdt(const void* fdt, const sim_t* sim, reg_t* base) const = 0;
  virtual std::string generate_dts(const sim_t* sim) const = 0;
  virtual ~device_factory_t() {}
};

// Type for holding all registered MMIO plugins by name.
using mmio_device_map_t = std::map<std::string, device_factory_t*>;

mmio_device_map_t& mmio_device_map();

#define REGISTER_DEVICE(name, parse, generate) \
  class name##_factory_t : public device_factory_t { \
  public: \
  name##_factory_t() { \
    std::string str(#name); \
    if (!mmio_device_map().emplace(str, this).second) throw std::runtime_error("Plugin \"" + str + "\" already registered"); \
  }; \
  name##_t* parse_from_fdt(const void* fdt, const sim_t* sim, reg_t* base) const override { return parse(fdt, sim, base); } \
  std::string generate_dts(const sim_t* sim) const override { return generate(sim); } \
  }; device_factory_t *name##_factory = new name##_factory_t();

#endif