1 #ifndef _RISCV_DEVICES_H
2 #define _RISCV_DEVICES_H
12 class abstract_device_t
{
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() {}
19 class bus_t
: public abstract_device_t
{
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
);
25 std::pair
<reg_t
, abstract_device_t
*> find_device(reg_t addr
);
28 std::map
<reg_t
, abstract_device_t
*> devices
;
31 class rom_device_t
: public abstract_device_t
{
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
; }
38 std::vector
<char> data
;
41 class mem_t
: public abstract_device_t
{
43 mem_t(size_t size
) : len(size
) {
45 throw std::runtime_error("zero bytes of target memory requested");
46 data
= (char*)calloc(1, size
);
48 throw std::runtime_error("couldn't allocate " + std::to_string(size
) + " bytes of target memory");
50 mem_t(const mem_t
& that
) = delete;
51 ~mem_t() { free(data
); }
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
; }
63 class clint_t
: public abstract_device_t
{
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
);
71 typedef uint64_t mtime_t
;
72 typedef uint64_t mtimecmp_t
;
73 typedef uint32_t msip_t
;
74 std::vector
<processor_t
*>& procs
;
76 std::vector
<mtimecmp_t
> mtimecmp
;