forked from czajowaty/ad_resources_dumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirtualPsxRam.cpp
183 lines (153 loc) · 5.61 KB
/
VirtualPsxRam.cpp
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
#include "VirtualPsxRam.hpp"
#include <algorithm>
#include <cstring>
VirtualPsxRam::VirtualPsxRam()
{ clear(); }
void VirtualPsxRam::clear()
{
ram_.fill(0);
initializedRegions_.clear();
}
QVector<PsxRamAddress::Region> const& VirtualPsxRam::initializedRegions() const
{ return initializedRegions_; }
uint8_t VirtualPsxRam::readByte(PsxRamAddress address) const
{ return read<uint8_t>(address); }
int8_t VirtualPsxRam::readSBbyte(PsxRamAddress address) const
{ return read<int8_t>(address); }
uint16_t VirtualPsxRam::readWord(PsxRamAddress address) const
{ return read<uint16_t>(address); }
int16_t VirtualPsxRam::readSWord(PsxRamAddress address) const
{ return read<int16_t>(address); }
uint32_t VirtualPsxRam::readDWord(PsxRamAddress address) const
{ return read<uint32_t>(address); }
int32_t VirtualPsxRam::readSDWord(PsxRamAddress address) const
{ return read<int32_t>(address); }
PsxRamAddress VirtualPsxRam::readAddress(PsxRamAddress address) const
{ return read<PsxRamAddress::Raw>(address); }
void VirtualPsxRam::readRegion(
PsxRamAddress::Region const& region,
uint8_t* buffer) const
{
PsxRamAddress inPsxRamAddress = PsxRamConst::toNoSegRamAddress(
region.address);
if (!isInPsxRamRegion(inPsxRamAddress, region.size))
{ throwReadOutOfBoundsError(region.address, region.size); }
if (!isInitializedRegion({inPsxRamAddress, region.size}))
{ throwUnitializedReadError(region.address, region.size); }
std::memcpy(buffer, inBufferPointer(inPsxRamAddress), region.size);
}
bool VirtualPsxRam::isInitializedRegion(
PsxRamAddress::Region const& region) const
{
for (auto const& initializedRegion : initializedRegions_)
{
if (initializedRegion.doesContain(region))
{ return true; }
}
return false;
}
void VirtualPsxRam::writeByte(uint8_t byte, PsxRamAddress address)
{ write(byte, address); }
void VirtualPsxRam::writeSByte(int8_t sbyte, PsxRamAddress address)
{ write(sbyte, address); }
void VirtualPsxRam::writeWord(uint16_t word, PsxRamAddress address)
{ write(word, address); }
void VirtualPsxRam::writeSWord(int16_t sword, PsxRamAddress address)
{ write(sword, address); }
void VirtualPsxRam::writeDWord(uint32_t dword, PsxRamAddress address)
{ write(dword, address); }
void VirtualPsxRam::writeSDWord(int32_t sdword, PsxRamAddress address)
{ write(sdword, address); }
void VirtualPsxRam::writeAddress(
PsxRamAddress addressToWrite,
PsxRamAddress addressToWriteTo)
{ write(PsxRamConst::toKSeg0RamAddress(addressToWrite), addressToWriteTo); }
void VirtualPsxRam::load(QByteArray const& data, PsxRamAddress address)
{
write(
reinterpret_cast<uint8_t const*>(data.constData()),
data.size(),
address);
}
void VirtualPsxRam::write(
uint8_t const* bytes,
uint32_t bytesNumber,
PsxRamAddress address)
{
PsxRamAddress inPsxRamAddress = PsxRamConst::toNoSegRamAddress(address);
if (!isInPsxRamRegion(inPsxRamAddress, bytesNumber))
{ throwWriteOutOfBoundsError(address, bytesNumber); }
std::memcpy(inBufferPointer(inPsxRamAddress), bytes, bytesNumber);
initializeRegion({inPsxRamAddress, bytesNumber});
}
bool VirtualPsxRam::isInPsxRamRegion(
PsxRamAddress address,
uint32_t size) const
{
if (size > 0)
{ address.addOffset(size - 1); }
return PsxRamConst::isInPsxRamAddress(address);
}
uint8_t* VirtualPsxRam::inBufferPointer(PsxRamAddress address)
{ return ram_.begin() + address.raw(); }
uint8_t const* VirtualPsxRam::inBufferPointer(PsxRamAddress address) const
{ return ram_.cbegin() + address.raw(); }
void VirtualPsxRam::initializeRegion(PsxRamAddress::Region const& region)
{
for (int i = 0; i < initializedRegions_.size(); ++i)
{
auto& initializedRegion = initializedRegions_[i];
if (initializedRegion.doesContain(region))
{ return; }
if (region.doesContain(initializedRegion))
{
initializedRegion = region;
return;
}
if (
initializedRegion.doesIntersect(region) ||
initializedRegion.isAdjacent(region))
{
auto startAddress =
initializedRegion.address <= region.address ?
initializedRegion.address :
region.address;
auto endAddress = initializedRegion.end() >= region.end() ?
initializedRegion.end() :
region.end();
uint32_t size =
endAddress > startAddress ?
endAddress - startAddress + 1 :
0;
initializedRegion = {startAddress, size};
return;
}
}
initializedRegions_.append(region);
}
void VirtualPsxRam::throwReadOutOfBoundsError(
PsxRamAddress address,
uint32_t size) const
{ throwOutOfBoundsError("Read", address, size); }
void VirtualPsxRam::throwWriteOutOfBoundsError(
PsxRamAddress address,
uint32_t size) const
{ throwOutOfBoundsError("Write", address, size); }
void VirtualPsxRam::throwOutOfBoundsError(
char const* typeString,
PsxRamAddress address,
uint32_t size) const
{
throw QString("%1 out of bounds error (address: 0x%2, size: 0x%3).")
.arg(typeString)
.arg(address.raw(), 0, 16)
.arg(size, 0, 16);
}
void VirtualPsxRam::throwUnitializedReadError(
PsxRamAddress address,
uint32_t size) const
{
throw QString("Read from uninitialized region (address: 0x%1, size: 0x%2).")
.arg(address.raw(), 0, 16)
.arg(size, 0, 16);
}