-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.c
1412 lines (1251 loc) · 44 KB
/
compiler.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
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*$T compiler.c GC 1.136 03/09/02 17:28:30 */
/*$6
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Compiles blocks of code to native x86 code for speed. A block is terminated by a MIPS® jump, branch, or ERET
instruction. Blocks can be "linked" if they reside in the same 4KB page. Linking blocks avoids the need for a block
of code to return to the compiler to fetch the next block when it is done executing. This is achieved by setting a
jump target at the end of the block to the head of the next destination block. When the destination block's start
address becomes known, the jump target is filled with that address. With the help of Protected Memory, we know when
these blocks need to be invalidated. (when there is a store opcode, or dma write, etc)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/*
* 1964 Copyright (C) 1999-2002 Joel Middendorf, <schibo@emulation64.com> This
* program is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version. This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. To contact the
* authors: email: schibo@emulation64.com, rice1964@yahoo.com
*/
#include <windows.h>
#include "debug_option.h"
#include "dynarec/dynarec.h"
#include "hle.h"
#include "emulator.h"
#include "r4300i.h"
#include "timer.h"
#include "memory.h"
#include "1964ini.h"
#include "interrupt.h"
#include "dynarec/regcache.h"
#include "dynarec/x86.h"
#include "dynarec/dynaLog.h"
#include "compiler.h"
#include "dynarec/dynacpu.h"
#include "win32/wingui.h"
#ifdef DEBUG_COMMON
#include "win32/windebug.h"
extern char *DebugPrintInstruction(uint32 instruction);
#endif
extern char *DebugPrintInstructionWithOutRefresh(uint32 Instruction);
extern char *DebugPrintInstr(uint32 Instruction);
extern uint32 TLB_Error_Vector;
uint8 *dyna_CodeTable = NULL;
uint8 *dyna_RecompCode = NULL;
uint8 *sDYN_PC_LOOKUP[0x10000];
uint8 *Block;
uint8 *RDRAM_Copy;
struct CompilerStatus compilerstatus;
extern uint32 *g_LookupPtr; /* This global will be set at returning from a block */
extern uint32 g_pc_is_rdram; /* This global will be set at returning from a block */
void Set_Translate_PC(void);
void Interrupts(uint32 JumpType, uint32 targetpc, uint32 DoLink, uint32 LinkVal);
void DisplayLinkPC(void);
void DisplayPC(void);
void RefreshDynaDuringGamePlay(void);
void AnalyzeBlock(void);
void Dyna_Code_Check_None(void);
void Dyna_Code_Check_QWORD(void);
void Dyna_Code_Check_DWORD(void);
void Dyna_Code_Check_BLOCK(void);
void Dyna_Code_Check_None_Boot(void);
void (*Dyna_Code_Check[]) () =
{
Dyna_Code_Check_None,
Dyna_Code_Check_None,
Dyna_Code_Check_DWORD,
Dyna_Code_Check_QWORD,
Dyna_Code_Check_QWORD,
Dyna_Code_Check_BLOCK,
Dyna_Code_Check_BLOCK,
Dyna_Code_Check_None
};
void (*Dyna_Check_Codes) () = NULL;
BLOCK_ENTRY *block_queue_head = NULL;
BLOCK_ENTRY *current_block_entry = NULL;
void dequeue_heading_block_entry(void);
BLOCK_ENTRY *get_new_block_entry(uint32 pc);
BOOL IsBlockCompiled(uint32 pc);
BLOCK_ENTRY *add_new_block_entry(uint32 pc);
uint32 GetCompiledBlockPtr(uint32 pc);
/*
=======================================================================================================================
Returns the 32bit MIPS instruction at the current address in PC (Program Counter register)
=======================================================================================================================
*/
__forceinline uint32 DynaFetchInstruction(uint32 pc)
{
/*~~~~~~~~~~~~~*/
uint32 code = 0;
/*~~~~~~~~~~~~~*/
compilerstatus.realpc_fetched = pc;
if(NOT_IN_KO_K1_SEG(compilerstatus.realpc_fetched))
{
compilerstatus.realpc_fetched = TranslateITLBAddress(compilerstatus.realpc_fetched);
if(ITLB_Error)
{
return code;
}
}
__try
{
compilerstatus.pcptr = pLOAD_UWORD_PARAM(compilerstatus.realpc_fetched);
code = *compilerstatus.pcptr;
if((compilerstatus.realpc_fetched & 0x1FFFFFFF) < current_rdram_size)
{
if(currentromoptions.Code_Check == CODE_CHECK_PROTECT_MEMORY)
{
ProtectBlock(compilerstatus.realpc_fetched);
}
* (uint32 *) &RDRAM_Copy[compilerstatus.realpc_fetched & 0x1FFFFFFF] = code;
if(currentromoptions.Link_4KB_Blocks != USE4KBLINKBLOCK_YES)
{
if(sDYN_PC_LOOKUP[compilerstatus.realpc_fetched >> 16] == gMemoryState.dummyAllZero)
UnmappedMemoryExceptionHelper(compilerstatus.realpc_fetched);
*(uint32 *)
(
(uint8 *) sDYN_PC_LOOKUP[compilerstatus.realpc_fetched >> 16] +
(uint16) compilerstatus.realpc_fetched
) = 0;
}
}
}
__except(NULL, EXCEPTION_EXECUTE_HANDLER)
{
DisplayError("%08X: Dyna PC out of range", pc);
}
return code;
}
/*
=======================================================================================================================
=======================================================================================================================
*/
unsigned __int32 DynaFetchInstruction2(uint32 pc)
{
/*~~~~~~~~~~~~~~~~*/
uint32 code = 0;
uint32 savepc = pc;
/*~~~~~~~~~~~~~~~~*/
compilerstatus.realpc_fetched = savepc;
if(NOT_IN_KO_K1_SEG(compilerstatus.realpc_fetched))
{
ITLB_Error = FALSE;
compilerstatus.realpc_fetched = TranslateITLBAddress(compilerstatus.realpc_fetched);
if(ITLB_Error)
{
DisplayError("Warning, ITLB error happens during Dyna instruction fetch, and this is not the beginning of the block.");
TRACE1("ITLB error happens when fetching branch delay slot opcode, pc=%08X", compilerstatus.realpc_fetched);
HandleExceptions(TLB_Error_Vector);
ITLB_Error = FALSE;
compilerstatus.realpc_fetched = savepc;
compilerstatus.realpc_fetched = TranslateITLBAddress(compilerstatus.realpc_fetched);
if(ITLB_Error)
{
DisplayError("Warning, ITLB error happens during Dyna instruction fetch, and this is not the beginning of the block");
TRACE1
(
"Warning, ITLB error happens when fetching branch delay slot opcode the 2nd time, pc=%08X",
compilerstatus.realpc_fetched
);
HandleExceptions(TLB_Error_Vector);
ITLB_Error = FALSE;
compilerstatus.realpc_fetched = savepc;
compilerstatus.realpc_fetched = TranslateITLBAddress(compilerstatus.realpc_fetched);
if(ITLB_Error)
{
TRACE1
(
"Warning, ITLB error happens when fetching branch delay slot opcode the 3rd time, pc=%08X",
compilerstatus.realpc_fetched
);
DisplayError("Cannot solve ITLB exception");
compilerstatus.realpc_fetched = savepc - 4;
compilerstatus.realpc_fetched = TranslateITLBAddress(compilerstatus.realpc_fetched);
compilerstatus.realpc_fetched += 4;
ITLB_Error = FALSE;
goto step2;
}
}
}
ITLB_Error = FALSE;
}
step2:
__try
{
code = LOAD_UWORD_PARAM(compilerstatus.realpc_fetched);
if((compilerstatus.realpc_fetched & 0x1FFFFFFF) < current_rdram_size)
{
if(currentromoptions.Code_Check == CODE_CHECK_PROTECT_MEMORY)
{
ProtectBlock(compilerstatus.realpc_fetched);
}
* (uint32 *) &RDRAM_Copy[compilerstatus.realpc_fetched & 0x1FFFFFFF] = code;
if(currentromoptions.Link_4KB_Blocks != USE4KBLINKBLOCK_YES)
{
if(sDYN_PC_LOOKUP[compilerstatus.realpc_fetched >> 16] == gMemoryState.dummyAllZero)
UnmappedMemoryExceptionHelper(compilerstatus.realpc_fetched);
*(uint32 *)
(
(uint8 *) sDYN_PC_LOOKUP[compilerstatus.realpc_fetched >> 16] +
(uint16) compilerstatus.realpc_fetched
) = 0;
}
}
}
__except(NULL, EXCEPTION_EXECUTE_HANDLER)
{
DisplayError("%08X: Dyna PC out of range", pc);
}
return code;
}
/*
=======================================================================================================================
Compiles a block of native x86 machine code. Compilation of a block ends at a MIPS® jump, branch, or eret
instruction.
=======================================================================================================================
*/
uint32 Dyna_Compile_Single_Block(void)
{
/*~~~~~~~~~~~~~~~~~~~~~~*/
uint32 *LookupPtr;
int templCodePosition;
int k; /* variable for the instruction reordering */
/*~~~~~~~~~~~~~~~~~~~~~~*/
compilerstatus.Is_Compiling++;
begin:
LOGGING_DYNA(LogDyna("\n\n** Compile Single Block at PC=%08X\n", gHWS_pc));
if(currentromoptions.Advanced_Block_Analysis == USEBLOCKANALYSIS_YES)
{
AnalyzeBlock();
}
if(ITLB_Error)
{
/*
* DisplayError("TLB error happens during compiling, PC=%08X",
* compilerstatus.TempPC);
*/
if((gHWS_COP0Reg[STATUS] & EXL) == 0) /* Exception not in exception */
{
gHWS_COP0Reg[EPC] = gHWS_pc;
gHWS_COP0Reg[STATUS] |= EXL; /* set EXL = 1 */
}
else
{
gHWS_COP0Reg[EPC] = gHWS_pc;
/* DisplayError("Warning, Exception happens in exception"); */
TRACE1("Warning, Exception happens in exception, pc=%08X", compilerstatus.TempPC);
}
/*
* TLB_TRACE(TRACE2("ITLB exception at Instruction fetching, PC=%08X,
* Vector=%08X", gHWS_pc, TLB_Error_Vector));
*/
gHWS_pc = TLB_Error_Vector;
Set_Translate_PC();
gHWS_COP0Reg[CAUSE] &= NOT_BD; /* clear BD */
Block = (uint8 *) *g_LookupPtr;
ITLB_Error = FALSE;
if(Block != NULL && g_pc_is_rdram) Dyna_Check_Codes();
if(Block == NULL)
{
goto start_compile;
}
else
{
compilerstatus.Is_Compiling--;
return(uint32) Block;
}
}
start_compile:
k = 0;
if(compilerstatus.Is_Compiling > 1) DisplayError("Compiler is re-entered, cannot support it.");
templCodePosition = compilerstatus.lCodePosition;
gMultiPass.WriteCode = 0;
gMultiPass.WhichPass = COMPILE_MAP_ONLY;
ThisYear = 2001;
ITLB_Error = FALSE; /* this is moved to here. */
compilerstatus.TempPC = gHWS_pc;
compilerstatus.realpc_fetched = gHWS_pc;
gMultiPass.PhysAddrAfterMap = compilerstatus.BlockStart;
if(gMultiPass.UseOnePassOnly == 1)
{
gMultiPass.WriteCode = 1;
gMultiPass.WhichPass = COMPILE_ALL;
}
compilerstatus.KEEP_RECOMPILING = 1;
compilerstatus.cp0Counter = 0;
compilerstatus.InstructionCount = 0;
/* align block to qword */
while((compilerstatus.lCodePosition & 0xffffff80) != compilerstatus.lCodePosition) WC8(0x90);
if((compilerstatus.lCodePosition - templCodePosition) >= 2) compilerstatus.lCodePosition -= 2;
compilerstatus.lCodePosition += 2; /* increase the compilerstatus.lCodePosition by 2 leave two bytes in front of
* the block */
/* to store block size */
compilerstatus.BlockStart = (uint32) (&dyna_RecompCode[compilerstatus.lCodePosition]);
/* get instruction */
gHWS_code = DynaFetchInstruction(gHWS_pc + (Instruction_Order[k++] << 2));
if(ITLB_Error)
{
/*
* DisplayError("TLB error happens during compiling, PC=%08X",
* compilerstatus.TempPC);
*/
if((gHWS_COP0Reg[STATUS] & EXL) == 0) /* Exception not in exception */
{
gHWS_COP0Reg[EPC] = compilerstatus.TempPC;
gHWS_COP0Reg[STATUS] |= EXL; /* set EXL = 1 */
}
else
{
DisplayError("Warning, Exception happens in exception");
TRACE1("Warning, Exception happens in exception, pc=%08X", compilerstatus.TempPC);
}
gHWS_pc = TLB_Error_Vector;
/*
* TLB_TRACE(TRACE2("ITLB exception at Instruction fetching, PC=%08X,
* Vector=%08X", compilerstatus.TempPC, TLB_Error_Vector));
*/
Set_Translate_PC();
gHWS_COP0Reg[CAUSE] &= NOT_BD; /* clear BD */
Block = (uint8 *) *g_LookupPtr;
ITLB_Error = FALSE;
if(Block != NULL && g_pc_is_rdram) Dyna_Check_Codes();
if(Block == NULL)
{
goto begin; /* redo_compile; */
}
else
{
compilerstatus.Is_Compiling--;
return(uint32) Block;
}
}
else
{
if(sDYN_PC_LOOKUP[compilerstatus.realpc_fetched >> 16] == gMemoryState.dummyAllZero)
{
UnmappedMemoryExceptionHelper(compilerstatus.realpc_fetched);
}
LookupPtr = (uint32 *) ((uint8 *) sDYN_PC_LOOKUP[compilerstatus.realpc_fetched >> 16] + (uint16) compilerstatus.realpc_fetched);
Block = (uint8 *) compilerstatus.BlockStart;
*(uint16 *) (Block - 2) = 0; /* store block size */
}
if(currentromoptions.Use_Register_Caching == USEREGC_NO) FlushAllRegisters();
DYNA_DEBUG_INSTRUCTION(gHWS_code);
DYNA_LOG_INSTRUCTION(gHWS_code);
#ifdef DEBUG_COMMON
MOV_ImmToMemory(1, ModRM_disp32, (unsigned long) &gHWS_pc, gHWS_pc);
#endif
/*
* Rice: Right now, i'm only doing some HLE for Mario(US), and i know the exact
* address £
* of the functions. This needs to be replaced with a call to a crc detection
* algortithm. £
* Then HLE will be stable for other games. HLE default is "no" at the moment, and
* user-disabled.
*/
gHWS_code = DynaFetchInstruction(gHWS_pc);
if(currentromoptions.Use_HLE == USEHLE_YES)
{
/*~~~~~~~~~~~~~~~~*/
int OpcodeCount = 1;
/*~~~~~~~~~~~~~~~~*/
/*
* Using HLE £
* DisplayError("Using HLE");
*/
if(gHWS_pc == 0x80322c20)
{
OpcodeCount = 14;
X86_CALL((uint32) & osSendMessage);
compilerstatus.cp0Counter += (OpcodeCount);
}
else if(gHWS_pc == 0x803274d0)
{
OpcodeCount = 6; /* save jr for the recompile loop */
X86_CALL((uint32) & osDisableInt);
compilerstatus.cp0Counter += (OpcodeCount);
}
else if(gHWS_pc == 0x803274f0)
{
OpcodeCount = 5;
X86_CALL((uint32) & osRestoreInt);
compilerstatus.cp0Counter += (OpcodeCount);
}
else if(gHWS_pc == 0x80327c80)
{
OpcodeCount = 31 /* 24+5 */ ;
X86_CALL((uint32) & osEnqueueAndYield);
compilerstatus.cp0Counter += (OpcodeCount);
}
/*
* else if (gHWS_pc == 0x80327d68) £
* { £
* OpcodeCount = 0/*+24+5
*/
else if(gHWS_pc == 0x80327d58)
{
OpcodeCount = 1;
X86_CALL((uint32) & osPopThread);
gHWS_pc += (OpcodeCount - 1) << 2; /* Opcode count - 4 (the first one) */
compilerstatus.cp0Counter += (OpcodeCount);
}
else
{
dyna_instruction[((unsigned) (gHWS_code >> 26))](&gHardwareState);
compilerstatus.cp0Counter++;
}
gHWS_pc += (OpcodeCount - 1) << 2; /* Opcode count - 4 (the first one) */
compilerstatus.cp0Counter += (OpcodeCount); /* times counter factor */
}
else
{
/*
* No HLE £
* DisplayError("Not using HLE");
*/
dyna_instruction[((unsigned) (gHWS_code >> 26))](&gHardwareState);
compilerstatus.InstructionCount++;
}
while(compilerstatus.KEEP_RECOMPILING)
{
/* This code is disabled for multipass because it does not work. */
if(gMultiPass.UseOnePassOnly == 1)
{
/*
* Need to break out at the end of 4KB block if we are using protected memory or
* we are in TLB mapped address
*/
if
(
(gHWS_pc + 4) / 0x1000 != gHWS_pc / 0x1000
&& (NOT_IN_KO_K1_SEG(gHWS_pc) || currentromoptions.Code_Check == CODE_CHECK_PROTECT_MEMORY)
)
{
MOV_ImmToMemory(1, ModRM_disp32, (unsigned long) &gHWS_pc, gHWS_pc + 4);
/* end of compiled block */
compilerstatus.KEEP_RECOMPILING = FALSE;
FlushAllRegisters();
Interrupts(0, 0, 0, 0); /* JUMP_TYPE_INDIRECT); */
/*
* TRACE2("Block at %08x covers 4KB boundry at pc=%08X, breaks out",
* compilerstatus.TempPC, gHWS_pc+4);
*/
break;
}
}
gHWS_pc += 4;
compilerstatus.InstructionCount++;
gHWS_code = DynaFetchInstruction(gHWS_pc + (Instruction_Order[k] << 2));
if(ITLB_Error)
{
/*
* DisplayError("TLB error happens during compiling, PC=%08X",
* compilerstatus.TempPC); £
* TLB_TRACE(TRACE1("ITLB exception at Instruction fetching during compiling,
* PC=%08X", compilerstatus.TempPC));
*/
HandleExceptions(TLB_Error_Vector);
gHWS_code = DynaFetchInstruction(gHWS_pc + (Instruction_Order[k] << 2));
}
k++;
if(currentromoptions.Use_Register_Caching == USEREGC_NO) FlushAllRegisters();
DYNA_DEBUG_INSTRUCTION(gHWS_code);
#ifdef DEBUG_COMMON
MOV_ImmToMemory(1, ModRM_disp32, (unsigned long) &gHWS_pc, gHWS_pc);
#endif
dyna_instruction[((unsigned) (gHWS_code >> 26))](&gHardwareState);
}
#ifdef DEBUG_COMMON
if(compilerstatus.InstructionCount > 255)
{ /* DisplayError("Compiled Block is too large, size=%d, pc=%08X, end at %08X", cp0Counter+1,
* compilerstatus.TempPC, compilerstatus.TempPC+(cp0Counter+1)*4); */
TRACE3
(
"Compiled Block is too large, size=%d, pc=%08X, end at %08X",
compilerstatus.cp0Counter + 1,
compilerstatus.TempPC,
compilerstatus.TempPC + (compilerstatus.cp0Counter + 1) * 4
);
}
#endif
/* Save info for dyna code check/check block to use */
*LookupPtr = (_u32) Block; /* Need to assign the value after compiling, otherwise the value will */
/* be set to 0 when doing DynaFetchInstruction() */
*(uint16 *) (Block - 2) = (uint16) compilerstatus.InstructionCount + 1;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* store block size */
DEBUG_PRINT_DYNA_COMPILE_INFO gHWS_pc = compilerstatus.TempPC;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if(compilerstatus.DynaBufferOverError) /* ok, we have a buffer error, need to refresh the dyna buffer and recompile
* this */
/* block again. */
{
TRACE0("Dyna Buffer Overrun, refresh dyna");
/* DisplayError("Dyna Buffer Overrun, refresh dyna"); */
gMultiPass.WriteCode = 1;
gMultiPass.WhichPass = COMPILE_MAP_ONLY;
RefreshDynaDuringGamePlay();
if(currentromoptions.Link_4KB_Blocks == USE4KBLINKBLOCK_YES)
{
compilerstatus.Is_Compiling--;
return 0;
}
else
{
compilerstatus.DynaBufferOverError = FALSE;
goto begin;
}
}
/*
* I am trying to map TLB address also into sDYN_PC_LOOKUP £
* if( NOT_IN_KO_K1_SEG(gHWS_pc) ) { uint32 ptr; if( sDYN_PC_LOOKUP[gHWS_pc>>16]
* == gMemoryState.dummyAllZero ) UnmappedMemoryExceptionHelper(gHWS_pc); ptr
* (uint32)sDYN_PC_LOOKUP[gHWS_pc>>16]; (uint32*)(ptr + (uint16)gHWS_pc) = Block;
* } £
* Not sure if necessary.
*/
gMultiPass.WriteCode = 1;
gMultiPass.WhichPass = COMPILE_ALL;
compilerstatus.Is_Compiling--;
return(uint32) Block;
}
/*
=======================================================================================================================
type = 0 WORD £
1 HALFWORD £
2 BYTE £
3 DWORD
=======================================================================================================================
*/
void Invalidate4KBlock(uint32 addr, char *opcodename, int type, uint64 newvalue)
{
#ifdef DEBUG_COMMON
if(addr / 0x1000 == gHWS_pc / 0x1000)
{
TRACE1("Warning, invalidate the block while PC=%08X is in the block", gHWS_pc);
}
#endif
if(IN_KO_K1_SEG(addr))
addr = addr & 0xDFFFFFFF;
else
{
CODE_DETECT_TRACE(TRACE0("Warning, cannot invalidate a block not in RDRAM"));
return;
}
if(addr < 0x80000000 + current_rdram_size)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
uint32 offset = addr - 0x80000000;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
switch(type)
{
case WORDTYPE:
{
if((uint32) newvalue == *(uint32 *) (RDRAM_Copy + offset))
return;
else if(*(uint32 *) (RDRAM_Copy + offset) == DUMMYOPCODE)
return;
else
*(uint32 *) (RDRAM_Copy + offset) = DUMMYOPCODE;
}
break;
case HWORDTYPE:
{
if((uint16) newvalue == *(uint16 *) (RDRAM_Copy + offset))
return;
else if(*(uint32 *) (RDRAM_Copy + offset) == DUMMYOPCODE)
return;
else
*(uint32 *) (RDRAM_Copy + offset) = DUMMYOPCODE;
}
break;
case BYTETYPE:
{
if((uint8) newvalue == *(uint8 *) (RDRAM_Copy + offset))
return;
else if(*(uint32 *) (RDRAM_Copy + offset) == DUMMYOPCODE)
return;
else
*(uint32 *) (RDRAM_Copy + offset) = DUMMYOPCODE;
}
break;
case DWORDTYPE:
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
uint64 dummy = (newvalue >> 32) | (newvalue << 32);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if(dummy == *(uint64 *) (RDRAM_Copy + offset))
return;
else if
(
*(uint32 *) (RDRAM_Copy + offset) == DUMMYOPCODE
&& *(uint32 *) (RDRAM_Copy + offset + 4) == DUMMYOPCODE
)
return;
else
{
*(uint32 *) (RDRAM_Copy + offset) = DUMMYOPCODE;
*(uint32 *) (RDRAM_Copy + offset + 4) = DUMMYOPCODE;
}
}
break;
case NOCHECKTYPE:
break;
default:
CODE_DETECT_TRACE(TRACE0("Warning, incorrect data type"));
return;
}
InvalidateOneBlock(addr);
UnprotectBlock(addr);
CODE_DETECT_TRACE(TRACE2("Protect Memory in %s found self-mod code at %08X, invalidate the block", opcodename, addr));
}
/*
* else £
* { £
* CODE_DETECT_TRACE(TRACE0("Warning, protected memory is no in RDRAM")); £
* }
*/
}
/*
=======================================================================================================================
=======================================================================================================================
*/
void InvalidateOneBlock(uint32 pc)
{
/*~~~~~~~~~~~*/
uint32 offset;
/*~~~~~~~~~~~*/
/*
* up to here, we have identified the new value as different from the old value £
* and the old value was used as code, not as data, so we need to invalidate
* the whole 4KB block and unprotect the 4KB block
*/
for(offset = (pc & 0xFFFFF000); offset < (pc & 0xFFFFF000) + 0x1000; offset += 4)
{
if(sDYN_PC_LOOKUP[offset >> 16] != gMemoryState.dummyAllZero)
*(uint32 *) ((uint8 *) sDYN_PC_LOOKUP[offset >> 16] + (uint16) offset) = 0;
else
break;
}
}
/*
=======================================================================================================================
Validate the compiled block, and doing dyna code checking £
=======================================================================================================================
*/
void Dyna_Code_Check_None(void)
{
}
/*
=======================================================================================================================
Validate the compiled block, and doing dyna code checking by checking QWORD method £
=======================================================================================================================
*/
void Dyna_Code_Check_QWORD(void)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
register uint32 pc = g_pc_is_rdram;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if(*(uint32 *) (RDRAM_Copy + pc) != *(uint32 *) (gMS_RDRAM + pc))
{
Block = NULL;
DEBUG_DYNA_MOD_CODE_TRACE(TRACE1("Found mod-code at %08x", g_pc_is_rdram));
*(uint32 *) ((uint8 *) sDYN_PC_LOOKUP[(pc | 0x80000000) >> 16] + (uint16) pc) = 0;
*(uint32 *) (RDRAM_Copy + pc) = *(uint32 *) (gMS_RDRAM + pc);
}
else if(*(uint32 *) (RDRAM_Copy + pc + 4) != *(uint32 *) (gMS_RDRAM + pc + 4))
{
Block = NULL;
DEBUG_DYNA_MOD_CODE_TRACE(TRACE1("Found mod-code at %08x", g_pc_is_rdram));
*(uint32 *) ((uint8 *) sDYN_PC_LOOKUP[((pc + 4) | 0x80000000) >> 16] + (uint16) (pc + 4)) = 0;
*(uint32 *) (RDRAM_Copy + pc + 4) = *(uint32 *) (gMS_RDRAM + pc + 4);
}
}
/*
=======================================================================================================================
Validate the compiled block, and doing dyna code checking by checking DWORD method £
=======================================================================================================================
*/
void Dyna_Code_Check_DWORD(void)
{
if(*(uint32 *) (RDRAM_Copy + g_pc_is_rdram) != *(uint32 *) (gMS_RDRAM + g_pc_is_rdram))
{
Block = NULL;
DEBUG_DYNA_MOD_CODE_TRACE(TRACE1("Found mod-code at %08x", g_pc_is_rdram));
*(uint32 *) (RDRAM_Copy + g_pc_is_rdram) = *(uint32 *) (gMS_RDRAM + g_pc_is_rdram);
}
}
/*
=======================================================================================================================
Validate the compiled block, and doing dyna code checking by checking whole block method £
=======================================================================================================================
*/
void Dyna_Code_Check_BLOCK(void)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
register int length; /* How to get the length of the block */
uint32 pc = g_pc_is_rdram;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
for(length = *(uint16 *) (Block - 2) - 1; length >= 0; length--, pc += 4)
{
if(*(uint32 *) (RDRAM_Copy + pc) != *(uint32 *) (gMS_RDRAM + pc))
{
Block = NULL;
DEBUG_DYNA_MOD_CODE_TRACE(TRACE1("Found mod-code at %08x", g_pc_is_rdram));
while(length >= 0)
{
*(uint32 *) ((uint8 *) sDYN_PC_LOOKUP[(pc | 0x80000000) >> 16] + (uint16) pc) = 0;
*(uint32 *) (RDRAM_Copy + pc) = *(uint32 *) (gMS_RDRAM + pc);
length--;
pc += 4;
}
break;
}
}
}
/*
=======================================================================================================================
Validate the compiled block, and doing dyna code checking by checking QWORD method £
=======================================================================================================================
*/
void Dyna_Code_Check_None_Boot(void)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
register uint32 pc = g_pc_is_rdram;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if(*(uint32 *) (RDRAM_Copy + pc) != *(uint32 *) (gMS_RDRAM + pc))
{
Block = NULL;
DEBUG_DYNA_MOD_CODE_TRACE(TRACE1("Found mod-code at %08x", g_pc_is_rdram));
/*
* if( currentromoptions.Link_4KB_Blocks == USE4KBLINKBLOCK_YES ) £
* { £
* InvalidateOneBlock(g_pc_is_rdram|0x80000000); £
* } £
* else
*/
{
*(uint32 *) ((uint8 *) sDYN_PC_LOOKUP[(pc | 0x80000000) >> 16] + (uint16) pc) = 0;
*(uint32 *) (RDRAM_Copy + pc) = *(uint32 *) (gMS_RDRAM + pc);
}
}
else if(*(uint32 *) (RDRAM_Copy + pc + 4) != *(uint32 *) (gMS_RDRAM + pc + 4))
{
Block = NULL;
DEBUG_DYNA_MOD_CODE_TRACE(TRACE1("Found mod-code at %08x", g_pc_is_rdram));
/*
* if( currentromoptions.Link_4KB_Blocks == USE4KBLINKBLOCK_YES ) £
* { £
* InvalidateOneBlock(g_pc_is_rdram|0x80000000); £
* } £
* else
*/
{
*(uint32 *) ((uint8 *) sDYN_PC_LOOKUP[((pc + 4) | 0x80000000) >> 16] + (uint16) (pc + 4)) = 0;
*(uint32 *) (RDRAM_Copy + pc + 4) = *(uint32 *) (gMS_RDRAM + pc + 4);
}
}
if(emustatus.DListCount > 80) /* yes, we have detect the first self-modify code */
{
Dyna_Check_Codes = Dyna_Code_Check[emustatus.CodeCheckMethod - 1];
TRACE0("Reset the Dyna Code Check Method => None");
}
}
/*
=======================================================================================================================
Link block, by target1
=======================================================================================================================
*/
void Link1(void)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
uint32 offset = (uint32) current_block_entry->block_ptr - 1 - (uint32)
(&RecompCode[block_queue_head->jmp_to_target_1_code_addr]);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* JMP Short */
if((offset < 128) || (offset > 0xffffff81))
{
(*((unsigned _int8 *) (&RecompCode[block_queue_head->jmp_to_target_1_code_addr - 1]))) = 0xEB;
(*((unsigned _int8 *) (&RecompCode[block_queue_head->jmp_to_target_1_code_addr]))) = (_int8) offset;
}
else
{ /* JMP Long (Near) */
(*((unsigned _int32 *) (&RecompCode[block_queue_head->jmp_to_target_1_code_addr]))) =
(uint32) current_block_entry->block_ptr -
4 -
(uint32) (&RecompCode[block_queue_head->jmp_to_target_1_code_addr]);
}
}
/*
=======================================================================================================================
Link block, by target2
=======================================================================================================================
*/
void Link2(void)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
uint32 offset = (uint32) current_block_entry->block_ptr - 1 - (_int32)
(&RecompCode[block_queue_head->jmp_to_target_2_code_addr]);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* JMP Short */
if((offset < 128) || (offset > 0xffffff81))
{
(*((unsigned _int8 *) (&RecompCode[block_queue_head->jmp_to_target_2_code_addr - 1]))) = 0xEB;
(*((unsigned _int8 *) (&RecompCode[block_queue_head->jmp_to_target_2_code_addr]))) = (_int8) offset;
}
else
{ /* JMP Long (Near) */
(*((unsigned _int32 *) (&RecompCode[block_queue_head->jmp_to_target_2_code_addr]))) =
(uint32) current_block_entry->block_ptr -
4 -
(uint32) (&RecompCode[block_queue_head->jmp_to_target_2_code_addr]);
}
}
/*
=======================================================================================================================
Link block, by pointer.
=======================================================================================================================
*/
void LinkPtr(void)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
uint32 ptr = GetCompiledBlockPtr(block_queue_head->target_2_pc);
uint32 offset = (uint32) ptr - 1 - (uint32) (&RecompCode[block_queue_head->jmp_to_target_2_code_addr]);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* JMP Short */
if((offset < 128) || (offset > 0xffffff81))
{
(*((unsigned _int8 *) (&RecompCode[block_queue_head->jmp_to_target_2_code_addr - 1]))) = 0xEB;
(*((unsigned _int8 *) (&RecompCode[block_queue_head->jmp_to_target_2_code_addr]))) = (uint8) offset;
}
/* JMP Long (Near) */
else
{
(*((uint32 *) (&RecompCode[block_queue_head->jmp_to_target_2_code_addr]))) = (uint32) ptr - 4 - (uint32) (&RecompCode[block_queue_head->jmp_to_target_2_code_addr]);
}
}
/*
=======================================================================================================================
This function will compile all linked blocks at the current gHWS_PC (PC). £
During execution, at the end of each block, before linking to the next target block, it will check the interrupt
event, £
if there is a new interrupt, we will exit from the linked blocks and return to emu main loop. £
other terminating conditions are: £
- targeted PC is not fixed, but by JR, JALR or ERET £
- targeted PC is not in the same 4KB block £
Return value: pointer to the compiled block
=======================================================================================================================
*/
uint32 Dyna_Compile_4KB_Block(void)
{
/*~~~~~~~~~~~~~~~~~~~~~~*/
uint32 saved_very_1st_pc;
int blockcount = 0;
uint32 blk;
uint32 *ptr;
uint32 maptopc;
/*~~~~~~~~~~~~~~~~~~~~~~*/
redo:
LOGGING_DYNA(LogDyna("\n\n** Compile Block in 4KB at PC=%08X\n", gHWS_pc));
saved_very_1st_pc = gHWS_pc;
/* step1: push the current gHWS_pc into queue */
block_queue_head = get_new_block_entry(gHWS_pc);
while(block_queue_head != NULL)
{
/*
* Step2: Get PC from the queue £
* Step3.1: Check if the block at PC has already been compiled, if, then compile
* it, go to step 4
*/
if(!block_queue_head->HasBeenCompiled)
{
/*~~~~~~~~~~~*/
uint32 savepc;
/*~~~~~~~~~~~*/
current_block_entry = block_queue_head;
gHWS_pc = current_block_entry->block_pc;
savepc = gHWS_pc;
current_block_entry->block_ptr = Dyna_Compile_Single_Block();
if(compilerstatus.DynaBufferOverError) break;
if(savepc != gHWS_pc)
{
if(gHWS_COP0Reg[EPC] == saved_very_1st_pc)
{
/* there happens an ITLB error when compiling the 1st block */
TRACE1("In compiling 4KB, is there a ITLB happens? pc=%08X", savepc);
return current_block_entry->block_ptr;
}
else
{
DisplayError("ITLB error happens when compiling 4KB blocks, not at the 1st block");