-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathVUShared.cpp
1942 lines (1679 loc) · 53.8 KB
/
VUShared.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
#include "VUShared.h"
#include "BitManip.h"
#include "../MIPS.h"
#include "offsetof_def.h"
#include "FpAddTruncate.h"
#include "../FpUtils.h"
#include "Vpu.h"
#define STATUS_Z 0x01
#define STATUS_S 0x02
#define STATUS_I 0x10
#define STATUS_D 0x20
#define STATUS_ZS 0x40
#define STATUS_SS 0x80
// clang-format off
const VUShared::REGISTER_PIPEINFO VUShared::g_pipeInfoQ =
{
offsetof(CMIPS, m_State.nCOP2Q),
offsetof(CMIPS, m_State.pipeQ.heldValue),
offsetof(CMIPS, m_State.pipeQ.counter)
};
const VUShared::REGISTER_PIPEINFO VUShared::g_pipeInfoP =
{
offsetof(CMIPS, m_State.nCOP2P),
offsetof(CMIPS, m_State.pipeP.heldValue),
offsetof(CMIPS, m_State.pipeP.counter)
};
const VUShared::FLAG_PIPEINFO VUShared::g_pipeInfoMac =
{
offsetof(CMIPS, m_State.nCOP2MF),
offsetof(CMIPS, m_State.pipeMac.index),
offsetof(CMIPS, m_State.pipeMac.values),
offsetof(CMIPS, m_State.pipeMac.pipeTimes)
};
const VUShared::FLAG_PIPEINFO VUShared::g_pipeInfoSticky =
{
offsetof(CMIPS, m_State.nCOP2SF),
offsetof(CMIPS, m_State.pipeSticky.index),
offsetof(CMIPS, m_State.pipeSticky.values),
offsetof(CMIPS, m_State.pipeSticky.pipeTimes)
};
const VUShared::FLAG_PIPEINFO VUShared::g_pipeInfoClip =
{
offsetof(CMIPS, m_State.nCOP2CF),
offsetof(CMIPS, m_State.pipeClip.index),
offsetof(CMIPS, m_State.pipeClip.values),
offsetof(CMIPS, m_State.pipeClip.pipeTimes)
};
// clang-format on
using namespace VUShared;
bool VUShared::DestinationHasElement(uint8 nDest, unsigned int nElement)
{
return (nDest & (1 << (nElement ^ 0x03))) != 0;
}
void VUShared::ComputeMemAccessAddr(CMipsJitter* codeGen, unsigned int baseRegister, uint32 baseOffset, uint32 destOffset, uint32 addressMask)
{
PushIntegerRegister(codeGen, baseRegister);
if(baseOffset != 0)
{
codeGen->PushCst(baseOffset);
codeGen->Add();
}
codeGen->Shl(4);
if(destOffset != 0)
{
codeGen->PushCst(destOffset);
codeGen->Add();
}
//Mask address
codeGen->PushCst(addressMask);
codeGen->And();
}
uint32 VUShared::GetDestOffset(uint8 dest)
{
assert(__builtin_popcount(dest) == 1);
if(dest & 0x0001) return 0xC;
if(dest & 0x0002) return 0x8;
if(dest & 0x0004) return 0x4;
if(dest & 0x0008) return 0x0;
return 0;
}
uint32* VUShared::GetVectorElement(CMIPS* pCtx, unsigned int nReg, unsigned int nElement)
{
switch(nElement)
{
case 0:
return &pCtx->m_State.nCOP2[nReg].nV0;
break;
case 1:
return &pCtx->m_State.nCOP2[nReg].nV1;
break;
case 2:
return &pCtx->m_State.nCOP2[nReg].nV2;
break;
case 3:
return &pCtx->m_State.nCOP2[nReg].nV3;
break;
}
return NULL;
}
size_t VUShared::GetVectorElement(unsigned int nRegister, unsigned int nElement)
{
return offsetof(CMIPS, m_State.nCOP2[nRegister].nV[nElement]);
}
uint32* VUShared::GetAccumulatorElement(CMIPS* pCtx, unsigned int nElement)
{
switch(nElement)
{
case 0:
return &pCtx->m_State.nCOP2A.nV0;
break;
case 1:
return &pCtx->m_State.nCOP2A.nV1;
break;
case 2:
return &pCtx->m_State.nCOP2A.nV2;
break;
case 3:
return &pCtx->m_State.nCOP2A.nV3;
break;
}
return NULL;
}
size_t VUShared::GetAccumulatorElement(unsigned int nElement)
{
return offsetof(CMIPS, m_State.nCOP2A.nV[nElement]);
}
void TestVectorNaN(CMIPS* context, uint32 dest, uint32 offset)
{
for(int i = 0; i < 4; i++)
{
if(!DestinationHasElement(dest, i)) continue;
FRAMEWORK_MAYBE_UNUSED uint32 value = reinterpret_cast<uint32*>(context)[(offset / 4) + i];
assert((value & 0x7F800000) != 0x7F800000);
}
}
void CheckVectorNaN(CMipsJitter* codeGen, uint8 dest, size_t vector)
{
codeGen->PushCtx();
codeGen->PushCst(dest);
codeGen->PushCst(vector);
codeGen->Call(reinterpret_cast<void*>(&TestVectorNaN), 3, Jitter::CJitter::RETURN_VALUE_NONE);
}
void VUShared::PullVector(CMipsJitter* codeGen, uint8 dest, size_t vector)
{
if(dest == 0)
{
codeGen->PullTop();
return;
}
assert(vector != offsetof(CMIPS, m_State.nCOP2[0]));
codeGen->MD_PullRel(vector,
DestinationHasElement(dest, 0),
DestinationHasElement(dest, 1),
DestinationHasElement(dest, 2),
DestinationHasElement(dest, 3));
}
void VUShared::PushBcElement(CMipsJitter* codeGen, size_t offset)
{
if(
(offset == offsetof(CMIPS, m_State.nCOP2[0].nV0)) ||
(offset == offsetof(CMIPS, m_State.nCOP2[0].nV1)) ||
(offset == offsetof(CMIPS, m_State.nCOP2[0].nV2)))
{
codeGen->MD_PushCstExpand(0.0f);
}
else if(offset == offsetof(CMIPS, m_State.nCOP2[0].nV3))
{
codeGen->MD_PushCstExpand(1.0f);
}
else if(
(offset >= offsetof(CMIPS, m_State.nCOP2[1].nV0)) &&
(offset <= offsetof(CMIPS, m_State.nCOP2[31].nV3)))
{
size_t vectorOffset = offset & ~(sizeof(uint128) - 1);
size_t vectorElem = (offset - offsetof(CMIPS, m_State.nCOP2[0].nV0)) % sizeof(uint128);
codeGen->MD_PushRelElementExpand(vectorOffset, vectorElem / 4);
}
else
{
codeGen->MD_PushRelExpand(offset);
}
}
void VUShared::PushIntegerRegister(CMipsJitter* codeGen, unsigned int regIdx)
{
regIdx &= 0xF;
if(regIdx == 0)
{
codeGen->PushCst(0);
}
else
{
codeGen->PushRel(offsetof(CMIPS, m_State.nCOP2VI[regIdx]));
}
}
void VUShared::PullIntegerRegister(CMipsJitter* codeGen, unsigned int regIdx)
{
regIdx &= 0xF;
assert(regIdx != 0);
codeGen->PullRel(offsetof(CMIPS, m_State.nCOP2VI[regIdx]));
}
void VUShared::TestSZFlags(CMipsJitter* codeGen, uint8 dest, size_t regOffset, uint32 relativePipeTime, uint32 compileHints)
{
codeGen->MD_PushRel(regOffset);
codeGen->MD_MakeSignZero();
//Clear flags of inactive FMAC units
if(dest != 0xF)
{
codeGen->PushCst((dest << 4) | dest);
codeGen->And();
}
//Update sticky flags
{
codeGen->PushTop();
//Load previous value
{
codeGen->PushRelAddrRef(offsetof(CMIPS, m_State.pipeSticky.values));
codeGen->PushRel(offsetof(CMIPS, m_State.pipeSticky.index));
codeGen->PushCst(1);
codeGen->Sub();
codeGen->PushCst(FLAG_PIPELINE_SLOTS - 1);
codeGen->And();
codeGen->LoadFromRefIdx();
}
codeGen->Or();
QueueInFlagPipeline(g_pipeInfoSticky, codeGen, LATENCY_MAC, relativePipeTime);
}
if((compileHints & COMPILEHINT_SKIP_FMAC_UPDATE) == 0)
{
QueueInFlagPipeline(g_pipeInfoMac, codeGen, LATENCY_MAC, relativePipeTime);
}
else
{
codeGen->PullTop();
}
}
void VUShared::GetStatus(CMipsJitter* codeGen, size_t dstOffset, uint32 relativePipeTime)
{
//Get STATUS flag using information from other values (MACflags and sticky flags)
CheckFlagPipeline(g_pipeInfoMac, codeGen, relativePipeTime);
CheckFlagPipeline(g_pipeInfoSticky, codeGen, relativePipeTime);
//Reset result
codeGen->PushCst(0);
codeGen->PullRel(dstOffset);
//Check Z flag
codeGen->PushRel(offsetof(CMIPS, m_State.nCOP2MF));
codeGen->PushCst(0x000F);
codeGen->And();
codeGen->PushCst(0);
codeGen->BeginIf(Jitter::CONDITION_NE);
{
codeGen->PushRel(dstOffset);
codeGen->PushCst(STATUS_Z);
codeGen->Or();
codeGen->PullRel(dstOffset);
}
codeGen->EndIf();
//Check S flag
codeGen->PushRel(offsetof(CMIPS, m_State.nCOP2MF));
codeGen->PushCst(0x00F0);
codeGen->And();
codeGen->PushCst(0);
codeGen->BeginIf(Jitter::CONDITION_NE);
{
codeGen->PushRel(dstOffset);
codeGen->PushCst(STATUS_S);
codeGen->Or();
codeGen->PullRel(dstOffset);
}
codeGen->EndIf();
//Check ZS flag
codeGen->PushRel(offsetof(CMIPS, m_State.nCOP2SF));
codeGen->PushCst(0x000F);
codeGen->And();
codeGen->PushCst(0);
codeGen->BeginIf(Jitter::CONDITION_NE);
{
codeGen->PushRel(dstOffset);
codeGen->PushCst(STATUS_ZS);
codeGen->Or();
codeGen->PullRel(dstOffset);
}
codeGen->EndIf();
//Check SS flag
codeGen->PushRel(offsetof(CMIPS, m_State.nCOP2SF));
codeGen->PushCst(0x00F0);
codeGen->And();
codeGen->PushCst(0);
codeGen->BeginIf(Jitter::CONDITION_NE);
{
codeGen->PushRel(dstOffset);
codeGen->PushCst(STATUS_SS);
codeGen->Or();
codeGen->PullRel(dstOffset);
}
codeGen->EndIf();
//Check D flag
codeGen->PushRel(offsetof(CMIPS, m_State.nCOP2DF));
codeGen->PushCst(0);
codeGen->BeginIf(Jitter::CONDITION_NE);
{
codeGen->PushRel(dstOffset);
codeGen->PushCst(STATUS_D);
codeGen->Or();
codeGen->PullRel(dstOffset);
}
codeGen->EndIf();
//TODO: Check other flags
}
void VUShared::SetStatus(CMipsJitter* codeGen, size_t srcOffset)
{
//Only sticky flags can be set
//Clear sticky flags
codeGen->PushCst(0);
codeGen->PullRel(offsetof(CMIPS, m_State.nCOP2SF));
codeGen->PushRel(srcOffset);
codeGen->PushCst(STATUS_ZS);
codeGen->And();
codeGen->PushCst(0);
codeGen->BeginIf(Jitter::CONDITION_NE);
{
codeGen->PushCst(0x000F);
codeGen->PushRel(offsetof(CMIPS, m_State.nCOP2SF));
codeGen->Or();
codeGen->PullRel(offsetof(CMIPS, m_State.nCOP2SF));
}
codeGen->EndIf();
codeGen->PushRel(srcOffset);
codeGen->PushCst(STATUS_SS);
codeGen->And();
codeGen->PushCst(0);
codeGen->BeginIf(Jitter::CONDITION_NE);
{
codeGen->PushCst(0x00F0);
codeGen->PushRel(offsetof(CMIPS, m_State.nCOP2SF));
codeGen->Or();
codeGen->PullRel(offsetof(CMIPS, m_State.nCOP2SF));
}
codeGen->EndIf();
codeGen->PushRel(offsetof(CMIPS, m_State.nCOP2SF));
ResetFlagPipeline(VUShared::g_pipeInfoSticky, codeGen);
}
void VUShared::ADD_base(CMipsJitter* codeGen, uint8 dest, size_t fd, size_t fs, size_t ft, bool expand, uint32 relativePipeTime, uint32 compileHints)
{
codeGen->MD_PushRel(fs);
codeGen->MD_ClampS();
if(expand)
{
PushBcElement(codeGen, ft);
}
else
{
codeGen->MD_PushRel(ft);
}
codeGen->MD_AddS();
PullVector(codeGen, dest, fd);
TestSZFlags(codeGen, dest, fd, relativePipeTime, compileHints);
}
void VUShared::ADDA_base(CMipsJitter* codeGen, uint8 dest, size_t fs, size_t ft, bool expand, uint32 relativePipeTime, uint32 compileHints)
{
codeGen->MD_PushRel(fs);
if(expand)
{
PushBcElement(codeGen, ft);
}
else
{
codeGen->MD_PushRel(ft);
}
codeGen->MD_AddS();
PullVector(codeGen, dest, offsetof(CMIPS, m_State.nCOP2A));
TestSZFlags(codeGen, dest, offsetof(CMIPS, m_State.nCOP2A), relativePipeTime, compileHints);
}
void VUShared::MADD_base(CMipsJitter* codeGen, uint8 dest, size_t fd, size_t fs, size_t ft, bool expand, uint32 relativePipeTime, uint32 compileHints)
{
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2A));
codeGen->MD_PushRel(fs);
//Clamping is needed by Baldur's Gate Deadly Alliance here because it multiplies junk values (potentially NaN/INF) by 0
codeGen->MD_ClampS();
if(expand)
{
PushBcElement(codeGen, ft);
codeGen->MD_ClampS(); //Fatal Frame 1's door-blocking bug can be fixed by this
}
else
{
codeGen->MD_PushRel(ft);
}
codeGen->MD_MulS();
codeGen->MD_AddS();
PullVector(codeGen, dest, fd);
TestSZFlags(codeGen, dest, fd, relativePipeTime, compileHints);
}
void VUShared::MADDA_base(CMipsJitter* codeGen, uint8 dest, size_t fs, size_t ft, bool expand, uint32 relativePipeTime, uint32 compileHints)
{
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2A));
codeGen->MD_PushRel(fs);
//Clamping is needed by Dynasty Warriors 2 here because it multiplies junk values (potentially NaN/INF) by some other value
codeGen->MD_ClampS();
if(expand)
{
PushBcElement(codeGen, ft);
}
else
{
codeGen->MD_PushRel(ft);
}
codeGen->MD_ClampS();
codeGen->MD_MulS();
codeGen->MD_AddS();
PullVector(codeGen, dest, offsetof(CMIPS, m_State.nCOP2A));
TestSZFlags(codeGen, dest, offsetof(CMIPS, m_State.nCOP2A), relativePipeTime, compileHints);
}
void VUShared::SUB_base(CMipsJitter* codeGen, uint8 dest, size_t fd, size_t fs, size_t ft, bool expand, uint32 relativePipeTime, uint32 compileHints)
{
codeGen->MD_PushRel(fs);
codeGen->MD_ClampS();
if(expand)
{
PushBcElement(codeGen, ft);
}
else
{
codeGen->MD_PushRel(ft);
}
codeGen->MD_ClampS();
codeGen->MD_SubS();
PullVector(codeGen, dest, fd);
TestSZFlags(codeGen, dest, fd, relativePipeTime, compileHints);
}
void VUShared::SUBA_base(CMipsJitter* codeGen, uint8 dest, size_t fs, size_t ft, bool expand, uint32 relativePipeTime, uint32 compileHints)
{
codeGen->MD_PushRel(fs);
if(expand)
{
PushBcElement(codeGen, ft);
}
else
{
codeGen->MD_PushRel(ft);
}
codeGen->MD_SubS();
PullVector(codeGen, dest, offsetof(CMIPS, m_State.nCOP2A));
TestSZFlags(codeGen, dest, offsetof(CMIPS, m_State.nCOP2A), relativePipeTime, compileHints);
}
void VUShared::MSUB_base(CMipsJitter* codeGen, uint8 dest, size_t fd, size_t fs, size_t ft, bool expand, uint32 relativePipeTime, uint32 compileHints)
{
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2A));
codeGen->MD_PushRel(fs);
if(expand)
{
PushBcElement(codeGen, ft);
}
else
{
codeGen->MD_PushRel(ft);
}
codeGen->MD_MulS();
codeGen->MD_SubS();
PullVector(codeGen, dest, fd);
TestSZFlags(codeGen, dest, fd, relativePipeTime, compileHints);
}
void VUShared::MSUBA_base(CMipsJitter* codeGen, uint8 dest, size_t fs, size_t ft, bool expand, uint32 relativePipeTime, uint32 compileHints)
{
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2A));
codeGen->MD_PushRel(fs);
if(expand)
{
PushBcElement(codeGen, ft);
}
else
{
codeGen->MD_PushRel(ft);
}
codeGen->MD_MulS();
codeGen->MD_SubS();
PullVector(codeGen, dest, offsetof(CMIPS, m_State.nCOP2A));
TestSZFlags(codeGen, dest, offsetof(CMIPS, m_State.nCOP2A), relativePipeTime, compileHints);
}
void VUShared::MUL_base(CMipsJitter* codeGen, uint8 dest, size_t fd, size_t fs, size_t ft, bool expand, uint32 relativePipeTime, uint32 compileHints)
{
codeGen->MD_PushRel(fs);
codeGen->MD_ClampS();
if(expand)
{
PushBcElement(codeGen, ft);
}
else
{
codeGen->MD_PushRel(ft);
}
codeGen->MD_ClampS();
codeGen->MD_MulS();
PullVector(codeGen, dest, fd);
TestSZFlags(codeGen, dest, fd, relativePipeTime, compileHints);
}
void VUShared::MULA_base(CMipsJitter* codeGen, uint8 dest, size_t fs, size_t ft, bool expand, uint32 relativePipeTime, uint32 compileHints)
{
codeGen->MD_PushRel(fs);
codeGen->MD_ClampS();
if(expand)
{
PushBcElement(codeGen, ft);
}
else
{
codeGen->MD_PushRel(ft);
}
codeGen->MD_MulS();
PullVector(codeGen, dest, offsetof(CMIPS, m_State.nCOP2A));
TestSZFlags(codeGen, dest, offsetof(CMIPS, m_State.nCOP2A), relativePipeTime, compileHints);
}
void VUShared::MINI_base(CMipsJitter* codeGen, uint8 dest, size_t fd, size_t fs, size_t ft, bool expand)
{
const auto pushFt = [&]() {
if(expand)
{
PushBcElement(codeGen, ft);
}
else
{
codeGen->MD_PushRel(ft);
}
};
codeGen->MD_PushRel(fs);
codeGen->MD_ClampS();
pushFt();
codeGen->MD_ClampS();
codeGen->MD_CmpLtS();
auto cmp = codeGen->GetTopCursor();
//Mask FT
codeGen->PushTop();
codeGen->MD_Not();
pushFt();
codeGen->MD_And();
//Mask FS
codeGen->PushCursor(cmp);
codeGen->MD_PushRel(fs);
codeGen->MD_And();
codeGen->MD_Or();
PullVector(codeGen, dest, fd);
codeGen->PullTop();
}
void VUShared::MAX_base(CMipsJitter* codeGen, uint8 dest, size_t fd, size_t fs, size_t ft, bool expand)
{
const auto pushFt = [&]() {
if(expand)
{
PushBcElement(codeGen, ft);
}
else
{
codeGen->MD_PushRel(ft);
}
};
codeGen->MD_PushRel(fs);
codeGen->MD_ClampS();
pushFt();
codeGen->MD_ClampS();
codeGen->MD_CmpGtS();
auto cmp = codeGen->GetTopCursor();
//Mask FT
codeGen->PushTop();
codeGen->MD_Not();
pushFt();
codeGen->MD_And();
//Mask FS
codeGen->PushCursor(cmp);
codeGen->MD_PushRel(fs);
codeGen->MD_And();
codeGen->MD_Or();
PullVector(codeGen, dest, fd);
codeGen->PullTop();
}
void VUShared::ABS(CMipsJitter* codeGen, uint8 nDest, uint8 nFt, uint8 nFs)
{
if(nFt == 0) return;
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2[nFs]));
codeGen->MD_AbsS();
PullVector(codeGen, nDest, offsetof(CMIPS, m_State.nCOP2[nFt]));
}
void VUShared::ADD(CMipsJitter* codeGen, uint8 dest, uint8 fd, uint8 fs, uint8 ft, uint32 relativePipeTime, uint32 compileHints)
{
ADD_base(codeGen, dest,
offsetof(CMIPS, m_State.nCOP2[(fd != 0) ? fd : 32]),
offsetof(CMIPS, m_State.nCOP2[fs]),
offsetof(CMIPS, m_State.nCOP2[ft]),
false, relativePipeTime, compileHints);
}
void VUShared::ADDbc(CMipsJitter* codeGen, uint8 dest, uint8 fd, uint8 fs, uint8 ft, uint8 bc, uint32 relativePipeTime, uint32 compileHints)
{
if((fd != 0) && (ft == 0) && (bc != 3))
{
//Using VF0 (other than W component) as broadcast value, which basically means add with 0.
//Do a simple move to the destination register to avoid clamping which can alter the other addend.
//Piposaru 2001 uses this to move some integer values (0xFFFF8000) between VF regs.
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2[fs]));
PullVector(codeGen, dest, offsetof(CMIPS, m_State.nCOP2[fd]));
TestSZFlags(codeGen, dest, offsetof(CMIPS, m_State.nCOP2[fd]), relativePipeTime, compileHints);
}
else
{
ADD_base(codeGen, dest,
offsetof(CMIPS, m_State.nCOP2[(fd != 0) ? fd : 32]),
offsetof(CMIPS, m_State.nCOP2[fs]),
offsetof(CMIPS, m_State.nCOP2[ft].nV[bc]),
true, relativePipeTime, compileHints);
}
}
void VUShared::ADDi(CMipsJitter* codeGen, uint8 nDest, uint8 nFd, uint8 nFs, uint32 relativePipeTime, uint32 compileHints)
{
if(nFd == 0)
{
//Use the temporary register to store the result
nFd = 32;
}
//Use FpAddTruncate on all platforms, except JavaScript
//On JavaScript, using it doesn't seem to help Tri-Ace games
//there's probably some other rounding issue on that platform
#if !defined(__EMSCRIPTEN__)
if(compileHints & COMPILEHINT_USE_ACCURATE_ADDI)
{
for(unsigned int i = 0; i < 4; i++)
{
if(!VUShared::DestinationHasElement(nDest, i)) continue;
codeGen->PushRel(offsetof(CMIPS, m_State.nCOP2[nFs].nV[i]));
codeGen->PushRel(offsetof(CMIPS, m_State.nCOP2I));
codeGen->Call(reinterpret_cast<void*>(&FpAddTruncate), 2, Jitter::CJitter::RETURN_VALUE_32);
codeGen->PullRel(offsetof(CMIPS, m_State.nCOP2[nFd].nV[i]));
}
}
else
#endif
{
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2[nFs]));
codeGen->MD_PushRelExpand(offsetof(CMIPS, m_State.nCOP2I));
codeGen->MD_AddS();
PullVector(codeGen, nDest, offsetof(CMIPS, m_State.nCOP2[nFd]));
}
TestSZFlags(codeGen, nDest, offsetof(CMIPS, m_State.nCOP2[nFd]), relativePipeTime, compileHints);
}
void VUShared::ADDq(CMipsJitter* codeGen, uint8 dest, uint8 fd, uint8 fs, uint32 relativePipeTime, uint32 compileHints)
{
ADD_base(codeGen, dest,
offsetof(CMIPS, m_State.nCOP2[(fd != 0) ? fd : 32]),
offsetof(CMIPS, m_State.nCOP2[fs]),
offsetof(CMIPS, m_State.nCOP2Q),
true, relativePipeTime, compileHints);
}
void VUShared::ADDA(CMipsJitter* codeGen, uint8 dest, uint8 fs, uint8 ft, uint32 relativePipeTime, uint32 compileHints)
{
ADDA_base(codeGen, dest,
offsetof(CMIPS, m_State.nCOP2[fs]),
offsetof(CMIPS, m_State.nCOP2[ft]),
false, relativePipeTime, compileHints);
}
void VUShared::ADDAbc(CMipsJitter* codeGen, uint8 dest, uint8 fs, uint8 ft, uint8 bc, uint32 relativePipeTime, uint32 compileHints)
{
ADDA_base(codeGen, dest,
offsetof(CMIPS, m_State.nCOP2[fs]),
offsetof(CMIPS, m_State.nCOP2[ft].nV[bc]),
true, relativePipeTime, compileHints);
}
void VUShared::ADDAi(CMipsJitter* codeGen, uint8 dest, uint8 fs, uint32 relativePipeTime, uint32 compileHints)
{
ADDA_base(codeGen, dest,
offsetof(CMIPS, m_State.nCOP2[fs]),
offsetof(CMIPS, m_State.nCOP2I),
true, relativePipeTime, compileHints);
}
void VUShared::ADDAq(CMipsJitter* codeGen, uint8 dest, uint8 fs, uint32 relativePipeTime, uint32 compileHints)
{
ADDA_base(codeGen, dest,
offsetof(CMIPS, m_State.nCOP2[fs]),
offsetof(CMIPS, m_State.nCOP2Q),
true, relativePipeTime, compileHints);
}
void VUShared::CLIP(CMipsJitter* codeGen, uint8 nFs, uint8 nFt, uint32 relativePipeTime)
{
size_t tempOffset = offsetof(CMIPS, m_State.nCOP2T);
//Load previous value
{
codeGen->PushRelAddrRef(offsetof(CMIPS, m_State.pipeClip.values));
codeGen->PushRel(offsetof(CMIPS, m_State.pipeClip.index));
codeGen->PushCst(1);
codeGen->Sub();
codeGen->PushCst(FLAG_PIPELINE_SLOTS - 1);
codeGen->And();
codeGen->LoadFromRefIdx();
codeGen->PullRel(tempOffset);
}
//Create some space for the new test results
codeGen->PushRel(tempOffset);
codeGen->Shl(6);
codeGen->PullRel(tempOffset);
for(unsigned int i = 0; i < 3; i++)
{
//c > +|w|
codeGen->FP_PushRel32(offsetof(CMIPS, m_State.nCOP2[nFs].nV[i]));
codeGen->FP_PushRel32(offsetof(CMIPS, m_State.nCOP2[nFt].nV[3]));
codeGen->FP_AbsS();
codeGen->FP_CmpS(Jitter::CONDITION_AB);
codeGen->PushCst(0);
codeGen->BeginIf(Jitter::CONDITION_NE);
{
codeGen->PushRel(tempOffset);
codeGen->PushCst(1 << ((i * 2) + 0));
codeGen->Or();
codeGen->PullRel(tempOffset);
}
codeGen->EndIf();
//c < -|w|
codeGen->FP_PushRel32(offsetof(CMIPS, m_State.nCOP2[nFs].nV[i]));
codeGen->FP_PushRel32(offsetof(CMIPS, m_State.nCOP2[nFt].nV[3]));
codeGen->FP_AbsS();
codeGen->FP_NegS();
codeGen->FP_CmpS(Jitter::CONDITION_BL);
codeGen->PushCst(0);
codeGen->BeginIf(Jitter::CONDITION_NE);
{
codeGen->PushRel(tempOffset);
codeGen->PushCst(1 << ((i * 2) + 1));
codeGen->Or();
codeGen->PullRel(tempOffset);
}
codeGen->EndIf();
}
codeGen->PushRel(tempOffset);
QueueInFlagPipeline(g_pipeInfoClip, codeGen, LATENCY_MAC, relativePipeTime);
}
void VUShared::DIV(CMipsJitter* codeGen, uint8 nFs, uint8 nFsf, uint8 nFt, uint8 nFtf, uint32 relativePipeTime)
{
size_t destination = g_pipeInfoQ.heldValue;
QueueInPipeline(g_pipeInfoQ, codeGen, LATENCY_DIV, relativePipeTime);
//Check for zero
FpUtils::IsZero(codeGen, GetVectorElement(nFt, nFtf));
codeGen->BeginIf(Jitter::CONDITION_EQ);
{
FpUtils::ComputeDivisionByZero(codeGen,
GetVectorElement(nFs, nFsf),
GetVectorElement(nFt, nFtf));
codeGen->PullRel(destination);
codeGen->PushCst(1);
codeGen->PullRel(offsetof(CMIPS, m_State.nCOP2DF));
}
codeGen->Else();
{
codeGen->FP_PushRel32(GetVectorElement(nFs, nFsf));
codeGen->FP_PushRel32(GetVectorElement(nFt, nFtf));
codeGen->FP_DivS();
codeGen->FP_PullRel32(destination);
codeGen->PushCst(0);
codeGen->PullRel(offsetof(CMIPS, m_State.nCOP2DF));
}
codeGen->EndIf();
}
void VUShared::FTOI0(CMipsJitter* codeGen, uint8 nDest, uint8 nFt, uint8 nFs)
{
if(nFt == 0) return;
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2[nFs]));
codeGen->MD_ToWordTruncate();
PullVector(codeGen, nDest, offsetof(CMIPS, m_State.nCOP2[nFt]));
}
void VUShared::FTOI4(CMipsJitter* codeGen, uint8 nDest, uint8 nFt, uint8 nFs)
{
if(nFt == 0) return;
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2[nFs]));
codeGen->MD_PushCstExpand(16.0f);
codeGen->MD_MulS();
codeGen->MD_ToWordTruncate();
PullVector(codeGen, nDest, offsetof(CMIPS, m_State.nCOP2[nFt]));
}
void VUShared::FTOI12(CMipsJitter* codeGen, uint8 nDest, uint8 nFt, uint8 nFs)
{
if(nFt == 0) return;
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2[nFs]));
codeGen->MD_PushCstExpand(4096.0f);
codeGen->MD_MulS();
codeGen->MD_ToWordTruncate();
PullVector(codeGen, nDest, offsetof(CMIPS, m_State.nCOP2[nFt]));
}
void VUShared::FTOI15(CMipsJitter* codeGen, uint8 nDest, uint8 nFt, uint8 nFs)
{
if(nFt == 0) return;
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2[nFs]));
codeGen->MD_PushCstExpand(32768.0f);
codeGen->MD_MulS();
codeGen->MD_ToWordTruncate();
PullVector(codeGen, nDest, offsetof(CMIPS, m_State.nCOP2[nFt]));
}
void VUShared::IADD(CMipsJitter* codeGen, uint8 id, uint8 is, uint8 it)
{
if((id & 0xF) == 0) return;
PushIntegerRegister(codeGen, is);
PushIntegerRegister(codeGen, it);
codeGen->Add();
PullIntegerRegister(codeGen, id);
}
void VUShared::IADDI(CMipsJitter* codeGen, uint8 it, uint8 is, uint8 imm5)
{
if((it & 0xF) == 0) return;
PushIntegerRegister(codeGen, is);
codeGen->PushCst(imm5 | ((imm5 & 0x10) != 0 ? 0xFFFFFFE0 : 0x0));
codeGen->Add();
PullIntegerRegister(codeGen, it);
}
void VUShared::IAND(CMipsJitter* codeGen, uint8 id, uint8 is, uint8 it)
{
if((id & 0xF) == 0) return;
PushIntegerRegister(codeGen, is);
PushIntegerRegister(codeGen, it);
codeGen->And();
PullIntegerRegister(codeGen, id);
}
void VUShared::ILWbase(CMipsJitter* codeGen, uint8 it)
{
codeGen->LoadFromRefIdx(1);
PullIntegerRegister(codeGen, it);
}
void VUShared::ILWR(CMipsJitter* codeGen, uint8 dest, uint8 it, uint8 is, uint32 addressMask)
{
if((it & 0xF) == 0) return;
//Compute address
codeGen->PushRelRef(offsetof(CMIPS, m_vuMem));
VUShared::ComputeMemAccessAddr(codeGen, is, 0, GetDestOffset(dest), addressMask);
ILWbase(codeGen, it);
}
void VUShared::IOR(CMipsJitter* codeGen, uint8 id, uint8 is, uint8 it)
{
if((id & 0xF) == 0) return;
PushIntegerRegister(codeGen, is);
PushIntegerRegister(codeGen, it);
codeGen->Or();
PullIntegerRegister(codeGen, id);
}
void VUShared::ISUB(CMipsJitter* codeGen, uint8 id, uint8 is, uint8 it)
{
if((id & 0xF) == 0) return;
PushIntegerRegister(codeGen, is);
PushIntegerRegister(codeGen, it);
codeGen->Sub();
PullIntegerRegister(codeGen, id);
}
void VUShared::ITOF0(CMipsJitter* codeGen, uint8 dest, uint8 ft, uint8 fs)
{
if(ft == 0) return;
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2[fs]));
codeGen->MD_ToSingle();
PullVector(codeGen, dest, offsetof(CMIPS, m_State.nCOP2[ft]));
}
void VUShared::ITOF4(CMipsJitter* codeGen, uint8 dest, uint8 ft, uint8 fs)
{
if(ft == 0) return;
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2[fs]));
codeGen->MD_ToSingle();
codeGen->MD_PushCstExpand(16.0f);
codeGen->MD_DivS();
PullVector(codeGen, dest, offsetof(CMIPS, m_State.nCOP2[ft]));
}
void VUShared::ITOF12(CMipsJitter* codeGen, uint8 dest, uint8 ft, uint8 fs)
{
if(ft == 0) return;
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2[fs]));
codeGen->MD_ToSingle();
codeGen->MD_PushCstExpand(4096.0f);
codeGen->MD_DivS();
PullVector(codeGen, dest, offsetof(CMIPS, m_State.nCOP2[ft]));
}
void VUShared::ITOF15(CMipsJitter* codeGen, uint8 dest, uint8 ft, uint8 fs)
{
if(ft == 0) return;
codeGen->MD_PushRel(offsetof(CMIPS, m_State.nCOP2[fs]));
codeGen->MD_ToSingle();
codeGen->MD_PushCstExpand(32768.0f);