-
Notifications
You must be signed in to change notification settings - Fork 256
/
HLEIntercept.cpp
1108 lines (929 loc) · 38.5 KB
/
HLEIntercept.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
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
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
// ******************************************************************
// *
// * .,-::::: .,:: .::::::::. .,:: .:
// * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;;
// * [[[ '[[,,[[' [[[__[[\. '[[,,[['
// * $$$ Y$$$P $$""""Y$$ Y$$$P
// * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo,
// * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm,
// *
// * Cxbx->Win32->CxbxKrnl->HLEIntercept.cpp
// *
// * This file is part of the Cxbx project.
// *
// * Cxbx and Cxbe are free software; you can redistribute them
// * and/or modify them 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 recieved a copy of the GNU General Public License
// * along with this program; see the file COPYING.
// * If not, write to the Free Software Foundation, Inc.,
// * 59 Temple Place - Suite 330, Bostom, MA 02111-1307, USA.
// *
// * (c) 2002-2003 Aaron Robinson <caustik@caustik.com>
// *
// * All rights reserved
// *
// ******************************************************************
#define _CXBXKRNL_INTERNAL
#include <cmath>
#include "CxbxKrnl.h"
#include "Emu.h"
#include "EmuFS.h"
#include "EmuXTL.h"
#include "EmuShared.h"
#include "HLEDataBase.h"
#include "HLEIntercept.h"
static xbaddr EmuLocateFunction(OOVPA *Oovpa, xbaddr lower, xbaddr upper);
static void EmuInstallPatches(OOVPATable *OovpaTable, uint32 OovpaTableSize, Xbe::Header *pXbeHeader);
static void EmuXRefFailure();
#include <shlobj.h>
#include <vector>
uint32 fcount = 0;
void * funcExclude[2048] = { nullptr };
uint32 g_BuildVersion;
static std::vector<xbaddr> vCacheOut;
static bool bCacheInp = false;
static std::vector<xbaddr> vCacheInp;
static std::vector<xbaddr>::const_iterator vCacheInpIter;
bool bLLE_APU = false; // Set this to true for experimental APU (sound) LLE
bool bLLE_GPU = false; // Set this to true for experimental GPU (graphics) LLE
bool bLLE_JIT = false; // Set this to true for experimental JIT
bool bXRefFirstPass; // For search speed optimization, set in EmuHLEIntercept, read in EmuLocateFunction
uint32 UnResolvedXRefs; // Tracks XRef location, used (read/write) in EmuHLEIntercept and EmuLocateFunction
void EmuHLEIntercept(Xbe::Header *pXbeHeader)
{
Xbe::Certificate *pCertificate = (Xbe::Certificate*)pXbeHeader->dwCertificateAddr;
Xbe::LibraryVersion *pLibraryVersion = (Xbe::LibraryVersion*)pXbeHeader->dwLibraryVersionsAddr;
char szCacheFileName[MAX_PATH];
printf("\n");
printf("*******************************************************************************\n");
printf("* Cxbx-Reloaded High Level Emulation database last modified %s\n", szHLELastCompileTime);
printf("*******************************************************************************\n");
printf("\n");
//
// initialize HLE cache file
//
{
SHGetSpecialFolderPath(NULL, szCacheFileName, CSIDL_APPDATA, TRUE);
strcat(szCacheFileName, "\\Cxbx-Reloaded\\");
CreateDirectory(szCacheFileName, NULL);
char *spot = strrchr(szCacheFileName, '\\');
//
// create HLECache directory
//
strcpy(spot, "\\HLECache");
CreateDirectory(szCacheFileName, NULL);
//
// open title's cache file
//
sprintf(spot+9, "\\%08x.dat", pCertificate->dwTitleId);
FILE *pCacheFile = fopen(szCacheFileName, "rb");
if(pCacheFile != NULL)
{
bool bVerified = false;
//
// verify last compiled timestamp
//
char szCacheLastCompileTime[64];
memset(szCacheLastCompileTime, 0, 64);
if(fread(szCacheLastCompileTime, 64, 1, pCacheFile) == 1)
{
if(strcmp(szCacheLastCompileTime, szHLELastCompileTime) == 0)
{
bVerified = true;
}
}
//
// load function addresses
//
if(bVerified)
{
while(true)
{
xbaddr cur;
if(fread(&cur, 4, 1, pCacheFile) != 1)
break;
vCacheInp.push_back(cur);
}
bCacheInp = true;
vCacheInpIter = vCacheInp.begin();
printf("HLE: Loaded HLE Cache for 0x%.08X\n", pCertificate->dwTitleId);
}
fclose(pCacheFile);
}
}
//
// initialize Microsoft XDK emulation
//
if(pLibraryVersion != 0)
{
printf("HLE: Detected Microsoft XDK application...\n");
UnResolvedXRefs = XREF_COUNT; // = sizeof(XRefDataBase) / sizeof(xbaddr)
uint32 dwLibraryVersions = pXbeHeader->dwLibraryVersions;
uint32 LastUnResolvedXRefs = UnResolvedXRefs+1;
uint32 OrigUnResolvedXRefs = UnResolvedXRefs;
bool bFoundD3D = false;
bXRefFirstPass = true; // Set to false for search speed optimization
// Mark all Xrefs initially as undetermined
memset((void*)XRefDataBase, XREF_ADDR_UNDETERMINED, sizeof(XRefDataBase));
// Request a few fundamental XRefs to be derived instead of checked
XRefDataBase[XREF_D3DDEVICE] = XREF_ADDR_DERIVE;
XRefDataBase[XREF_D3DRS_CULLMODE] = XREF_ADDR_DERIVE;
XRefDataBase[XREF_D3DTSS_TEXCOORDINDEX] = XREF_ADDR_DERIVE;
for(int p=0;UnResolvedXRefs < LastUnResolvedXRefs;p++)
{
printf("HLE: Starting pass #%d...\n", p+1);
LastUnResolvedXRefs = UnResolvedXRefs;
for(uint32 v=0;v<dwLibraryVersions;v++)
{
uint16 BuildVersion = pLibraryVersion[v].wBuildVersion;
uint16 OrigBuildVersion = BuildVersion;
// Aliases - for testing purposes only
if(BuildVersion == 4039) { BuildVersion = 4034; }
if(BuildVersion == 4238) { BuildVersion = 4361; } // I don't think this XDK was released.
if(BuildVersion == 4242) { BuildVersion = 4361; }
if(BuildVersion == 4400) { BuildVersion = 4361; }
if(BuildVersion == 4531) { BuildVersion = 4432; }
if(BuildVersion == 4721) { BuildVersion = 4627; }
if(BuildVersion == 4831) { BuildVersion = 4627; }
if(BuildVersion == 4928) { BuildVersion = 4627; }
if(BuildVersion == 5455) { BuildVersion = 5558; }
if(BuildVersion == 5659) { BuildVersion = 5558; }
if(BuildVersion == 5120) { BuildVersion = 5233; }
if(BuildVersion == 5933) { BuildVersion = 5849; } // These XDK versions are pretty much the same
/*
if(BuildVersion == 3944) { BuildVersion = 3925; }
if(BuildVersion == 4039) { BuildVersion = 4034; }
if(BuildVersion == 4242) { BuildVersion = 4432; }
if(BuildVersion == 4531) { BuildVersion = 4432; }
if(BuildVersion == 4721) { BuildVersion = 4432; }
if(BuildVersion == 4831) { BuildVersion = 4432; }
if(BuildVersion == 4928) { BuildVersion = 4432; }
if(BuildVersion == 5028) { BuildVersion = 4432; }
if(BuildVersion == 5120) { BuildVersion = 4432; }
if(BuildVersion == 5344) { BuildVersion = 4432; }
if(BuildVersion == 5455) { BuildVersion = 4432; }
if(BuildVersion == 5933) { BuildVersion = 4432; }
*/
char szLibraryName[9] = {0};
char szOrigLibraryName[9] = {0};
for(uint32 c=0;c<8;c++)
{
szLibraryName[c] = pLibraryVersion[v].szName[c];
szOrigLibraryName[c] = pLibraryVersion[v].szName[c];
}
// TODO: HACK: D3DX8 is packed into D3D8 database
if (strcmp(szLibraryName, Lib_D3DX8) == 0)
{
strcpy(szLibraryName, Lib_D3D8);
}
if(strcmp(szLibraryName, Lib_D3D8LTCG) == 0)
{
// Skip scanning for D3D8LTCG symbols when LLE GPU is selected
if (bLLE_GPU)
continue;
// Test (do not release uncommented!)
/*strcpy(szLibraryName, Lib_D3D8);*/
}
if (strcmp(szLibraryName, Lib_D3D8) == 0)
{
// Skip scanning for D3D8 symbols when LLE GPU is selected
if (bLLE_GPU)
continue;
// Prevent scanning D3D8 again (since D3D8X is packed into it above)
if (bFoundD3D)
{
//DbgPrintf("Redundant\n");
continue;
}
bFoundD3D = true;
// Some 3911 titles have different D3D8 builds
if (BuildVersion <= 3948)
BuildVersion = 3925;
// Testing... don't release with this code in it!
// TODO: 5233 and 5558
// if(BuildVersion == 4134)
// BuildVersion = 4627;
}
else if(strcmp(szLibraryName, Lib_DSOUND) == 0)
{
// Skip scanning for DSOUND symbols when LLE APU is selected
if (bLLE_APU)
continue;
// Several 3911 titles has different DSound builds.
if(BuildVersion < 4034)
{
BuildVersion = 3936;
}
// Redirect other highly similar DSOUND library versions
if(BuildVersion == 4361 || BuildVersion == 4400 || BuildVersion == 4432 ||
BuildVersion == 4531 )
BuildVersion = 4627;
}
else if(strcmp(szLibraryName, Lib_XAPILIB) == 0)
{
// Change a few XAPILIB versions to similar counterparts
if(BuildVersion == 3944)
BuildVersion = 3911;
if(BuildVersion == 3950)
BuildVersion = 3911;
if(OrigBuildVersion == 4531)
BuildVersion = 4627;
}
else if (strcmp(szLibraryName, Lib_XGRAPHC) == 0)
{
// Skip scanning for XGRAPHC (XG) symbols when LLE GPU is selected
if (bLLE_GPU)
continue;
// if(BuildVersion == 4432)
// BuildVersion = 4361;
if (BuildVersion == 3944)
BuildVersion = 3911;
if (OrigBuildVersion == 4531)
BuildVersion = 4361;
}
if(bXRefFirstPass)
{
if(strcmp(Lib_XAPILIB, szLibraryName) == 0 &&
(BuildVersion == 3911 || BuildVersion == 4034 || BuildVersion == 4134 || BuildVersion == 4361
|| BuildVersion == 4432 || BuildVersion == 4627 || BuildVersion == 5028 || BuildVersion == 5233
|| BuildVersion == 5344 || BuildVersion == 5558 || BuildVersion == 5788 || BuildVersion == 5849))
{
xbaddr lower = pXbeHeader->dwBaseAddr;
xbaddr upper = pXbeHeader->dwBaseAddr + pXbeHeader->dwSizeofImage;
}
else if(strcmp(Lib_D3D8, szLibraryName) == 0 /*&& strcmp(Lib_D3D8LTCG, szOrigLibraryName)*/ &&
(BuildVersion == 3925 || BuildVersion == 4134 || BuildVersion == 4361 || BuildVersion == 4432
|| BuildVersion == 4627 || BuildVersion == 5028 || BuildVersion == 5233 || BuildVersion == 5344
|| BuildVersion == 5558 || BuildVersion == 5788 || BuildVersion == 5849))
{
// Save D3D8 build version
g_BuildVersion = BuildVersion;
xbaddr lower = pXbeHeader->dwBaseAddr;
xbaddr upper = pXbeHeader->dwBaseAddr + pXbeHeader->dwSizeofImage;
xbaddr pFunc = (xbaddr)nullptr;
if(BuildVersion == 3925)
pFunc = EmuLocateFunction((OOVPA*)&D3DDevice_SetRenderState_CullMode_3925, lower, upper);
else if(BuildVersion < 5233)
pFunc = EmuLocateFunction((OOVPA*)&D3DDevice_SetRenderState_CullMode_4034, lower, upper);
else
pFunc = EmuLocateFunction((OOVPA*)&D3DDevice_SetRenderState_CullMode_5233, lower, upper);
// locate D3DDeferredRenderState
if(pFunc != (xbaddr)nullptr)
{
// offset for stencil cull enable render state in the deferred render state buffer
xbaddr DerivedAddr_D3DRS_CULLMODE = NULL;
int Decrement = 0; // TODO : Rename into something understandable
int Increment = 0; // TODO : Rename into something understandable
int patchOffset = 0; // TODO : Rename into something understandable
// Read address of D3DRS_CULLMODE from D3DDevice_SetRenderState_CullMode
// TODO : Simplify this when XREF_D3DRS_CULLMODE derivation is deemed stable
{
if (BuildVersion == 3925)
{
DerivedAddr_D3DRS_CULLMODE = *(xbaddr*)(pFunc + 0x25);
Decrement = 0x1FC; // TODO: Clean up (?)
Increment = 82 * 4;
patchOffset = 105 * 4; // TODO: Verify
//Decrement = 0x19F; // TODO: Clean up (?)
//Increment = 72 * 4;
//patchOffset = 142*4; // TODO: Verify
}
else if (BuildVersion == 4034)
{
DerivedAddr_D3DRS_CULLMODE = *(xbaddr*)(pFunc + 0x2B);
Decrement = 0x248; // TODO: Verify
Increment = 82 * 4;
patchOffset = 142 * 4;
}
else if (BuildVersion == 4134 || BuildVersion == 4361)
{
DerivedAddr_D3DRS_CULLMODE = *(xbaddr*)(pFunc + 0x2B);
Decrement = 0x200;
Increment = 82 * 4;
patchOffset = 142 * 4;
}
else if (BuildVersion == 4432)
{
DerivedAddr_D3DRS_CULLMODE = *(xbaddr*)(pFunc + 0x2B);
Decrement = 0x204;
Increment = 83 * 4;
patchOffset = 143 * 4;
}
else if (BuildVersion == 4627 || BuildVersion == 5028 || BuildVersion == 5233 || BuildVersion == 5344
|| BuildVersion == 5558 || BuildVersion == 5788 || BuildVersion == 5849)
{
// WARNING: Not thoroughly tested (just seemed very correct right away)
DerivedAddr_D3DRS_CULLMODE = *(xbaddr*)(pFunc + 0x2B);
Decrement = 0x24C;
Increment = 92 * 4;
patchOffset = 162 * 4;
}
// Temporary verification - is XREF_D3DDEVICE derived correctly?
xbaddr DerivedAddr_D3DDevice = *(xbaddr*)((xbaddr)pFunc + 0x03);
if (XRefDataBase[XREF_D3DDEVICE] != DerivedAddr_D3DDevice)
{
if (XRefDataBase[XREF_D3DDEVICE] != XREF_ADDR_DERIVE)
EmuWarning("Second derived XREF_D3DDEVICE differs from first!");
XRefDataBase[XREF_D3DDEVICE] = DerivedAddr_D3DDevice;
}
// Temporary verification - is XREF_D3DRS_CULLMODE derived correctly?
if (XRefDataBase[XREF_D3DRS_CULLMODE] != DerivedAddr_D3DRS_CULLMODE)
{
if (XRefDataBase[XREF_D3DRS_CULLMODE] != XREF_ADDR_DERIVE)
EmuWarning("Second derived XREF_D3DRS_CULLMODE differs from first!");
XRefDataBase[XREF_D3DRS_CULLMODE] = DerivedAddr_D3DRS_CULLMODE;
}
}
// Derive address of EmuD3DDeferredRenderState from D3DRS_CULLMODE
XTL::EmuD3DDeferredRenderState = (DWORD*)(DerivedAddr_D3DRS_CULLMODE - Decrement + Increment);
patchOffset -= Increment;
// Derive address of a few other deferred render state slots (to help xref-based function location)
XRefDataBase[XREF_D3DRS_MULTISAMPLEMODE] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset - 8*4;
XRefDataBase[XREF_D3DRS_MULTISAMPLERENDERTARGETMODE] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset - 7*4;
XRefDataBase[XREF_D3DRS_STENCILCULLENABLE] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset + 0*4;
XRefDataBase[XREF_D3DRS_ROPZCMPALWAYSREAD] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset + 1*4;
XRefDataBase[XREF_D3DRS_ROPZREAD] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset + 2*4;
XRefDataBase[XREF_D3DRS_DONOTCULLUNCOMPRESSED] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset + 3*4;
for(int v=0;v<44;v++)
{
XTL::EmuD3DDeferredRenderState[v] = X_D3DRS_UNK;
}
printf("HLE: 0x%.08X -> EmuD3DDeferredRenderState\n", XTL::EmuD3DDeferredRenderState);
//DbgPrintf("HLE: 0x%.08X -> XREF_D3DRS_ROPZCMPALWAYSREAD\n", XRefDataBase[XREF_D3DRS_ROPZCMPALWAYSREAD] );
}
else
{
XTL::EmuD3DDeferredRenderState = nullptr;
CxbxKrnlCleanup("EmuD3DDeferredRenderState was not found!");
}
// locate D3DDeferredTextureState
{
pFunc = (xbaddr)nullptr;
if(BuildVersion == 3925)
pFunc = EmuLocateFunction((OOVPA*)&D3DDevice_SetTextureState_TexCoordIndex_3925, lower, upper);
// TODO : What about 4034? Use it once it's offset to XREF_D3DTSS_TEXCOORDINDEX is verified
else if(BuildVersion == 4134)
pFunc = EmuLocateFunction((OOVPA*)&D3DDevice_SetTextureState_TexCoordIndex_4134, lower, upper);
else if(BuildVersion == 4361 || BuildVersion == 4432)
pFunc = EmuLocateFunction((OOVPA*)&D3DDevice_SetTextureState_TexCoordIndex_4361, lower, upper);
else if(BuildVersion == 4627 || BuildVersion == 5028 || BuildVersion == 5233 || BuildVersion == 5344
|| BuildVersion == 5558 || BuildVersion == 5788 || BuildVersion == 5849)
pFunc = EmuLocateFunction((OOVPA*)&D3DDevice_SetTextureState_TexCoordIndex_4627, lower, upper);
if (pFunc != (xbaddr)nullptr)
{
xbaddr DerivedAddr_D3DTSS_TEXCOORDINDEX = NULL;
int Decrement = 0x70; // TODO : Rename into something understandable
// TODO : Remove this when XREF_D3DTSS_TEXCOORDINDEX derivation is deemed stable
{
if (BuildVersion == 3925) // 0x18F180
DerivedAddr_D3DTSS_TEXCOORDINDEX = *(xbaddr*)(pFunc + 0x11);
else if (BuildVersion == 4134)
DerivedAddr_D3DTSS_TEXCOORDINDEX = *(xbaddr*)(pFunc + 0x18);
else
DerivedAddr_D3DTSS_TEXCOORDINDEX = *(xbaddr*)(pFunc + 0x19);
// Temporary verification - is XREF_D3DTSS_TEXCOORDINDEX derived correctly?
if (XRefDataBase[XREF_D3DTSS_TEXCOORDINDEX] != DerivedAddr_D3DTSS_TEXCOORDINDEX)
{
if (XRefDataBase[XREF_D3DTSS_TEXCOORDINDEX] != XREF_ADDR_DERIVE)
EmuWarning("Second derived XREF_D3DTSS_TEXCOORDINDEX differs from first!");
XRefDataBase[XREF_D3DTSS_TEXCOORDINDEX] = DerivedAddr_D3DTSS_TEXCOORDINDEX;
}
}
XTL::EmuD3DDeferredTextureState = (DWORD*)(DerivedAddr_D3DTSS_TEXCOORDINDEX - Decrement);
for(int s=0;s<4;s++)
{
for(int v=0;v<32;v++)
XTL::EmuD3DDeferredTextureState[v+s*32] = X_D3DTSS_UNK;
}
printf("HLE: 0x%.08X -> EmuD3DDeferredTextureState\n", XTL::EmuD3DDeferredTextureState);
}
else
{
XTL::EmuD3DDeferredTextureState = nullptr;
CxbxKrnlCleanup("EmuD3DDeferredTextureState was not found!");
}
}
}
//else if(strcmp(Lib_D3D8LTCG, szLibraryName) == 0 &&
// (BuildVersion == 5849)) // 5849 only so far...
// {
// // Save D3D8 build version
// g_BuildVersion = BuildVersion;
// xbaddr lower = pXbeHeader->dwBaseAddr;
// xbaddr upper = pXbeHeader->dwBaseAddr + pXbeHeader->dwSizeofImage;
// xbaddr pFunc = (xbaddr)nullptr;
// if(BuildVersion == 5849)
// pFunc = EmuLocateFunction((OOVPA*)&D3DDevice_SetRenderState_CullMode_LTCG_5849, lower, upper);
// // locate D3DDeferredRenderState
// if(pFunc != nullptr)
// {
// // offset for stencil cull enable render state in the deferred render state buffer
// int patchOffset = 0;
// if(BuildVersion == 5849)
// {
// // WARNING: Not thoroughly tested (just seemed very correct right away)
// XTL::EmuD3DDeferredRenderState = (DWORD*)(*(DWORD*)(pFunc + 0x2B) - 0x24C + 92*4);
// patchOffset = 162*4 - 92*4;
// }
// XRefDataBase[XREF_D3DDEVICE] = *(DWORD*)((DWORD)pFunc + 0x03);
// XRefDataBase[XREF_D3DRS_MULTISAMPLEMODE] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset - 8*4;
// XRefDataBase[XREF_D3DRS_MULTISAMPLERENDERTARGETMODE] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset - 7*4;
// XRefDataBase[XREF_D3DRS_STENCILCULLENABLE] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset + 0*4;
// XRefDataBase[XREF_D3DRS_ROPZCMPALWAYSREAD] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset + 1*4;
// XRefDataBase[XREF_D3DRS_ROPZREAD] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset + 2*4;
// XRefDataBase[XREF_D3DRS_DONOTCULLUNCOMPRESSED] = (xbaddr)XTL::EmuD3DDeferredRenderState + patchOffset + 3*4;
// for(int v=0;v<44;v++)
// {
// XTL::EmuD3DDeferredRenderState[v] = X_D3DRS_UNK;
// }
// DbgPrintf("HLE: 0x%.08X -> EmuD3DDeferredRenderState\n", XTL::EmuD3DDeferredRenderState);
// }
// else
// {
// XTL::EmuD3DDeferredRenderState = nullptr;
// CxbxKrnlCleanup("EmuD3DDeferredRenderState was not found!");
// }
// // locate D3DDeferredTextureState
// {
// pFunc = (xbaddr)nullptr;
// if(BuildVersion == 3925)
// pFunc = EmuLocateFunction((OOVPA*)&D3DDevice_SetTextureState_TexCoordIndex_LTCG_5849, lower, upper);
// if(pFunc != (xbaddr)nullptr)
// {
// if(BuildVersion == 3925) // 0x18F180
// XTL::EmuD3DDeferredTextureState = (DWORD*)(*(DWORD*)(pFunc + 0x11) - 0x70); // TODO: Verify
// else if(BuildVersion == 4134)
// XTL::EmuD3DDeferredTextureState = (DWORD*)(*(DWORD*)(pFunc + 0x18) - 0x70); // TODO: Verify
// else
// XTL::EmuD3DDeferredTextureState = (DWORD*)(*(DWORD*)(pFunc + 0x19) - 0x70);
// for(int s=0;s<4;s++)
// {
// for(int v=0;v<32;v++)
// XTL::EmuD3DDeferredTextureState[v+s*32] = X_D3DTSS_UNK;
// }
// DbgPrintf("HLE: 0x%.08X -> EmuD3DDeferredTextureState\n", XTL::EmuD3DDeferredTextureState);
// }
// else
// {
// XTL::EmuD3DDeferredTextureState = nullptr;
// CxbxKrnlCleanup("EmuD3DDeferredTextureState was not found!");
// }
// }
// }
}
printf("HLE: * Searching HLE database for %s version 1.0.%d... ", szLibraryName, BuildVersion);
const HLEData *FoundHLEData = nullptr;
for(uint32 d = 0; d < HLEDataBaseCount; d++) {
if (BuildVersion == HLEDataBase[d].BuildVersion && strcmp(szLibraryName, HLEDataBase[d].Library) == 0) {
FoundHLEData = &HLEDataBase[d];
break;
}
}
if (FoundHLEData) {
if (g_bPrintfOn) printf("Found\n");
EmuInstallPatches(FoundHLEData->OovpaTable, FoundHLEData->OovpaTableSize, pXbeHeader);
} else {
if (g_bPrintfOn) printf("Skipped\n");
}
}
bXRefFirstPass = false;
}
// display Xref summary
printf("HLE: Resolved %d cross reference(s)\n", OrigUnResolvedXRefs - UnResolvedXRefs);
}
vCacheInp.clear();
//
// update cache file
//
/* Turn of the nasty HLE cacheing (When you are adding oovaps anyway), it's in dire need of a better file identify system
if(vCacheOut.size() > 0)
{
FILE *pCacheFile = fopen(szCacheFileName, "wb");
if(pCacheFile != NULL)
{
DbgPrintf("HLE: Saving HLE Cache for 0x%.08X...\n", pCertificate->dwTitleId);
//
// write last compiled timestamp
//
char szCacheLastCompileTime[64];
memset(szCacheLastCompileTime, 0, 64);
strcpy(szCacheLastCompileTime, szHLELastCompileTime);
fwrite(szCacheLastCompileTime, 64, 1, pCacheFile);
//
// write function addresses
//
std::vector<void*>::const_iterator cur;
for(cur = vCacheOut.begin();cur != vCacheOut.end(); ++cur)
{
fwrite(&(*cur), 4, 1, pCacheFile);
}
}
fclose(pCacheFile);
}
*/
vCacheOut.clear();
printf("\n");
return;
}
static inline void EmuInstallPatch(xbaddr FunctionAddr, void *Patch)
{
uint08 *FuncBytes = (uint08*)FunctionAddr;
*(uint08*)&FuncBytes[0] = OPCODE_JMP_E9; // = opcode for JMP rel32 (Jump near, relative, displacement relative to next instruction)
*(uint32*)&FuncBytes[1] = (uint32)Patch - FunctionAddr - 5;
}
static inline void GetXRefEntry(OOVPA *oovpa, int index, OUT uint32 &xref, OUT uint08 &offset)
{
// Note : These are stored swapped by the XREF_ENTRY macro, hence this difference from GetOovpaEntry :
xref = (uint32)((LOOVPA<1>*)oovpa)->Lovp[index].Offset;
offset = ((LOOVPA<1>*)oovpa)->Lovp[index].Value;
}
static inline void GetOovpaEntry(OOVPA *oovpa, int index, OUT uint32 &offset, OUT uint08 &value)
{
offset = (uint32)((LOOVPA<1>*)oovpa)->Lovp[index].Offset;
value = ((LOOVPA<1>*)oovpa)->Lovp[index].Value;
}
static boolean CompareOOVPAToAddress(OOVPA *Oovpa, xbaddr cur)
{
uint32 v = 0; // verification counter
// Check all XRefs, stop if any does not match
for (; v < Oovpa->XRefCount; v++)
{
uint32 XRef;
uint08 Offset;
// get currently registered (un)known address
GetXRefEntry(Oovpa, v, XRef, Offset);
xbaddr XRefAddr = XRefDataBase[XRef];
// Undetermined XRef cannot be checked yet
// (EmuLocateFunction already checked this, but this check
// is cheap enough to keep, and keep this function generic).
if (XRefAddr == XREF_ADDR_UNDETERMINED)
return false;
// Don't verify an xref that has to be (but isn't yet) derived
if (XRefAddr == XREF_ADDR_DERIVE)
continue;
xbaddr ActualAddr = *(xbaddr*)(cur + Offset);
// check if PC-relative or direct reference matches XRef
if ((ActualAddr + cur + Offset + 4 != XRefAddr) && (ActualAddr != XRefAddr))
return false;
}
// Check all (Offset,Value)-pairs, stop if any does not match
for (; v < Oovpa->Count; v++)
{
uint32 Offset;
uint08 ExpectedValue;
// get offset + value pair
GetOovpaEntry(Oovpa, v, Offset, ExpectedValue);
uint08 ActualValue = *(uint08*)(cur + Offset);
if (ActualValue != ExpectedValue)
return false;
}
// all offsets matched
return true;
}
// locate the given function, searching within lower and upper bounds
static xbaddr EmuLocateFunction(OOVPA *Oovpa, xbaddr lower, xbaddr upper)
{
// skip out if this is an unnecessary search
if (!bXRefFirstPass && Oovpa->XRefCount == XRefZero && Oovpa->XRefSaveIndex == XRefNoSaveIndex)
return (xbaddr)nullptr;
uint32_t derive_indices = 0;
// Check all XRefs are known (if not, don't do a useless scan) :
for (uint32 v = 0; v < Oovpa->XRefCount; v++)
{
uint32 XRef;
uint08 Offset;
// get currently registered (un)known address
GetXRefEntry(Oovpa, v, XRef, Offset);
xbaddr XRefAddr = XRefDataBase[XRef];
// Undetermined XRef cannot be checked yet
if (XRefAddr == XREF_ADDR_UNDETERMINED)
// Skip this scan over the address range
return (xbaddr)nullptr;
// Don't verify an xref that has to be (but isn't yet) derived
if (XRefAddr == XREF_ADDR_DERIVE)
{
// Mark (up to index 32) which xref needs to be derived
derive_indices |= (1 << v);
continue;
}
}
// correct upper bound with highest Oovpa offset
uint32 count = Oovpa->Count;
{
uint32 Offset;
uint08 Value; // ignored
GetOovpaEntry(Oovpa, count - 1, Offset, Value);
upper -= Offset;
}
// search all of the image memory
for (xbaddr cur = lower; cur < upper; cur++)
if (CompareOOVPAToAddress(Oovpa, cur))
{
// do we need to save the found address?
if (Oovpa->XRefSaveIndex != XRefNoSaveIndex)
{
// is the XRef not saved yet?
switch (XRefDataBase[Oovpa->XRefSaveIndex]) {
case XREF_ADDR_NOT_FOUND:
{
EmuWarning("Found OOVPA after first finding nothing?");
// fallthrough to XREF_ADDR_UNDETERMINED
}
case XREF_ADDR_UNDETERMINED:
{
// save and count the found address
UnResolvedXRefs--;
XRefDataBase[Oovpa->XRefSaveIndex] = cur;
break;
}
case XREF_ADDR_DERIVE:
{
EmuWarning("Cannot derive a save index!");
break;
}
default:
{
if (XRefDataBase[Oovpa->XRefSaveIndex] != cur)
EmuWarning("Found OOVPA on other address than in XRefDataBase!");
break;
}
}
}
while (derive_indices > 0)
{
uint32 XRef;
uint08 Offset;
DWORD derive_index;
// Extract an index from the indices mask :
_BitScanReverse(&derive_index, derive_indices); // MSVC intrinsic; GCC has __builtin_clz
derive_indices ^= (1 << derive_index);
// get currently registered (un)known address
GetXRefEntry(Oovpa, derive_index, XRef, Offset);
// Calculate the address where the XRef resides
xbaddr XRefAddr = cur + Offset;
// Read the address it points to
XRefAddr = *((xbaddr*)XRefAddr);
/* For now assume it's a direct reference;
// TODO : Check if it's PC-relative reference?
if (XRefAddr + cur + Offset + 4 < XBE_MAX_VA)
XRefAddr = XRefAddr + cur + Offset + 4;
*/
// Does the address seem valid?
if (XRefAddr < XBE_MAX_VA)
{
// save and count the derived address
UnResolvedXRefs--;
XRefDataBase[XRef] = XRefAddr;
printf("Derived OOVPA!\n");
}
}
return cur;
}
// found nothing
return (xbaddr)nullptr;
}
// install function interception wrappers
static void EmuInstallPatches(OOVPATable *OovpaTable, uint32 OovpaTableSize, Xbe::Header *pXbeHeader)
{
xbaddr lower = pXbeHeader->dwBaseAddr;
// Find the highest address contained within an executable segment
xbaddr upper = pXbeHeader->dwBaseAddr;
Xbe::SectionHeader* headers = reinterpret_cast<Xbe::SectionHeader*>(pXbeHeader->dwSectionHeadersAddr);
for (uint32_t i = 0; i < pXbeHeader->dwSections; i++) {
xbaddr end_addr = headers[i].dwVirtualAddr + headers[i].dwVirtualSize;
if (headers[i].dwFlags.bExecutable && end_addr > upper) {
upper = end_addr;
}
}
// traverse the full OOVPA table
for(size_t a=0;a<OovpaTableSize/sizeof(OOVPATable);a++)
{
OOVPA *Oovpa = OovpaTable[a].Oovpa;
xbaddr pFunc = (xbaddr)nullptr;
if(bCacheInp && (vCacheInpIter != vCacheInp.end()))
{
pFunc = *vCacheInpIter;
++vCacheInpIter;
}
else
{
pFunc = EmuLocateFunction(Oovpa, lower, upper);
if (pFunc != (xbaddr)nullptr)
vCacheOut.push_back(pFunc);
}
if(pFunc != (xbaddr)nullptr)
{
printf("HLE: 0x%.08X -> %s\n", pFunc, OovpaTable[a].szFuncName);
if(OovpaTable[a].emuPatch == nullptr)
{
// Only place an XRef trapping patch when the OOVPA registration wasn't disabled
if ((OovpaTable[a].Flags & Flag_DontScan) == 0)
{
// Write breakpoint opcode
*(uint8_t*)pFunc = OPCODE_INT3_CC;
EmuInstallPatch(pFunc + 1, EmuXRefFailure);
}
}
else
{
EmuInstallPatch(pFunc, OovpaTable[a].emuPatch);
funcExclude[fcount++] = (void *)pFunc;
}
}
}
}
// alert for the situation where an Xref function body is hit
static void EmuXRefFailure()
{
CxbxKrnlCleanup("XRef-only function body reached. Fatal Error.");
}
#ifdef _DEBUG_TRACE
struct HLEVerifyContext {
const HLEData *main_data;
OOVPA *oovpa, *against;
const HLEData *against_data;
uint32 main_index, against_index;
};
std::string HLEErrorString(const HLEData *data, uint32 index)
{
std::string result =
"OOVPATable " + (std::string)(data->Library) + "_" + std::to_string(data->BuildVersion)
+ "[" + std::to_string(index) + "] "
+ (std::string)(data->OovpaTable[index].szFuncName);
return result;
}
void HLEError(HLEVerifyContext *context, char *format, ...)
{
std::string output = "HLE Error ";
if (context->main_data != nullptr)
output += "in " + HLEErrorString(context->main_data, context->main_index);
if (context->against != nullptr && context->against_data != nullptr)
output += ", comparing against " + HLEErrorString(context->against_data, context->against_index);
// format specific error message
char buffer[200];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
output += " : " + (std::string)buffer + (std::string)"\n";
printf(output.c_str());
}
void VerifyHLEDataBaseAgainst(HLEVerifyContext *context); // forward
void VerifyHLEOOVPA(HLEVerifyContext *context, OOVPA *oovpa)
{
if (context->against == nullptr) {
// TODO : verify XRefSaveIndex and XRef's (how?)
// verify offsets are in increasing order
uint32 prev_offset;
uint08 dummy_value;
GetOovpaEntry(oovpa, oovpa->XRefCount, prev_offset, dummy_value);
for (int p = oovpa->XRefCount + 1; p < oovpa->Count; p++) {
uint32 curr_offset;
GetOovpaEntry(oovpa, p, curr_offset, dummy_value);
if (!(curr_offset > prev_offset)) {
HLEError(context, "Lovp[%d] : Offset (0x%x) must be larger then previous offset (0x%x)",
p, curr_offset, prev_offset);
}
}
// find duplicate OOVPA's across all other data-table-oovpa's
context->oovpa = oovpa;
context->against = oovpa;
VerifyHLEDataBaseAgainst(context);
context->against = nullptr; // reset scanning state
return;
}
// prevent checking an oovpa against itself
if (context->against == oovpa)
return;
// compare {Offset, Value}-pairs between two OOVPA's
OOVPA *left = context->against, *right = oovpa;
int l = 0, r = 0;
uint32 left_offset, right_offset;
uint08 left_value, right_value;
GetOovpaEntry(left, l, left_offset, left_value);
GetOovpaEntry(right, r, right_offset, right_value);
int unique_offset_left = 0;
int unique_offset_right = 0;
int equal_offset_value = 0;
int equal_offset_different_value = 0;
while (true) {
bool left_next = true;
bool right_next = true;
if (left_offset < right_offset) {
unique_offset_left++;
right_next = false;
}
else if (left_offset > right_offset) {
unique_offset_right++;
left_next = false;
}
else if (left_value == right_value) {
equal_offset_value++;
}
else {