forked from dingusdev/dingusppc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memaccess.h
235 lines (199 loc) · 9.85 KB
/
memaccess.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Set of macros for accessing host memory in units of various sizes
and endianness.
*/
#ifndef MEM_ACCESS_H
#define MEM_ACCESS_H
#include "endianswap.h"
#include <cinttypes>
#include <loguru.hpp>
/* read an aligned big-endian WORD (16bit) */
#define READ_WORD_BE_A( addr) (BYTESWAP_16(*((uint16_t*)(addr))))
/* read an aligned big-endian DWORD (32bit) */
#define READ_DWORD_BE_A(addr) (BYTESWAP_32(*((uint32_t*)(addr))))
/* read an aligned big-endian QWORD (64bit) */
#define READ_QWORD_BE_A(addr) (BYTESWAP_64(*((uint64_t*)(addr))))
/* read an aligned little-endian WORD (16bit) */
#define READ_WORD_LE_A( addr) (*(uint16_t*)(addr))
/* read an aligned little-endian DWORD (32bit) */
#define READ_DWORD_LE_A(addr) (*(uint32_t*)(addr))
/* read an aligned little-endian QWORD (64bit) */
#define READ_QWORD_LE_A(addr) (*(uint64_t*)(addr))
/* read an unaligned big-endian WORD (16bit) */
#define READ_WORD_BE_U( addr) ((((uint8_t*)(addr))[0] << 8) | ((uint8_t*)(addr))[1])
/* read an unaligned big-endian DWORD (32bit) */
#define READ_DWORD_BE_U(addr) \
((((uint8_t*)(addr))[0] << 24) | (((uint8_t*)(addr))[1] << 16) | \
(((uint8_t*)(addr))[2] << 8) | ((uint8_t*)(addr))[3] )
/* read an unaligned big-endian QWORD (32bit) */
#define READ_QWORD_BE_U(addr) \
((uint64_t(((uint8_t*)(addr))[0]) << 56) | (uint64_t(((uint8_t*)(addr))[1]) << 48) | \
(uint64_t(((uint8_t*)(addr))[2]) << 40) | (uint64_t(((uint8_t*)(addr))[3]) << 32) | \
(uint64_t(((uint8_t*)(addr))[4]) << 24) | ( ((uint8_t*)(addr))[5] << 16) | \
( ((uint8_t*)(addr))[6] << 8) | ((uint8_t*)(addr))[7] )
/* read an unaligned little-endian WORD (16bit) */
#define READ_WORD_LE_U( addr) ((((uint8_t*)(addr))[1] << 8) | ((uint8_t*)(addr))[0])
/* read an unaligned little-endian DWORD (32bit) */
#define READ_DWORD_LE_U(addr) \
((((uint8_t*)(addr))[3] << 24) | (((uint8_t*)(addr))[2] << 16) | \
(((uint8_t*)(addr))[1] << 8) | ((uint8_t*)(addr))[0] )
/* read an unaligned little-endian DWORD (64bit) */
#define READ_QWORD_LE_U(addr) \
((uint64_t(((uint8_t*)(addr))[7]) << 56) | (uint64_t(((uint8_t*)(addr))[6]) << 48) | \
(uint64_t(((uint8_t*)(addr))[5]) << 40) | (uint64_t(((uint8_t*)(addr))[4]) << 32) | \
(uint64_t(((uint8_t*)(addr))[3]) << 24) | ( ((uint8_t*)(addr))[2] << 16) | \
( ((uint8_t*)(addr))[1] << 8) | ((uint8_t*)(addr))[0] )
/* write an aligned big-endian WORD (16bit) */
#define WRITE_WORD_BE_A( addr, val) (*((uint16_t*)(addr)) = BYTESWAP_16(val))
/* write an aligned big-endian DWORD (32bit) */
#define WRITE_DWORD_BE_A(addr, val) (*((uint32_t*)(addr)) = BYTESWAP_32(val))
/* write an aligned big-endian QWORD (64bit) */
#define WRITE_QWORD_BE_A(addr, val) (*((uint64_t*)(addr)) = BYTESWAP_64(val))
/* write an unaligned big-endian WORD (16bit) */
#define WRITE_WORD_BE_U(addr, val) \
do { \
((uint8_t*)(addr))[0] = ((val) >> 8); \
((uint8_t*)(addr))[1] = (uint8_t)(val); \
} while (0)
/* write an unaligned big-endian DWORD (32bit) */
#define WRITE_DWORD_BE_U(addr, val) \
do { \
((uint8_t*)(addr))[0] = ((val) >> 24); \
((uint8_t*)(addr))[1] = ((val) >> 16); \
((uint8_t*)(addr))[2] = ((val) >> 8); \
((uint8_t*)(addr))[3] = (uint8_t)(val); \
} while (0)
/* write an unaligned big-endian DWORD (64bit) */
#define WRITE_QWORD_BE_U(addr, val) \
do { \
((uint8_t*)(addr))[0] = ((uint64_t)(val) >> 56); \
((uint8_t*)(addr))[1] = ((uint64_t)(val) >> 48); \
((uint8_t*)(addr))[2] = ((uint64_t)(val) >> 40); \
((uint8_t*)(addr))[3] = ((uint64_t)(val) >> 32); \
((uint8_t*)(addr))[4] = ( (val) >> 24); \
((uint8_t*)(addr))[5] = ( (val) >> 16); \
((uint8_t*)(addr))[6] = ( (val) >> 8); \
((uint8_t*)(addr))[7] = (uint8_t)(val) ; \
} while (0)
/* write an aligned little-endian WORD (16bit) */
#define WRITE_WORD_LE_A( addr, val) (*((uint16_t*)(addr)) = (val))
/* write an aligned little-endian DWORD (32bit) */
#define WRITE_DWORD_LE_A(addr, val) (*((uint32_t*)(addr)) = (val))
/* write an aligned little-endian QWORD (64bit) */
#define WRITE_QWORD_LE_A(addr, val) (*((uint64_t*)(addr)) = (val))
/* write an unaligned little-endian WORD (16bit) */
#define WRITE_WORD_LE_U(addr, val) \
do { \
((uint8_t*)(addr))[0] = (uint8_t)(val); \
((uint8_t*)(addr))[1] = ((val) >> 8); \
} while (0)
/* write an unaligned little-endian DWORD (32bit) */
#define WRITE_DWORD_LE_U(addr, val) \
do { \
((uint8_t*)(addr))[0] = (uint8_t)(val); \
((uint8_t*)(addr))[1] = ((val) >> 8); \
((uint8_t*)(addr))[2] = ((val) >> 16); \
((uint8_t*)(addr))[3] = ((val) >> 24); \
} while (0)
/* write an unaligned little-endian DWORD (64bit) */
#define WRITE_QWORD_LE_U(addr, val) \
do { \
((uint8_t*)(addr))[0] = (uint8_t)(val) ; \
((uint8_t*)(addr))[1] = ( (val) >> 8); \
((uint8_t*)(addr))[2] = ( (val) >> 16); \
((uint8_t*)(addr))[3] = ( (val) >> 24); \
((uint8_t*)(addr))[4] = ((uint64_t)(val) >> 32); \
((uint8_t*)(addr))[5] = ((uint64_t)(val) >> 40); \
((uint8_t*)(addr))[6] = ((uint64_t)(val) >> 48); \
((uint8_t*)(addr))[7] = ((uint64_t)(val) >> 56); \
} while (0)
/* read value of the specified size from memory starting at addr,
perform byte swapping when necessary so that the source
byte order remains unchanged. */
inline uint32_t read_mem(const uint8_t* buf, uint32_t size) {
switch (size) {
case 4:
return READ_DWORD_BE_A(buf);
break;
case 2:
return READ_WORD_BE_A(buf);
break;
case 1:
return *buf;
break;
default:
LOG_F(WARNING, "READ_MEM: invalid size %d!", size);
return 0;
}
}
/* read value of the specified size from memory starting at addr,
perform byte swapping when necessary so that the destination data
will be in the reversed byte order. */
inline uint32_t read_mem_rev(const uint8_t* buf, uint32_t size) {
switch (size) {
case 4:
return READ_DWORD_LE_A(buf);
break;
case 2:
return READ_WORD_LE_A(buf);
break;
case 1:
return *buf;
break;
default:
LOG_F(WARNING, "READ_MEM_REV: invalid size %d!", size);
return 0;
}
}
/* write the specified value of the specified size to memory pointed
to by addr, perform necessary byte swapping so that the byte order
of the destination remains unchanged. */
inline void write_mem(uint8_t* buf, uint32_t value, uint32_t size) {
switch (size) {
case 4:
WRITE_DWORD_BE_A(buf, value);
break;
case 2:
WRITE_WORD_BE_A(buf, value & 0xFFFFU);
break;
case 1:
*buf = value & 0xFF;
break;
default:
LOG_F(WARNING, "WRITE_MEM: invalid size %d!", size);
}
}
/* write the specified value of the specified size to memory pointed
to by addr, perform necessary byte swapping so that the destination
data in memory will be in the reversed byte order. */
inline void write_mem_rev(uint8_t* buf, uint32_t value, uint32_t size) {
switch (size) {
case 4:
WRITE_DWORD_LE_A(buf, value);
break;
case 2:
WRITE_WORD_LE_A(buf, value & 0xFFFFU);
break;
case 1:
*buf = value & 0xFF;
break;
default:
LOG_F(WARNING, "WRITE_MEM_REV: invalid size %d!", size);
}
}
#endif /* MEM_ACCESS_H */