forked from czajowaty/ad_resources_dumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirtualPsxRam.hpp
77 lines (70 loc) · 2.97 KB
/
VirtualPsxRam.hpp
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
#ifndef VIRTUALPSXRAM_HPP
#define VIRTUALPSXRAM_HPP
#include "PsxRamAddress.hpp"
#include "PsxRamConst.hpp"
#include <QString>
#include <QVector>
#include <array>
class VirtualPsxRam
{
using PsxRamBuffer = std::array<uint8_t, PsxRamConst::SIZE>;
public:
VirtualPsxRam();
void clear();
QVector<PsxRamAddress::Region> const& initializedRegions() const;
uint8_t readByte(PsxRamAddress address) const;
int8_t readSBbyte(PsxRamAddress address) const;
uint16_t readWord(PsxRamAddress address) const;
int16_t readSWord(PsxRamAddress address) const;
uint32_t readDWord(PsxRamAddress address) const;
int32_t readSDWord(PsxRamAddress address) const;
PsxRamAddress readAddress(PsxRamAddress address) const;
void readRegion(PsxRamAddress::Region const& region, uint8_t* buffer) const;
void writeByte(uint8_t byte, PsxRamAddress address);
void writeSByte(int8_t sbyte, PsxRamAddress address);
void writeWord(uint16_t word, PsxRamAddress address);
void writeSWord(int16_t sword, PsxRamAddress address);
void writeDWord(uint32_t dword, PsxRamAddress address);
void writeSDWord(int32_t sdword, PsxRamAddress address);
void writeAddress(
PsxRamAddress addressToWrite,
PsxRamAddress addressToWriteTo);
void load(QByteArray const& data, PsxRamAddress address);
private:
constexpr std::size_t size() const
{ return ram_.size(); }
template <typename T>
T read(PsxRamAddress address) const
{
constexpr uint32_t const size = sizeof(T);
PsxRamAddress inPsxRamAddress = PsxRamConst::toNoSegRamAddress(
address);
if (!isInPsxRamRegion(inPsxRamAddress, size))
{ throwReadOutOfBoundsError(address, size); }
if (!isInitializedRegion({inPsxRamAddress, size}))
{ throwUnitializedReadError(inPsxRamAddress, size); }
return *reinterpret_cast<T const*>(inBufferPointer(inPsxRamAddress));
}
bool isInitializedRegion(PsxRamAddress::Region const& region) const;
template <typename T>
void write(T t, PsxRamAddress address)
{ write(reinterpret_cast<uint8_t const*>(t), sizeof(t), address); }
void write(
uint8_t const* bytes,
uint32_t bytesNumber,
PsxRamAddress address);
bool isInPsxRamRegion(PsxRamAddress address, uint32_t size) const;
uint8_t* inBufferPointer(PsxRamAddress address);
uint8_t const* inBufferPointer(PsxRamAddress address) const;
void initializeRegion(PsxRamAddress::Region const& region);
void throwReadOutOfBoundsError(PsxRamAddress address, uint32_t size) const;
void throwWriteOutOfBoundsError(PsxRamAddress address, uint32_t size) const;
void throwOutOfBoundsError(
char const* typeString,
PsxRamAddress address,
uint32_t size) const;
void throwUnitializedReadError(PsxRamAddress address, uint32_t size) const;
PsxRamBuffer ram_;
QVector<PsxRamAddress::Region> initializedRegions_;
};
#endif // VIRTUALPSXRAM_HPP