forked from zeromus/pj64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncCPU.cpp
625 lines (553 loc) · 18.6 KB
/
SyncCPU.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
* Project 64 - A Nintendo 64 emulator.
*
* (c) Copyright 2001 zilmar (zilmar@emulation64.com) and
* Jabo (jabo@emulation64.com).
*
* pj64 homepage: www.pj64.net
*
* Permission to use, copy, modify and distribute Project64 in both binary and
* source form, for non-commercial purposes, is hereby granted without fee,
* providing that this license information and copyright notice appear with
* all copies and any derived work.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event shall the authors be held liable for any damages
* arising from the use of this software.
*
* Project64 is freeware for PERSONAL USE only. Commercial users should
* seek permission of the copyright holders first. Commercial use includes
* charging money for Project64 or software derived from Project64.
*
* The copyright holders request that bug fixes and improvements to the code
* should be forwarded to them so if they want them.
*
*/
#include <Windows.h>
#include <stdio.h>
#include "main.h"
#include "cpu.h"
#include "debugger.h"
#include "plugin.h"
#define MaxMemory 2000
void StartErrorLog ( void );
void StopErrorLog ( void );
void WriteSyncMemoryLineDump ( char * Label, BYTE * Memory );
DWORD CurrentBlock, *TLB_SyncReadMap, *TLB_SyncWriteMap, * MemAddrUsed[2] = { NULL,NULL };
int SyncNextInstruction, SyncJumpToLocation;
HANDLE hErrorLogFile = NULL;
N64_REGISTERS SyncRegisters;
int MemAddrUsedCount[2];
BYTE * SyncMemory;
//TLB
FASTTLB SyncFastTlb[64];
TLB SyncTlb[32];
int Sync_MemoryFilter( DWORD dwExptCode, LPEXCEPTION_POINTERS lpEP);
void AllocateSyncMemory ( void ) {
DWORD * TempReadMap, *TempWriteMap;
BYTE * TempMemPtr;
FreeSyncMemory();
if(SyncMemory==NULL) {
DisplayError(GS(MSG_MEM_ALLOC_ERROR));
ExitThread(0);
}
if(VirtualAlloc(SyncMemory, RdramSize, MEM_COMMIT, PAGE_READWRITE)==NULL) {
DisplayError(GS(MSG_MEM_ALLOC_ERROR));
ExitThread(0);
}
if(VirtualAlloc(SyncMemory + 0x04000000, 0x2000, MEM_COMMIT, PAGE_READWRITE)==NULL) {
DisplayError(GS(MSG_MEM_ALLOC_ERROR));
ExitThread(0);
}
TLB_SyncReadMap = (DWORD *)VirtualAlloc(NULL,0xFFFFF * sizeof(DWORD),MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);
if (TLB_SyncReadMap == NULL) {
DisplayError(GS(MSG_MEM_ALLOC_ERROR));
ExitThread(0);
}
TLB_SyncWriteMap = (DWORD *)VirtualAlloc(NULL,0xFFFFF * sizeof(DWORD),MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);
if (TLB_SyncWriteMap == NULL) {
DisplayError(GS(MSG_MEM_ALLOC_ERROR));
ExitThread(0);
}
MemAddrUsed[0] = (DWORD *)malloc(MaxMemory * sizeof(DWORD *));
MemAddrUsed[1] = (DWORD *)malloc(MaxMemory * sizeof(DWORD *));
TempReadMap = TLB_ReadMap;
TLB_ReadMap = TLB_SyncReadMap;
TLB_SyncReadMap = TempReadMap;
TempWriteMap = TLB_WriteMap;
TLB_WriteMap = TLB_SyncWriteMap;
TLB_SyncWriteMap = TempWriteMap;
TempMemPtr = N64MEM;
N64MEM = SyncMemory;
RDRAM = (unsigned char *)(N64MEM);
DMEM = (unsigned char *)(N64MEM+0x04000000);
IMEM = (unsigned char *)(N64MEM+0x04001000);
SyncMemory = TempMemPtr;
InitilizeTLB();
memcpy(SyncFastTlb,FastTlb,sizeof(FastTlb));
memcpy(SyncTlb,tlb,sizeof(tlb));
}
void __cdecl Error_Message (char * Message, ...) {
DWORD dwWritten;
char Msg[400];
va_list ap;
if (hErrorLogFile == NULL) { StartErrorLog(); }
va_start( ap, Message );
vsprintf( Msg, Message, ap );
va_end( ap );
strcat(Msg,"\r\n");
WriteFile( hErrorLogFile,Msg,strlen(Msg),&dwWritten,NULL );
}
void FreeSyncMemory (void) {
if (TLB_SyncReadMap) {
VirtualFree( TLB_SyncReadMap, 0 , MEM_RELEASE);
TLB_SyncReadMap = NULL;
}
if (TLB_SyncWriteMap) {
VirtualFree( TLB_SyncWriteMap, 0 , MEM_RELEASE);
TLB_SyncWriteMap = NULL;
}
if (MemAddrUsed[0]) { free(MemAddrUsed[0]); }
MemAddrUsed[0] = NULL;
if (MemAddrUsed[1]) { free(MemAddrUsed[1]); }
MemAddrUsed[1] = NULL;
}
void ProtectMemory (void) {
DWORD OldProtect;
VirtualProtect(N64MEM,RdramSize,PAGE_READONLY,&OldProtect);
VirtualProtect(N64MEM + 0x04000000,0x2000,PAGE_READONLY,&OldProtect);
}
void ReInitializeRSP (void) {
RSP_INFO_1_0 RspInfo10;
RSP_INFO_1_1 RspInfo11;
RspInfo10.CheckInterrupts = CheckInterrupts;
RspInfo11.CheckInterrupts = CheckInterrupts;
RspInfo10.ProcessDlist = ProcessDList;
RspInfo11.ProcessDlist = ProcessDList;
RspInfo10.ProcessAlist = ProcessAList;
RspInfo11.ProcessAlist = ProcessAList;
RspInfo10.ProcessRdpList = ProcessRDPList;
RspInfo11.ProcessRdpList = ProcessRDPList;
RspInfo11.ShowCFB = ShowCFB;
RspInfo10.hInst = hInst;
RspInfo11.hInst = hInst;
RspInfo10.RDRAM = N64MEM;
RspInfo11.RDRAM = N64MEM;
RspInfo10.DMEM = DMEM;
RspInfo11.DMEM = DMEM;
RspInfo10.IMEM = IMEM;
RspInfo11.IMEM = IMEM;
RspInfo10.MemoryBswaped = FALSE;
RspInfo11.MemoryBswaped = FALSE;
RspInfo10.MI__INTR_REG = &MI_INTR_REG;
RspInfo11.MI__INTR_REG = &MI_INTR_REG;
RspInfo10.SP__MEM_ADDR_REG = &SP_MEM_ADDR_REG;
RspInfo11.SP__MEM_ADDR_REG = &SP_MEM_ADDR_REG;
RspInfo10.SP__DRAM_ADDR_REG = &SP_DRAM_ADDR_REG;
RspInfo11.SP__DRAM_ADDR_REG = &SP_DRAM_ADDR_REG;
RspInfo10.SP__RD_LEN_REG = &SP_RD_LEN_REG;
RspInfo11.SP__RD_LEN_REG = &SP_RD_LEN_REG;
RspInfo10.SP__WR_LEN_REG = &SP_WR_LEN_REG;
RspInfo11.SP__WR_LEN_REG = &SP_WR_LEN_REG;
RspInfo10.SP__STATUS_REG = &SP_STATUS_REG;
RspInfo11.SP__STATUS_REG = &SP_STATUS_REG;
RspInfo10.SP__DMA_FULL_REG = &SP_DMA_FULL_REG;
RspInfo11.SP__DMA_FULL_REG = &SP_DMA_FULL_REG;
RspInfo10.SP__DMA_BUSY_REG = &SP_DMA_BUSY_REG;
RspInfo11.SP__DMA_BUSY_REG = &SP_DMA_BUSY_REG;
RspInfo10.SP__PC_REG = &SP_PC_REG;
RspInfo11.SP__PC_REG = &SP_PC_REG;
RspInfo10.SP__SEMAPHORE_REG = &SP_SEMAPHORE_REG;
RspInfo11.SP__SEMAPHORE_REG = &SP_SEMAPHORE_REG;
RspInfo10.DPC__START_REG = &DPC_START_REG;
RspInfo11.DPC__START_REG = &DPC_START_REG;
RspInfo10.DPC__END_REG = &DPC_END_REG;
RspInfo11.DPC__END_REG = &DPC_END_REG;
RspInfo10.DPC__CURRENT_REG = &DPC_CURRENT_REG;
RspInfo11.DPC__CURRENT_REG = &DPC_CURRENT_REG;
RspInfo10.DPC__STATUS_REG = &DPC_STATUS_REG;
RspInfo11.DPC__STATUS_REG = &DPC_STATUS_REG;
RspInfo10.DPC__CLOCK_REG = &DPC_CLOCK_REG;
RspInfo11.DPC__CLOCK_REG = &DPC_CLOCK_REG;
RspInfo10.DPC__BUFBUSY_REG = &DPC_BUFBUSY_REG;
RspInfo11.DPC__BUFBUSY_REG = &DPC_BUFBUSY_REG;
RspInfo10.DPC__PIPEBUSY_REG = &DPC_PIPEBUSY_REG;
RspInfo11.DPC__PIPEBUSY_REG = &DPC_PIPEBUSY_REG;
RspInfo10.DPC__TMEM_REG = &DPC_TMEM_REG;
RspInfo11.DPC__TMEM_REG = &DPC_TMEM_REG;
if (RSPVersion == 0x0100) { InitiateRSP_1_0(RspInfo10, &RspTaskValue); }
if (RSPVersion == 0x0101) { InitiateRSP_1_1(RspInfo11, &RspTaskValue); }
}
void StartErrorLog (void) {
char path_buffer[_MAX_PATH], drive[_MAX_DRIVE] ,dir[_MAX_DIR];
char fname[_MAX_FNAME],ext[_MAX_EXT], LogFileName[_MAX_PATH];
GetModuleFileName(NULL,path_buffer,sizeof(path_buffer));
_splitpath( path_buffer, drive, dir, fname, ext );
_makepath( LogFileName, drive, dir, "CPU Error", "log" );
hErrorLogFile = CreateFile(LogFileName,GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
SetFilePointer(hErrorLogFile,0,NULL,FILE_BEGIN);
Error_Message("=== Error Has occured !?! ===");
Error_Message("Block Address: 0x%X",CurrentBlock);
if (PROGRAM_COUNTER != Registers.PROGRAM_COUNTER) {
Error_Message("interp PC: 0x%08X",PROGRAM_COUNTER);
Error_Message("Recomp PC: 0x%08X",Registers.PROGRAM_COUNTER);
} else {
Error_Message("PC: 0x%08X",PROGRAM_COUNTER);
}
Error_Message("");
}
void StartSyncCPU (void ) {
DWORD Addr;
BYTE * Block;
#ifdef Log_x86Code
Start_x86_Log();
#endif
if (GfxRomOpen != NULL) { GfxRomOpen(); }
if (ContRomOpen != NULL) { ContRomOpen(); }
ResetRecompCode();
AllocateSyncMemory();
Registers.PROGRAM_COUNTER = PROGRAM_COUNTER;
Registers.HI.DW = HI.DW;
Registers.LO.DW = LO.DW;
Registers.DMAUsed = DMAUsed;
memcpy(&SyncRegisters,&Registers,sizeof(Registers));
memcpy(N64MEM,SyncMemory,RdramSize);
memcpy(N64MEM + 0x04000000,SyncMemory + 0x04000000,0x2000);
ProtectMemory();
SyncNextInstruction = NORMAL;
SyncJumpToLocation = -1;
__try {
for (;;) {
Addr = PROGRAM_COUNTER;
if (UseTlb) {
if (!TranslateVaddr(&Addr)) {
DoTLBMiss(NextInstruction == DELAY_SLOT,PROGRAM_COUNTER);
NextInstruction = NORMAL;
Addr = PROGRAM_COUNTER;
if (!TranslateVaddr(&Addr)) {
DisplayError("Failed to tranlate PC to a PAddr: %X\n\nEmulation stopped",PROGRAM_COUNTER);
ExitThread(0);
}
}
} else {
Addr &= 0x1FFFFFFF;
}
if (NextInstruction == DELAY_SLOT) {
__try {
Block = (BYTE*)*(DelaySlotTable + (Addr >> 12));
} __except(EXCEPTION_EXECUTE_HANDLER) {
DisplayError("Executing Delay Slot from non maped space");
ExitThread(0);
}
if (Block == NULL) {
Block = CompileDelaySlot();
*(DelaySlotTable + (Addr >> 12)) = Block;
NextInstruction = NORMAL;
}
_asm {
pushad
call Block
popad
}
continue;
}
__try {
Block = (BYTE*)*(JumpTable + (Addr >> 2));
} __except(EXCEPTION_EXECUTE_HANDLER) {
DisplayError(GS(MSG_NONMAPPED_SPACE));
ExitThread(0);
}
if (Block == NULL) {
__try {
Block = Compiler4300iBlock();
} __except(EXCEPTION_EXECUTE_HANDLER) {
DisplayError("Reset Recompiler Code");
//ResetRecompCode();
//Block = Compiler4300iBlock();
}
*(JumpTable + (Addr >> 2)) = Block;
NextInstruction = NORMAL;
}
_asm {
pushad
call Block
popad
}
}
} __except( Sync_MemoryFilter( GetExceptionCode(), GetExceptionInformation()) ) {
DisplayError(GS(MSG_UNKNOWN_MEM_ACTION));
ExitThread(0);
}
}
void StopErrorLog (void) {
if (hErrorLogFile) {
CloseHandle(hErrorLogFile);
hErrorLogFile = NULL;
}
}
void SwitchSyncRegisters (void) {
FASTTLB TempFastTlb[64];
TLB Temptlb[32];
BYTE * TempMemPtr;
DWORD * Temp;
if (GPR == Registers.GPR) {
Registers.PROGRAM_COUNTER = PROGRAM_COUNTER;
Registers.HI.DW = HI.DW;
Registers.LO.DW = LO.DW;
Registers.DMAUsed = DMAUsed;
SetupRegisters(&SyncRegisters);
} else {
SyncRegisters.PROGRAM_COUNTER = PROGRAM_COUNTER;
SyncRegisters.HI.DW = HI.DW;
SyncRegisters.LO.DW = LO.DW;
SyncRegisters.DMAUsed = DMAUsed;
SetupRegisters(&Registers);
}
Temp = MemAddrUsed[1];
MemAddrUsed[1] = MemAddrUsed[0];
MemAddrUsed[0] = Temp;
MemAddrUsedCount[1] = MemAddrUsedCount[0];
MemAddrUsedCount[0] = 0;
memcpy(TempFastTlb,FastTlb,sizeof(FastTlb));
memcpy(FastTlb,SyncFastTlb,sizeof(FastTlb));
memcpy(SyncFastTlb,TempFastTlb,sizeof(FastTlb));
memcpy(Temptlb,tlb,sizeof(tlb));
memcpy(tlb,SyncTlb,sizeof(tlb));
memcpy(SyncTlb,Temptlb,sizeof(tlb));
Temp = TLB_ReadMap;
TLB_ReadMap = TLB_SyncReadMap;
TLB_SyncReadMap = Temp;
Temp = TLB_WriteMap;
TLB_WriteMap = TLB_SyncWriteMap;
TLB_SyncWriteMap = Temp;
TempMemPtr = N64MEM;
N64MEM = SyncMemory;
RDRAM = (unsigned char *)(N64MEM);
DMEM = (unsigned char *)(N64MEM+0x04000000);
IMEM = (unsigned char *)(N64MEM+0x04001000);
SyncMemory = TempMemPtr;
ReInitializeRSP();
ProtectMemory();
}
void SyncSystem (void) {
int count, i, error;
error = FALSE;
if (PROGRAM_COUNTER != Registers.PROGRAM_COUNTER) {
error = TRUE;
Error_Message("*** Program counter is not equal!!!");
Error_Message("");
}
//GPR
for (count = 0; count < 32; count ++) {
if (Registers.GPR[count].DW != SyncRegisters.GPR[count].DW) {
error = TRUE;
Error_Message("*** %s (GPR %d) is not equal!!!",GPR_Name[count],count);
Error_Message("interp value: 0x%08X%08X",SyncRegisters.GPR[count].UW[1],SyncRegisters.GPR[count].UW[0]);
Error_Message("Recomp value: 0x%08X%08X",Registers.GPR[count].UW[1],Registers.GPR[count].UW[0]);
Error_Message("");
}
}
//COP0
FixRandomReg();
for (count = 0; count < 33; count ++) {
if (Registers.CP0[count] != SyncRegisters.CP0[count]) {
error = TRUE;
Error_Message("*** %s (CP0 %d) is not equal!!!",count == 32?"Fake cause":Cop0_Name[count],count);
Error_Message("interp value: 0x%08X",SyncRegisters.CP0[count]);
Error_Message("Recomp value: 0x%08X",Registers.CP0[count]);
Error_Message("");
}
}
//FPU
for (count = 0; count < 32; count ++) {
if (Registers.FPR[count].DW != SyncRegisters.FPR[count].DW) {
error = TRUE;
Error_Message("*** %s (FPR %d) is not equal!!!",FPR_Name[count],count);
Error_Message("interp value: 0x%08X%08X",SyncRegisters.FPR[count].UW[1],SyncRegisters.FPR[count].UW[0]);
Error_Message("Recomp value: 0x%08X%08X",Registers.FPR[count].UW[1],Registers.FPR[count].UW[0]);
Error_Message("");
}
}
if (Registers.FPCR[31] != SyncRegisters.FPCR[31]) {
error = TRUE;
Error_Message("*** %s is not equal!!!",FPR_Ctrl_Name[31]);
Error_Message("interp value: 0x%08X",SyncRegisters.FPCR[31]);
Error_Message("Recomp value: 0x%08X",Registers.FPCR[31]);
Error_Message("");
}
//HI
if (Registers.HI.DW != HI.DW) {
error = TRUE;
Error_Message("*** HI register is not equal!!!");
Error_Message("interp value: 0x%08X",HI.DW);
Error_Message("Recomp value: 0x%08X",Registers.HI.DW);
Error_Message("");
}
//LO
if (Registers.LO.DW != LO.DW) {
error = TRUE;
Error_Message("*** LO register is not equal!!!");
Error_Message("interp value: 0x%08X",LO.DW);
Error_Message("Recomp value: 0x%08X",Registers.LO.DW);
Error_Message("");
}
//TLB
for (count = 0; count < 32; count ++) {
if (tlb[count].PageMask.Value != SyncTlb[count].PageMask.Value) {
error = TRUE;
Error_Message("*** tlb[%d].PageMask is not equal!!!",count);
Error_Message("interp value: 0x%08X",SyncTlb[count].PageMask.Value);
Error_Message("Recomp value: 0x%08X",tlb[count].PageMask.Value );
Error_Message("");
}
if (tlb[count].EntryHi.Value != SyncTlb[count].EntryHi.Value) {
error = TRUE;
Error_Message("*** tlb[%d].EntryHi is not equal!!!",count);
Error_Message("interp value: 0x%08X",SyncTlb[count].EntryHi.Value);
Error_Message("Recomp value: 0x%08X",tlb[count].EntryHi.Value );
Error_Message("");
}
if (tlb[count].EntryLo0.Value != SyncTlb[count].EntryLo0.Value) {
error = TRUE;
Error_Message("*** tlb[%d].PageMask is not equal!!!",count);
Error_Message("interp value: 0x%08X",SyncTlb[count].EntryLo0.Value);
Error_Message("Recomp value: 0x%08X",tlb[count].EntryLo0.Value );
Error_Message("");
}
if (tlb[count].EntryLo1.Value != SyncTlb[count].EntryLo1.Value) {
error = TRUE;
Error_Message("*** tlb[%d].PageMask is not equal!!!",count);
Error_Message("interp value: 0x%08X",SyncTlb[count].EntryLo1.Value);
Error_Message("Recomp value: 0x%08X",tlb[count].EntryLo1.Value );
Error_Message("");
}
}
//Mips Interface
for (count = 0; count < 4; count ++) {
if (Registers.MI[count] != SyncRegisters.MI[count]) {
error = TRUE;
Error_Message("*** MI %d is not equal!!!",count);
Error_Message("interp value: 0x%08X",SyncRegisters.MI[count]);
Error_Message("Recomp value: 0x%08X",Registers.MI[count]);
Error_Message("");
}
}
//PI Interface
for (count = 0; count < 12; count ++) {
if (Registers.PI[count] != SyncRegisters.PI[count]) {
error = TRUE;
Error_Message("*** PI %d is not equal!!!",count);
Error_Message("interp value: 0x%08X",SyncRegisters.PI[count]);
Error_Message("Recomp value: 0x%08X",Registers.PI[count]);
Error_Message("");
}
}
//Memory
for (count = 0; count < MemAddrUsedCount[0]; count ++) {
int count2;
for (count2 = 0; count2 < MemAddrUsedCount[1]; count2 ++) {
if (MemAddrUsed[0][count] == MemAddrUsed[1][count2]) {
MemAddrUsed[1][count2] = (DWORD)-1;
}
}
}
for (i = 0; i < 2; i++) {
for (count = 0; count < MemAddrUsedCount[i]; count ++) {
DWORD count2;
if (MemAddrUsed[i][count] == (DWORD)-1) { continue; }
if (memcmp(N64MEM + MemAddrUsed[i][count], SyncMemory + MemAddrUsed[i][count],0xFFF) == 0) {
continue;
}
error = TRUE;
for (count2 = MemAddrUsed[i][count]; count2 < (MemAddrUsed[i][count] + 0x1000); count2 += 0x10) {
if (memcmp(N64MEM + count2, SyncMemory + count2,0x10) == 0) { continue; }
Error_Message("*** Memory (Address: %X) is not equal!!!",count2);
WriteSyncMemoryLineDump("Interp",N64MEM + count2);
WriteSyncMemoryLineDump("Recomp",SyncMemory + count2);
Error_Message("");
}
}
}
if (error) {
SwitchSyncRegisters();
DisplayError("Sync Error has occured see log for details\n\nEmulation Has stoped");
ExitThread(0);
}
}
void SyncToPC (void) {
int RecNextInstruction, RecJumpToLocation;
//LogMessage("Recompiler (PC: %X count: %X)",PROGRAM_COUNTER,CP0[9]);
RecNextInstruction = NextInstruction;
RecJumpToLocation = JumpToLocation;
NextInstruction = SyncNextInstruction;
JumpToLocation = SyncJumpToLocation;
SwitchSyncRegisters();
/*if ((DWORD)RecompPos > 0x609844D8) {
while (PROGRAM_COUNTER != Registers.PROGRAM_COUNTER) {
ExecuteInterpreterOpCode();
}
} else {
while (CP0[9] != Registers.CP0[9]) {
ExecuteInterpreterOpCode();
}
}*/
while (CP0[9] != Registers.CP0[9]) {
ExecuteInterpreterOpCode();
}
SyncRegisters.MI[2] = Registers.MI[2]; //MI_INTR_REG
SyncRegisters.CP0[32] = Registers.CP0[32]; //FAKE_CAUSE_REGISTER
SyncSystem ();
SwitchSyncRegisters();
SyncNextInstruction = NextInstruction;
SyncJumpToLocation = JumpToLocation;
NextInstruction = RecNextInstruction;
JumpToLocation = RecJumpToLocation;
MemAddrUsedCount[0] = 0;
MemAddrUsedCount[1] = 0;
}
int Sync_MemoryFilter( DWORD dwExptCode, LPEXCEPTION_POINTERS lpEP) {
DWORD MemAddress = (char *)lpEP->ExceptionRecord->ExceptionInformation[1] - (char *)N64MEM;
EXCEPTION_RECORD exRec;
if (dwExptCode != EXCEPTION_ACCESS_VIOLATION) {
return EXCEPTION_CONTINUE_SEARCH;
}
exRec = *lpEP->ExceptionRecord;
if ((int)((char *)lpEP->ExceptionRecord->ExceptionInformation[1] - (char*)N64MEM) < 0) {
return EXCEPTION_CONTINUE_SEARCH;
}
if ((int)((char *)lpEP->ExceptionRecord->ExceptionInformation[1] - (char*)N64MEM) > 0x1FFFFFFF) {
return EXCEPTION_CONTINUE_SEARCH;
}
if (MemAddress < RdramSize || (MemAddress >= 0x4000000 && MemAddress < 0x4002000)) {
DWORD OldProtect;
VirtualProtect(N64MEM+(MemAddress & 0xFFFFF000),0xFFF,PAGE_READWRITE,&OldProtect);
MemAddrUsed[0][MemAddrUsedCount[0]] = (MemAddress & 0xFFFFF000);
MemAddrUsedCount[0] += 1;
if (MemAddrUsedCount[0] == MaxMemory) {
DisplayError("Used up all sync blocks ????");
ExitThread(0);
}
if (CPU_Type != ModCode_ProtectedMemory) { return EXCEPTION_CONTINUE_EXECUTION; }
}
return r4300i_CPU_MemoryFilter(dwExptCode,lpEP);
}
void WriteSyncMemoryLineDump (char * Label, BYTE * Memory) {
char Hex[100], Ascii[30];
DWORD count;
memset(&Hex,0,sizeof(Hex));
memset(&Ascii,0,sizeof(Ascii));
for (count = 0; count < 0x10; count ++ ) {
if ((count % 4) != 0 || count == 0) {
sprintf(Hex,"%s %02X",Hex,Memory[count]);
} else {
sprintf(Hex,"%s - %02X",Hex,Memory[count]);
}
if (Memory[count] < 30 || Memory[count] > 127) {
strcat(Ascii,".");
} else {
sprintf(Ascii,"%s%c",Ascii,Memory[count]);
}
}
Error_Message("%s:%s %s",Label,Hex,Ascii);
}