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/MemMapFunctions.cpp
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 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
static bool reported = false;
38
if (!reported) {
39
Reporting::ReportMessage("Unknown GetPointerWrite %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]);
40
reported = true;
41
}
42
43
// Size is not known, we pass 0 to signal that.
44
Core_MemoryException(address, 0, currentMIPS->pc, MemoryExceptionType::WRITE_BLOCK);
45
return nullptr;
46
}
47
}
48
49
const u8 *GetPointer(const u32 address) {
50
if ((address & 0x3E000000) == 0x08000000 || // RAM
51
(address & 0x3F800000) == 0x04000000 || // VRAM
52
(address & 0xBFFFC000) == 0x00010000 || // Scratchpad
53
((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize)) { // More RAM (remasters, etc.)
54
return GetPointerUnchecked(address);
55
} else {
56
static bool reported = false;
57
if (!reported) {
58
Reporting::ReportMessage("Unknown GetPointer %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]);
59
reported = true;
60
}
61
// Size is not known, we pass 0 to signal that.
62
Core_MemoryException(address, 0, currentMIPS->pc, MemoryExceptionType::READ_BLOCK);
63
return nullptr;
64
}
65
}
66
67
u8 *GetPointerWriteRange(const u32 address, const u32 size) {
68
u8 *ptr = GetPointerWrite(address);
69
if (ptr) {
70
if (ValidSize(address, size) != size) {
71
// That's a memory exception! TODO: Adjust reported address to the end of the range?
72
Core_MemoryException(address, size, currentMIPS->pc, MemoryExceptionType::WRITE_BLOCK);
73
return nullptr;
74
} else {
75
return ptr;
76
}
77
} else {
78
// Error was reported in GetPointerWrite already, if we're not ignoring errors.
79
return nullptr;
80
}
81
}
82
83
const u8 *GetPointerRange(const u32 address, const u32 size) {
84
const u8 *ptr = GetPointer(address);
85
if (ptr) {
86
if (ValidSize(address, size) != size) {
87
// That's a memory exception! TODO: Adjust reported address to the end of the range?
88
Core_MemoryException(address, size, currentMIPS->pc, MemoryExceptionType::READ_BLOCK);
89
return nullptr;
90
} else {
91
return ptr;
92
}
93
} else {
94
// Error was reported in GetPointer already, if we're not ignoring errors.
95
return nullptr;
96
}
97
}
98
99
template <typename T>
100
inline void ReadFromHardware(T &var, const u32 address) {
101
// TODO: Figure out the fastest order of tests for both read and write (they are probably different).
102
if ((address & 0x3E000000) == 0x08000000) {
103
// RAM
104
var = *((const T*)GetPointerUnchecked(address));
105
} else if ((address & 0x3F800000) == 0x04000000) {
106
// VRAM
107
var = *((const T*)GetPointerUnchecked(address));
108
} else if ((address & 0xBFFFC000) == 0x00010000) {
109
// Scratchpad
110
var = *((const T*)GetPointerUnchecked(address));
111
} else if ((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize) {
112
// More RAM (remasters, etc.)
113
var = *((const T*)GetPointerUnchecked(address));
114
} else {
115
static bool reported = false;
116
if (!reported) {
117
Reporting::ReportMessage("ReadFromHardware: Invalid address %08x near PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]);
118
reported = true;
119
}
120
Core_MemoryException(address, sizeof(T), currentMIPS->pc, MemoryExceptionType::READ_WORD);
121
var = 0;
122
}
123
}
124
125
template <typename T>
126
inline void WriteToHardware(u32 address, const T data) {
127
if ((address & 0x3E000000) == 0x08000000) {
128
// RAM
129
*(T*)GetPointerUnchecked(address) = data;
130
} else if ((address & 0x3F800000) == 0x04000000) {
131
// VRAM
132
*(T*)GetPointerUnchecked(address) = data;
133
} else if ((address & 0xBFFFC000) == 0x00010000) {
134
// Scratchpad
135
*(T*)GetPointerUnchecked(address) = data;
136
} else if ((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize) {
137
// More RAM (remasters, etc.)
138
*(T*)GetPointerUnchecked(address) = data;
139
} else {
140
static bool reported = false;
141
if (!reported) {
142
Reporting::ReportMessage("WriteToHardware: Invalid address %08x near PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]);
143
reported = true;
144
}
145
Core_MemoryException(address, sizeof(T), currentMIPS->pc, MemoryExceptionType::WRITE_WORD);
146
}
147
}
148
149
bool IsRAMAddress(const u32 address) {
150
if ((address & 0x3E000000) == 0x08000000) {
151
return true;
152
} else if ((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize) {
153
return true;
154
} else {
155
return false;
156
}
157
}
158
159
bool IsScratchpadAddress(const u32 address) {
160
return (address & 0xBFFFC000) == 0x00010000;
161
}
162
163
u8 Read_U8(const u32 address) {
164
u8 value = 0;
165
ReadFromHardware<u8>(value, address);
166
return (u8)value;
167
}
168
169
u16 Read_U16(const u32 address) {
170
u16_le value = 0;
171
ReadFromHardware<u16_le>(value, address);
172
return (u16)value;
173
}
174
175
u32 Read_U32(const u32 address) {
176
u32_le value = 0;
177
ReadFromHardware<u32_le>(value, address);
178
return value;
179
}
180
181
u64 Read_U64(const u32 address) {
182
u64_le value = 0;
183
ReadFromHardware<u64_le>(value, address);
184
return value;
185
}
186
187
u32 Read_U8_ZX(const u32 address) {
188
return (u32)Read_U8(address);
189
}
190
191
u32 Read_U16_ZX(const u32 address) {
192
return (u32)Read_U16(address);
193
}
194
195
void Write_U8(const u8 _Data, const u32 address) {
196
WriteToHardware<u8>(address, _Data);
197
}
198
199
void Write_U16(const u16 _Data, const u32 address) {
200
WriteToHardware<u16_le>(address, _Data);
201
}
202
203
void Write_U32(const u32 _Data, const u32 address) {
204
WriteToHardware<u32_le>(address, _Data);
205
}
206
207
void Write_U64(const u64 _Data, const u32 address) {
208
WriteToHardware<u64_le>(address, _Data);
209
}
210
211
} // namespace Memory
212
213