summaryrefslogtreecommitdiff
path: root/libpcsxcore/gpu.c
blob: 2ec06e1ba7538371ede436ba3f4636aef2b4cccf (plain) (blame)
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
/*  Copyright (c) 2010, shalma.
 *  Portions Copyright (c) 2002, Pete Bernert.
 *
 *  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 02111-1307 USA
 */

#include "psxhw.h"
#include "gpu.h"
#include "psxdma.h"

extern unsigned int hSyncCount;

#define GPUSTATUS_ODDLINES            0x80000000
#define GPUSTATUS_DMABITS             0x60000000 // Two bits
#define GPUSTATUS_READYFORCOMMANDS    0x10000000
#define GPUSTATUS_READYFORVRAM        0x08000000
#define GPUSTATUS_IDLE                0x04000000

#define GPUSTATUS_DISPLAYDISABLED     0x00800000
#define GPUSTATUS_INTERLACED          0x00400000
#define GPUSTATUS_RGB24               0x00200000
#define GPUSTATUS_PAL                 0x00100000
#define GPUSTATUS_DOUBLEHEIGHT        0x00080000
#define GPUSTATUS_WIDTHBITS           0x00070000 // Three bits
#define GPUSTATUS_MASKENABLED         0x00001000
#define GPUSTATUS_MASKDRAWN           0x00000800
#define GPUSTATUS_DRAWINGALLOWED      0x00000400
#define GPUSTATUS_DITHER              0x00000200


static __inline int prim__( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain ?? - unknown command\n", addr );
#endif

	return 0;
}


static __inline int prim00( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 00 - reset\n", addr );
#endif

	return 0;
}


static __inline int prim01( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 01 - clear cache\n", addr );
#endif

	return 0;
}


static __inline int prim02( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 02 - clear screen [#2]\n", addr );
#endif

	return 2;
}


static __inline int prim03( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 03 - enable display [#???]\n", addr );
#endif

	return 0;
}


static __inline int prim04( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 04 - dma setup [#???]\n", addr );
#endif

	return 0;
}


static __inline int prim05( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 05 - start display area [#???]\n", addr );
#endif

	return 0;
}


static __inline int prim06( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 06 - h-display area [#???]\n", addr );
#endif

	return 0;
}


static __inline int prim07( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 07 - v-display area [#???]\n", addr );
#endif

	return 0;
}


static __inline int prim08( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 08 - display mode [#???]\n", addr );
#endif

	return 0;
}


static __inline int prim10( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 10 - gpu info [#???]\n", addr );
#endif

	return 0;
}


static __inline int prim20( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 20 - 3-poly mono [#3]\n", addr );
#endif

	return 3;
}


static __inline int prim24( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 24 - 3-poly texture [#6]\n", addr );
#endif

	return 6;
}


static __inline int prim28( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 28 - 4-poly mono [#4]\n", addr );
#endif

	return 4;
}


static __inline int prim2C( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 2c - 4-poly texture [#8]\n", addr );
#endif

	return 8;
}


static __inline int prim30( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 30 - Gouraud tri [#5]\n", addr );
#endif

	return 5;
}


static __inline int prim34( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 34 - Gouraud tri texture [#8]\n", addr );
#endif

	return 8;
}


static __inline int prim38( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 38 - Gouraud quad [#7]\n", addr );
#endif

	return 7;
}


static __inline int prim3C( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 3c - Gouraud quad texture [#11]\n", addr );
#endif

	return 11;
}


static __inline int prim40( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 40 - mono 2-line [#2]\n", addr );
#endif

	return 2;
}


static __inline int prim48( u32 addr )
{
	int lcv;

#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 48 - mono 3-line [#_#]\n", addr );
#endif

	lcv = 1;

	while( (psxMu32( addr ) & 0xF000F000) != 0x50005000  )
	{
		addr += 4;
		lcv++;
	}

	return lcv;
}


static __inline int prim4C( u32 addr )
{
	int lcv;

#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 4c - mono 4-line [#_#]\n", addr );
#endif

	lcv = 1;

	while( (psxMu32( addr ) & 0xF000F000) != 0x50005000  )
	{
		addr += 4;
		lcv++;
	}

	return lcv;
}


static __inline int prim50( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 50 - 2-point gradated [#3]\n", addr );
#endif

	return 3;
}


static __inline int prim58( u32 addr )
{
	int lcv;

#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 58 - 3-point gradated [#__#]\n", addr );
#endif

	lcv = 1;

	while( (psxMu32( addr ) & 0xF000F000) != 0x50005000  )
	{
		addr += 4;
		lcv++;
	}

	return lcv;
}


static __inline int prim5C( u32 addr )
{
	int lcv;

#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 5c - 4-point gradated [#__#]\n", addr );
#endif

	lcv = 1;

	while( (psxMu32( addr ) & 0xF000F000) != 0x50005000  )
	{
		addr += 4;
		lcv++;
	}

	return lcv;
}


static __inline int prim60( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 60 - rect [#2]\n", addr );
#endif

	return 2;
}


static __inline int prim64( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 64 - sprite [#3]\n", addr );
#endif

	return 3;
}


static __inline int prim68( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 68 - dot [#1]\n", addr );
#endif

	return 1;
}


static __inline int prim70( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 70 - 8x8 rectangle [#1]\n", addr );
#endif

	return 1;
}


static __inline int prim74( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 74 - 8x8 sprite [#2]\n", addr );
#endif

	return 2;
}


static __inline int prim78( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 78 - 16x16 rectangle [#1]\n", addr );
#endif

	return 1;
}


static __inline int prim7C( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 7c - 16x16 sprite [#2]\n", addr );
#endif

	return 2;
}


static __inline int prim80( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain 80 - move image [#3]\n", addr );
#endif

	return 3;
}


static __inline int primA0( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain a0 - send image to [#2+__#]\n", addr );
#endif

	// count transfer bytes 2 + (h*w)
	return 2+0;
}


static __inline int primC0( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain c0 - copy image from [#2+__#]\n", addr );
#endif

	// count transfer bytes 2 + (h*w)
	return 2+0;
}


static __inline int primE1( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain E1 - draw mode setting\n", addr );
#endif

	return 0;
}


static __inline int primE2( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain E2 - texture window setting\n", addr );
#endif

	return 0;
}


static __inline int primE3( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain E3 - set draw area top left\n", addr );
#endif

	return 0;
}


static __inline int primE4( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain E4 - set draw area bottom right\n", addr );
#endif

	return 0;
}


static __inline int primE5( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain E5 - drawing offset\n", addr );
#endif

	return 0;
}


static __inline int primE6( u32 addr )
{
#ifdef CHAIN_LOG
	PSXDMA_LOG( "%X @ Chain E6 - mask setting\n", addr );
#endif

	return 0;
}



// Agemo's idea for chain unrolling
int (*gpuPrim[0x100])( u32 addr ) = 
{
	//  00      01      02      03      04       05     06      07
	prim00, prim01, prim02, prim03, prim04, prim05, prim06, prim07, //00-07
	prim08, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //08-0F
	prim10, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //10-17
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //18-1F
	prim20, prim20, prim20, prim20, prim24, prim24, prim24, prim24, //20-27
	prim28, prim28, prim28, prim28, prim2C, prim2C, prim2C, prim2C, //28-2F
	prim30, prim30, prim30, prim30, prim34, prim34, prim34, prim34, //30-37
	prim38, prim38, prim38, prim38, prim3C, prim3C, prim3C, prim3C, //38-3F
	prim40, prim40, prim40, prim40, prim__, prim__, prim__, prim__, //40-47
	prim48, prim48, prim48, prim48, prim4C, prim4C, prim4C, prim4C, //48-4F
	prim50, prim50, prim50, prim50, prim__, prim__, prim__, prim__, //50-57
	prim58, prim58, prim58, prim58, prim5C, prim5C, prim5C, prim5C, //58-5F
	prim60, prim60, prim60, prim60, prim64, prim64, prim64, prim64, //60-67
	prim68, prim68, prim68, prim68, prim__, prim__, prim__, prim__, //68-6F
	prim70, prim70, prim70, prim70, prim74, prim74, prim74, prim74, //70-77
	prim78, prim78, prim78, prim78, prim7C, prim7C, prim7C, prim7C, //78-7F
	prim80, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //80-87
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //88-8F
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //90-97
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //98-9F
	primA0, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //A0-A7
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //A8-AF
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //B0-B7
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //B8-BF
	primC0, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //C0-C7
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //C8-CF
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //D0-D7
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //D8-DF
	prim__, primE1, primE2, primE3, primE4, primE5, primE6, prim__, //E0-E7
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //E8-EF
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__, //F0-F7
	prim__, prim__, prim__, prim__, prim__, prim__, prim__, prim__  //F8-FF
};
	

u32 chain_ptr_addr;
u32 chain_ptr_mem;
u8 chain_infinite_loop;


/*
DMA2 Chain slicing
	
Let GPU run x bytes. Then let game edit linked lists.


TODO:
- Monitor chain_ptr_addr for r/w
- Find better way to do sync timing (speed hit)


Einhander: 10-
- art gallery images + corruption
- note: pretty picky about timing

Vampire Hunter D: 5-
- opening menus (perfect - zero black screen flicker)
- note: really picky about timing
*/

#define DMA2_CHAIN_PAUSE 5



// Taken from PEOPS SOFTGPU
u32 lUsedAddr[3];

static inline boolean CheckForEndlessLoop(u32 laddr) {
	if (laddr == lUsedAddr[1]) return TRUE;
	if (laddr == lUsedAddr[2]) return TRUE;

	if (laddr < lUsedAddr[0]) lUsedAddr[1] = laddr;
	else lUsedAddr[2] = laddr;

	lUsedAddr[0] = laddr;

	return FALSE;
}

static u32 gpuDmaChainSize(u32 addr) {
	u32 size;
	u32 DMACommandCounter = 0;

	lUsedAddr[0] = lUsedAddr[1] = lUsedAddr[2] = 0xffffff;

	// initial linked list ptr (word)
	size = 1;
	chain_infinite_loop = 0;

	do {
		addr &= 0x1ffffc;

		DMACommandCounter = size;
		if (DMACommandCounter >= DMA2_CHAIN_PAUSE) break;


		//if (DMACommandCounter++ > 2000000) break;
		if (CheckForEndlessLoop(addr))
		{
			chain_infinite_loop = 1;
			break;
		}


		// # 32-bit blocks to transfer
		size += psxMu8( addr + 3 );


		if( psxMu8( addr + 3 ) != 0 ) {
			u32 lcv, stop;

			lcv = addr + 4;
			stop = addr + 4 + 4 * psxMu8( addr + 3 );


			while( lcv < stop )
			{
				u32 code;

				code = psxMu8( lcv+3 );
				
				lcv += 4;

				
				/*
				Check for frame buffer transfers (affects GPU dma timing)
				
				Final Fantasy 4: supercharge to avoid slowdown
				- $80: ~1/4 * 1x = slowest possible
				- $80: ~1/64 * 1x = fastest possible
				*/
				if( code == 0x80 )
				{
					s16 x0,x1,y0,y1,width, height;

					// PEOPS
					x0 = (psxMu32( lcv + 4*1 ) >> 0) & 0xffff;
					y0 = (psxMu32( lcv + 4*1 ) >> 16) & 0xffff;
					x1 = (psxMu32( lcv + 4*2 ) >> 0) & 0xffff;
					y1 = (psxMu32( lcv + 4*2 ) >> 16) & 0xffff;
					width = (psxMu32( lcv + 4*3 ) >> 0) & 0xffff;
					height = (psxMu32( lcv + 4*3 ) >> 16) & 0xffff;

					x0 &= 0x3ff;
					x1 &= 0x3ff;
					y0 &= 0x1ff;
					y1 &= 0x1ff;

					if( (x0 == x1) && (y0 == y1) ) break;

					
					// Xenogears - main menu cursor
					if( width <= 0 ) break;
					if( height <= 0 ) break;


					/*
					PEOPS

					Final Fantasy 9: battles
					Medievil: stage 1 opening

					Intense hang, slowdown if we count as full-blown vram transfer
					*/
					if( (x0+width) > 1024 ||
							(y0+height) > 512 ||
							(x1+width) > 1024 ||
							(y1+height) > 512 ) break;


					size += (width * height) / 32;
				}
				else if( code == 0xa0 )
				{}
				else if( code == 0xc0 )
				{}


				lcv += 4 * gpuPrim[code & 0xff]( lcv );
			}
		}

		
		// next 32-bit pointer
		addr = psxMu32( addr & ~0x3 ) & 0xffffff;
		size += 1;
	} while (addr != 0xffffff);


	chain_ptr_addr = addr;

	if( !chain_infinite_loop && addr != 0xffffff )
	{
		// save data at stop ptr
		chain_ptr_mem = psxMu32( addr );

		// insert stop ptr
		psxMu32ref( addr ) = 0x00ffffff;
	}

	
	return size;
}

int gpuReadStatus() {
	int hard;


	// GPU plugin
	hard = GPU_readStatus();


	// Gameshark Lite - wants to see VRAM busy
	// - Must enable GPU 'Fake Busy States' hack
	if( (hard & GPUSTATUS_IDLE) == 0 )
		hard &= ~GPUSTATUS_READYFORVRAM;

	return hard;
}

void psxDma2(u32 madr, u32 bcr, u32 chcr) { // GPU
	u32 *ptr;
	u32 size;

	switch (chcr) {
		case 0x01000200: // vram2mem
#ifdef PSXDMA_LOG
			PSXDMA_LOG("*** DMA2 GPU - vram2mem *** %lx addr = %lx size = %lx\n", chcr, madr, bcr);
#endif
			ptr = (u32 *)PSXM(madr);
			if (ptr == NULL) {
#ifdef CPU_LOG
				CPU_LOG("*** DMA2 GPU - vram2mem *** NULL Pointer!!!\n");
#endif
				break;
			}
			// BA blocks * BS words (word = 32-bits)
			size = (bcr >> 16) * (bcr & 0xffff);
			GPU_readDataMem(ptr, size);
			psxCpu->Clear(madr, size);

			// already 32-bit word size ((size * 4) / 4)
			GPUDMA_INT(size);
			return;

		case 0x01000201: // mem2vram
#ifdef PSXDMA_LOG
			PSXDMA_LOG("*** DMA 2 - GPU mem2vram *** %lx addr = %lx size = %lx\n", chcr, madr, bcr);
#endif
			ptr = (u32 *)PSXM(madr);
			if (ptr == NULL) {
#ifdef CPU_LOG
				CPU_LOG("*** DMA2 GPU - mem2vram *** NULL Pointer!!!\n");
#endif
				break;
			}
			// BA blocks * BS words (word = 32-bits)
			size = (bcr >> 16) * (bcr & 0xffff);
			GPU_writeDataMem(ptr, size);

			// already 32-bit word size ((size * 4) / 4)
			GPUDMA_INT(size);
			return;

		case 0x01000401: // dma chain
#ifdef PSXDMA_LOG
			PSXDMA_LOG("*** DMA 2 - GPU dma chain *** %lx addr = %lx size = %lx\n", chcr, madr, bcr);
#endif

			// Tekken 3 - dma2 overwrite while still running (infinite loop)
			if( !chain_infinite_loop && chain_ptr_addr != 0xffffff )
			{
				// put back old value first
				psxMu32ref( chain_ptr_addr ) = chain_ptr_mem;
			}

			size = gpuDmaChainSize(madr);
			GPU_dmaChain((u32 *)psxM, madr & 0x1fffff);
			
			GPUDMA_INT(size);
			return;

#ifdef PSXDMA_LOG
		default:
			PSXDMA_LOG("*** DMA 2 - GPU unknown *** %lx addr = %lx size = %lx\n", chcr, madr, bcr);
			break;
#endif
	}

	HW_DMA2_CHCR &= SWAP32(~0x01000000);
	DMA_INTERRUPT(2);
}

void gpuInterrupt() {
	/*
	GPU processing during DMA2 chains

	1 - never updates linked list (99/100 games)
	
	2 - updates linked list -before- GPU gets to it
	- Einhander: fixes art gallery images + post-gallery corruption (2+ cycles / 4 bytes)
	- Vampire Hunter D: shows title screen (3+ cycles / 4 bytes)

	3 - updates linked list -after- GPU reads it, -before- chain finishes
	- Skullmonkeys: fails to show menus
	*/

	if( HW_DMA2_CHCR == 0x01000401 )
	{
		u32 size,addr;


		// check valid data left
		if( !chain_infinite_loop && chain_ptr_addr != 0xffffff )
		{
			// put back old value first
			psxMu32ref( chain_ptr_addr ) = chain_ptr_mem;


			addr = chain_ptr_addr;
			size = gpuDmaChainSize( addr );
			
			GPU_dmaChain((u32 *)psxM, addr);


			GPUDMA_INT(size);

			// continue chain
			return;
		}
	}


	HW_DMA2_CHCR &= SWAP32(~0x01000000);
	DMA_INTERRUPT(2);
}