CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/MemMap.h
Views: 1401
1
// Copyright (C) 2003 Dolphin Project / 2012 PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official SVN repository and contact information can be found at
16
// http://code.google.com/p/dolphin-emu/
17
18
#pragma once
19
20
#include "ppsspp_config.h"
21
22
#include <cstring>
23
#include <cstdint>
24
#ifndef offsetof
25
#include <stddef.h>
26
#endif
27
28
#include "Common/CommonTypes.h"
29
#include "Common/Swap.h"
30
#include "Core/Opcode.h"
31
32
// PPSSPP is very aggressive about trying to do memory accesses directly, for speed.
33
// This can be a problem when debugging though, as stray memory reads and writes will
34
// crash the whole emulator.
35
// If safe memory is enabled and JIT is disabled, all memory access will go through the proper
36
// memory access functions, and thus won't crash the emu when they go out of bounds.
37
#if defined(_DEBUG)
38
//#define SAFE_MEMORY
39
#endif
40
41
// Global declarations
42
class PointerWrap;
43
44
typedef void (*writeFn8 )(const u8, const u32);
45
typedef void (*writeFn16)(const u16,const u32);
46
typedef void (*writeFn32)(const u32,const u32);
47
typedef void (*writeFn64)(const u64,const u32);
48
49
typedef void (*readFn8 )(u8&, const u32);
50
typedef void (*readFn16)(u16&, const u32);
51
typedef void (*readFn32)(u32&, const u32);
52
typedef void (*readFn64)(u64&, const u32);
53
54
namespace Memory {
55
// Base is a pointer to the base of the memory map. Yes, some MMU tricks
56
// are used to set up a full GC or Wii memory map in process memory. on
57
// 32-bit, you have to mask your offsets with 0x3FFFFFFF. This means that
58
// some things are mirrored too many times, but eh... it works.
59
60
// In 64-bit, this might point to "high memory" (above the 32-bit limit),
61
// so be sure to load it into a 64-bit register.
62
extern u8 *base;
63
64
// This replaces RAM_NORMAL_SIZE at runtime.
65
extern u32 g_MemorySize;
66
extern u32 g_PSPModel;
67
68
// UWP has such limited memory management that we need to mask
69
// even in 64-bit mode. Also, when using the sanitizer, we need to mask as well.
70
#if PPSSPP_ARCH(32BIT) || PPSSPP_PLATFORM(UWP) || USE_ASAN || PPSSPP_PLATFORM(IOS) || defined(__EMSCRIPTEN__)
71
#define MASKED_PSP_MEMORY
72
#endif
73
74
enum
75
{
76
// This may be adjusted by remaster games.
77
RAM_NORMAL_SIZE = 0x02000000,
78
// Used if the PSP model is PSP-2000 (Slim).
79
RAM_DOUBLE_SIZE = RAM_NORMAL_SIZE * 2,
80
81
VRAM_SIZE = 0x00200000,
82
83
SCRATCHPAD_SIZE = 0x00004000,
84
85
#ifdef MASKED_PSP_MEMORY
86
// This wraparound should work for PSP too.
87
MEMVIEW32_MASK = 0x3FFFFFFF,
88
#endif
89
};
90
91
enum {
92
MV_MIRROR_PREVIOUS = 1,
93
MV_IS_PRIMARY_RAM = 0x100,
94
MV_IS_EXTRA1_RAM = 0x200,
95
MV_IS_EXTRA2_RAM = 0x400,
96
MV_KERNEL = 0x800 // Can be skipped on platforms where memory is tight.
97
};
98
99
struct MemoryView
100
{
101
u8 **out_ptr;
102
u32 virtual_address;
103
u32 size;
104
u32 flags;
105
};
106
107
// Uses a memory arena to set up an emulator-friendly memory map
108
bool MemoryMap_Setup(u32 flags);
109
void MemoryMap_Shutdown(u32 flags);
110
111
// Init and Shutdown
112
bool Init();
113
void Shutdown();
114
void DoState(PointerWrap &p);
115
void Clear();
116
// False when shutdown has already been called.
117
bool IsActive();
118
119
class MemoryInitedLock {
120
public:
121
MemoryInitedLock();
122
~MemoryInitedLock();
123
};
124
125
// This doesn't lock memory access or anything, it just makes sure memory isn't freed.
126
// Use it when accessing PSP memory from external threads.
127
MemoryInitedLock Lock();
128
129
// used by JIT to read instructions. Does not resolve replacements.
130
Opcode Read_Opcode_JIT(const u32 _Address);
131
// used by JIT. Reads in the "Locked cache" mode
132
void Write_Opcode_JIT(const u32 _Address, const Opcode& _Value);
133
134
// Should be used by analyzers, disassemblers etc. Does resolve replacements.
135
Opcode Read_Instruction(const u32 _Address, bool resolveReplacements = false);
136
Opcode ReadUnchecked_Instruction(const u32 _Address, bool resolveReplacements = false);
137
138
u8 Read_U8(const u32 _Address);
139
u16 Read_U16(const u32 _Address);
140
u32 Read_U32(const u32 _Address);
141
u64 Read_U64(const u32 _Address);
142
143
inline u8* GetPointerWriteUnchecked(const u32 address) {
144
#ifdef MASKED_PSP_MEMORY
145
return (u8 *)(base + (address & MEMVIEW32_MASK));
146
#else
147
return (u8 *)(base + address);
148
#endif
149
}
150
151
inline const u8* GetPointerUnchecked(const u32 address) {
152
#ifdef MASKED_PSP_MEMORY
153
return (const u8 *)(base + (address & MEMVIEW32_MASK));
154
#else
155
return (const u8 *)(base + address);
156
#endif
157
}
158
159
inline u32 ReadUnchecked_U32(const u32 address) {
160
#ifdef MASKED_PSP_MEMORY
161
return *(u32_le *)(base + (address & MEMVIEW32_MASK));
162
#else
163
return *(u32_le *)(base + address);
164
#endif
165
}
166
167
inline float ReadUnchecked_Float(const u32 address) {
168
#ifdef MASKED_PSP_MEMORY
169
return *(float_le *)(base + (address & MEMVIEW32_MASK));
170
#else
171
return *(float_le *)(base + address);
172
#endif
173
}
174
175
inline u16 ReadUnchecked_U16(const u32 address) {
176
#ifdef MASKED_PSP_MEMORY
177
return *(u16_le *)(base + (address & MEMVIEW32_MASK));
178
#else
179
return *(u16_le *)(base + address);
180
#endif
181
}
182
183
inline u8 ReadUnchecked_U8(const u32 address) {
184
#ifdef MASKED_PSP_MEMORY
185
return (*(u8 *)(base + (address & MEMVIEW32_MASK)));
186
#else
187
return (*(u8 *)(base + address));
188
#endif
189
}
190
191
inline void WriteUnchecked_U32(u32 data, u32 address) {
192
#ifdef MASKED_PSP_MEMORY
193
*(u32_le *)(base + (address & MEMVIEW32_MASK)) = data;
194
#else
195
*(u32_le *)(base + address) = data;
196
#endif
197
}
198
199
inline void WriteUnchecked_Float(float data, u32 address) {
200
#ifdef MASKED_PSP_MEMORY
201
*(float_le *)(base + (address & MEMVIEW32_MASK)) = data;
202
#else
203
*(float_le *)(base + address) = data;
204
#endif
205
}
206
207
inline void WriteUnchecked_U16(u16 data, u32 address) {
208
#ifdef MASKED_PSP_MEMORY
209
*(u16_le *)(base + (address & MEMVIEW32_MASK)) = data;
210
#else
211
*(u16_le *)(base + address) = data;
212
#endif
213
}
214
215
inline void WriteUnchecked_U8(u8 data, u32 address) {
216
#ifdef MASKED_PSP_MEMORY
217
(*(u8 *)(base + (address & MEMVIEW32_MASK))) = data;
218
#else
219
(*(u8 *)(base + address)) = data;
220
#endif
221
}
222
223
inline float Read_Float(u32 address)
224
{
225
u32 ifloat = Read_U32(address);
226
float f;
227
memcpy(&f, &ifloat, sizeof(float));
228
return f;
229
}
230
231
// used by JIT. Return zero-extended 32bit values
232
u32 Read_U8_ZX(const u32 address);
233
u32 Read_U16_ZX(const u32 address);
234
235
void Write_U8(const u8 data, const u32 address);
236
void Write_U16(const u16 data, const u32 address);
237
void Write_U32(const u32 data, const u32 address);
238
void Write_U64(const u64 data, const u32 address);
239
240
inline void Write_Float(float f, u32 address)
241
{
242
u32 u;
243
memcpy(&u, &f, sizeof(float));
244
Write_U32(u, address);
245
}
246
247
u8* GetPointerWrite(const u32 address);
248
const u8* GetPointer(const u32 address);
249
250
u8 *GetPointerWriteRange(const u32 address, const u32 size);
251
const u8 *GetPointerRange(const u32 address, const u32 size);
252
253
bool IsRAMAddress(const u32 address);
254
inline bool IsVRAMAddress(const u32 address) {
255
return ((address & 0x3F800000) == 0x04000000);
256
}
257
inline bool IsDepthTexVRAMAddress(const u32 address) {
258
return ((address & 0x3FE00000) == 0x04200000) || ((address & 0x3FE00000) == 0x04600000);
259
}
260
261
// 0x08000000 -> 0x08800000
262
inline bool IsKernelAddress(const u32 address) {
263
return ((address & 0x3F800000) == 0x08000000);
264
}
265
266
// 0x08000000 -> 0x08400000
267
inline bool IsKernelAndNotVolatileAddress(const u32 address) {
268
return ((address & 0x3FC00000) == 0x08000000);
269
}
270
271
bool IsScratchpadAddress(const u32 address);
272
273
inline void MemcpyUnchecked(void *to_data, const u32 from_address, const u32 len) {
274
memcpy(to_data, GetPointerUnchecked(from_address), len);
275
}
276
277
inline void MemcpyUnchecked(const u32 to_address, const void *from_data, const u32 len) {
278
memcpy(GetPointerWriteUnchecked(to_address), from_data, len);
279
}
280
281
inline void MemcpyUnchecked(const u32 to_address, const u32 from_address, const u32 len) {
282
MemcpyUnchecked(GetPointerWriteUnchecked(to_address), from_address, len);
283
}
284
285
inline bool IsValidAddress(const u32 address) {
286
if ((address & 0x3E000000) == 0x08000000) {
287
return true;
288
} else if ((address & 0x3F800000) == 0x04000000) {
289
return true;
290
} else if ((address & 0xBFFFC000) == 0x00010000) {
291
return true;
292
} else if ((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize) {
293
return true;
294
} else {
295
return false;
296
}
297
}
298
299
inline bool IsValid4AlignedAddress(const u32 address) {
300
if ((address & 0x3E000003) == 0x08000000) {
301
return true;
302
} else if ((address & 0x3F800003) == 0x04000000) {
303
return true;
304
} else if ((address & 0xBFFFC003) == 0x00010000) {
305
return true;
306
} else if ((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize) {
307
return (address & 3) == 0;
308
} else {
309
return false;
310
}
311
}
312
313
314
inline u32 MaxSizeAtAddress(const u32 address){
315
if ((address & 0x3E000000) == 0x08000000) {
316
return 0x08000000 + g_MemorySize - (address & 0x3FFFFFFF);
317
} else if ((address & 0x3F800000) == 0x04000000) {
318
return 0x04800000 - (address & 0x3FFFFFFF);
319
} else if ((address & 0xBFFFC000) == 0x00010000) {
320
return 0x00014000 - (address & 0x3FFFFFFF);
321
} else if ((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize) {
322
return 0x08000000 + g_MemorySize - (address & 0x3FFFFFFF);
323
} else {
324
return 0;
325
}
326
}
327
328
inline const char *GetCharPointerUnchecked(const u32 address) {
329
return (const char *)GetPointerUnchecked(address);
330
}
331
332
// NOTE: Unlike the similar IsValidRange/IsValidAddress functions, this one is linear cost vs the size of the string,
333
// for hopefully-obvious reasons.
334
inline bool IsValidNullTerminatedString(const u32 address) {
335
u32 max_size = MaxSizeAtAddress(address);
336
if (max_size == 0) {
337
return false;
338
}
339
340
const char *c = GetCharPointerUnchecked(address);
341
if (memchr(c, '\0', max_size)) {
342
return true;
343
}
344
return false;
345
}
346
347
inline u32 ValidSize(const u32 address, const u32 requested_size) {
348
u32 max_size = MaxSizeAtAddress(address);
349
if (requested_size > max_size) {
350
return max_size;
351
}
352
return requested_size;
353
}
354
355
// NOTE: If size == 0, any address will be accepted. This may not be ideal for all cases.
356
inline bool IsValidRange(const u32 address, const u32 size) {
357
return ValidSize(address, size) == size;
358
}
359
360
// Used for auto-converted char * parameters, which can sometimes legitimately be null -
361
// so we don't want to get caught in GetPointer's crash reporting
362
// TODO: This should use IsValidNullTerminatedString, but may be expensive since this is used so much - needs evaluation.
363
inline const char *GetCharPointer(const u32 address) {
364
if (address && IsValidAddress(address)) {
365
return GetCharPointerUnchecked(address);
366
} else {
367
return nullptr;
368
}
369
}
370
371
} // namespace Memory
372
373
// Avoiding a global include for NotifyMemInfo.
374
void PSPPointerNotifyRW(int rw, uint32_t ptr, uint32_t bytes, const char *tag, size_t tagLen);
375
376
template <typename T>
377
struct PSPPointer
378
{
379
u32_le ptr;
380
381
inline T &operator*() const
382
{
383
#ifdef MASKED_PSP_MEMORY
384
return *(T *)(Memory::base + (ptr & Memory::MEMVIEW32_MASK));
385
#else
386
return *(T *)(Memory::base + ptr);
387
#endif
388
}
389
390
inline T &operator[](int i) const
391
{
392
#ifdef MASKED_PSP_MEMORY
393
return *((T *)(Memory::base + (ptr & Memory::MEMVIEW32_MASK)) + i);
394
#else
395
return *((T *)(Memory::base + ptr) + i);
396
#endif
397
}
398
399
inline T *operator->() const
400
{
401
#ifdef MASKED_PSP_MEMORY
402
return (T *)(Memory::base + (ptr & Memory::MEMVIEW32_MASK));
403
#else
404
return (T *)(Memory::base + ptr);
405
#endif
406
}
407
408
inline PSPPointer<T> operator+(int i) const
409
{
410
PSPPointer other;
411
other.ptr = ptr + i * sizeof(T);
412
return other;
413
}
414
415
inline PSPPointer<T> &operator=(u32 p)
416
{
417
ptr = p;
418
return *this;
419
}
420
421
inline PSPPointer<T> &operator+=(int i)
422
{
423
ptr = ptr + i * sizeof(T);
424
return *this;
425
}
426
427
inline PSPPointer<T> operator-(int i) const
428
{
429
PSPPointer other;
430
other.ptr = ptr - i * sizeof(T);
431
return other;
432
}
433
434
inline PSPPointer<T> &operator-=(int i)
435
{
436
ptr = ptr - i * sizeof(T);
437
return *this;
438
}
439
440
inline PSPPointer<T> &operator++()
441
{
442
ptr += sizeof(T);
443
return *this;
444
}
445
446
inline PSPPointer<T> operator++(int i)
447
{
448
PSPPointer<T> other;
449
other.ptr = ptr;
450
ptr += sizeof(T);
451
return other;
452
}
453
454
inline PSPPointer<T> &operator--()
455
{
456
ptr -= sizeof(T);
457
return *this;
458
}
459
460
inline PSPPointer<T> operator--(int i)
461
{
462
PSPPointer<T> other;
463
other.ptr = ptr;
464
ptr -= sizeof(T);
465
return other;
466
}
467
468
inline operator T*()
469
{
470
#ifdef MASKED_PSP_MEMORY
471
return (T *)(Memory::base + (ptr & Memory::MEMVIEW32_MASK));
472
#else
473
return (T *)(Memory::base + ptr);
474
#endif
475
}
476
477
inline operator const T*() const
478
{
479
#ifdef MASKED_PSP_MEMORY
480
return (const T *)(Memory::base + (ptr & Memory::MEMVIEW32_MASK));
481
#else
482
return (const T *)(Memory::base + ptr);
483
#endif
484
}
485
486
bool IsValid() const {
487
return Memory::IsValidRange(ptr, (u32)sizeof(T));
488
}
489
490
T *PtrOrNull() {
491
if (IsValid())
492
return (T *)*this;
493
return nullptr;
494
}
495
496
const T *PtrOrNull() const {
497
if (IsValid())
498
return (const T *)*this;
499
return nullptr;
500
}
501
502
template <size_t tagLen>
503
void NotifyWrite(const char(&tag)[tagLen]) const {
504
PSPPointerNotifyRW(1, (uint32_t)ptr, (uint32_t)sizeof(T), tag, tagLen - 1);
505
}
506
507
template <size_t tagLen>
508
void NotifyRead(const char(&tag)[tagLen]) const {
509
PSPPointerNotifyRW(2, (uint32_t)ptr, (uint32_t)sizeof(T), tag, tagLen - 1);
510
}
511
512
size_t ElementSize() const
513
{
514
return sizeof(T);
515
}
516
517
static PSPPointer<T> Create(u32 ptr) {
518
PSPPointer<T> p;
519
p = ptr;
520
return p;
521
}
522
};
523
524
constexpr u32 PSP_GetScratchpadMemoryBase() { return 0x00010000;}
525
constexpr u32 PSP_GetScratchpadMemoryEnd() { return 0x00014000;}
526
527
constexpr u32 PSP_GetKernelMemoryBase() { return 0x08000000;}
528
inline u32 PSP_GetUserMemoryEnd() { return PSP_GetKernelMemoryBase() + Memory::g_MemorySize;}
529
constexpr u32 PSP_GetKernelMemoryEnd() { return 0x08400000;}
530
531
// "Volatile" RAM is between 0x08400000 and 0x08800000, can be requested by the
532
// game through sceKernelVolatileMemTryLock.
533
constexpr u32 PSP_GetVolatileMemoryStart() { return 0x08400000; }
534
constexpr u32 PSP_GetVolatileMemoryEnd() { return 0x08800000; }
535
536
constexpr u32 PSP_GetUserMemoryBase() { return 0x08800000; }
537
constexpr u32 PSP_GetDefaultLoadAddress() { return 0; }
538
constexpr u32 PSP_GetVidMemBase() { return 0x04000000; }
539
constexpr u32 PSP_GetVidMemEnd() { return 0x04800000; }
540
541
template <typename T>
542
inline bool operator==(const PSPPointer<T> &lhs, const PSPPointer<T> &rhs) {
543
return lhs.ptr == rhs.ptr;
544
}
545
546
template <typename T>
547
inline bool operator!=(const PSPPointer<T> &lhs, const PSPPointer<T> &rhs) {
548
return lhs.ptr != rhs.ptr;
549
}
550
551
template <typename T>
552
inline bool operator<(const PSPPointer<T> &lhs, const PSPPointer<T> &rhs) {
553
return lhs.ptr < rhs.ptr;
554
}
555
556
template <typename T>
557
inline bool operator>(const PSPPointer<T> &lhs, const PSPPointer<T> &rhs) {
558
return lhs.ptr > rhs.ptr;
559
}
560
561
template <typename T>
562
inline bool operator<=(const PSPPointer<T> &lhs, const PSPPointer<T> &rhs) {
563
return lhs.ptr <= rhs.ptr;
564
}
565
566
template <typename T>
567
inline bool operator>=(const PSPPointer<T> &lhs, const PSPPointer<T> &rhs) {
568
return lhs.ptr >= rhs.ptr;
569
}
570
571