-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathpbkit.c
3730 lines (2891 loc) · 127 KB
/
pbkit.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
//pbKit core functions
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2007 Guillaume Lamonoca
// SPDX-FileCopyrightText: 2017 espes
// SPDX-FileCopyrightText: 2017-2020 Jannik Vogel
// SPDX-FileCopyrightText: 2018-2022 Stefan Schmidt
// SPDX-FileCopyrightText: 2019 Lucas Jansson
// SPDX-FileCopyrightText: 2021 Erik Abair
//#define DBG
//#define LOG
#include <hal/video.h>
#include <hal/xbox.h>
#include <xboxkrnl/xboxkrnl.h>
#include <hal/debug.h>
#include <stdbool.h>
#include <assert.h>
#include "pbkit.h"
#include "outer.h"
#include "nv_objects.h" //shared with renouveau files
#include "nv20_shader.h" //(search "nouveau" on wiki)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
//a macro used to build up a valid method
#define EncodeMethod(subchannel,command,nparam) ((nparam<<18)+(subchannel<<13)+command)
#define INSTANCE_MEM_MAXSIZE 0x5000 //20Kb
#define ADDR_SYSMEM 1
#define ADDR_FBMEM 2
#define ADDR_AGPMEM 3
#define DMA_CLASS_2 2
#define DMA_CLASS_3 3
#define DMA_CLASS_3D 0x3D
#define GR_CLASS_30 0x30
#define GR_CLASS_39 0x39
#define GR_CLASS_62 0x62
#define GR_CLASS_97 0x97
#define GR_CLASS_9F 0x9F
#define GPU_IRQ 3
#define XTAL_16MHZ 16.6667f
#define DW_XTAL_16MHZ 16666666
#define MAX_EXTRA_BUFFERS 8
#define MAXRAM 0x03FFAFFF
#define NONE -1
#define TICKSTIMEOUT 100 //if Dma doesn't react in that time, send a warning
#define PB_SETOUTER 0xB2A
#define PB_SETNOISE 0xBAA
#define PB_FINISHED 0xFAB
struct s_CtxDma
{
DWORD ChannelID;
DWORD Inst; //Addr in PRAMIN area, unit=16 bytes blocks, baseaddr=VIDEO_BASE+NV_PRAMIN
DWORD Class;
DWORD isGr;
};
static unsigned int pb_ColorFmt = NV097_SET_SURFACE_FORMAT_COLOR_LE_A8R8G8B8;
static unsigned int pb_DepthFmt = NV097_SET_SURFACE_FORMAT_ZETA_Z24S8;
static int pb_running=0;
static DWORD pb_vbl_counter=0;
#ifdef DBG
static int pb_trace_mode=1;
#else
static int pb_trace_mode=0;
#endif
//if set, we wait after each block sending (pb_end)
//so we are sure GPU received all the data (slower)
//and that any GPU error comes from last block sent.
static int pb_disable_gpu=0;
//if set, prevents GPU from delaying CPU when FIFO is
//full (allows to see how fast CPU code is fast alone)
static KINTERRUPT pb_InterruptObject;
static KDPC pb_DPCObject;
static HANDLE pb_VBlankEvent;
static DWORD pb_OldMCEnable;
static DWORD pb_OldMCInterrupt;
static DWORD pb_OldFBConfig0;
static DWORD pb_OldFBConfig1;
static DWORD pb_OldVideoStart;
static DWORD *pb_DmaBuffer8; //points at 32 contiguous bytes (Dma Channel ID 8 buffer)
static DWORD *pb_DmaBuffer2; //points at 32 contiguous bytes (Dma Channel ID 2 buffer)
static DWORD *pb_DmaBuffer7; //points at 32 contiguous bytes (Dma Channel ID 7 buffer)
static DWORD pb_Size=512*1024;//push buffer size, must be >64Kb and a power of 2
static uint32_t *pb_Head; //points at push buffer head
static uint32_t *pb_Tail; //points at push buffer tail
static uint32_t *pb_Put=NULL; //where next command+params are to be written
static float pb_CpuFrequency;
static DWORD pb_GpuInstMem;
static DWORD pb_PushBase;
static DWORD pb_PushLimit;
static DWORD pb_FifoHTAddr;
static DWORD pb_FifoFCAddr;
static DWORD pb_FifoU1Addr;
static DWORD pb_3DGrCtxInst[2]={0,0};//Adress of the two 3D graphic contexts (addr=inst<<4+NV_PRAMIN)
static DWORD pb_GrCtxTableInst; //Adress of the table that points at the two graphic contexts
static DWORD pb_GrCtxInst[2]; //Adress of the two graphic contexts (addr=inst<<4+NV_PRAMIN)
static int pb_GrCtxID; //Current context ID : 0,1 or NONE
static DWORD pb_FifoBigInst; //graphic contexts are stored there, and much more (addr=inst<<4+NV_PRAMIN)
static DWORD pb_FreeInst; //next free space in PRAMIN area (addr=inst<<4+NV_PRAMIN)
static int pb_GammaRampIdx=0;
static int pb_GammaRampbReady[3]={0,0,0};
static BYTE pb_GammaRamp[3][3][256];
static int pb_BackBufferNxt=0;
static int pb_BackBufferNxtVBL=0;
static int pb_BackBufferbReady[3]={0,0,0};
static int pb_BackBufferIndex[3];
static DWORD pb_FifoChannelsReady=0;
static DWORD pb_FifoChannelsMode=NV_PFIFO_MODE_ALL_PIO;
static DWORD pb_FifoChannelID=0;
static DWORD pb_PutRunSize=0;
static DWORD pb_GetRunSize;
static DWORD pb_FrameBuffersCount;
static DWORD pb_FrameBuffersWidth;
static DWORD pb_FrameBuffersHeight;
static DWORD pb_FrameBuffersAddr;
static DWORD pb_FrameBuffersPitch;
static DWORD pb_FBAddr[3]; //frame buffers addresses
static DWORD pb_FBSize; //size of 1 buffer
static DWORD pb_FBGlobalSize; //size of all buffers
static DWORD pb_FBVFlag;
static DWORD pb_GPUFrameBuffersFormat;//encoded format for GPU
static DWORD pb_EXAddr[8]; //extra buffers addresses
static DWORD pb_ExtraBuffersCount=0;
static DWORD pb_DepthStencilAddr;
static DWORD pb_DepthStencilPitch;
static int pb_DepthStencilLast;
static DWORD pb_DSAddr; //depth stencil address
static DWORD pb_DSSize; //size of depth stencil buffer
static DWORD pb_GPUDepthStencilFormat;//encoded format for GPU
static int pb_front_index;
static int pb_back_index;
static DWORD pb_Viewport_x;
static DWORD pb_Viewport_y;
static DWORD pb_Viewport_width;
static DWORD pb_Viewport_height;
static DWORD pb_Viewport_zmin;
static DWORD pb_Viewport_zmax;
static float pb_XScale;
static float pb_YScale;
static float pb_ZScale;
static float pb_GlobalScale;
static float pb_Bias;
static int pb_debug_screen_active;
static DWORD pb_DmaChID9Inst;
static DWORD pb_DmaChID10Inst;
static DWORD pb_DmaChID11Inst;
static volatile DWORD *pb_DmaUserAddr;
static DWORD pb_PushIndex;
static DWORD *pb_PushStart;
static DWORD *pb_PushNext;
static int pb_BeginEndPair=0;
static float pb_FixedPipelineConstants[12]={
0.0f, 0.5f, 1.0f, 2.0f,
-1.0f, 0.0f, 1.0f, 2.0f,
0.0f, 0.0f, -1.0f, 0.0f };
static float pb_IdentityMatrix[16]={
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
static DWORD pb_TilePitches[16]={
0x0200,0x0400,0x0600,0x0800,
0x0A00,0x0C00,0x0E00,0x1000,
0x1400,0x1800,0x1C00,0x2800,
0x3000,0x3800,0x5000,0x7000 };
static float pb_BiasTable[7]={
0.0f,
0.585f,
1.0f,
1.322f,
1.585f,
1.907f,
2.0f };
static HAL_SHUTDOWN_REGISTRATION pb_shutdown_registration;
//forward references
static void pb_load_gr_ctx(int ctx_id);
static NTAPI VOID pb_shutdown_notification_routine (PHAL_SHUTDOWN_REGISTRATION ShutdownRegistration);
//private pb_text_screen functions
#define ROWS 16
#define COLS 60
static char pb_text_screen[ROWS][COLS];
static int pb_next_row=0;
static int pb_next_col=0;
static unsigned char systemFont[] =
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,56,56,56,56,56,0,56,56,
108,108,0,0,0,0,0,0,0,108,254,254,108,254,254,108,
48,126,224,124,14,254,252,48,98,230,204,24,48,102,206,140,
120,220,252,120,250,222,252,118,28,28,56,0,0,0,0,0,
14,28,28,28,28,28,28,14,112,56,56,56,56,56,56,112,
0,0,0,230,124,56,124,206,0,0,28,28,127,127,28,28,
0,0,0,0,0,28,28,56,0,0,0,0,124,124,0,0,
0,0,0,0,0,0,56,56,28,28,56,56,112,112,224,224,
124,254,238,238,238,254,254,124,56,120,248,56,56,254,254,254,
252,254,14,60,112,254,254,254,252,254,14,60,14,254,254,252,
238,238,238,254,254,14,14,14,254,254,224,252,14,254,254,252,
124,252,224,252,238,254,254,124,252,254,14,14,28,28,56,56,
124,254,238,124,238,254,254,124,124,254,238,126,14,254,254,252,
0,0,28,28,0,28,28,28,0,0,28,28,0,28,28,56,
6,14,28,56,56,28,14,6,0,0,124,124,0,124,124,124,
112,56,28,14,14,28,56,112,124,254,206,28,56,0,56,56,
124,198,190,182,190,182,200,126,124,254,238,254,238,238,238,238,
252,254,206,252,206,254,254,252,124,254,238,224,238,254,254,124,
252,254,238,238,238,254,254,252,254,254,224,248,224,254,254,254,
126,254,224,248,224,224,224,224,126,254,224,238,238,254,254,124,
238,238,238,254,238,238,238,238,254,254,56,56,56,254,254,254,
254,254,14,14,238,254,254,124,238,238,252,248,252,238,238,238,
224,224,224,224,224,254,254,126,130,198,238,254,254,238,238,238,
206,238,254,254,254,254,238,230,124,254,238,238,238,254,254,124,
252,254,238,238,252,224,224,224,124,254,238,238,254,254,252,118,
252,254,238,238,252,238,238,238,126,254,224,124,14,254,254,252,
254,254,56,56,56,56,56,56,238,238,238,238,238,254,254,124,
238,238,238,238,238,238,124,56,238,238,238,254,254,238,198,130,
238,238,124,56,124,238,238,238,238,238,124,124,56,56,112,112,
254,254,28,56,112,254,254,254,124,124,112,112,112,124,124,124,
112,112,56,56,28,28,14,14,124,124,28,28,28,124,124,124,
56,124,238,198,0,0,0,0,0,0,0,0,0,254,254,254,
56,56,28,0,0,0,0,0,0,124,254,238,254,238,238,238,
0,252,254,206,252,206,254,252,0,124,254,238,224,238,254,124,
0,252,254,238,238,238,254,252,0,254,254,224,248,224,254,254,
0,126,254,224,248,224,224,224,0,126,254,224,238,238,254,124,
0,238,238,238,254,238,238,238,0,254,254,56,56,56,254,254,
0,254,254,14,14,238,254,124,0,238,238,252,248,252,238,238,
0,224,224,224,224,224,254,126,0,130,198,238,254,254,238,238,
0,206,238,254,254,254,238,230,0,124,254,238,238,238,254,124,
0,252,254,238,238,252,224,224,0,124,254,238,238,254,252,118,
0,252,254,238,238,252,238,238,0,126,254,224,124,14,254,252,
0,254,254,56,56,56,56,56,0,238,238,238,238,238,254,124,
0,238,238,238,238,238,124,56,0,238,238,238,254,238,198,130,
0,238,238,124,56,124,238,238,0,238,238,124,124,56,56,112,
0,254,254,28,56,112,254,254,60,124,112,112,112,124,124,60,
56,56,56,0,56,56,56,56,120,124,28,28,28,124,124,120,
236,254,118,0,0,0,0,0,0,16,56,124,254,254,254,254,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};
static void pb_scrollup(void)
{
int i;
for(i=0;i<ROWS-1;i++)
memcpy(&pb_text_screen[i][0],&pb_text_screen[i+1][0],COLS);
memset(&pb_text_screen[ROWS-1][0],0,COLS);
}
static void pb_print_char(char c)
{
if (c=='\n')
{
pb_next_row++;
if (pb_next_row>=ROWS) { pb_next_row=ROWS-1; pb_scrollup(); }
pb_next_col=0;
}
else
if (c=='\r')
{
pb_next_col=0;
}
else
if (c==8)
{
pb_next_col--;
if (pb_next_col<0) pb_next_col=0;
}
else
if (c>=32)
{
pb_text_screen[pb_next_row][pb_next_col]=c;
pb_next_col++;
if (pb_next_col>=COLS)
{
pb_next_row++;
if (pb_next_row>=ROWS) { pb_next_row=ROWS-1; pb_scrollup(); }
pb_next_col=0;
}
}
}
//private functions
static void pb_set_gamma_ramp(BYTE *pGammaRamp)
{
int i;
VIDEOREG8(NV_USER_DAC_WRITE_MODE_ADDRESS)=0; //&NV_USER_DAC_WRITE_MODE_ADDRESS_VALUE
for(i=0;i<256;i++)
{
VIDEOREG8(NV_USER_DAC_PALETTE_DATA)=pGammaRamp[i]; //&NV_USER_DAC_PALETTE_DATA_VALUE
VIDEOREG8(NV_USER_DAC_PALETTE_DATA)=pGammaRamp[i+256]; //&NV_USER_DAC_PALETTE_DATA_VALUE
VIDEOREG8(NV_USER_DAC_PALETTE_DATA)=pGammaRamp[i+512]; //&NV_USER_DAC_PALETTE_DATA_VALUE
}
}
static void pb_vbl_handler(void)
{
BYTE old_color_addr; //important index to preserve if we are called from Dpc or Isr
int flag;
int next;
int index;
old_color_addr=VIDEOREG8(NV_PRMCIO_CRX__COLOR);
pb_vbl_counter++;
//Index of next back buffer to show up (0-4)
next=pb_BackBufferNxtVBL;
//Is the next back buffer to show up is ready?
if (pb_BackBufferbReady[next]==1)
{
//screen swapping has been done already, theoretically, in ISR
pb_BackBufferbReady[next]=0;
index=pb_GammaRampIdx;
if (pb_GammaRampbReady[index])
{
pb_set_gamma_ramp(&pb_GammaRamp[index][0][0]);
pb_GammaRampbReady[index]=0;
index=(index+1)%3;
pb_GammaRampIdx=index;
}
VIDEOREG(NV_PGRAPH_INCREMENT)|=NV_PGRAPH_INCREMENT_READ_3D_TRIGGER;
//rotate next back buffer & gamma ramp index
next=(next+1)%3;
pb_BackBufferNxtVBL=next;
}
do
{
VIDEOREG(PCRTC_INTR)=PCRTC_INTR_VBLANK_RESET;
}while(VIDEOREG(NV_PMC_INTR_0)&NV_PMC_INTR_0_PCRTC_PENDING);
NtPulseEvent(pb_VBlankEvent, NULL);
// if (UserCallback) UserCallback(); //user callback must be brief and preserve fpu state
VIDEOREG8(NV_PRMCIO_CRX__COLOR)=old_color_addr; //restore color index
}
static void pb_cache_flush(void)
{
__asm__ __volatile__ ("sfence");
//assembler instruction "sfence" : waits end of previous instructions
VIDEOREG(NV_PFB_WC_CACHE)|=NV_PFB_WC_CACHE_FLUSH_TRIGGER;
while(VIDEOREG(NV_PFB_WC_CACHE)&NV_PFB_WC_CACHE_FLUSH_IN_PROGRESS) {};
}
static void pb_subprog(DWORD subprogID, DWORD paramA, DWORD paramB)
{
//inner registers 0x1D8C & 0x1D90 match 2 outer registers :
//[0x1D8C]=[NV20_TCL_PRIMITIVE_3D_PARAMETER_A]=VIDEOREG(NV_PGRAPH_PARAMETER_A)=[0xFD401A88]
//[0x1D90]=[NV20_TCL_PRIMITIVE_3D_PARAMETER_B]=VIDEOREG(NV_PGRAPH_PARAMETER_B)=[0xFD40186C]
//so they can be used by a push buffer sequence to set parameters
//before triggering a subprogram by the command 0x0100 which will
//throw an interrupt and have CPU execute its code right here.
//Here just test the subprogID value and execute your own subprogram
//associated code (avoid using subprogID=0, it seems to be reserved)
int next;
switch(subprogID)
{
case PB_SETOUTER: //sets an outer register
VIDEOREG(paramA)=paramB;
break;
case PB_SETNOISE: //Dxt1NoiseEnable: copy paramA in NV_PGRAPH_RDI(sel 0xE0 adr 0x50 & sel 0xDF adr 0x08)
VIDEOREG(NV_PGRAPH_RDI_INDEX)=((0xE0<<16)&NV_PGRAPH_RDI_INDEX_SELECT)|((0x50)&NV_PGRAPH_RDI_INDEX_ADDRESS);
VIDEOREG(NV_PGRAPH_RDI_DATA)=paramA;
VIDEOREG(NV_PGRAPH_RDI_INDEX)=((0xDF<<16)&NV_PGRAPH_RDI_INDEX_SELECT)|((0x08)&NV_PGRAPH_RDI_INDEX_ADDRESS);
VIDEOREG(NV_PGRAPH_RDI_DATA)=paramA;
break;
case PB_FINISHED: //warns that all drawing has been finished for the frame
next=pb_BackBufferNxt;
pb_BackBufferIndex[next]=paramA;
pb_BackBufferbReady[next]=1;
next=(next+1)%3;
pb_BackBufferNxt=next;
break;
default:
debugPrint( "Unknown subProgID %lu has been detected by DPC (A=%lx B=%lx).\n",
subprogID,
paramA,
paramB );
break;
}
}
static DWORD pb_gr_handler(void)
{
DWORD status;
DWORD trapped_address;
int trapped_ctx_id;
DWORD nsource;
DWORD GrClass;
DWORD DataLow;
int i;
DWORD *p;
VIDEOREG(NV_PGRAPH_FIFO)=NV_PGRAPH_FIFO_ACCESS_DISABLE;
status=VIDEOREG(NV_PGRAPH_INTR);
trapped_address=VIDEOREG(NV_PGRAPH_TRAPPED_ADDR);
nsource=VIDEOREG(NV_PGRAPH_NSOURCE);
trapped_ctx_id=(trapped_address&NV_PGRAPH_TRAPPED_ADDR_CHID)>>20;
trapped_address&=NV_PGRAPH_TRAPPED_ADDR_MTHD;
if (status&NV_PGRAPH_INTR_CONTEXT_SWITCH_PENDING)
{
VIDEOREG(NV_PGRAPH_INTR)=NV_PGRAPH_INTR_CONTEXT_SWITCH_RESET;
while(VIDEOREG(NV_PGRAPH_STATUS));
pb_load_gr_ctx(trapped_ctx_id);
}
if (status&NV_PGRAPH_INTR_MISSING_HW_PENDING)
{
VIDEOREG(NV_PGRAPH_INTR)=NV_PGRAPH_INTR_MISSING_HW_RESET;
}
if ( (status&NV_PGRAPH_INTR_NOTIFY_PENDING)||
(status&NV_PGRAPH_INTR_ERROR_PENDING) )
{
if (nsource&NV_PGRAPH_NSOURCE_ILLEGAL_MTHD_PENDING)
{
if (status&NV_PGRAPH_INTR_NOTIFY_PENDING)
VIDEOREG(NV_PGRAPH_INTR)= NV_PGRAPH_INTR_NOTIFY_RESET|
NV_PGRAPH_INTR_ERROR_RESET|
NV_PGRAPH_INTR_SINGLE_STEP_RESET|
NV_PGRAPH_INTR_MORE_RESET;
else
VIDEOREG(NV_PGRAPH_INTR)=NV_PGRAPH_INTR_ERROR_RESET;
}
}
status=VIDEOREG(NV_PGRAPH_INTR);
if (status)
{
VIDEOREG(NV_PGRAPH_INTR)=NV_PGRAPH_INTR_CONTEXT_SWITCH_RESET;
if ( (status!=NV_PGRAPH_INTR_CONTEXT_SWITCH_PENDING)&&
(status!=NV_PGRAPH_INTR_SINGLE_STEP_PENDING) )
{
if (status&NV_PGRAPH_INTR_MISSING_HW_PENDING)
{
while(VIDEOREG(NV_PGRAPH_STATUS)) {};
}
if (nsource)
{
if ( (status&NV_PGRAPH_INTR_NOTIFY_PENDING)||
(status&NV_PGRAPH_INTR_ERROR_PENDING) )
{
GrClass=VIDEOREG(NV_PGRAPH_CTX_SWITCH1)&NV_PGRAPH_CTX_SWITCH1_GRCLASS;
DataLow=VIDEOREG(NV_PGRAPH_TRAPPED_DATA_LOW); //&NV_PGRAPH_TRAPPED_DATA_LOW_VALUE
if ((nsource&NV_PGRAPH_NSOURCE_ILLEGAL_MTHD_PENDING)==0)
{
if (trapped_address==0x0100)
{
//The following line may be a bad idea. But without it, interrupt fires permanently...
VIDEOREG(NV_PGRAPH_INTR)=NV_PGRAPH_INTR_ERROR_RESET;
//calls subprogram
pb_subprog(DataLow,VIDEOREG(NV_PGRAPH_PARAMETER_A),VIDEOREG(NV_PGRAPH_PARAMETER_B));
}
else
{
pb_show_debug_screen();
debugPrint("\n");
if (nsource&NV_PGRAPH_NSOURCE_DATA_ERROR_PENDING) debugPrint("GPU Error : invalid data error!\n");
if (nsource&NV_PGRAPH_NSOURCE_PROTECTION_ERROR_PENDING) debugPrint("GPU Error : protection error!\n");
if (nsource&NV_PGRAPH_NSOURCE_RANGE_EXCEPTION_PENDING) debugPrint("GPU Error : range exception error!\n");
if (nsource&NV_PGRAPH_NSOURCE_LIMIT_COLOR_PENDING) debugPrint("GPU Error : color buffer limit error!\n");
if (nsource&NV_PGRAPH_NSOURCE_LIMIT_ZETA_PENDING) debugPrint("GPU Error : zeta buffer limit error!\n");
if (nsource&NV_PGRAPH_NSOURCE_DMA_R_PROTECTION_PENDING) debugPrint("GPU Error : dma read protection error!\n");
if (nsource&NV_PGRAPH_NSOURCE_DMA_W_PROTECTION_PENDING) debugPrint("GPU Error : dma write protection error!\n");
if (nsource&NV_PGRAPH_NSOURCE_FORMAT_EXCEPTION_PENDING) debugPrint("GPU Error : format exception error!\n");
if (nsource&NV_PGRAPH_NSOURCE_PATCH_EXCEPTION_PENDING) debugPrint("GPU Error : patch exception error!\n");
if (nsource&NV_PGRAPH_NSOURCE_STATE_INVALID_PENDING) debugPrint("GPU Error : object state invalid error!\n");
if (nsource&NV_PGRAPH_NSOURCE_DOUBLE_NOTIFY_PENDING) debugPrint("GPU Error : double notify error!\n");
if (nsource&NV_PGRAPH_NSOURCE_NOTIFY_IN_USE_PENDING) debugPrint("GPU Error : notify in use error!\n");
if (nsource&NV_PGRAPH_NSOURCE_METHOD_CNT_PENDING) debugPrint("GPU Error : method count error!\n");
if (nsource&NV_PGRAPH_NSOURCE_BFR_NOTIFICATION_PENDING) debugPrint("GPU Error : buffer notification error!\n");
if (nsource&NV_PGRAPH_NSOURCE_DMA_VTX_PROTECTION_PENDING) debugPrint("GPU Error : DMA vertex protection error!\n");
if (nsource&NV_PGRAPH_NSOURCE_IDX_INLINE_REUSE_PENDING) debugPrint("Graphics index inline reuse error!\n");
if (nsource&NV_PGRAPH_NSOURCE_INVALID_OPERATION_PENDING) debugPrint("GPU Error : invalid operation error!\n");
if (nsource&NV_PGRAPH_NSOURCE_FD_INVALID_OPERATION_PENDING) debugPrint("GPU Error : FD invalid operation error!\n");
if (nsource&NV_PGRAPH_NSOURCE_TEX_A_PROTECTION_PENDING) debugPrint("GPU Error : texture A protection error!\n");
if (nsource&NV_PGRAPH_NSOURCE_TEX_B_PROTECTION_PENDING) debugPrint("GPU Error : texture B protection error!\n");
debugPrint( "Error binary flags : %08lx\n"
"Channel ID : %d (0=3D)\n"
"Channel class : %lx\n"
"Push buffer inner register target : %04lx\n"
"Push buffer data (lo) or instance : %08lx\n"
"Push buffer data (hi) or instance : %08x\n"
"Multi-purpose register A [0x1D8C] : %08x\n"
"Multi-purpose register B [0x1D90] : %08x\n\n",
nsource,
trapped_ctx_id,
GrClass,
trapped_address,
DataLow,
VIDEOREG(NV_PGRAPH_TRAPPED_DATA_HIGH),
VIDEOREG(NV_PGRAPH_PARAMETER_A),
VIDEOREG(NV_PGRAPH_PARAMETER_B) );
if (pb_trace_mode==0) debugPrint("Report is accurate only if pb_trace_mode=1 (slower)\n");
debugPrint("System halted\n");
//calling XReboot() from here doesn't work well.
while(1) {};
}
}
}
}
if (status&NV_PGRAPH_INTR_BUFFER_NOTIFY_PENDING)
{
while (VIDEOREG(NV_PGRAPH_STATUS)) {};
}
}
}
VIDEOREG(NV_PGRAPH_FIFO)=NV_PGRAPH_FIFO_ACCESS_ENABLE;
return VIDEOREG(NV_PGRAPH_INTR);
}
void pb_wait_until_gr_not_busy(void)
{
DWORD status;
while(VIDEOREG(NV_PGRAPH_STATUS)!=NV_PGRAPH_STATUS_NOT_BUSY)
{
status=VIDEOREG(NV_PMC_INTR_0);
if (status&NV_PMC_INTR_0_PGRAPH_PENDING) pb_gr_handler();
if (status&NV_PMC_INTR_0_PCRTC_PENDING) pb_vbl_handler();
}
}
static void pb_load_gr_ctx(int ctx_id)
{
DWORD old_fifo_access;
DWORD dummy;
int i;
if (VIDEOREG(NV_PGRAPH_INTR)!=NV_PGRAPH_INTR_NOT_PENDING) pb_gr_handler();
old_fifo_access=VIDEOREG(NV_PGRAPH_FIFO);
VIDEOREG(NV_PGRAPH_FIFO)=NV_PGRAPH_FIFO_ACCESS_DISABLE;
pb_wait_until_gr_not_busy();
if ((ctx_id!=pb_GrCtxID)&&(ctx_id!=NONE))
{
VIDEOREG(NV_PGRAPH_CHANNEL_CTX_POINTER)=pb_GrCtxInst[ctx_id]&NV_PGRAPH_CHANNEL_CTX_POINTER_INST;
VIDEOREG(NV_PGRAPH_CHANNEL_CTX_STATUS)=NV_PGRAPH_CHANNEL_CTX_STATUS_UNLOADED;
pb_wait_until_gr_not_busy();
VIDEOREG(NV_PGRAPH_CTX_CONTROL)=NV_PGRAPH_CTX_CONTROL_DEVICE_ENABLED;
}
pb_GrCtxID=ctx_id;
if (ctx_id==NONE)
{
VIDEOREG(NV_PGRAPH_CTX_CONTROL)=NV_PGRAPH_CTX_CONTROL_DEVICE_ENABLED|NV_PGRAPH_CTX_CONTROL_TIME_NOT_EXPIRED;
VIDEOREG(NV_PGRAPH_FFINTFC_ST2)=NV_PGRAPH_FFINTFC_ST2_CHID_STATUS_VALID;
VIDEOREG(NV_PGRAPH_FIFO)=old_fifo_access|NV_PGRAPH_FIFO_ACCESS_ENABLE;
}
else
{
if (pb_3DGrCtxInst[ctx_id])
{
VIDEOREG(NV_PGRAPH_DEBUG_0) = NV_PGRAPH_DEBUG_0_IDX_STATE_RESET|
NV_PGRAPH_DEBUG_0_VTX_STATE_RESET|
NV_PGRAPH_DEBUG_0_CAS_STATE_RESET;
dummy=VIDEOREG(NV_PGRAPH_DEBUG_0);
VIDEOREG(NV_PGRAPH_DEBUG_0)=NV_PGRAPH_DEBUG_0_NO_RESET;
dummy=VIDEOREG(NV_PGRAPH_DEBUG_0);
VIDEOREG(NV_PGRAPH_RDI_INDEX)=((0x00)&NV_PGRAPH_RDI_INDEX_ADDRESS)|((0x3D<<16)&NV_PGRAPH_RDI_INDEX_SELECT);
for(i=0;i<15;i++) VIDEOREG(NV_PGRAPH_RDI_DATA)=0;
}
VIDEOREG(NV_PGRAPH_DEBUG_1)|=NV_PGRAPH_DEBUG_1_CACHE_INVALIDATE;
VIDEOREG(NV_PGRAPH_CTX_USER)=(ctx_id<<24)&NV_PGRAPH_CTX_USER_CHID;
VIDEOREG(NV_PGRAPH_CHANNEL_CTX_POINTER)=pb_GrCtxInst[ctx_id]&NV_PGRAPH_CHANNEL_CTX_POINTER_INST;
VIDEOREG(NV_PGRAPH_CHANNEL_CTX_STATUS)=NV_PGRAPH_CHANNEL_CTX_STATUS_LOADED;
pb_wait_until_gr_not_busy();
VIDEOREG(NV_PGRAPH_CTX_USER)=(VIDEOREG(NV_PGRAPH_CTX_USER)&~NV_PGRAPH_CTX_USER_CHID)|((ctx_id<<24)&NV_PGRAPH_CTX_USER_CHID);
VIDEOREG(NV_PGRAPH_CTX_CONTROL) = NV_PGRAPH_CTX_CONTROL_TIME_NOT_EXPIRED|
NV_PGRAPH_CTX_CONTROL_CHID_VALID|
NV_PGRAPH_CTX_CONTROL_DEVICE_ENABLED;
VIDEOREG(NV_PGRAPH_FFINTFC_ST2)&=(NV_PGRAPH_FFINTFC_ST2_CHSWITCH_CLEAR&NV_PGRAPH_FFINTFC_ST2_FIFOHOLD_CLEAR);
}
}
static DWORD pb_fifo_handler(void)
{
DWORD i;
DWORD status;
DWORD pull;
DWORD get_address;
int skip_waiting;
skip_waiting=0;
status=VIDEOREG(NV_PFIFO_INTR_0);
if (status&NV_PFIFO_INTR_0_SEMAPHORE_PENDING)
{
VIDEOREG(NV_PFIFO_INTR_0)=NV_PFIFO_INTR_0_SEMAPHORE_RESET;
}
if (status&NV_PFIFO_INTR_0_ACQUIRE_TIMEOUT_PENDING)
{
VIDEOREG(NV_PFIFO_INTR_0)=NV_PFIFO_INTR_0_ACQUIRE_TIMEOUT_RESET;
}
status=VIDEOREG(NV_PFIFO_INTR_0);
if (status&NV_PFIFO_INTR_0_CACHE_ERROR_PENDING)
{
pull=VIDEOREG(NV_PFIFO_CACHE1_PULL0);
get_address=VIDEOREG(NV_PFIFO_CACHE1_GET); //&NV_PFIFO_CACHE1_GET_ADDRESS (0x3FC)
get_address>>=2;
VIDEOREG(NV_PFIFO_CACHES)=NV_PFIFO_CACHES_ALL_DISABLE;
VIDEOREG(NV_PFIFO_CACHE1_PULL0)=NV_PFIFO_CACHE1_PULL0_ACCESS_DISABLE;
VIDEOREG(NV_PFIFO_INTR_0)=NV_PFIFO_INTR_0_CACHE_ERROR_RESET;
for(i=0;i<65535;i++)
{
if ((pull&NV_PFIFO_CACHE1_PULL0_HASH_STATE_BUSY)==0) break;
pull=VIDEOREG(NV_PFIFO_CACHE1_PULL0);
}
if ( (pull&NV_PFIFO_CACHE1_PULL0_DEVICE_SOFTWARE)||
(pull&NV_PFIFO_CACHE1_PULL0_HASH_FAILED) )
{
VIDEOREG(NV_PFIFO_CACHE1_GET)=((get_address+1)<<2)&NV_PFIFO_CACHE1_GET_ADDRESS;
}
VIDEOREG(NV_PFIFO_CACHE1_HASH)=0; //&NV_PFIFO_CACHE1_HASH_INSTANCE
VIDEOREG(NV_PFIFO_CACHE1_PULL0)=NV_PFIFO_CACHE1_PULL0_ACCESS_ENABLE;
VIDEOREG(NV_PFIFO_CACHES)=NV_PFIFO_CACHES_REASSIGN_ENABLED;
}
if (status&NV_PFIFO_INTR_0_DMA_PUSHER_PENDING)
{
pb_show_debug_screen();
debugPrint("Software Put=%08lx\n",(DWORD)pb_Put);
debugPrint("Hardware Put=%08x\n",VIDEOREG(NV_PFIFO_CACHE1_DMA_PUT));
debugPrint("Hardware Get=%08x\n",VIDEOREG(NV_PFIFO_CACHE1_DMA_GET));
debugPrint("Dma push buffer engine encountered invalid data at these addresses.\n");
VIDEOREG(NV_PFIFO_INTR_0)=NV_PFIFO_INTR_0_DMA_PUSHER_RESET;
VIDEOREG(NV_PFIFO_CACHE1_DMA_STATE)=NV_PFIFO_CACHE1_DMA_STATE_METHOD_COUNT_0;
if (VIDEOREG(NV_PFIFO_CACHE1_DMA_PUT)!=VIDEOREG(NV_PFIFO_CACHE1_DMA_GET))
VIDEOREG(NV_PFIFO_CACHE1_DMA_GET)+=(1<<2);
}
if (status&NV_PFIFO_INTR_0_DMA_PT_PENDING)
{
VIDEOREG(NV_PFIFO_INTR_0)=NV_PFIFO_INTR_0_DMA_PT_RESET;
}
if (VIDEOREG(NV_PFIFO_CACHE1_DMA_PUSH)&NV_PFIFO_CACHE1_DMA_PUSH_STATE_BUSY)
{
if ((VIDEOREG8(NV_PFIFO_CACHE1_STATUS)&NV_PFIFO_CACHE1_STATUS_LOW_MARK_EMPTY)==0)
do
{
if (VIDEOREG(NV_PFIFO_INTR_0)==NV_PFIFO_INTR_0_NOT_PENDING)
{
if (VIDEOREG(NV_PGRAPH_INTR)) pb_fifo_handler();
if (VIDEOREG(NV_PMC_INTR_0)&NV_PMC_INTR_0_PCRTC_PENDING) pb_vbl_handler();
if ((VIDEOREG8(NV_PFIFO_CACHE1_STATUS)&NV_PFIFO_CACHE1_STATUS_LOW_MARK_EMPTY)==0)
continue; //jump to loop start
}
if ((VIDEOREG8(NV_PFIFO_CACHE1_STATUS)&NV_PFIFO_CACHE1_STATUS_LOW_MARK_EMPTY)==0)
{
skip_waiting=1;
break;
}
}while(VIDEOREG8(NV_PFIFO_CACHE1_STATUS)&NV_PFIFO_CACHE1_STATUS_LOW_MARK_EMPTY);
if (skip_waiting==0)
{
//wait
while(VIDEOREG8(NV_PFIFO_CACHES)&NV_PFIFO_CACHES_DMA_SUSPEND_BUSY);
VIDEOREG(NV_PFIFO_CACHE1_DMA_PUSH)&=NV_PFIFO_CACHE1_DMA_PUSH_STATUS_RUNNING;
}
}
if (VIDEOREG(NV_PFIFO_INTR_0)==NV_PFIFO_INTR_0_NOT_PENDING)
{
VIDEOREG(NV_PFIFO_CACHE1_PULL0)=NV_PFIFO_CACHE1_PULL0_ACCESS_ENABLE;
VIDEOREG(NV_PFIFO_CACHES)=NV_PFIFO_CACHES_REASSIGN_ENABLED;
}
return VIDEOREG(NV_PFIFO_INTR_0)|(VIDEOREG(NV_PFIFO_DEBUG_0)&NV_PFIFO_DEBUG_0_CACHE_ERROR0_PENDING);
}
static void pb_set_fifo_channel(int channel)
{
DWORD old_caches,old_push,old_pull,old_channel;
DWORD *p;
DWORD pending_flags;
old_caches=VIDEOREG(NV_PFIFO_CACHES);
old_push=VIDEOREG(NV_PFIFO_CACHE1_PUSH0);
old_pull=VIDEOREG(NV_PFIFO_CACHE1_PULL0);
VIDEOREG(NV_PFIFO_CACHES)=NV_PFIFO_CACHES_ALL_DISABLE;
VIDEOREG(NV_PFIFO_CACHE1_PUSH0)=NV_PFIFO_CACHE1_PUSH0_ACCESS_DISABLE;
VIDEOREG(NV_PFIFO_CACHE1_PULL0)=NV_PFIFO_CACHE1_PULL0_ACCESS_DISABLE;
old_channel=VIDEOREG(NV_PFIFO_CACHE1_PUSH1)&NV_PFIFO_CACHE1_PUSH1_CHID;
//backup old channel details into PRAMIN area
p=(DWORD *)(VIDEO_BASE+pb_FifoFCAddr+old_channel*64);
*(p+0)=VIDEOREG(NV_PFIFO_CACHE1_DMA_PUT); //&NV_PFIFO_CACHE1_DMA_PUT_OFFSET
*(p+1)=VIDEOREG(NV_PFIFO_CACHE1_DMA_GET); //&NV_PFIFO_CACHE1_DMA_GET_OFFSET
*(p+2)=VIDEOREG(NV_PFIFO_CACHE1_REF); //&NV_PFIFO_CACHE1_REF_CNT
*(p+3)=VIDEOREG(NV_PFIFO_CACHE1_DMA_INSTANCE); //&NV_PFIFO_CACHE1_DMA_INSTANCE_ADDRESS
*(p+4)=VIDEOREG(NV_PFIFO_CACHE1_DMA_STATE);
*(p+5)=VIDEOREG(NV_PFIFO_CACHE1_DMA_FETCH);
*(p+6)=VIDEOREG(NV_PFIFO_CACHE1_ENGINE);
*(p+7)=VIDEOREG(NV_PFIFO_CACHE1_PULL1);
*(p+8)=VIDEOREG(NV_PFIFO_CACHE1_ACQUIRE_2); //&NV_PFIFO_CACHE1_ACQUIRE_2_VALUE
*(p+9)=VIDEOREG(NV_PFIFO_CACHE1_ACQUIRE_1); //&NV_PFIFO_CACHE1_ACQUIRE_1_TIMESTAMP
*(p+10)=VIDEOREG(NV_PFIFO_CACHE1_ACQUIRE_0); //&NV_PFIFO_CACHE1_ACQUIRE_0_TIMEOUT
*(p+11)=VIDEOREG(NV_PFIFO_CACHE1_SEMAPHORE);
*(p+12)=VIDEOREG(NV_PFIFO_CACHE1_DMA_SUBROUTINE);
if (VIDEOREG(NV_PFIFO_CACHE1_PUSH1)&NV_PFIFO_CACHE1_PUSH1_MODE_DMA)
{
pending_flags=VIDEOREG(NV_PFIFO_DMA);
pending_flags&=~(1<<old_channel);
if (VIDEOREG(NV_PFIFO_CACHE1_DMA_PUT)!=VIDEOREG(NV_PFIFO_CACHE1_DMA_GET))
pending_flags|=(1<<old_channel);
VIDEOREG(NV_PFIFO_DMA)=pending_flags;
}
//let's switch from old_channel to channel
VIDEOREG(NV_PFIFO_CACHE1_PUSH1)=channel&NV_PFIFO_CACHE1_PUSH1_CHID;
if (channel!=1)
if (pb_FifoChannelsMode&(1<<channel)) //Channel mode was DMA?
VIDEOREG(NV_PFIFO_CACHE1_PUSH1)|=NV_PFIFO_CACHE1_PUSH1_MODE_DMA;
//restore channel details from VRAM
p=(DWORD *)(VIDEO_BASE+pb_FifoFCAddr+channel*64);
VIDEOREG(NV_PFIFO_CACHE1_DMA_PUT)=*(p+0); //&NV_PFIFO_CACHE1_DMA_PUT_OFFSET
VIDEOREG(NV_PFIFO_CACHE1_DMA_GET)=*(p+1); //&NV_PFIFO_CACHE1_DMA_GET_OFFSET
VIDEOREG(NV_PFIFO_CACHE1_REF)=*(p+2); //&NV_PFIFO_CACHE1_REF_CNT
VIDEOREG(NV_PFIFO_CACHE1_DMA_INSTANCE)=*(p+3); //&NV_PFIFO_CACHE1_DMA_INSTANCE_ADDRESS
VIDEOREG(NV_PFIFO_CACHE1_DMA_STATE)=*(p+4);
VIDEOREG(NV_PFIFO_CACHE1_DMA_FETCH)=*(p+5);
VIDEOREG(NV_PFIFO_CACHE1_ENGINE)=*(p+6);
VIDEOREG(NV_PFIFO_CACHE1_PULL1)=*(p+7);
VIDEOREG(NV_PFIFO_CACHE1_ACQUIRE_2)=*(p+8); //&NV_PFIFO_CACHE1_ACQUIRE_2_VALUE
VIDEOREG(NV_PFIFO_CACHE1_ACQUIRE_1)=*(p+9); //&NV_PFIFO_CACHE1_ACQUIRE_1_TIMESTAMP
VIDEOREG(NV_PFIFO_CACHE1_ACQUIRE_0)=*(p+10); //&NV_PFIFO_CACHE1_ACQUIRE_0_TIMEOUT
VIDEOREG(NV_PFIFO_CACHE1_SEMAPHORE)=*(p+11);
VIDEOREG(NV_PFIFO_CACHE1_DMA_SUBROUTINE)=*(p+12);
if (channel!=1)
if (pb_FifoChannelsMode&(1<<channel)) //Channel mode was DMA?
VIDEOREG(NV_PFIFO_CACHE1_DMA_PUSH)=NV_PFIFO_CACHE1_DMA_PUSH_ACCESS_ENABLE;
VIDEOREG(NV_PFIFO_TIMESLICE)=NV_PFIFO_TIMESLICE_TIMER_EXPIRED;
VIDEOREG(NV_PFIFO_CACHE1_PULL0)=old_pull;
VIDEOREG(NV_PFIFO_CACHE1_PUSH0)=old_push;
VIDEOREG(NV_PFIFO_CACHES)=old_caches;
}
static void __stdcall DPC(PKDPC Dpc, PVOID DeferredContext, PVOID SystemArgument1, PVOID SystemArgument2)
{
//Deferred Procedure Call (delayed treatment, triggered by ISR)
//DPCs avoid crashes inside non reentrant user callbacks called by nested ISRs.
//CAUTION : if you use fpu in DPC you have to save & restore yourself fpu state!!!
//(fpu=floating point unit, i.e the coprocessor executing floating point opcodes)
DWORD more;
DWORD status;
do
{
more=0;
status=VIDEOREG(NV_PMC_INTR_0);