Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/MemMapFunctions.cpp
3185 views
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 git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "Common/CommonTypes.h"
19
#include "Common/LogReporting.h"
20
21
#include "Core/Core.h"
22
#include "Core/MemMap.h"
23
#include "Core/Config.h"
24
#include "Core/ConfigValues.h"
25
26
#include "Core/MIPS/MIPS.h"
27
28
namespace Memory {
29
30
u8 *GetPointerWrite(const u32 address) {
31
if ((address & 0x3E000000) == 0x08000000 || // RAM
32
(address & 0x3F800000) == 0x04000000 || // VRAM
33
(address & 0xBFFFC000) == 0x00010000 || // Scratchpad
34
((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize)) { // More RAM (remasters, etc.)
35
return GetPointerWriteUnchecked(address);
36
} else {
37
// Size is not known, we pass 0 to signal that.
38
Core_MemoryException(address, 0, currentMIPS->pc, MemoryExceptionType::WRITE_BLOCK);
39
return nullptr;
40
}
41
}
42
43
const u8 *GetPointer(const u32 address) {
44
if ((address & 0x3E000000) == 0x08000000 || // RAM
45
(address & 0x3F800000) == 0x04000000 || // VRAM
46
(address & 0xBFFFC000) == 0x00010000 || // Scratchpad
47
((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize)) { // More RAM (remasters, etc.)
48
return GetPointerUnchecked(address);
49
} else {
50
// Size is not known, we pass 0 to signal that.
51
Core_MemoryException(address, 0, currentMIPS->pc, MemoryExceptionType::READ_BLOCK);
52
return nullptr;
53
}
54
}
55
56
u8 *GetPointerWriteRange(const u32 address, const u32 size) {
57
u8 *ptr = GetPointerWrite(address);
58
if (ptr) {
59
if (ValidSize(address, size) != size) {
60
// That's a memory exception! TODO: Adjust reported address to the end of the range?
61
Core_MemoryException(address, size, currentMIPS->pc, MemoryExceptionType::WRITE_BLOCK);
62
return nullptr;
63
} else {
64
return ptr;
65
}
66
} else {
67
// Error was handled in GetPointerWrite already, if we're not ignoring errors.
68
return nullptr;
69
}
70
}
71
72
const u8 *GetPointerRange(const u32 address, const u32 size) {
73
const u8 *ptr = GetPointer(address);
74
if (ptr) {
75
if (ValidSize(address, size) != size) {
76
// That's a memory exception! TODO: Adjust reported address to the end of the range?
77
Core_MemoryException(address, size, currentMIPS->pc, MemoryExceptionType::READ_BLOCK);
78
return nullptr;
79
} else {
80
return ptr;
81
}
82
} else {
83
// Error was handled in GetPointer already, if we're not ignoring errors.
84
return nullptr;
85
}
86
}
87
88
template <typename T>
89
inline void ReadFromHardware(T &var, const u32 address) {
90
if ((address & 0x3E000000) == 0x08000000 || // RAM
91
(address & 0x3F800000) == 0x04000000 || // VRAM
92
(address & 0xBFFFC000) == 0x00010000 || // Scratchpad
93
((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize)) { // More RAM (remasters, etc.)
94
var = *((const T*)GetPointerUnchecked(address));
95
} else {
96
Core_MemoryException(address, sizeof(T), currentMIPS->pc, MemoryExceptionType::READ_WORD);
97
var = 0;
98
}
99
}
100
101
template <typename T>
102
inline void WriteToHardware(u32 address, const T data) {
103
if ((address & 0x3E000000) == 0x08000000 || // RAM
104
(address & 0x3F800000) == 0x04000000 || // VRAM
105
(address & 0xBFFFC000) == 0x00010000 || // Scratchpad
106
((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize)) { // More RAM (remasters, etc.)
107
*(T*)GetPointerUnchecked(address) = data;
108
} else {
109
Core_MemoryException(address, sizeof(T), currentMIPS->pc, MemoryExceptionType::WRITE_WORD);
110
}
111
}
112
113
bool IsRAMAddress(const u32 address) {
114
if ((address & 0x3E000000) == 0x08000000) {
115
return true;
116
} else if ((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize) {
117
return true;
118
} else {
119
return false;
120
}
121
}
122
123
bool IsScratchpadAddress(const u32 address) {
124
return (address & 0xBFFFC000) == 0x00010000;
125
}
126
127
u8 Read_U8(const u32 address) {
128
u8 value = 0;
129
ReadFromHardware<u8>(value, address);
130
return (u8)value;
131
}
132
133
u16 Read_U16(const u32 address) {
134
u16_le value = 0;
135
ReadFromHardware<u16_le>(value, address);
136
return (u16)value;
137
}
138
139
u32 Read_U32(const u32 address) {
140
u32_le value = 0;
141
ReadFromHardware<u32_le>(value, address);
142
return value;
143
}
144
145
u64 Read_U64(const u32 address) {
146
u64_le value = 0;
147
ReadFromHardware<u64_le>(value, address);
148
return value;
149
}
150
151
u32 Read_U8_ZX(const u32 address) {
152
return (u32)Read_U8(address);
153
}
154
155
u32 Read_U16_ZX(const u32 address) {
156
return (u32)Read_U16(address);
157
}
158
159
void Write_U8(const u8 _Data, const u32 address) {
160
WriteToHardware<u8>(address, _Data);
161
}
162
163
void Write_U16(const u16 _Data, const u32 address) {
164
WriteToHardware<u16_le>(address, _Data);
165
}
166
167
void Write_U32(const u32 _Data, const u32 address) {
168
WriteToHardware<u32_le>(address, _Data);
169
}
170
171
void Write_U64(const u64 _Data, const u32 address) {
172
WriteToHardware<u64_le>(address, _Data);
173
}
174
175
} // namespace Memory
176
177