forked from balika011/prxtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirtualMem.C
221 lines (190 loc) · 3.98 KB
/
VirtualMem.C
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/***************************************************************
* PRXTool : Utility for PSP executables.
* (c) TyRaNiD 2k5
*
* VirtualMem.C - An implementation of a class to virtualise
* a memory space.
***************************************************************/
#include <stdio.h>
#include <string.h>
#include "VirtualMem.h"
#include "output.h"
#define CHECK_ADDR(addr, size) ((check_addr(addr, size, m_iBaseAddr, m_iSize)) && (m_pData != NULL))
inline bool check_addr(u32 addr, u32 size, u32 baseaddr, u32 basesize)
{
if(addr >= baseaddr)
{
if((addr + size) < (baseaddr + basesize))
{
return true;
}
}
return false;
}
CVirtualMem::CVirtualMem()
{
m_pData = NULL;
m_iSize = 0;
m_iBaseAddr = 0;
m_endian = MEM_LITTLE_ENDIAN;
}
CVirtualMem::CVirtualMem(u8 *pData, u32 iSize, u32 iBaseAddr, MemEndian endian)
{
m_pData = pData;
m_iSize = iSize;
m_iBaseAddr = iBaseAddr;
m_endian = endian;
COutput::Printf(LEVEL_DEBUG, "pData %p, iSize %x, iBaseAddr 0x%08X, endian %d\n",
pData, iSize, iBaseAddr, endian);
}
CVirtualMem::~CVirtualMem()
{
/* Do nothing */
}
u8 CVirtualMem::GetU8(u32 iAddr)
{
if(CHECK_ADDR(iAddr, 1))
{
return m_pData[iAddr - m_iBaseAddr];
}
COutput::Printf(LEVEL_DEBUG, "Invalid memory address 0x%08X\n", iAddr);
return 0;
}
u16 CVirtualMem::GetU16(u32 iAddr)
{
if(CHECK_ADDR(iAddr, 2))
{
if(m_endian == MEM_LITTLE_ENDIAN)
{
return LH_LE(*((u16*) &m_pData[iAddr - m_iBaseAddr]));
}
else if(m_endian == MEM_BIG_ENDIAN)
{
return LH_BE(*((u16*) &m_pData[iAddr - m_iBaseAddr]));
}
else
{
COutput::Printf(LEVEL_DEBUG, "Invalid endian format\n");
}
}
else
{
COutput::Printf(LEVEL_DEBUG, "Invalid memory address 0x%08X\n", iAddr);
}
return 0;
}
u32 CVirtualMem::GetU32(u32 iAddr_2)
{
s32 iAddr = iAddr_2;
if(CHECK_ADDR(iAddr, 4))
{
if(m_endian == MEM_LITTLE_ENDIAN)
{
return LW_LE(*((u32*) &m_pData[iAddr - m_iBaseAddr]));
}
else if(m_endian == MEM_BIG_ENDIAN)
{
return LW_BE(*((u32*) &m_pData[iAddr - m_iBaseAddr]));
}
else
{
COutput::Printf(LEVEL_DEBUG, "Invalid endian format\n");
}
}
else
{
COutput::Printf(LEVEL_DEBUG, "Invalid memory address 0x%08X\n", iAddr);
}
return 0;
}
s8 CVirtualMem::GetS8(u32 iAddr)
{
if(CHECK_ADDR(iAddr, 1))
{
return m_pData[iAddr - m_iBaseAddr];
}
COutput::Printf(LEVEL_DEBUG, "Invalid memory address 0x%08X\n", iAddr);
return 0;
}
s16 CVirtualMem::GetS16(u32 iAddr)
{
if(CHECK_ADDR(iAddr, 2))
{
if(m_endian == MEM_LITTLE_ENDIAN)
{
return LH_LE(*((u16*) &m_pData[iAddr - m_iBaseAddr]));
}
else if(m_endian == MEM_BIG_ENDIAN)
{
return LH_BE(*((u16*) &m_pData[iAddr - m_iBaseAddr]));
}
else
{
COutput::Printf(LEVEL_DEBUG, "Invalid endian format\n");
}
}
else
{
COutput::Printf(LEVEL_DEBUG, "Invalid memory address 0x%08X\n", iAddr);
}
return 0;
}
s32 CVirtualMem::GetS32(u32 iAddr)
{
if(CHECK_ADDR(iAddr, 4))
{
if(m_endian == MEM_LITTLE_ENDIAN)
{
return LW_LE(*((u32*) &m_pData[iAddr - m_iBaseAddr]));
}
else if(m_endian == MEM_BIG_ENDIAN)
{
return LW_BE(*((u32*) &m_pData[iAddr - m_iBaseAddr]));
}
else
{
COutput::Printf(LEVEL_DEBUG, "Invalid endian format\n");
}
}
else
{
COutput::Printf(LEVEL_DEBUG, "Invalid memory address 0x%08X\n", iAddr);
}
return 0;
}
void *CVirtualMem::GetPtr(u32 iAddr)
{
if(CHECK_ADDR(iAddr, 1))
{
return &m_pData[iAddr - m_iBaseAddr];
}
else
{
COutput::Printf(LEVEL_DEBUG, "Ptr out of region 0x%08X\n", iAddr);
}
return NULL;
}
/* Get the amount of data available from this address */
u32 CVirtualMem::GetSize(u32 iAddr)
{
u32 iSizeLeft = 0;
/* Check we have at least 1 byte left */
if(CHECK_ADDR(iAddr, 1))
{
iSizeLeft = m_iSize - (iAddr - m_iBaseAddr);
}
return iSizeLeft;
}
u32 CVirtualMem::Copy(void *pDest, u32 iAddr, u32 iSize)
{
u32 iCopySize;
void *ptr;
iCopySize = GetSize(iAddr);
iCopySize = iCopySize > iSize ? iSize : iCopySize;
if(iCopySize > 0)
{
ptr = GetPtr(iAddr);
memcpy(pDest, ptr, iCopySize);
}
return iCopySize;
}