-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.h
102 lines (79 loc) · 2.11 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
96
97
98
99
100
101
#ifndef MEMORY
#define MEMORY
#include "common.h"
#include <vector>
namespace mips
{
namespace mem
{
extern int g_vram_base_addr;
extern int g_input_dips[];
enum {
MEM_AREA_RAM = 0,
MEM_AREA_ROM = 1,
};
struct mem_area {
addr_t address;
void *base_ptr;
size_t size;
unsigned flags;
mem_area() {
address = 0;
base_ptr = nullptr;
size = 0;
flags = 0;
}
#if 0
mem_area& operator=(const mem_area& rhs) {
address = rhs.address;
base_ptr = rhs.address;
size = rhs.size;
flags = ths.flags;
}
#endif
mem_area& operator=(const mem_area& rhs) = default;
inline bool has(addr_t addr) const {
return (addr >= address && addr <= (address + size));
}
template<typename T>
T read(addr_t adr) const {
if (has(address)) {
return *((T*) ((uint8_t*) base_ptr + adr - address));
}
return 0;
}
template<typename T>
void write(addr_t adr, T value) {
if (has(address)) {
T *ptr = ((T*) ((uint8_t*) base_ptr + adr - address));
// verifica se a memória é apenas para leitura
if (~flags & MEM_AREA_ROM)
*ptr = value;
}
}
template<typename T>
inline T fast_read(addr_t adr) const {
return *((T*) ((uint8_t*) base_ptr + (adr - address)));
}
template<typename T>
inline void fast_write(addr_t adr, T value) {
T *ptr = ((T*) ((uint8_t*) base_ptr + adr - address));
// verifica se a memória é apenas para leitura
if (~flags & MEM_AREA_ROM)
*ptr = value;
}
};
void register_area(const mem_area& area);
void unregister_area(const mem_area& area);
const vector<mem_area> *get_areas();
void write_byte(addr_t address, uint8_t value);
void write_half(addr_t address, uint16_t value);
void write_word(addr_t address, uint32_t value);
void write_dword(addr_t address, uint64_t value);
uint8_t read_byte(addr_t address);
uint16_t read_half(addr_t address);
uint32_t read_word(addr_t address);
uint64_t read_dword(addr_t address);
}
}
#endif // MEMORY