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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
|
#include "dolphin/os/OS.h"
#include "dolphin/vi/vi.h"
#include "dolphin/gx/GX.h"
const char* __VIVersion = "<< Dolphin SDK - VI\trelease build: Sep 5 2002 05:33:13 (0x2301) >>";
typedef struct {
u8 equ;
u16 acv;
u16 prbOdd;
u16 prbEven;
u16 psbOdd;
u16 psbEven;
u8 bs1;
u8 bs2;
u8 bs3;
u8 bs4;
u16 be1;
u16 be2;
u16 be3;
u16 be4;
u16 nhlines;
u16 hlw;
u8 hsy;
u8 hcs;
u8 hce;
u8 hbe640;
u16 hbs640;
u8 hbeCCIR656;
u16 hbsCCIR656;
} VITiming;
typedef struct {
u16 DispPosX;
u16 DispPosY;
u16 DispSizeX;
u16 DispSizeY;
u16 AdjustedDispPosX;
u16 AdjustedDispPosY;
u16 AdjustedDispSizeY;
u16 AdjustedPanPosY;
u16 AdjustedPanSizeY;
u16 FBSizeX;
u16 FBSizeY;
u16 PanPosX;
u16 PanPosY;
u16 PanSizeX;
u16 PanSizeY;
VIXFBMode FBMode;
u32 nonInter;
u32 tv;
u8 wordPerLine;
u8 std;
u8 wpl;
u32 bufAddr;
u32 tfbb;
u32 bfbb;
u8 xof;
BOOL black;
BOOL threeD;
u32 rbufAddr;
u32 rtfbb;
u32 rbfbb;
VITiming* timing;
} SomeVIStruct;
static BOOL IsInitialized;
static volatile u32 retraceCount;
static volatile u32 flushFlag;
static OSThreadQueue retraceQueue;
static void (*PreCB)(u32);
static void (*PostCB)(u32);
static u32 encoderType;
static s16 displayOffsetH;
static s16 displayOffsetV;
static volatile u32 changeMode;
static volatile u64 changed;
static volatile u32 shdwChangeMode;
static volatile u16 regs[59];
static volatile u64 shdwChanged;
static VITiming* CurrTiming;
static u32 CurrTvMode;
static u32 NextBufAddr;
static u32 CurrBufAddr;
static volatile u16 shdwRegs[59];
#define MARK_CHANGED(index) (changed |= 1LL << (63 - (index)))
static VITiming timing[10] = {
{ 6, 240, 24, 25, 3, 2, 12, 13, 12, 13, 520, 519, 520, 519, 525, 429, 64, 71, 105, 162, 373, 122, 412 },
{ 6, 240, 24, 24, 4, 4, 12, 12, 12, 12, 520, 520, 520, 520, 526, 429, 64, 71, 105, 162, 373, 122, 412 },
{ 5, 287, 35, 36, 1, 0, 13, 12, 11, 10, 619, 618, 617, 620, 625, 432, 64, 75, 106, 172, 380, 133, 420 },
{ 5, 287, 33, 33, 2, 2, 13, 11, 13, 11, 619, 621, 619, 621, 624, 432, 64, 75, 106, 172, 380, 133, 420 },
{ 6, 240, 24, 25, 3, 2, 16, 15, 14, 13, 518, 517, 516, 519, 525, 429, 64, 78, 112, 162, 373, 122, 412 },
{ 6, 240, 24, 24, 4, 4, 16, 14, 16, 14, 518, 520, 518, 520, 526, 429, 64, 78, 112, 162, 373, 122, 412 },
{ 12, 480, 48, 48, 6, 6, 24, 24, 24, 24, 1038, 1038, 1038, 1038, 1050, 429, 64, 71, 105, 162, 373, 122, 412 },
{ 12, 480, 44, 44, 10, 10, 24, 24, 24, 24, 1038, 1038, 1038, 1038, 1050, 429, 64, 71, 105, 168, 379, 122, 412 },
{ 6, 241, 24, 25, 1, 0, 12, 13, 12, 13, 520, 519, 520, 519, 525, 429, 64, 71, 105, 159, 370, 122, 412 },
{ 12, 480, 48, 48, 6, 6, 24, 24, 24, 24, 1038, 1038, 1038, 1038, 1050, 429, 64, 71, 105, 180, 391, 122, 412 }
};
static u16 taps[25] = {
0x01F0, 0x01DC,
0x01AE, 0x0174,
0x0129, 0x00DB,
0x008E, 0x0046,
0x000C, 0x00E2,
0x00CB, 0x00C0,
0x00C4, 0x00CF,
0x00DE, 0x00EC,
0x00FC, 0x0008,
0x000F, 0x0013,
0x0013, 0x000F,
0x000C, 0x0008,
0x0001
};
static SomeVIStruct HorVer;
static u32 FBSet;
// prototypes
static u32 getCurrentFieldEvenOdd(void);
VITiming* __VISetExtraTiming(VITiming* t);
void __VIEnableRawPositionInterrupt(s16 x, s16 y, void (*callback)(s16, s16));
void (*__VIDisableRawPositionInterrupt())(s16, s16);
void __VIDisplayPositionToXY(u32 hct, u32 vct, s16* x, s16* y);
void __VISetLatchMode(u32 mode);
int __VIGetLatch0Position(s16* px, s16* py);
int __VIGetLatch1Position(s16* px, s16* py);
int __VIGetLatchPosition(u32 port, s16* px, s16* py);
void __VIGetCurrentPosition(s16* x, s16* y);
static u32 getEncoderType(void) {
return 1;
}
static s32 cntlzd(u64 bit) {
u32 hi;
u32 lo;
s32 value;
hi = bit >> 32;
lo = bit & 0xFFFFFFFF;
value = __cntlzw(hi);
if (value < 32) {
return value;
}
return __cntlzw(lo) + 32;
}
static int VISetRegs(void) {
s32 regIndex;
if (shdwChangeMode != 1 || getCurrentFieldEvenOdd() != 0) {
while (shdwChanged != 0) {
regIndex = cntlzd(shdwChanged);
__VIRegs[regIndex] = shdwRegs[regIndex];
shdwChanged &= ~((u64)1 << (63 - regIndex));
}
shdwChangeMode = 0;
CurrTiming = HorVer.timing;
CurrTvMode = HorVer.tv;
CurrBufAddr = NextBufAddr;
return 1;
}
return 0;
}
static void __VIRetraceHandler(__OSInterrupt unused, OSContext* context) {
OSContext exceptionContext;
u16 reg;
u32 inter;
#if DEBUG
static u32 dbgCount;
#endif
inter = 0;
reg = __VIRegs[0x18];
if (reg & 0x8000) {
__VIRegs[0x18] = reg & ~0x8000;
inter |= 1;
}
reg = __VIRegs[0x1A];
if (reg & 0x8000) {
__VIRegs[0x1A] = reg & ~0x8000;
inter |= 2;
}
reg = __VIRegs[0x1C];
if (reg & 0x8000) {
__VIRegs[0x1C] = reg & ~0x8000;
inter |= 4;
}
reg = __VIRegs[0x1E];
if (reg & 0x8000) {
__VIRegs[0x1E] = reg & ~0x8000;
inter |= 8;
}
reg = __VIRegs[0x1E];
if ((inter & 4) || (inter & 8)) {
OSSetCurrentContext(context);
return;
}
if (inter == 0) {
ASSERTLINE(955, FALSE);
}
retraceCount += 1;
OSClearContext(&exceptionContext);
OSSetCurrentContext(&exceptionContext);
if (PreCB) {
PreCB(retraceCount);
}
if (flushFlag != 0) {
#if DEBUG
dbgCount = 0;
#endif
if (VISetRegs() != 0) {
flushFlag = 0;
SIRefreshSamplingRate();
}
}
#if DEBUG
else if (changed != 0) {
dbgCount++;
if (dbgCount > 60) {
OSReport("Warning: VIFlush() was not called for 60 frames although VI settings were changed\n");
dbgCount = 0;
}
}
#endif
if (PostCB) {
OSClearContext(&exceptionContext);
PostCB(retraceCount);
}
OSWakeupThread(&retraceQueue);
OSClearContext(&exceptionContext);
OSSetCurrentContext(context);
}
VIRetraceCallback VISetPreRetraceCallback(VIRetraceCallback cb) {
BOOL enabled;
VIRetraceCallback oldcb;
oldcb = PreCB;
enabled = OSDisableInterrupts();
PreCB = cb;
OSRestoreInterrupts(enabled);
return oldcb;
}
VIRetraceCallback VISetPostRetraceCallback(VIRetraceCallback cb) {
BOOL enabled;
VIRetraceCallback oldcb;
oldcb = PostCB;
enabled = OSDisableInterrupts();
PostCB = cb;
OSRestoreInterrupts(enabled);
return oldcb;
}
#pragma dont_inline on
static VITiming* getTiming(VITVMode mode) {
switch (mode) {
case VI_TVMODE_NTSC_INT: return &timing[0];
case VI_TVMODE_NTSC_DS: return &timing[1];
case VI_TVMODE_PAL_INT: return &timing[2];
case VI_TVMODE_PAL_DS: return &timing[3];
case VI_TVMODE_EURGB60_INT: return &timing[0];
case VI_TVMODE_EURGB60_DS: return &timing[1];
case VI_TVMODE_MPAL_INT: return &timing[4];
case VI_TVMODE_MPAL_DS: return &timing[5];
case VI_TVMODE_NTSC_PROG: return &timing[6];
case 3: return &timing[7];
case VI_TVMODE_DEBUG_PAL_INT: return &timing[2];
case VI_TVMODE_DEBUG_PAL_DS: return &timing[3];
case 24: return &timing[8];
case 26: return &timing[9];
default:
return NULL;
}
}
#pragma dont_inline reset
void __VIInit(VITVMode mode) {
VITiming* tm;
u32 nonInter;
u32 tv;
volatile u32 a;
u16 hct;
u16 vct;
u32 encoderType;
encoderType = getEncoderType();
if (encoderType == 0) {
__VIInitPhilips();
}
nonInter = mode & 2;
tv = (u32)mode >> 2;
*(u32*)OSPhysicalToCached(0xCC) = tv;
if (encoderType == 0) {
tv = 3;
}
tm = getTiming(mode);
__VIRegs[1] = 2;
// why?
for (a = 0; a < 1000; a++) {}
__VIRegs[1] = 0;
__VIRegs[3] = (u32)tm->hlw;
__VIRegs[2] = tm->hce | (tm->hcs << 8);
__VIRegs[5] = tm->hsy | ((tm->hbe640 & 0x1FF) << 7);
__VIRegs[4] = (tm->hbe640 >> 9) | ((tm->hbs640 & 0xFFFF) << 1);
if (encoderType == 0) {
__VIRegs[0x39] = tm->hbeCCIR656 | 0x8000;
__VIRegs[0x3A] = (u32)tm->hbsCCIR656;
}
__VIRegs[0] = (u32)tm->equ;
__VIRegs[7] = (u32)(tm->prbOdd + (tm->acv * 2) - 2);
__VIRegs[6] = (u32)(tm->psbOdd + 2);
__VIRegs[9] = (u32)(tm->prbEven + (tm->acv * 2) - 2);
__VIRegs[8] = (u32)(tm->psbEven + 2);
__VIRegs[11] = tm->bs1 | (tm->be1 << 5);
__VIRegs[10] = tm->bs3 | (tm->be3 << 5);
__VIRegs[13] = tm->bs2 | (tm->be2 << 5);
__VIRegs[12] = tm->bs4 | (tm->be4 << 5);
__VIRegs[36] = 0x2828;
__VIRegs[27] = 1;
__VIRegs[26] = 0x1001;
hct = tm->hlw + 1;
vct = (tm->nhlines / 2) + 1;
__VIRegs[25] = (u16)(u32)hct;
__VIRegs[24] = vct | 0x1000;
if (mode != VI_TVMODE_NTSC_PROG && mode != VI_TVMODE_NTSC_3D && mode != VI_TVMODE_GCA_PROG) {
__VIRegs[1] = (nonInter << 2) | 1 | (tv << 8);
__VIRegs[54] = 0;
return;
}
__VIRegs[1] = (tv << 8) | 5;
__VIRegs[54] = 1;
}
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define CLAMP(val, min, max) ((val) > (max) ? (max) : (val) < (min) ? (min) : (val))
static void AdjustPosition(u16 acv) {
s32 coeff;
s32 frac;
HorVer.AdjustedDispPosX = CLAMP((s16)HorVer.DispPosX + displayOffsetH, 0, 0x2D0 - HorVer.DispSizeX);
coeff = (HorVer.FBMode == VI_XFBMODE_SF) ? 2 : 1;
frac = HorVer.DispPosY & 1;
HorVer.AdjustedDispPosY = MAX((s16)HorVer.DispPosY + displayOffsetV, frac);
HorVer.AdjustedDispSizeY = HorVer.DispSizeY
+ MIN((s16)HorVer.DispPosY + displayOffsetV - frac, 0)
- MAX((s16)HorVer.DispPosY + (s16)HorVer.DispSizeY + displayOffsetV - (((s16)acv * 2) - frac), 0);
HorVer.AdjustedPanPosY = HorVer.PanPosY
- (MIN((s16)HorVer.DispPosY + displayOffsetV - frac, 0) / coeff);
HorVer.AdjustedPanSizeY = HorVer.PanSizeY
+ (MIN((s16)HorVer.DispPosY + displayOffsetV - frac, 0) / coeff)
- (MAX((s16)HorVer.DispPosY + (s16)HorVer.DispSizeY + displayOffsetV - (((s16)acv * 2) - frac), 0) / coeff);
}
static void ImportAdjustingValues(void) {
OSSram* sram = __OSLockSram();
ASSERTLINE(1322, sram);
displayOffsetH = sram->displayOffsetH;
displayOffsetV = 0;
__OSUnlockSram(0);
}
void VIInit(void) {
u16 dspCfg;
u32 value;
u32 tv;
u32 tvInBootrom;
if (IsInitialized) {
return;
}
OSRegisterVersion(__VIVersion);
IsInitialized = TRUE;
encoderType = getEncoderType();
if (!(__VIRegs[1] & 1)) {
__VIInit(VI_TVMODE_NTSC_INT);
}
retraceCount = 0;
changed = 0;
shdwChanged = 0;
changeMode = 0;
shdwChangeMode = 0;
flushFlag = 0;
__VIRegs[39] = taps[0] | ((taps[1] & 0x3F) << 10);
__VIRegs[38] = (taps[1] >> 6) | (taps[2] << 4);
__VIRegs[41] = taps[3] | ((taps[4] & 0x3F) << 10);
__VIRegs[40] = (taps[4] >> 6) | (taps[5] << 4);
__VIRegs[43] = taps[6] | ((taps[7] & 0x3F) << 10);
__VIRegs[42] = (taps[7] >> 6) | (taps[8] << 4);
__VIRegs[45] = taps[9] | (taps[10] << 8);
__VIRegs[44] = taps[11] | (taps[12] << 8);
__VIRegs[47] = taps[13] | (taps[14] << 8);
__VIRegs[46] = taps[15] | (taps[16] << 8);
__VIRegs[49] = taps[17] | (taps[18] << 8);
__VIRegs[48] = taps[19] | (taps[20] << 8);
__VIRegs[51] = taps[21] | (taps[22] << 8);
__VIRegs[50] = taps[23] | (taps[24] << 8);
__VIRegs[56] = 0x280;
ImportAdjustingValues();
tvInBootrom = *(u32*)OSPhysicalToCached(0xCC);
dspCfg = __VIRegs[1];
HorVer.nonInter = (s32) ((dspCfg >> 2U) & 1);
HorVer.tv = ((u32)(dspCfg) & 0x300) >> 8;
if (tvInBootrom == VI_PAL && HorVer.tv == VI_NTSC) {
HorVer.tv = VI_EURGB60;
}
tv = (HorVer.tv == 3) ? 0 : HorVer.tv;
HorVer.timing = getTiming((tv << 2) + HorVer.nonInter);
regs[1] = dspCfg;
CurrTiming = HorVer.timing;
CurrTvMode = HorVer.tv;
HorVer.DispSizeX = 640;
HorVer.DispSizeY = CurrTiming->acv * 2;
HorVer.DispPosX = (720 - HorVer.DispSizeX) / 2;
HorVer.DispPosY = 0;
AdjustPosition(CurrTiming->acv);
HorVer.FBSizeX = 640;
HorVer.FBSizeY = CurrTiming->acv * 2;
HorVer.PanPosX = 0;
HorVer.PanPosY = 0;
HorVer.PanSizeX = 640;
HorVer.PanSizeY = CurrTiming->acv * 2;
HorVer.FBMode = 0;
HorVer.wordPerLine = 40;
HorVer.std = 40;
HorVer.wpl = 40;
HorVer.xof = 0;
HorVer.black = 1;
HorVer.threeD = 0;
OSInitThreadQueue(&retraceQueue);
value = __VIRegs[24];
value &= ~0x8000;
#if !DEBUG
value = (u16)value;
#endif
__VIRegs[24] = value;
value = __VIRegs[26];
value = value & ~0x8000;
#if !DEBUG
value = (u16)value;
#endif
__VIRegs[26] = value;
PreCB = NULL;
PostCB = NULL;
__OSSetInterruptHandler(0x18, __VIRetraceHandler);
__OSUnmaskInterrupts(0x80);
}
void VIWaitForRetrace(void) {
BOOL enabled;
u32 count;
enabled = OSDisableInterrupts();
count = retraceCount;
do {
OSSleepThread(&retraceQueue);
} while (count == retraceCount);
OSRestoreInterrupts(enabled);
}
static void setInterruptRegs(VITiming* tm) {
#if DEBUG
u16 vct, hct;
#else
u16 hct, vct;
#endif
u16 borrow;
vct = tm->nhlines / 2;
borrow = tm->nhlines % 2;
if (borrow != 0) {
hct = tm->hlw;
} else {
hct = 0;
}
vct++;
hct++;
regs[25] = (u16)(u32)hct;
MARK_CHANGED(25);
regs[24] = vct | 0x1000;
MARK_CHANGED(24);
vct; // fixes regalloc
}
static void setPicConfig(u16 fbSizeX, VIXFBMode xfbMode, u16 panPosX, u16 panSizeX, u8* wordPerLine, u8* std, u8* wpl, u8* xof) {
*wordPerLine = (fbSizeX + 15) / 16;
*std = (xfbMode == VI_XFBMODE_SF) ? *wordPerLine : (u8)(*wordPerLine * 2);
*xof = panPosX % 16;
*wpl = (*xof + panSizeX + 15) / 16;
regs[0x24] = *std | (*wpl << 8);
changed |= 0x8000000;
}
static void setBBIntervalRegs(VITiming* tm) {
u16 val;
val = tm->bs1 | (tm->be1 << 5);
regs[11] = val;
changed |= 0x10000000000000;
val = tm->bs3 | (tm->be3 << 5);
regs[10] = val;
changed |= 0x20000000000000;
val = tm->bs2 | (tm->be2 << 5);
regs[13] = val;
changed |= 0x4000000000000;
val = tm->bs4 | (tm->be4 << 5);
regs[12] = val;
changed |= (1LL << (63-12));
}
static void setScalingRegs(u16 panSizeX, u16 dispSizeX, BOOL threeD) {
u32 scale;
panSizeX = threeD ? (panSizeX << 1) : panSizeX;
if (panSizeX < dispSizeX) {
scale = (u32)(dispSizeX + (panSizeX << 8) - 1) / dispSizeX;
regs[37] = scale | 0x1000;
changed |= 0x04000000;
regs[56] = (u32)panSizeX;
changed |= 0x80;
} else {
regs[37] = 0x100;
changed |= 0x04000000;
}
}
static void calcFbbs(u32 bufAddr, u16 panPosX, u16 panPosY, u8 wordPerLine, VIXFBMode xfbMode, u16 dispPosY, u32* tfbb, u32* bfbb) {
u32 bytesPerLine;
u32 xoffInWords;
u32 tmp;
xoffInWords = (panPosX & ~0xF) >> 4;
bytesPerLine = (wordPerLine & 0xFF) << 5;
*tfbb = bufAddr + (xoffInWords << 5) + (bytesPerLine * panPosY);
*bfbb = (xfbMode == VI_XFBMODE_SF) ? *tfbb : *tfbb + bytesPerLine;
if (dispPosY % 2 == 1) {
tmp = *tfbb;
*tfbb = *bfbb;
*bfbb = tmp;
}
*tfbb &= 0x3FFFFFFF;
*bfbb &= 0x3FFFFFFF;
}
static void setFbbRegs(SomeVIStruct* HorVer, u32* tfbb, u32* bfbb, u32* rtfbb, u32* rbfbb) {
u32 shifted;
calcFbbs(HorVer->bufAddr, HorVer->PanPosX, HorVer->AdjustedPanPosY, HorVer->wordPerLine, HorVer->FBMode, HorVer->AdjustedDispPosY, tfbb, bfbb);
if (HorVer->threeD) {
calcFbbs(HorVer->rbufAddr, HorVer->PanPosX, HorVer->AdjustedPanPosY, HorVer->wordPerLine, HorVer->FBMode, HorVer->AdjustedDispPosY, rtfbb, rbfbb);
}
if (*tfbb < 0x01000000U && *bfbb < 0x01000000U && *rtfbb < 0x01000000U && *rbfbb < 0x01000000U) {
shifted = 0;
} else {
shifted = 1;
}
if (shifted) {
*tfbb >>= 5;
*bfbb >>= 5;
*rtfbb >>= 5;
*rbfbb >>= 5;
}
regs[15] = (u16)*tfbb & 0xFFFF;
MARK_CHANGED(15);
regs[14] = (shifted << 12) | ((*tfbb >> 16) | (HorVer->xof << 8));
MARK_CHANGED(14);
regs[19] = (u16)*bfbb & 0xFFFF;
MARK_CHANGED(19);
regs[18] = (*bfbb >> 16);
MARK_CHANGED(18);
if (HorVer->threeD) {
regs[17] = (u16)*rtfbb & 0xFFFF;
MARK_CHANGED(17);
regs[16] = *rtfbb >> 16;
MARK_CHANGED(16);
regs[21] = (u16)*rbfbb & 0xFFFF;
MARK_CHANGED(21);
regs[20] = *rbfbb >> 16;
MARK_CHANGED(20);
}
}
static void setHorizontalRegs(VITiming* tm, u16 dispPosX, u16 dispSizeX) {
u32 hbe;
u32 hbs;
u32 hbeLo;
u32 hbeHi;
regs[3] = (u16)(u32)tm->hlw;
MARK_CHANGED(3);
regs[2] = tm->hce | (tm->hcs << 8);
MARK_CHANGED(2);
hbe = tm->hbe640 - 40 + dispPosX;
hbs = tm->hbs640 + 40 + dispPosX - (720 - dispSizeX);
hbeLo = hbe & 0x1FF;
hbeHi = hbe >> 9;
regs[5] = tm->hsy | (hbeLo << 7);
MARK_CHANGED(5);
regs[4] = hbeHi | (hbs * 2);
MARK_CHANGED(4);
}
static void setVerticalRegs(u16 dispPosY, u16 dispSizeY, u8 equ, u16 acv, u16 prbOdd, u16 prbEven, u16 psbOdd, u16 psbEven, BOOL black) {
u16 actualPrbOdd;
u16 actualPrbEven;
u16 actualPsbOdd;
u16 actualPsbEven;
u16 actualAcv;
u16 c;
u16 d;
if (regs[54] & 1) {
c = 1;
d = 2;
} else {
c = 2;
d = 1;
}
if ((dispPosY % 2) == 0) {
actualPrbOdd = prbOdd + (d * dispPosY);
actualPsbOdd = psbOdd + (d * (((c * acv) - dispSizeY) - dispPosY));
actualPrbEven = prbEven + (d * dispPosY);
actualPsbEven = psbEven + (d * (((c * acv) - dispSizeY) - dispPosY));
} else {
actualPrbOdd = prbEven + (d * dispPosY);
actualPsbOdd = psbEven + (d * (((c * acv) - dispSizeY) - dispPosY));
actualPrbEven = prbOdd + (d * dispPosY);
actualPsbEven = psbOdd + (d * (((c * acv) - dispSizeY) - dispPosY));
}
actualAcv = dispSizeY / c;
if (black) {
actualPrbOdd += 2 * actualAcv - 2;
actualPsbOdd += 2;
actualPrbEven += 2 * actualAcv - 2;
actualPsbEven += 2;
actualAcv = 0;
}
regs[0] = equ | (actualAcv << 4);
MARK_CHANGED(0);
regs[7] = (u16)(u32)actualPrbOdd;
MARK_CHANGED(7);
regs[6] = (u16)(u32)actualPsbOdd;
MARK_CHANGED(6);
regs[9] = (u16)(u32)actualPrbEven;
MARK_CHANGED(9);
regs[8] = (u16)(u32)actualPsbEven;
MARK_CHANGED(8);
}
static void PrintDebugPalCaution(void) {
static u32 message;
if (message == 0) {
message = 1;
OSReport("***************************************\n");
OSReport(" ! ! ! C A U T I O N ! ! ! \n");
OSReport("This TV format \"DEBUG_PAL\" is only for \n");
OSReport("temporary solution until PAL DAC board \n");
OSReport("is available. Please do NOT use this \n");
OSReport("mode in real games!!! \n");
OSReport("***************************************\n");
}
}
void VIConfigure(GXRenderModeObj* rm) {
VITiming* tm;
u32 regDspCfg;
BOOL enabled;
u32 newNonInter;
u32 tvInBootrom;
u32 tvInGame;
enabled = OSDisableInterrupts();
newNonInter = rm->viTVmode & 3;
if (HorVer.nonInter != newNonInter) {
changeMode = 1;
HorVer.nonInter = newNonInter;
}
ASSERTMSGLINEV(1926, (rm->viHeight & 1) == 0,
"VIConfigure(): Odd number(%d) is specified to viHeight\n",
rm->viHeight);
#if DEBUG
if (rm->xFBmode == VI_XFBMODE_DF || newNonInter == VI_TVMODE_NTSC_PROG || newNonInter == 3) {
ASSERTMSGLINEV(1933, rm->xfbHeight == rm->viHeight,
"VIConfigure(): xfbHeight(%d) is not equal to viHeight(%d) when DF XFB mode or progressive mode is specified\n",
rm->xfbHeight, rm->viHeight);
}
if (rm->xFBmode == VI_XFBMODE_SF && newNonInter != VI_TVMODE_NTSC_PROG && newNonInter != 3) {
ASSERTMSGLINEV(1941, rm->viHeight == rm->xfbHeight * 2,
"VIConfigure(): xfbHeight(%d) is not as twice as viHeight(%d) when SF XFB mode is specified\n",
rm->xfbHeight, rm->viHeight);
}
#endif
tvInGame = (u32)rm->viTVmode >> 2;
tvInBootrom = *(u32*)OSPhysicalToCached(0xCC);
if (tvInGame == VI_DEBUG_PAL) {
PrintDebugPalCaution();
}
switch (tvInBootrom) {
case VI_MPAL:
case VI_NTSC:
case VI_GCA:
if (tvInGame == VI_NTSC || tvInGame == VI_MPAL || tvInGame == VI_GCA) {
break;
}
goto panic;
case VI_PAL:
case VI_EURGB60:
if (tvInGame == VI_PAL || tvInGame == VI_EURGB60) {
break;
}
default:
panic:
OSPanic(__FILE__, 1884,
"VIConfigure(): Tried to change mode from (%d) to (%d), which is forbidden\n",
tvInBootrom, tvInGame);
}
if ((tvInGame == VI_NTSC) || (tvInGame == VI_MPAL)) {
HorVer.tv = tvInBootrom;
} else {
HorVer.tv = tvInGame;
}
HorVer.DispPosX = rm->viXOrigin;
HorVer.DispPosY = (HorVer.nonInter == 1) ? (u16)(rm->viYOrigin * 2) : rm->viYOrigin;
HorVer.DispSizeX = rm->viWidth;
HorVer.FBSizeX = rm->fbWidth;
HorVer.FBSizeY = rm->xfbHeight;
HorVer.FBMode = rm->xFBmode;
HorVer.PanSizeX = HorVer.FBSizeX;
HorVer.PanSizeY = HorVer.FBSizeY;
HorVer.PanPosX = 0;
HorVer.PanPosY = 0;
HorVer.DispSizeY = (HorVer.nonInter == 2) ? HorVer.PanSizeY :
(HorVer.nonInter == 3) ? HorVer.PanSizeY :
(HorVer.FBMode == VI_XFBMODE_SF) ? (u16)(HorVer.PanSizeY * 2) :
HorVer.PanSizeY;
HorVer.threeD = (HorVer.nonInter == 3) ? TRUE : FALSE;
tm = getTiming((HorVer.tv << 2) + HorVer.nonInter);
HorVer.timing = tm;
AdjustPosition(tm->acv);
ASSERTMSGLINEV(2022, rm->viXOrigin <= tm->hlw + 40 - tm->hbe640,
"VIConfigure(): viXOrigin(%d) cannot be greater than %d in this TV mode\n",
rm->viXOrigin, tm->hlw + 40 - tm->hbe640);
ASSERTMSGLINEV(2027, rm->viXOrigin + rm->viWidth >= 680 - tm->hbs640,
"VIConfigure(): viXOrigin + viWidth (%d) cannot be less than %d in this TV mode\n",
rm->viXOrigin + rm->viWidth, 680 - tm->hbs640);
if (encoderType == 0) {
HorVer.tv = 3;
}
setInterruptRegs(tm);
regDspCfg = regs[1];
if ((HorVer.nonInter == VI_PROGRESSIVE) || (HorVer.nonInter == VI_3D)) {
regDspCfg = (((u32)(regDspCfg)) & ~0x00000004) | (((u32)(1)) << 2);
} else {
regDspCfg = (((u32)(regDspCfg)) & ~0x00000004) | (((u32)(HorVer.nonInter & 1)) << 2);
}
regDspCfg = (((u32)(regDspCfg)) & ~0x00000008) | (((u32)(HorVer.threeD)) << 3);
if ((HorVer.tv == VI_DEBUG_PAL) || (HorVer.tv == VI_EURGB60) || (HorVer.tv == VI_GCA)) {
regDspCfg = (((u32)(regDspCfg)) & ~0x00000300) | (((u32)(0)) << 8);
} else {
regDspCfg = (((u32)(regDspCfg)) & ~0x00000300) | (((u32)(HorVer.tv)) << 8);
}
regs[1] = regDspCfg;
MARK_CHANGED(1);
regDspCfg = regs[54];
if (rm->viTVmode == VI_TVMODE_NTSC_PROG || rm->viTVmode == VI_TVMODE_NTSC_3D || rm->viTVmode == VI_TVMODE_GCA_PROG) {
regDspCfg = (u32)(regDspCfg & ~0x1) | 1;
} else {
regDspCfg = (u32)(regDspCfg & ~0x1);
}
regs[54] = (u16)regDspCfg;
MARK_CHANGED(54);
setScalingRegs(HorVer.PanSizeX, HorVer.DispSizeX, HorVer.threeD);
setHorizontalRegs(tm, HorVer.AdjustedDispPosX, HorVer.DispSizeX);
setBBIntervalRegs(tm);
setPicConfig(HorVer.FBSizeX, HorVer.FBMode, HorVer.PanPosX, HorVer.PanSizeX, &HorVer.wordPerLine, &HorVer.std, &HorVer.wpl, &HorVer.xof);
if (FBSet != 0) {
setFbbRegs(&HorVer, &HorVer.tfbb, &HorVer.bfbb, &HorVer.rtfbb, &HorVer.rbfbb);
}
setVerticalRegs(HorVer.AdjustedDispPosY, HorVer.AdjustedDispSizeY, tm->equ, tm->acv, tm->prbOdd, tm->prbEven, tm->psbOdd, tm->psbEven, HorVer.black);
OSRestoreInterrupts(enabled);
}
void VIConfigurePan(u16 xOrg, u16 yOrg, u16 width, u16 height) {
BOOL enabled;
VITiming* tm;
#if DEBUG
ASSERTMSGLINEV(2118, (xOrg & 1) == 0,
"VIConfigurePan(): Odd number(%d) is specified to xOrg\n",
xOrg);
if (HorVer.FBMode == VI_XFBMODE_DF) {
ASSERTMSGLINEV(2123, (height & 1) == 0,
"VIConfigurePan(): Odd number(%d) is specified to height when DF XFB mode\n",
height);
}
#endif
enabled = OSDisableInterrupts();
HorVer.PanPosX = xOrg;
HorVer.PanPosY = yOrg;
HorVer.PanSizeX = width;
HorVer.PanSizeY = height;
HorVer.DispSizeY = (HorVer.nonInter == 2) ? HorVer.PanSizeY :
(HorVer.nonInter == 3) ? HorVer.PanSizeY :
(HorVer.FBMode == VI_XFBMODE_SF) ? (u16)(HorVer.PanSizeY * 2) :
HorVer.PanSizeY;
tm = HorVer.timing;
AdjustPosition(tm->acv);
setScalingRegs(HorVer.PanSizeX, HorVer.DispSizeX, HorVer.threeD);
setPicConfig(HorVer.FBSizeX, HorVer.FBMode, HorVer.PanPosX, HorVer.PanSizeX, &HorVer.wordPerLine, &HorVer.std, &HorVer.wpl, &HorVer.xof);
if (FBSet != 0) {
setFbbRegs(&HorVer, &HorVer.tfbb, &HorVer.bfbb, &HorVer.rtfbb, &HorVer.rbfbb);
}
setVerticalRegs(HorVer.AdjustedDispPosY, HorVer.DispSizeY, tm->equ, tm->acv, tm->prbOdd, tm->prbEven, tm->psbOdd, tm->psbEven, HorVer.black);
OSRestoreInterrupts(enabled);
}
void VIFlush(void) {
BOOL enabled;
s32 regIndex;
enabled = OSDisableInterrupts();
shdwChangeMode |= changeMode;
changeMode = 0;
shdwChanged |= changed;
while (changed != 0) {
regIndex = cntlzd(changed);
shdwRegs[regIndex] = regs[regIndex];
changed &= ~((u64)1 << (63 - regIndex));
}
flushFlag = 1;
NextBufAddr = HorVer.bufAddr;
OSRestoreInterrupts(enabled);
}
void VISetNextFrameBuffer(void* fb) {
BOOL enabled;
ASSERTMSGLINEV(2216, ((u32)fb & 0x1F) == 0,
"VISetNextFrameBuffer(): Frame buffer address(0x%08x) is not 32byte aligned\n",
fb);
enabled = OSDisableInterrupts();
HorVer.bufAddr = (u32)fb;
FBSet = 1;
setFbbRegs(&HorVer, &HorVer.tfbb, &HorVer.bfbb, &HorVer.rtfbb, &HorVer.rbfbb);
OSRestoreInterrupts(enabled);
}
void VISetBlack(BOOL black) {
BOOL enabled;
VITiming* tm;
enabled = OSDisableInterrupts();
HorVer.black = black;
tm = HorVer.timing;
setVerticalRegs(HorVer.AdjustedDispPosY, HorVer.DispSizeY, tm->equ, tm->acv, tm->prbOdd, tm->prbEven, tm->psbOdd, tm->psbEven, HorVer.black);
OSRestoreInterrupts(enabled);
}
u32 VIGetRetraceCount(void) {
return retraceCount;
}
static u32 getCurrentHalfLine(void) {
u32 hcount;
u32 vcount0;
u32 vcount;
vcount = __VIRegs[22] & 0x7FF;
do {
vcount0 = vcount;
hcount = __VIRegs[23] & 0x7FF;
vcount = __VIRegs[22] & 0x7FF;
} while (vcount0 != vcount);
return ((vcount - 1) * 2) + ((hcount - 1) / CurrTiming->hlw);
}
static u32 getCurrentFieldEvenOdd(void) {
return (getCurrentHalfLine() < CurrTiming->nhlines) ? 1 : 0;
}
u32 VIGetNextField(void) {
s32 nextField;
BOOL enabled;
enabled = OSDisableInterrupts();
nextField = getCurrentFieldEvenOdd() ^ 1;
OSRestoreInterrupts(enabled);
return nextField ^ (HorVer.AdjustedDispPosY & 1);
}
u32 VIGetCurrentLine(void) {
u32 halfLine;
VITiming* tm;
BOOL enabled;
tm = CurrTiming;
enabled = OSDisableInterrupts();
halfLine = getCurrentHalfLine();
OSRestoreInterrupts(enabled);
if (halfLine >= tm->nhlines) {
halfLine -= tm->nhlines;
}
return halfLine >> 1U;
}
u32 VIGetTvFormat(void) {
u32 format;
BOOL enabled;
enabled = OSDisableInterrupts();
switch (CurrTvMode) {
case VI_NTSC:
case VI_DEBUG:
case 6:
format = VI_NTSC;
break;
case VI_PAL:
case VI_DEBUG_PAL:
format = VI_PAL;
break;
case VI_EURGB60:
case VI_MPAL:
format = CurrTvMode;
break;
default:
ASSERTLINE(2527, FALSE);
}
OSRestoreInterrupts(enabled);
return format;
}
u32 VIGetDTVStatus(void) {
u32 dtvStatus;
BOOL enabled = OSDisableInterrupts();
dtvStatus = __VIRegs[55] & 3;
OSRestoreInterrupts(enabled);
return dtvStatus & 1;
}
|