-
Notifications
You must be signed in to change notification settings - Fork 10
/
memory.h
95 lines (74 loc) · 1.69 KB
/
memory.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
//
// memory.h
//
// (C) R.P.Bellis 2021
//
//
#pragma once
#include "device.h"
/*
* generic memory interface, dynamically assigned space
*/
class GenericMemory : public MappedDevice {
protected:
std::vector<Byte> memory;
size_t size;
public:
GenericMemory(size_t size) : memory(size), size(size) {};
public:
virtual Byte read(Word offset) {
return (offset < size) ? memory[offset] : 0xff;
};
virtual void write(Word offset, Byte val) {
if (offset < size) {
memory[offset] = val;
}
};
};
/*
* RAM: can be written to
*/
class RAM : public GenericMemory {
public:
RAM(size_t size) : GenericMemory(size) {};
};
/*
* ROM: can't be written
* can be pre-loaded from a hex file
*/
class ROM : public GenericMemory {
public:
ROM(size_t size) : GenericMemory(size) {};
virtual void write(Word offset, Byte val) { // no-op
(void)offset;
(void)val;
}
public:
void load_intelhex(const char *filename, Word base);
};
/*
* ROM_Data
*
* For use when embedding static firmware data inside a compiled image
*/
class ROM_Data : public MappedDevice {
protected:
const uint8_t* memory;
size_t size; // size of virtual device
size_t memsize; // size of allocated data
public:
ROM_Data(const uint8_t* p, size_t size) : memory(p), size(size), memsize(size) {};
ROM_Data(const uint8_t* p, size_t size, size_t memsize) : memory(p), size(size), memsize(memsize) {};
virtual Byte read(Word offset) {
if (offset < size && offset < memsize) {
return memory[offset];
} else {
return 0xff;
}
}
virtual void write(Word offset, Byte val) { // no-op
(void)offset;
(void)val;
}
};