aboutsummaryrefslogtreecommitdiff
path: root/riscv/abstract_device.h
blob: 0726cd7c674292eee0072e3aea1b176b84f2e0f4 (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
48
#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>
#include <vector>

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 std::vector<std::string>& sargs) const = 0;
  virtual std::string generate_dts(const sim_t* sim, const std::vector<std::string>& sargs) const = 0;
  virtual ~device_factory_t() {}
};

// Type for holding all registered MMIO plugins by name.
using mmio_device_map_t = std::map<std::string, const 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 std::vector<std::string>& sargs) const override { return parse(fdt, sim, base, sargs); } \
  std::string generate_dts(const sim_t* sim, const std::vector<std::string>& sargs) const override { return generate(sim, sargs); } \
  }; device_factory_t *name##_factory = new name##_factory_t();

#endif