/[pcsx2_0.9.7]/trunk/pcsx2/Patch_Memory.cpp
ViewVC logotype

Contents of /trunk/pcsx2/Patch_Memory.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (9 years, 5 months ago) by william
File size: 12189 byte(s)
committing r3113 initial commit again...
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU Lesser General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with PCSX2.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #include "PrecompiledHeader.h"
17
18 #define _PC_ // disables MIPS opcode macros.
19
20 #include "IopCommon.h"
21 #include "Patch.h"
22
23 u32 SkipCount = 0, IterationCount = 0;
24 u32 IterationIncrement = 0, ValueIncrement = 0;
25 u32 PrevCheatType = 0, PrevCheatAddr = 0,LastType = 0;
26
27 void writeCheat()
28 {
29 switch (LastType)
30 {
31 case 0x0:
32 memWrite8(PrevCheatAddr,IterationIncrement & 0xFF);
33 break;
34 case 0x1:
35 memWrite16(PrevCheatAddr,IterationIncrement & 0xFFFF);
36 break;
37 case 0x2:
38 memWrite32(PrevCheatAddr,IterationIncrement);
39 break;
40 default:
41 break;
42 }
43 }
44
45 void handle_extended_t( IniPatch *p)
46 {
47 if (SkipCount > 0)
48 {
49 SkipCount--;
50 }
51 else switch (PrevCheatType)
52 {
53 case 0x3040: // vvvvvvvv 00000000 Inc
54 {
55 u32 mem = memRead32(PrevCheatAddr);
56 memWrite32(PrevCheatAddr, mem + (p->addr));
57 PrevCheatType = 0;
58 break;
59 }
60
61 case 0x3050: // vvvvvvvv 00000000 Dec
62 {
63 u32 mem = memRead32(PrevCheatAddr);
64 memWrite32(PrevCheatAddr, mem - (p->addr));
65 PrevCheatType = 0;
66 break;
67 }
68
69 case 0x4000: // vvvvvvvv iiiiiiii
70 for(u32 i = 0; i < IterationCount; i++)
71 {
72 memWrite32((u32)(PrevCheatAddr + (i * IterationIncrement)),(u32)(p->addr + ((u32)p->data * i)));
73 }
74 PrevCheatType = 0;
75 break;
76
77 case 0x5000: // dddddddd iiiiiiii
78 for(u32 i = 0; i < IterationCount; i++)
79 {
80 u8 mem = memRead8(PrevCheatAddr + i);
81 memWrite8(((u32)p->data) + i, mem);
82 }
83 PrevCheatType = 0;
84 break;
85
86 case 0x6000: // 000Xnnnn iiiiiiii
87 if (IterationIncrement == 0x0)
88 {
89 //LastType = ((u32)p->addr & 0x000F0000) >> 16;
90 u32 mem = memRead32(PrevCheatAddr);
91 if ((u32)p->addr < 0x100)
92 {
93 LastType = 0x0;
94 PrevCheatAddr = mem + ((u32)p->addr);
95 }
96 else if ((u32)p->addr < 0x1000)
97 {
98 LastType = 0x1;
99 PrevCheatAddr = mem + ((u32)p->addr * 2);
100 }
101 else
102 {
103 LastType = 0x2;
104 PrevCheatAddr = mem + ((u32)p->addr * 4);
105 }
106
107 // Check if needed to read another pointer
108 PrevCheatType = 0;
109 if (((mem & 0x0FFFFFFF) & 0x3FFFFFFC) != 0)
110 {
111 switch(LastType)
112 {
113 case 0x0:
114 memWrite8(PrevCheatAddr,(u8)p->data & 0xFF);
115 break;
116 case 0x1:
117 memWrite16(PrevCheatAddr,(u16)p->data & 0x0FFFF);
118 break;
119 case 0x2:
120 memWrite32(PrevCheatAddr,(u32)p->data);
121 break;
122 default:
123 break;
124 }
125 }
126 }
127 else
128 {
129 // Get Number of pointers
130 if (((u32)p->addr & 0x0000FFFF) == 0)
131 IterationCount = 1;
132 else
133 IterationCount = (u32)p->addr & 0x0000FFFF;
134
135 // Read first pointer
136 LastType = ((u32)p->addr & 0x000F0000) >> 16;
137 u32 mem = memRead32(PrevCheatAddr);
138
139 PrevCheatAddr = mem + (u32)p->data;
140 IterationCount--;
141
142 // Check if needed to read another pointer
143 if (IterationCount == 0)
144 {
145 PrevCheatType = 0;
146 if (((mem & 0x0FFFFFFF) & 0x3FFFFFFC) != 0) writeCheat();
147 }
148 else
149 {
150 if (((mem & 0x0FFFFFFF) & 0x3FFFFFFC) != 0)
151 PrevCheatType = 0;
152 else
153 PrevCheatType = 0x6001;
154 }
155 }
156 break;
157
158 case 0x6001: // 000Xnnnn iiiiiiii
159 {
160 // Read first pointer
161 u32 mem = memRead32(PrevCheatAddr & 0x0FFFFFFF);
162
163 PrevCheatAddr = mem + (u32)p->addr;
164 IterationCount--;
165
166 // Check if needed to read another pointer
167 if (IterationCount == 0)
168 {
169 PrevCheatType = 0;
170 if (((mem & 0x0FFFFFFF) & 0x3FFFFFFC) != 0) writeCheat();
171 }
172 else
173 {
174 mem = memRead32(PrevCheatAddr);
175
176 PrevCheatAddr = mem + (u32)p->data;
177 IterationCount--;
178 if (IterationCount == 0)
179 {
180 PrevCheatType = 0;
181 if (((mem & 0x0FFFFFFF) & 0x3FFFFFFC) != 0) writeCheat();
182 }
183 }
184 }
185 break;
186
187 default:
188 if ((p->addr & 0xF0000000) == 0x00000000) // 0aaaaaaa 0000000vv
189 {
190 memWrite8(p->addr & 0x0FFFFFFF, (u8)p->data & 0x000000FF);
191 PrevCheatType = 0;
192 }
193 else if ((p->addr & 0xF0000000) == 0x10000000) // 0aaaaaaa 0000vvvv
194 {
195 memWrite16(p->addr & 0x0FFFFFFF, (u16)p->data & 0x0000FFFF);
196 PrevCheatType = 0;
197 }
198 else if ((p->addr & 0xF0000000) == 0x20000000) // 0aaaaaaa vvvvvvvv
199 {
200 memWrite32(p->addr & 0x0FFFFFFF, (u32)p->data);
201 PrevCheatType = 0;
202 }
203 else if ((p->addr & 0xFFFF0000) == 0x30000000) // 300000vv 0aaaaaaa Inc
204 {
205 u8 mem = memRead8((u32)p->data);
206 memWrite8((u32)p->data, mem + (p->addr & 0x000000FF));
207 PrevCheatType = 0;
208 }
209 else if ((p->addr & 0xFFFF0000) == 0x30100000) // 301000vv 0aaaaaaa Dec
210 {
211 u8 mem = memRead8((u32)p->data);
212 memWrite8((u32)p->data, mem - (p->addr & 0x000000FF));
213 PrevCheatType = 0;
214 }
215 else if ((p->addr & 0xFFFF0000) == 0x30200000) // 3020vvvv 0aaaaaaa Inc
216 {
217 u16 mem = memRead16((u32)p->data);
218 memWrite16((u32)p->data, mem + (p->addr & 0x0000FFFF));
219 PrevCheatType = 0;
220 }
221 else if ((p->addr & 0xFFFF0000) == 0x30300000) // 3030vvvv 0aaaaaaa Dec
222 {
223 u16 mem = memRead16((u32)p->data);
224 memWrite16((u32)p->data, mem - (p->addr & 0x0000FFFF));
225 PrevCheatType = 0;
226 }
227 else if ((p->addr & 0xFFFF0000) == 0x30400000) // 30400000 0aaaaaaa Inc + Another line
228 {
229 PrevCheatType = 0x3040;
230 PrevCheatAddr = (u32)p->data;
231 }
232 else if ((p->addr & 0xFFFF0000) == 0x30500000) // 30500000 0aaaaaaa Inc + Another line
233 {
234 PrevCheatType = 0x3050;
235 PrevCheatAddr = (u32)p->data;
236 }
237 else if ((p->addr & 0xF0000000) == 0x40000000) // 4aaaaaaa nnnnssss + Another line
238 {
239 IterationCount = ((u32)p->data & 0xFFFF0000) / 0x10000;
240 IterationIncrement = ((u32)p->data & 0x0000FFFF) * 4;
241 PrevCheatAddr = (u32)p->addr & 0x0FFFFFFF;
242 PrevCheatType = 0x4000;
243 }
244 else if ((p->addr & 0xF0000000) == 0x50000000) // 5sssssss nnnnnnnn + Another line
245 {
246 PrevCheatAddr = (u32)p->addr & 0x0FFFFFFF;
247 IterationCount = ((u32)p->data);
248 PrevCheatType = 0x5000;
249 }
250 else if ((p->addr & 0xF0000000) == 0x60000000) // 6aaaaaaa 000000vv + Another line/s
251 {
252 PrevCheatAddr = (u32)p->addr & 0x0FFFFFFF;
253 IterationIncrement = ((u32)p->data);
254 IterationCount = 0;
255 PrevCheatType = 0x6000;
256 }
257 else if ((p->addr & 0xF0000000) == 0x70000000)
258 {
259 if ((p->data & 0x00F00000) == 0x00000000) // 7aaaaaaa 000000vv
260 {
261 u8 mem = memRead8((u32)p->addr & 0x0FFFFFFF);
262 memWrite8((u32)p->addr & 0x0FFFFFFF,(u8)(mem | (p->data & 0x000000FF)));
263 }
264 else if ((p->data & 0x00F00000) == 0x00100000) // 7aaaaaaa 0010vvvv
265 {
266 u16 mem = memRead16((u32)p->addr & 0x0FFFFFFF);
267 memWrite16((u32)p->addr & 0x0FFFFFFF,(u16)(mem | (p->data & 0x0000FFFF)));
268 }
269 else if ((p->data & 0x00F00000) == 0x00200000) // 7aaaaaaa 002000vv
270 {
271 u8 mem = memRead8((u32)p->addr&0x0FFFFFFF);
272 memWrite8((u32)p->addr & 0x0FFFFFFF,(u8)(mem & (p->data & 0x000000FF)));
273 }
274 else if ((p->data & 0x00F00000) == 0x00300000) // 7aaaaaaa 0030vvvv
275 {
276 u16 mem = memRead16((u32)p->addr & 0x0FFFFFFF);
277 memWrite16((u32)p->addr & 0x0FFFFFFF,(u16)(mem & (p->data & 0x0000FFFF)));
278 }
279 else if ((p->data & 0x00F00000) == 0x00400000) // 7aaaaaaa 004000vv
280 {
281 u8 mem = memRead8((u32)p->addr & 0x0FFFFFFF);
282 memWrite8((u32)p->addr & 0x0FFFFFFF,(u8)(mem ^ (p->data & 0x000000FF)));
283 }
284 else if ((p->data & 0x00F00000) == 0x00500000) // 7aaaaaaa 0050vvvv
285 {
286 u16 mem = memRead16((u32)p->addr & 0x0FFFFFFF);
287 memWrite16((u32)p->addr & 0x0FFFFFFF,(u16)(mem ^ (p->data & 0x0000FFFF)));
288 }
289 }
290 else if (p->addr < 0xE0000000)
291 {
292 if ((((u32)p->data & 0xFFFF0000) == 0x00000000) ||
293 (((u32)p->data & 0xFFFF0000) == 0x00100000) ||
294 (((u32)p->data & 0xFFFF0000) == 0x00200000) ||
295 (((u32)p->data & 0xFFFF0000) == 0x00300000))
296 {
297 u16 mem = memRead16((u32)p->addr & 0x0FFFFFFF);
298 if (mem != (0x0000FFFF & (u32)p->data)) SkipCount = 1;
299 PrevCheatType = 0;
300 }
301 }
302 else if (p->addr < 0xF0000000)
303 {
304 if (((u32)p->data & 0xC0000000) == 0x00000000)
305 if ((((u32)p->data & 0xF0000000) == 0x00000000) ||
306 (((u32)p->data & 0xF0000000) == 0x10000000) ||
307 (((u32)p->data & 0xF0000000) == 0x20000000) ||
308 (((u32)p->data & 0xF0000000) == 0x30000000))
309 {
310 u16 mem = memRead16((u32)p->data & 0x0FFFFFFF);
311 if (mem != (0x0000FFFF & (u32)p->addr)) SkipCount = ((u32)p->addr & 0xFFF0000) / 0x10000;
312 PrevCheatType = 0;
313 }
314 }
315 }
316 }
317
318 void _ApplyPatch(IniPatch *p)
319 {
320 if (p->enabled == 0) return;
321
322 switch (p->cpu)
323 {
324 case CPU_EE:
325 switch (p->type)
326 {
327 case BYTE_T:
328 if (memRead8(p->addr) != (u8)p->data)
329 memWrite8(p->addr, (u8)p->data);
330 break;
331
332 case SHORT_T:
333 if (memRead16(p->addr) != (u16)p->data)
334 memWrite16(p->addr, (u16)p->data);
335 break;
336
337 case WORD_T:
338 if (memRead32(p->addr) != (u32)p->data)
339 memWrite32(p->addr, (u32)p->data);
340 break;
341
342 case DOUBLE_T:
343 u64 mem;
344 memRead64(p->addr, &mem);
345 if (mem != p->data)
346 memWrite64(p->addr, &p->data);
347 break;
348
349 case EXTENDED_T:
350 handle_extended_t(p);
351 break;
352
353 default:
354 break;
355 }
356 break;
357
358 case CPU_IOP:
359 switch (p->type)
360 {
361 case BYTE_T:
362 if (iopMemRead8(p->addr) != (u8)p->data)
363 iopMemWrite8(p->addr, (u8)p->data);
364 break;
365 case SHORT_T:
366 if (iopMemRead16(p->addr) != (u16)p->data)
367 iopMemWrite16(p->addr, (u16)p->data);
368 break;
369 case WORD_T:
370 if (iopMemRead32(p->addr) != (u32)p->data)
371 iopMemWrite32(p->addr, (u32)p->data);
372 break;
373 default:
374 break;
375 }
376 break;
377
378 default:
379 break;
380 }
381 }

  ViewVC Help
Powered by ViewVC 1.1.22