-
Notifications
You must be signed in to change notification settings - Fork 11
/
gte.c
2391 lines (1962 loc) · 55 KB
/
gte.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
/***************************************************************************
* Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team *
* *
* 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., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/
/*
* GTE functions.
*/
#include "gte.h"
#ifdef GTE_DUMP
#define G_OP(name,delay) fprintf(gteLog, "* : %08X : %02d : %s\n", psxRegs.code, delay, name);
#define G_SD(reg) fprintf(gteLog, "+D%02d : %08X\n", reg, psxRegs.CP2D.r[reg]);
#define G_SC(reg) fprintf(gteLog, "+C%02d : %08X\n", reg, psxRegs.CP2C.r[reg]);
#define G_GD(reg) fprintf(gteLog, "-D%02d : %08X\n", reg, psxRegs.CP2D.r[reg]);
#define G_GC(reg) fprintf(gteLog, "-C%02d : %08X\n", reg, psxRegs.CP2C.r[reg]);
#else
#define G_OP(name,delay)
#define G_SD(reg)
#define G_SC(reg)
#define G_GD(reg)
#define G_GC(reg)
#endif
#define SUM_FLAG if(gteFLAG & 0x7F87E000) gteFLAG |= 0x80000000;
#if defined(HW_RVL) || defined(HW_DOL) || defined(BIG_ENDIAN)
#define SEL16(n) ((n)^1)
#define SEL8(n) ((n)^3)
#else
#define SEL16(n) (n)
#define SEL8(n) (n)
#endif
#define gteVX0 ((s16*)psxRegs.CP2D.r)[SEL16(0)]
#define gteVY0 ((s16*)psxRegs.CP2D.r)[SEL16(1)]
#define gteVZ0 ((s16*)psxRegs.CP2D.r)[SEL16(2)]
#define gteVX1 ((s16*)psxRegs.CP2D.r)[SEL16(4)]
#define gteVY1 ((s16*)psxRegs.CP2D.r)[SEL16(5)]
#define gteVZ1 ((s16*)psxRegs.CP2D.r)[SEL16(6)]
#define gteVX2 ((s16*)psxRegs.CP2D.r)[SEL16(8)]
#define gteVY2 ((s16*)psxRegs.CP2D.r)[SEL16(9)]
#define gteVZ2 ((s16*)psxRegs.CP2D.r)[SEL16(10)]
#define gteRGB psxRegs.CP2D.r[6]
#define gteOTZ ((s16*)psxRegs.CP2D.r)[SEL16(7*2)]
#define gteIR0 ((s32*)psxRegs.CP2D.r)[8]
#define gteIR1 ((s32*)psxRegs.CP2D.r)[9]
#define gteIR2 ((s32*)psxRegs.CP2D.r)[10]
#define gteIR3 ((s32*)psxRegs.CP2D.r)[11]
#define gteSXY0 ((s32*)psxRegs.CP2D.r)[12]
#define gteSXY1 ((s32*)psxRegs.CP2D.r)[13]
#define gteSXY2 ((s32*)psxRegs.CP2D.r)[14]
#define gteSXYP ((s32*)psxRegs.CP2D.r)[15]
#define gteSX0 ((s16*)psxRegs.CP2D.r)[SEL16(12*2)]
#define gteSY0 ((s16*)psxRegs.CP2D.r)[SEL16(12*2+1)]
#define gteSX1 ((s16*)psxRegs.CP2D.r)[SEL16(13*2)]
#define gteSY1 ((s16*)psxRegs.CP2D.r)[SEL16(13*2+1)]
#define gteSX2 ((s16*)psxRegs.CP2D.r)[SEL16(14*2)]
#define gteSY2 ((s16*)psxRegs.CP2D.r)[SEL16(14*2+1)]
#define gteSXP ((s16*)psxRegs.CP2D.r)[SEL16(15*2)]
#define gteSYP ((s16*)psxRegs.CP2D.r)[SEL16(15*2+1)]
#define gteSZx ((u16*)psxRegs.CP2D.r)[SEL16(16*2)]
#define gteSZ0 ((u16*)psxRegs.CP2D.r)[SEL16(17*2)]
#define gteSZ1 ((u16*)psxRegs.CP2D.r)[SEL16(18*2)]
#define gteSZ2 ((u16*)psxRegs.CP2D.r)[SEL16(19*2)]
#define gteRGB0 psxRegs.CP2D.r[20]
#define gteRGB1 psxRegs.CP2D.r[21]
#define gteRGB2 psxRegs.CP2D.r[22]
#define gteMAC0 psxRegs.CP2D.r[24]
#define gteMAC1 ((s32*)psxRegs.CP2D.r)[25]
#define gteMAC2 ((s32*)psxRegs.CP2D.r)[26]
#define gteMAC3 ((s32*)psxRegs.CP2D.r)[27]
#define gteIRGB psxRegs.CP2D.r[28]
#define gteORGB psxRegs.CP2D.r[29]
#define gteLZCS psxRegs.CP2D.r[30]
#define gteLZCR psxRegs.CP2D.r[31]
#define gteR ((u8 *)psxRegs.CP2D.r)[SEL8(6*4)]
#define gteG ((u8 *)psxRegs.CP2D.r)[SEL8(6*4+1)]
#define gteB ((u8 *)psxRegs.CP2D.r)[SEL8(6*4+2)]
#define gteCODE ((u8 *)psxRegs.CP2D.r)[SEL8(6*4+3)]
#define gteC gteCODE
#define gteR0 ((u8 *)psxRegs.CP2D.r)[SEL8(20*4)]
#define gteG0 ((u8 *)psxRegs.CP2D.r)[SEL8(20*4+1)]
#define gteB0 ((u8 *)psxRegs.CP2D.r)[SEL8(20*4+2)]
#define gteCODE0 ((u8 *)psxRegs.CP2D.r)[SEL8(20*4+3)]
#define gteC0 gteCODE0
#define gteR1 ((u8 *)psxRegs.CP2D.r)[SEL8(21*4)]
#define gteG1 ((u8 *)psxRegs.CP2D.r)[SEL8(21*4+1)]
#define gteB1 ((u8 *)psxRegs.CP2D.r)[SEL8(21*4+2)]
#define gteCODE1 ((u8 *)psxRegs.CP2D.r)[SEL8(21*4+3)]
#define gteC1 gteCODE1
#define gteR2 ((u8 *)psxRegs.CP2D.r)[SEL8(22*4)]
#define gteG2 ((u8 *)psxRegs.CP2D.r)[SEL8(22*4+1)]
#define gteB2 ((u8 *)psxRegs.CP2D.r)[SEL8(22*4+2)]
#define gteCODE2 ((u8 *)psxRegs.CP2D.r)[SEL8(22*4+3)]
#define gteC2 gteCODE2
#define gteR11 ((s16*)psxRegs.CP2C.r)[SEL16(0)]
#define gteR12 ((s16*)psxRegs.CP2C.r)[SEL16(1)]
#define gteR13 ((s16*)psxRegs.CP2C.r)[SEL16(2)]
#define gteR21 ((s16*)psxRegs.CP2C.r)[SEL16(3)]
#define gteR22 ((s16*)psxRegs.CP2C.r)[SEL16(4)]
#define gteR23 ((s16*)psxRegs.CP2C.r)[SEL16(5)]
#define gteR31 ((s16*)psxRegs.CP2C.r)[SEL16(6)]
#define gteR32 ((s16*)psxRegs.CP2C.r)[SEL16(7)]
#define gteR33 ((s16*)psxRegs.CP2C.r)[SEL16(8)]
#define gteTRX ((s32*)psxRegs.CP2C.r)[5]
#define gteTRY ((s32*)psxRegs.CP2C.r)[6]
#define gteTRZ ((s32*)psxRegs.CP2C.r)[7]
#define gteL11 ((s16*)psxRegs.CP2C.r)[SEL16(16)]
#define gteL12 ((s16*)psxRegs.CP2C.r)[SEL16(17)]
#define gteL13 ((s16*)psxRegs.CP2C.r)[SEL16(18)]
#define gteL21 ((s16*)psxRegs.CP2C.r)[SEL16(19)]
#define gteL22 ((s16*)psxRegs.CP2C.r)[SEL16(20)]
#define gteL23 ((s16*)psxRegs.CP2C.r)[SEL16(21)]
#define gteL31 ((s16*)psxRegs.CP2C.r)[SEL16(22)]
#define gteL32 ((s16*)psxRegs.CP2C.r)[SEL16(23)]
#define gteL33 ((s16*)psxRegs.CP2C.r)[SEL16(24)]
#define gteRBK ((s32*)psxRegs.CP2C.r)[13]
#define gteGBK ((s32*)psxRegs.CP2C.r)[14]
#define gteBBK ((s32*)psxRegs.CP2C.r)[15]
#define gteLR1 ((s16*)psxRegs.CP2C.r)[SEL16(32)]
#define gteLR2 ((s16*)psxRegs.CP2C.r)[SEL16(33)]
#define gteLR3 ((s16*)psxRegs.CP2C.r)[SEL16(34)]
#define gteLG1 ((s16*)psxRegs.CP2C.r)[SEL16(35)]
#define gteLG2 ((s16*)psxRegs.CP2C.r)[SEL16(36)]
#define gteLG3 ((s16*)psxRegs.CP2C.r)[SEL16(37)]
#define gteLB1 ((s16*)psxRegs.CP2C.r)[SEL16(38)]
#define gteLB2 ((s16*)psxRegs.CP2C.r)[SEL16(39)]
#define gteLB3 ((s16*)psxRegs.CP2C.r)[SEL16(40)]
#define gteRFC ((s32*)psxRegs.CP2C.r)[21]
#define gteGFC ((s32*)psxRegs.CP2C.r)[22]
#define gteBFC ((s32*)psxRegs.CP2C.r)[23]
#define gteOFX ((s32*)psxRegs.CP2C.r)[24]
#define gteOFY ((s32*)psxRegs.CP2C.r)[25]
#define gteH ((u16*)psxRegs.CP2C.r)[SEL16(52)]
#define gteDQA ((s16*)psxRegs.CP2C.r)[SEL16(54)]
#define gteDQB ((s32*)psxRegs.CP2C.r)[28]
#define gteZSF3 ((s16*)psxRegs.CP2C.r)[SEL16(58)]
#define gteZSF4 ((s16*)psxRegs.CP2C.r)[SEL16(60)]
#define gteFLAG psxRegs.CP2C.r[31]
__inline u32 MFC2(int reg) {
switch(reg) {
case 29:
gteORGB = (((gteIR1 >> 7) & 0x1f)) |
(((gteIR2 >> 7) & 0x1f)<<5) |
(((gteIR3 >> 7) & 0x1f)<<10);
// gteORGB = (gteIR1 ) |
// (gteIR2 << 5) |
// (gteIR3 << 10);
// gteORGB = ((gteIR1 & 0xf80)>>7) |
// ((gteIR2 & 0xf80)>>2) |
// ((gteIR3 & 0xf80)<<3);
return gteORGB;
default:
return psxRegs.CP2D.r[reg];
}
}
__inline void MTC2(u32 value, int reg) {
int a;
switch(reg) {
case 8: case 9: case 10: case 11:
psxRegs.CP2D.r[reg] = (short)value;
break;
case 15:
gteSXY0 = gteSXY1;
gteSXY1 = gteSXY2;
gteSXY2 = value;
gteSXYP = value;
break;
case 16: case 17: case 18: case 19:
psxRegs.CP2D.r[reg] = (value & 0xffff);
break;
case 28:
psxRegs.CP2D.r[28] = value;
gteIR1 = ((value ) & 0x1f) << 7;
gteIR2 = ((value >> 5) & 0x1f) << 7;
gteIR3 = ((value >> 10) & 0x1f) << 7;
// gteIR1 = (value ) & 0x1f;
// gteIR2 = (value >> 5) & 0x1f;
// gteIR3 = (value >> 10) & 0x1f;
// gteIR1 = ((value ) & 0x1f) << 4;
// gteIR2 = ((value >> 5) & 0x1f) << 4;
// gteIR3 = ((value >> 10) & 0x1f) << 4;
break;
case 30:
psxRegs.CP2D.r[30] = value;
a = psxRegs.CP2D.r[30];
if (a > 0) {
int i;
for (i=31; (a & (1 << i)) == 0 && i >= 0; i--);
psxRegs.CP2D.r[31] = 31 - i;
} else if (a < 0) {
int i;
a^= 0xffffffff;
for (i=31; (a & (1 << i)) == 0 && i >= 0; i--);
psxRegs.CP2D.r[31] = 31 - i;
} else {
psxRegs.CP2D.r[31] = 32;
}
break;
default:
psxRegs.CP2D.r[reg] = value;
}
}
void gteMFC2() {
if (!_Rt_) return;
psxRegs.GPR.r[_Rt_] = MFC2(_Rd_);
}
void gteCFC2() {
if (!_Rt_) return;
psxRegs.GPR.r[_Rt_] = psxRegs.CP2C.r[_Rd_];
}
void gteMTC2() {
MTC2(psxRegs.GPR.r[_Rt_], _Rd_);
}
void gteCTC2() {
psxRegs.CP2C.r[_Rd_] = psxRegs.GPR.r[_Rt_];
}
#define _oB_ (psxRegs.GPR.r[_Rs_] + _Imm_)
void gteLWC2() {
MTC2(psxMemRead32(_oB_), _Rt_);
}
void gteSWC2() {
psxMemWrite32(_oB_, MFC2(_Rt_));
}
__inline float NC_OVERFLOW1(float x) {
if (x<-2147483648.0) {gteFLAG |= 1<<29;}
else if (x> 2147483647.0) {gteFLAG |= 1<<26;}
return x;
}
__inline float NC_OVERFLOW2(float x) {
if (x<-2147483648.0) {gteFLAG |= 1<<28;}
else if (x> 2147483647.0) {gteFLAG |= 1<<25;}
return x;
}
__inline float NC_OVERFLOW3(float x) {
if (x<-2147483648.0) {gteFLAG |= 1<<27;}
else if (x> 2147483647.0) {gteFLAG |= 1<<24;}
return x;
}
__inline float NC_OVERFLOW4(float x) {
if (x<-2147483648.0) {gteFLAG |= 1<<16;}
else if (x> 2147483647.0) {gteFLAG |= 1<<15;}
return x;
}
__inline s32 FNC_OVERFLOW1(s64 x) {
if (x< (s64)0xffffffff80000000LL) {gteFLAG |= 1<<29;}
else if (x> 2147483647) {gteFLAG |= 1<<26;}
return (s32)x;
}
__inline s32 FNC_OVERFLOW2(s64 x) {
if (x< (s64)0xffffffff80000000LL) {gteFLAG |= 1<<28;}
else if (x> 2147483647) {gteFLAG |= 1<<25;}
return (s32)x;
}
__inline s32 FNC_OVERFLOW3(s64 x) {
if (x< (s64)0xffffffff80000000LL) {gteFLAG |= 1<<27;}
else if (x> 2147483647) {gteFLAG |= 1<<24;}
return (s32)x;
}
__inline s32 FNC_OVERFLOW4(s64 x) {
if (x< (s64)0xffffffff80000000LL) {gteFLAG |= 1<<16;}
else if (x> 2147483647) {gteFLAG |= 1<<15;}
return (s32)x;
}
#define _LIMX(negv, posv, flagb) { \
if (x < (negv)) { x = (negv); gteFLAG |= (1<<flagb); } else \
if (x > (posv)) { x = (posv); gteFLAG |= (1<<flagb); } return (x); \
}
__inline float limA1S(float x) { _LIMX(-32768.0, 32767.0, 24); }
__inline float limA2S(float x) { _LIMX(-32768.0, 32767.0, 23); }
__inline float limA3S(float x) { _LIMX(-32768.0, 32767.0, 22); }
__inline float limA1U(float x) { _LIMX(0.0, 32767.0, 24); }
__inline float limA2U(float x) { _LIMX(0.0, 32767.0, 23); }
__inline float limA3U(float x) { _LIMX(0.0, 32767.0, 22); }
__inline float limB1 (float x) { _LIMX(0.0, 255.0, 21); }
__inline float limB2 (float x) { _LIMX(0.0, 255.0, 20); }
__inline float limB3 (float x) { _LIMX(0.0, 255.0, 19); }
__inline float limC (float x) { _LIMX(0.0, 65535.0, 18); }
__inline float limD1 (float x) { _LIMX(-1024.0, 1023.0, 14); }
__inline float limD2 (float x) { _LIMX(-1024.0, 1023.0, 13); }
__inline float limE (float x) { _LIMX(0.0, 4095.0, 12); }
__inline float limG1(float x) {
if (x > 2147483647.0) { gteFLAG |= (1<<16); } else
if (x <-2147483648.0) { gteFLAG |= (1<<15); }
if (x > 1023.0) { x = 1023.0; gteFLAG |= (1<<14); } else
if (x < -1024.0) { x = -1024.0; gteFLAG |= (1<<14); }
return (x);
}
__inline float limG2(float x) {
if (x > 2147483647.0) { gteFLAG |= (1<<16); } else
if (x <-2147483648.0) { gteFLAG |= (1<<15); }
if (x > 1023.0) { x = 1023.0; gteFLAG |= (1<<13); } else
if (x < -1024.0) { x = -1024.0; gteFLAG |= (1<<13); }
return (x);
}
__inline s32 F12limA1S(s64 x) { _LIMX(-32768<<12, 32767<<12, 24); }
__inline s32 F12limA2S(s64 x) { _LIMX(-32768<<12, 32767<<12, 23); }
__inline s32 F12limA3S(s64 x) { _LIMX(-32768<<12, 32767<<12, 22); }
__inline s32 F12limA1U(s64 x) { _LIMX(0, 32767<<12, 24); }
__inline s32 F12limA2U(s64 x) { _LIMX(0, 32767<<12, 23); }
__inline s32 F12limA3U(s64 x) { _LIMX(0, 32767<<12, 22); }
__inline s16 FlimA1S(s32 x) { _LIMX(-32768, 32767, 24); }
__inline s16 FlimA2S(s32 x) { _LIMX(-32768, 32767, 23); }
__inline s16 FlimA3S(s32 x) { _LIMX(-32768, 32767, 22); }
__inline s16 FlimA1U(s32 x) { _LIMX(0, 32767, 24); }
__inline s16 FlimA2U(s32 x) { _LIMX(0, 32767, 23); }
__inline s16 FlimA3U(s32 x) { _LIMX(0, 32767, 22); }
__inline u8 FlimB1 (s32 x) { _LIMX(0, 255, 21); }
__inline u8 FlimB2 (s32 x) { _LIMX(0, 255, 20); }
__inline u8 FlimB3 (s32 x) { _LIMX(0, 255, 19); }
__inline u16 FlimC (s32 x) { _LIMX(0, 65535, 18); }
__inline s32 FlimD1 (s32 x) { _LIMX(-1024, 1023, 14); }
__inline s32 FlimD2 (s32 x) { _LIMX(-1024, 1023, 13); }
__inline s32 FlimE (s32 x) { _LIMX(0, 65535, 12); }
//__inline s32 FlimE (s32 x) { _LIMX(0, 4095, 12); }
__inline s32 FlimG1(s64 x) {
if (x > 2147483647) { gteFLAG |= (1<<16); } else
if (x < (s64)0xffffffff80000000LL) { gteFLAG |= (1<<15); }
if (x > 1023) { x = 1023; gteFLAG |= (1<<14); } else
if (x < -1024) { x = -1024; gteFLAG |= (1<<14); }
return (x);
}
__inline s32 FlimG2(s64 x) {
if (x > 2147483647) { gteFLAG |= (1<<16); } else
if (x < (s64)0xffffffff80000000LL) { gteFLAG |= (1<<15); }
if (x > 1023) { x = 1023; gteFLAG |= (1<<13); } else
if (x < -1024) { x = -1024; gteFLAG |= (1<<13); }
return (x);
}
#define MAC2IR() { \
if (gteMAC1 < (long)(-32768)) { gteIR1=(long)(-32768); gteFLAG|=1<<24;} \
else \
if (gteMAC1 > (long)( 32767)) { gteIR1=(long)( 32767); gteFLAG|=1<<24;} \
else gteIR1=(long)gteMAC1; \
if (gteMAC2 < (long)(-32768)) { gteIR2=(long)(-32768); gteFLAG|=1<<23;} \
else \
if (gteMAC2 > (long)( 32767)) { gteIR2=(long)( 32767); gteFLAG|=1<<23;} \
else gteIR2=(long)gteMAC2; \
if (gteMAC3 < (long)(-32768)) { gteIR3=(long)(-32768); gteFLAG|=1<<22;} \
else \
if (gteMAC3 > (long)( 32767)) { gteIR3=(long)( 32767); gteFLAG|=1<<22;} \
else gteIR3=(long)gteMAC3; \
}
#define MAC2IR1() { \
if (gteMAC1 < (long)0) { gteIR1=(long)0; gteFLAG|=1<<24;} \
else if (gteMAC1 > (long)(32767)) { gteIR1=(long)(32767); gteFLAG|=1<<24;} \
else gteIR1=(long)gteMAC1; \
if (gteMAC2 < (long)0) { gteIR2=(long)0; gteFLAG|=1<<23;} \
else if (gteMAC2 > (long)(32767)) { gteIR2=(long)(32767); gteFLAG|=1<<23;} \
else gteIR2=(long)gteMAC2; \
if (gteMAC3 < (long)0) { gteIR3=(long)0; gteFLAG|=1<<22;} \
else if (gteMAC3 > (long)(32767)) { gteIR3=(long)(32767); gteFLAG|=1<<22;} \
else gteIR3=(long)gteMAC3; \
}
//********END OF LIMITATIONS**********************************/
#define GTE_RTPS1(vn) { \
gteMAC1 = FNC_OVERFLOW1(((signed long)(gteR11*gteVX##vn + gteR12*gteVY##vn + gteR13*gteVZ##vn)>>12) + gteTRX); \
gteMAC2 = FNC_OVERFLOW2(((signed long)(gteR21*gteVX##vn + gteR22*gteVY##vn + gteR23*gteVZ##vn)>>12) + gteTRY); \
gteMAC3 = FNC_OVERFLOW3(((signed long)(gteR31*gteVX##vn + gteR32*gteVY##vn + gteR33*gteVZ##vn)>>12) + gteTRZ); \
}
/* gteMAC1 = NC_OVERFLOW1(((signed long)(gteR11*gteVX0 + gteR12*gteVY0 + gteR13*gteVZ0)>>12) + gteTRX);
gteMAC2 = NC_OVERFLOW2(((signed long)(gteR21*gteVX0 + gteR22*gteVY0 + gteR23*gteVZ0)>>12) + gteTRY);
gteMAC3 = NC_OVERFLOW3(((signed long)(gteR31*gteVX0 + gteR32*gteVY0 + gteR33*gteVZ0)>>12) + gteTRZ);*/
#if 0
#define GTE_RTPS2(vn) { \
if (gteSZ##vn == 0) { \
DSZ = 2.0f; gteFLAG |= 1<<17; \
} else { \
DSZ = (float)gteH / gteSZ##vn; \
if (DSZ > 2.0) { DSZ = 2.0f; gteFLAG |= 1<<17; } \
/* if (DSZ > 2147483647.0) { DSZ = 2.0f; gteFLAG |= 1<<17; }*/ \
} \
\
/* gteSX##vn = limG1(gteOFX/65536.0 + (limA1S(gteMAC1) * DSZ));*/ \
/* gteSY##vn = limG2(gteOFY/65536.0 + (limA2S(gteMAC2) * DSZ));*/ \
gteSX##vn = FlimG1(gteOFX/65536.0 + (gteIR1 * DSZ)); \
gteSY##vn = FlimG2(gteOFY/65536.0 + (gteIR2 * DSZ)); \
}
#define GTE_RTPS3() { \
DSZ = gteDQB/16777216.0 + (gteDQA/256.0) * DSZ; \
gteMAC0 = DSZ * 16777216.0; \
gteIR0 = limE(DSZ * 4096.0f); \
printf("zero %x, %x\n", gteMAC0, gteIR0); \
}
#endif
//#if 0
#define GTE_RTPS2(vn) { \
if (gteSZ##vn == 0) { \
FDSZ = 2 << 16; gteFLAG |= 1<<17; \
} else { \
FDSZ = ((u64)gteH << 32) / ((u64)gteSZ##vn << 16); \
if ((u64)FDSZ > (2 << 16)) { FDSZ = 2 << 16; gteFLAG |= 1<<17; } \
} \
\
gteSX##vn = FlimG1((gteOFX + (((s64)((s64)gteIR1 << 16) * FDSZ) >> 16)) >> 16); \
gteSY##vn = FlimG2((gteOFY + (((s64)((s64)gteIR2 << 16) * FDSZ) >> 16)) >> 16); \
}
#define GTE_RTPS3() { \
FDSZ = (s64)((s64)gteDQB + (((s64)((s64)gteDQA << 8) * FDSZ) >> 8)); \
gteMAC0 = FDSZ; \
gteIR0 = FlimE(FDSZ >> 12); \
}
//#endif
// gteMAC0 = (gteDQB/16777216.0 + (gteDQA/256.0) * DSZ) * 16777216.0;
// gteIR0 = limE((gteDQB/16777216.0 + (gteDQA/256.0) * DSZ) * 4096.0);
// gteMAC0 = ((gteDQB >> 24) + (gteDQA >> 8) * DSZ) * 16777216.0;
// gteIR0 = FlimE(((gteDQB >> 24) + (gteDQA >> 8) * DSZ) * 4096.0);
void gteRTPS() {
s64 FDSZ;
#ifdef GTE_DUMP
static int sample = 0; sample++;
#endif
#ifdef GTE_LOG
GTE_LOG("GTE_RTPS\n");
#endif
#ifdef GTE_DUMP
if(sample < 100)
{
G_OP("RTPS", 14);
G_SD(0);
G_SD(1);
G_SD(16); // Store original fifo
G_SD(17);
G_SD(18);
G_SD(19);
G_SC(0);
G_SC(1);
G_SC(2);
G_SC(3);
G_SC(4);
G_SC(5);
G_SC(6);
G_SC(7);
G_SC(24);
G_SC(25);
G_SC(26);
G_SC(27);
G_SC(28);
}
#endif
gteFLAG = 0;
GTE_RTPS1(0);
MAC2IR();
gteSZx = gteSZ0;
gteSZ0 = gteSZ1;
gteSZ1 = gteSZ2;
// gteSZ2 = limC(gteMAC3);
gteSZ2 = FlimC(gteMAC3);
gteSXY0 = gteSXY1;
gteSXY1 = gteSXY2;
GTE_RTPS2(2);
gteSXYP = gteSXY2;
GTE_RTPS3();
SUM_FLAG;
#ifdef GTE_DUMP
if(sample < 100)
{
G_GD(8);
G_GD(9);
G_GD(10);
G_GD(11);
//G_GD(12);
//G_GD(13);
G_GD(14);
G_GD(16);
G_GD(17);
G_GD(18);
G_GD(19);
G_GD(24);
G_GD(25);
G_GD(26);
G_GD(27);
G_GC(31);
}
#endif
}
void gteRTPT() {
s64 FDSZ;
#ifdef GTE_DUMP
static int sample = 0; sample++;
#endif
#ifdef GTE_LOG
GTE_LOG("GTE_RTPT\n");
#endif
#ifdef GTE_DUMP
if(sample < 100)
{
G_OP("RTPT", 22);
G_SD(0);
G_SD(1);
G_SD(2);
G_SD(3);
G_SD(4);
G_SD(5);
G_SD(16); // Store original fifo
G_SD(17);
G_SD(18);
G_SD(19);
G_SC(0);
G_SC(1);
G_SC(2);
G_SC(3);
G_SC(4);
G_SC(5);
G_SC(6);
G_SC(7);
G_SC(24);
G_SC(25);
G_SC(26);
G_SC(27);
G_SC(28);
}
#endif
gteFLAG = 0;
gteSZx = gteSZ2;
GTE_RTPS1(0);
// gteSZ0 = limC(gteMAC3);
gteSZ0 = FlimC(gteMAC3);
gteIR1 = FlimA1S(gteMAC1);
gteIR2 = FlimA2S(gteMAC2);
GTE_RTPS2(0);
GTE_RTPS1(1);
// gteSZ1 = limC(gteMAC3);
gteSZ1 = FlimC(gteMAC3);
gteIR1 = FlimA1S(gteMAC1);
gteIR2 = FlimA2S(gteMAC2);
GTE_RTPS2(1);
GTE_RTPS1(2);
MAC2IR();
// gteSZ2 = limC(gteMAC3);
gteSZ2 = FlimC(gteMAC3);
GTE_RTPS2(2);
gteSXYP = gteSXY2;
GTE_RTPS3();
SUM_FLAG;
#ifdef GTE_DUMP
if(sample < 100)
{
G_GD(8);
G_GD(9);
G_GD(10);
G_GD(11);
G_GD(12);
G_GD(13);
G_GD(14);
G_GD(16);
G_GD(17);
G_GD(18);
G_GD(19);
G_GD(24);
G_GD(25);
G_GD(26);
G_GD(27);
G_GC(31);
}
#endif
}
#define gte_C11 gteLR1
#define gte_C12 gteLR2
#define gte_C13 gteLR3
#define gte_C21 gteLG1
#define gte_C22 gteLG2
#define gte_C23 gteLG3
#define gte_C31 gteLB1
#define gte_C32 gteLB2
#define gte_C33 gteLB3
#define _MVMVA_FUNC(_v0, _v1, _v2, mx) { \
SSX = (_v0) * mx##11 + (_v1) * mx##12 + (_v2) * mx##13; \
SSY = (_v0) * mx##21 + (_v1) * mx##22 + (_v2) * mx##23; \
SSZ = (_v0) * mx##31 + (_v1) * mx##32 + (_v2) * mx##33; \
}
void gteMVMVA() {
s64 SSX, SSY, SSZ;
#ifdef GTE_LOG
GTE_LOG("GTE_MVMVA %lx\n", psxRegs.code & 0x1ffffff);
#endif
switch (psxRegs.code & 0x78000) {
case 0x00000: // V0 * R
_MVMVA_FUNC(gteVX0, gteVY0, gteVZ0, gteR); break;
case 0x08000: // V1 * R
_MVMVA_FUNC(gteVX1, gteVY1, gteVZ1, gteR); break;
case 0x10000: // V2 * R
_MVMVA_FUNC(gteVX2, gteVY2, gteVZ2, gteR); break;
case 0x18000: // IR * R
_MVMVA_FUNC((short)gteIR1, (short)gteIR2, (short)gteIR3, gteR);
break;
case 0x20000: // V0 * L
_MVMVA_FUNC(gteVX0, gteVY0, gteVZ0, gteL); break;
case 0x28000: // V1 * L
_MVMVA_FUNC(gteVX1, gteVY1, gteVZ1, gteL); break;
case 0x30000: // V2 * L
_MVMVA_FUNC(gteVX2, gteVY2, gteVZ2, gteL); break;
case 0x38000: // IR * L
_MVMVA_FUNC((short)gteIR1, (short)gteIR2, (short)gteIR3, gteL); break;
case 0x40000: // V0 * C
_MVMVA_FUNC(gteVX0, gteVY0, gteVZ0, gte_C); break;
case 0x48000: // V1 * C
_MVMVA_FUNC(gteVX1, gteVY1, gteVZ1, gte_C); break;
case 0x50000: // V2 * C
_MVMVA_FUNC(gteVX2, gteVY2, gteVZ2, gte_C); break;
case 0x58000: // IR * C
_MVMVA_FUNC((short)gteIR1, (short)gteIR2, (short)gteIR3, gte_C); break;
default:
SSX = SSY = SSZ = 0;
}
if (psxRegs.code & 0x80000) {
// SSX /= 4096.0; SSY /= 4096.0; SSZ /= 4096.0;
SSX>>= 12; SSY>>= 12; SSZ>>= 12;
}
switch (psxRegs.code & 0x6000) {
case 0x0000: // Add TR
SSX+= gteTRX;
SSY+= gteTRY;
SSZ+= gteTRZ;
break;
case 0x2000: // Add BK
SSX+= gteRBK;
SSY+= gteGBK;
SSZ+= gteBBK;
break;
case 0x4000: // Add FC
SSX+= gteRFC;
SSY+= gteGFC;
SSZ+= gteBFC;
break;
}
gteFLAG = 0;
//gteMAC1 = (long)SSX;
//gteMAC2 = (long)SSY;
//gteMAC3 = (long)SSZ;//okay the follow lines are correct??
/* gteMAC1 = NC_OVERFLOW1(SSX);
gteMAC2 = NC_OVERFLOW2(SSY);
gteMAC3 = NC_OVERFLOW3(SSZ);*/
gteMAC1 = FNC_OVERFLOW1(SSX);
gteMAC2 = FNC_OVERFLOW2(SSY);
gteMAC3 = FNC_OVERFLOW3(SSZ);
if (psxRegs.code & 0x400)
MAC2IR1()
else MAC2IR()
SUM_FLAG;
}
void gteNCLIP() {
#ifdef GTE_DUMP
static int sample = 0; sample++;
#endif
#ifdef GTE_LOG
GTE_LOG("GTE_NCLIP\n");
#endif
//gteLog
#ifdef GTE_DUMP
if(sample < 100)
{
G_OP("NCLIP", 8);
G_SD(12);
G_SD(13);
G_SD(14);
}
#endif
gteFLAG = 0;
gteMAC0 = gteSX0 * (gteSY1 - gteSY2) +
gteSX1 * (gteSY2 - gteSY0) +
gteSX2 * (gteSY0 - gteSY1);
//gteMAC0 = (gteSX0 - gteSX1) * (gteSY0 - gteSY2) - (gteSX0 - gteSX2) * (gteSY0 - gteSY1);
SUM_FLAG;
#ifdef GTE_DUMP
if(sample < 100)
{
G_GD(24);
G_GC(31);
}
#endif
}
void gteAVSZ3() {
#ifdef GTE_DUMP
static int sample = 0; sample++;
#endif
#ifdef GTE_LOG
GTE_LOG("GTE_AVSZ3\n");
#endif
#ifdef GTE_DUMP
if(sample < 100)
{
G_OP("AVSZ3", 5);
G_SD(16);
G_SD(17);
G_SD(18);
G_SD(19);
G_SC(29);
G_SC(30);
}
#endif
gteFLAG = 0;
gteMAC0 = ((gteSZ0 + gteSZ1 + gteSZ2) * (gteZSF3)) >> 12;
gteOTZ = FlimC(gteMAC0);
SUM_FLAG
#ifdef GTE_DUMP
if(sample < 100)
{
G_GD(7);
G_GD(24);
G_GC(31);
}
#endif
}
void gteAVSZ4() {
#ifdef GTE_DUMP
static int sample = 0; sample++;
#endif
#ifdef GTE_LOG
GTE_LOG("GTE_AVSZ4\n");
#endif
#ifdef GTE_DUMP
if(sample < 100)
{
G_OP("AVSZ4", 6);
G_SD(16);
G_SD(17);
G_SD(18);
G_SD(19);
G_SC(29);
G_SC(30);
}
#endif
gteFLAG = 0;
gteMAC0 = ((gteSZx + gteSZ0 + gteSZ1 + gteSZ2) * (gteZSF4))>> 12;
gteOTZ = FlimC(gteMAC0);
SUM_FLAG
#ifdef GTE_DUMP
if(sample < 100)
{
G_GD(7);
G_GD(24);
G_GC(31);
}
#endif
}
void gteSQR() {
#ifdef GTE_DUMP
static int sample = 0; sample++;
#endif
#ifdef GTE_LOG
GTE_LOG("GTE_SQR %lx\n", psxRegs.code & 0x1ffffff);
#endif
#ifdef GTE_DUMP
if(sample < 100)
{
G_OP("SQR", 5);
G_SD(9);
G_SD(10);
G_SD(11);
}
#endif
gteFLAG = 0;
if (psxRegs.code & 0x80000) {
gteMAC1 = FNC_OVERFLOW1((gteIR1 * gteIR1) >> 12);
gteMAC2 = FNC_OVERFLOW2((gteIR2 * gteIR2) >> 12);
gteMAC3 = FNC_OVERFLOW3((gteIR3 * gteIR3) >> 12);
} else {
gteMAC1 = FNC_OVERFLOW1(gteIR1 * gteIR1);
gteMAC2 = FNC_OVERFLOW2(gteIR2 * gteIR2);
gteMAC3 = FNC_OVERFLOW3(gteIR3 * gteIR3);
}
MAC2IR1();
SUM_FLAG
#ifdef GTE_DUMP
if(sample < 100)
{
G_GD(9);
G_GD(10);
G_GD(11);
G_GD(25);
G_GD(26);
G_GD(27);
G_GC(31);
}
#endif
}
#define GTE_NCCS(vn) \
gte_LL1 = F12limA1U((gteL11*gteVX##vn + gteL12*gteVY##vn + gteL13*gteVZ##vn) >> 12); \
gte_LL2 = F12limA2U((gteL21*gteVX##vn + gteL22*gteVY##vn + gteL23*gteVZ##vn) >> 12); \
gte_LL3 = F12limA3U((gteL31*gteVX##vn + gteL32*gteVY##vn + gteL33*gteVZ##vn) >> 12); \
gte_RRLT= F12limA1U(gteRBK + ((gteLR1*gte_LL1 + gteLR2*gte_LL2 + gteLR3*gte_LL3) >> 12)); \
gte_GGLT= F12limA2U(gteGBK + ((gteLG1*gte_LL1 + gteLG2*gte_LL2 + gteLG3*gte_LL3) >> 12)); \
gte_BBLT= F12limA3U(gteBBK + ((gteLB1*gte_LL1 + gteLB2*gte_LL2 + gteLB3*gte_LL3) >> 12)); \
\
gteMAC1 = (long)(((s64)((u32)gteR<<12)*gte_RRLT) >> 20);\
gteMAC2 = (long)(((s64)((u32)gteG<<12)*gte_GGLT) >> 20);\
gteMAC3 = (long)(((s64)((u32)gteB<<12)*gte_BBLT) >> 20);
void gteNCCS() {
s32 gte_LL1, gte_LL2, gte_LL3;
s32 gte_RRLT, gte_GGLT, gte_BBLT;
#ifdef GTE_DUMP
static int sample = 0; sample++;
#endif
#ifdef GTE_LOG
GTE_LOG("GTE_NCCS\n");
#endif
/*
gteFLAG = 0;
GTE_NCCS(0);
if (gteFLAG & 0x7f87e000) gteFLAG|=0x80000000;*/
#ifdef GTE_DUMP
if(sample < 100)
{
G_OP("NCCS", 17);
G_SD(0);
G_SD(1);
G_SD(6);
G_SC(8);
G_SC(9);
G_SC(10);
G_SC(11);
G_SC(12);
G_SC(13);
G_SC(14);
G_SC(15);
G_SC(16);
G_SC(17);
G_SC(18);
G_SC(19);
G_SC(20);
}
#endif
gteFLAG = 0;