aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/ps2/Gs2dScreen.cpp
blob: 8f655e6aa17dceecf6c94808edaf479a3f6095ba (plain)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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.
 *
 */

#define FORBIDDEN_SYMBOL_EXCEPTION_printf
#define FORBIDDEN_SYMBOL_EXCEPTION_abort
#define FORBIDDEN_SYMBOL_EXCEPTION_exit

#include "Gs2dScreen.h"
#include <kernel.h>
#include <malloc.h>
#include <string.h>
#include <assert.h>
#include <fileio.h>
#include <math.h>
#include "DmaPipe.h"
#include "GsDefs.h"
#include "graphics/surface.h"
#include "graphics/colormasks.h"
#include "backends/platform/ps2/ps2debug.h"

extern void *_gp;

enum Buffers {
	SCREEN = 0,
	MOUSE,
	TEXT,
	PRINTF
};

/*
	Supported modes:

	Mode #1 = SDTV progressive (NTSC: 224p / PAL: 256p)
	Mode #2 = SDTV interlaced  (NTSC: 448i / PAL: 512i) <- default
	Mode #3 = EDTV progressive (NTSC: 480p / PAL: 576p)
	Mode #4 = HDTV progressive (720p)
	Mode #5 = HDTV interlaced  (1080i)
	Mode #6 = VESA (640x480@60)
	Mode #7 = VESA (800x600@60)
	Mode #8 = VESA (1024x768@60)
*/

static ps2_mode_t ps2_mode[] = {

	// -> w, h, interlaced, pitch, mode, vck, magh, magv, dx, dy

	/* #1 : SDTV - progressive */
	{ 640, 224, 0, 640, 0x02, 2560, 4, 0, 160 /*158*/, 25 /*22*/ }, /* NTSC */
	{ 640, 256, 0, 640, 0x03, 2560, 4, 0, 170 /*163*/, 35 /*36*/ }, /* PAL */

	/* #2 : SDTV - interlaced */
	{ 640, 448, 1, 640, 0x02, 2560, 4, 0, 156 /*158*/, 50 /*45*/ }, /* NTSC */
	{ 640, 512, 1, 640, 0x03, 2560, 4, 0, 170 /*163*/, 70 /*72*/ }, /* PAL */

	/* #3 : EDTV */
	{ 720, 480, 0, 768, 0x50, 1440, 2, 0, 58, 35 }, /* NTSC */
	/* { 720, 576, 0, 768, 0x53, 1440, 2, 0, 62, 45 }, */ /* PAL : full */
	/* { 656, 576, 0, 704, 0x53, 1312, 2, 0, 62, 45 }, */ /* PAL : redux @ (0,0) */
	{ 656, 576, 0, 704, 0x53, 1312, 2, 0, 78 /*314*/, 45 }, /* PAL : redux @ center'd */

	/* #4/#5 : HDTV */
	{ 1280,  720, 0, 1280, 0x52, 1280, 1, 0, 76 /*302*/, 24 },
	{ 1920, 1080, 1, 1920, 0x51, 1920, 1, 0, 60 /*236*/ /*238*/, 40 },

	/* #6/#7/#8 : VESA 4:3 @ 60Hz */
	{ 640, 480, 0,  640, 0x1A, 1280, 2, 0, 70 /*276*/, 34 },
	{ 800, 600, 0,  832, 0x2B, 1600, 2, 0, 105 /*420*/, 26 },
	{ 1024, 768, 0, 1024, 0x3B, 2048, 2, 0, 144 /*580*/, 34 }
};

#define ANIM_STACK_SIZE (1024 * 32)

#define ORG_X 256
#define ORG_Y 256
#define ORIGIN_X (ORG_X << 4)
#define ORIGIN_Y (ORG_Y << 4)
#define TEX_POW 10

#define SCALE(x) ((x) << 4)

#define M_SIZE 128
#define M_POW 7

static volatile uint32 g_VblankCmd = 0, g_DmacCmd = 0;
static int g_VblankSema, g_DmacSema, g_AnimSema;
static bool g_RunAnim = false;
static GsVertex kFullScreen[2];
static TexVertex kMouseTex[2] = {
	{ SCALE(1), SCALE(1) },
	{ SCALE(M_SIZE - 1), SCALE(M_SIZE - 1) }
};
static TexVertex kPrintTex[2] = {
	{ SCALE(1), SCALE(1) },
	{ SCALE(320), SCALE(200) }
};

void runAnimThread(Gs2dScreen *param);

int vblankStartHandler(int cause) {
	// start of VBlank period
	if (g_VblankCmd) {                // is there a new image waiting?
		GS_DISPFB1 = g_VblankCmd; // show it.
		g_VblankCmd = 0;
		iSignalSema(g_VblankSema);
	}
	return 0;
}

int dmacHandler(int channel) {
	if (g_DmacCmd && (channel == 2)) { // GS DMA transfer finished,
		g_VblankCmd = g_DmacCmd;   // we want to show the image
		g_DmacCmd = 0;             // when the next vblank occurs
		iSignalSema(g_DmacSema);
	}
	return 0;
}

int vblankEndHandler(int cause) {
	if (g_RunAnim)
		iSignalSema(g_AnimSema);
	return 0;
}

void createAnimThread(Gs2dScreen *screen);

Gs2dScreen::Gs2dScreen(uint16 width, uint16 height) {

	_systemQuit = false;
	ee_sema_t newSema;
	newSema.init_count = 1;
	newSema.max_count = 1;
	g_VblankSema = CreateSema(&newSema);
	g_DmacSema = CreateSema(&newSema);
	_screenSema = CreateSema(&newSema);
	newSema.init_count = 0;
	newSema.max_count = 255;
	g_AnimSema = CreateSema(&newSema);
	assert((g_VblankSema >= 0) && (g_DmacSema >= 0) && (_screenSema >= 0) && (g_AnimSema >= 0));

	_vblankStartId = AddIntcHandler(INT_VBLANK_START, vblankStartHandler, 0);
	_vblankEndId   = AddIntcHandler(INT_VBLANK_END, vblankEndHandler, 0);
	_dmacId        = AddDmacHandler(2, dmacHandler, 0);

	_dmaPipe = new DmaPipe(0x2000);

	EnableIntc(INT_VBLANK_START);
	EnableIntc(INT_VBLANK_END);
	EnableDmac(2);

	_tvMode = 0; // force detection
	_gfxMode = 0;

	_width = width;
	_height = height;
	_pitch = (width + 127) & ~127;

	_screenBuf = (uint8 *)memalign(64, _width * _height);
	_overlayBuf = (uint16 *)memalign(64, _pitch * _height * 2);
	_clut = (uint32 *)memalign(64, 256 * 4);

	memset(_screenBuf, 0, _width * _height);
	memset(_clut, 0, 256 * sizeof(uint32));
	_clut[1] = GS_RGBA(0xC0, 0xC0, 0xC0, 0);
	clearOverlay();

	char romver[8];
	uint16 biosver;
	int fd = fioOpen("rom0:ROMVER", O_RDONLY);
	fioRead(fd, &romver, 8);
	fioClose(fd);
	biosver=atoi(romver);
	printf("ROMVER = %s\n", romver);
	printf("ver = %d\n", atoi(romver));

	if (!_tvMode) { // determine TV standard first
		if (ConfMan.hasKey("tv_mode", "PlayStation2")) {
			const char *tvname = ConfMan.get("tv_mode", "PlayStation2").c_str();

			if (strcmp("ntsc", tvname) == 0) {
				_tvMode = 2;
			}
			else if (strcmp("pal", tvname) == 0) {
				_tvMode = 1;
			}
			else
				_tvMode = 0;
		}

		if (!_tvMode) {
			if (romver[4] == 'E')
				_tvMode = TV_PAL;
			else
				_tvMode = TV_NTSC;

			printf("Auto-detect TV mode: PSX:%c PS2:%c\n", *(char *)(0x1FC7FF52), romver[4]);
		}
	}

	uint8 mode;
	if (!_gfxMode) { // determine GFX mode next
		if (ConfMan.hasKey("gfx_mode", "PlayStation2")) {
			_gfxMode = ConfMan.getInt("gfx_mode", "PlayStation2");
			// TODO: free more video mem to support these modes
			if (_gfxMode == 4 || _gfxMode == 5) {
				printf("Not enough video mem: using EDTV (3)\n");
				_gfxMode = 3;
			}
			else
			if (_gfxMode == 7 || _gfxMode == 8) {
				printf("Not enough video mem: using VGA (6)\n");
				_gfxMode = 6;
			}

			if (_gfxMode < 1 || _gfxMode > 8) _gfxMode = 2;
			else
			if (_gfxMode == 4 || _gfxMode == 5) _tvMode = TV_HDTV;
			else
			if (_gfxMode > 5) _tvMode = TV_VESA;
		}
		else
			_gfxMode = 2;
	}

	// Remap Mode Index
	mode = _gfxMode;
	if (_tvMode == TV_NTSC) {
		mode = (mode * 2) - 1;
	}
	else if (_tvMode == TV_PAL) {
		mode = (mode * 2);
	}
	else if (_tvMode == TV_HDTV) {
		mode += 3;
	}
	else /* VESA */ {
		_tvMode = TV_VESA;
		mode += 3;
	}
	mode--;

	switch (_tvMode) {
	case TV_NTSC:
		printf("Setting up TV mode: NTSC\n");
	break;
	case TV_PAL:
		printf("Setting up TV mode: PAL\n");
	break;
	case TV_HDTV:
		printf("Setting up TV mode: HDTV\n");
	break;
	case TV_VESA:
		printf("Setting up TV mode: VESA\n");
	break;
	}

	_tvWidth = ps2_mode[mode].w;
	_tvHeight = ps2_mode[mode].h;
	_tvPitch = ps2_mode[mode].pitch;

	printf("Setting up GFX mode: %d x %d\n", _tvWidth, _tvHeight);

	kFullScreen[0].z = kFullScreen[1].z = 0;
	kFullScreen[0].x = ORIGIN_X;
	kFullScreen[0].y = ORIGIN_Y;
	kFullScreen[1].x = SCALE(_tvWidth) + ORIGIN_X;
	kFullScreen[1].y = SCALE(_tvHeight) + ORIGIN_Y;
	_blitCoords[0] = kFullScreen[0];
	_blitCoords[1] = kFullScreen[1];
	_texCoords[0].u = SCALE(1);
	_texCoords[0].v = SCALE(1);
	_texCoords[1].u = SCALE(_width);
	_texCoords[1].v = SCALE(_height);

	uint32 tvFrameSize = _tvPitch * _tvHeight * 4;  // 32 bits per pixel

	// setup frame buffer pointers
	_frameBufPtr[0] = 0;
	_frameBufPtr[1] = tvFrameSize;
	_clutPtrs[SCREEN] = tvFrameSize * 2;
	_clutPtrs[MOUSE]  = _clutPtrs[SCREEN] + 0x1000; // the cluts in PSMCT32 take up half a memory page each
	_clutPtrs[TEXT]   = _clutPtrs[SCREEN] + 0x2000;
	_texPtrs[SCREEN]  = _clutPtrs[SCREEN] + 0x3000;
	_texPtrs[TEXT]    = 0;                          // these buffers are stored in the alpha gaps of the frame buffers
	_texPtrs[MOUSE]	  = 128 * 256 * 4;
	_texPtrs[PRINTF]  = _texPtrs[MOUSE] + M_SIZE * M_SIZE * 4;

	_showOverlay = false;
	_showMouse = false;
	_mouseScaleX = (_tvWidth << 8) / _width;
	_mouseScaleY = (_tvHeight << 8) / _height;
	setMouseXy(_width / 2, _height / 2);
	_mTraCol = 255;
	_shakeXOffset = 0;
	_shakeYOffset = 0;

	_overlayFormat.bytesPerPixel = 2;

	_overlayFormat.rLoss = 3;
	_overlayFormat.gLoss = 3;
	_overlayFormat.bLoss = 3;
	_overlayFormat.aLoss = 8; // 7

	_overlayFormat.rShift = 0;
	_overlayFormat.gShift = 5;
	_overlayFormat.bShift = 10;
	_overlayFormat.aShift = 0; // 15

	// setup hardware now.
	GS_CSR = CSR_RESET; // Reset GS
	asm ("sync.p");
	GS_CSR = 0;
	GsPutIMR(0x7F00);


	if (biosver < 220 && ps2_mode[mode].mode == 0x53) { // EDTV PAL : mode not in BIOS < 2.20
	                                                    // no worries... we work in magic ;-)
		/* 720x576p */
		asm ("di");
		asm ("sync.l; sync.p");
		GS_PMODE = 0;
		asm ("sync.l; sync.p");
		GS_SMODE1 = 0x1742890504;
		asm ("sync.l; sync.p");
		GS_SMODE2 = 0;
		GS_SYNCH1 = 0x402E02003C827;
		asm ("sync.l; sync.p");
		GS_SYNCH2 = 0x19CA67;
		asm ("sync.l; sync.p");
		GS_SYNCV = 0xA9000002700005;
		asm ("sync.l; sync.p");
		GS_SRFSH = 4;
		asm ("sync.l; sync.p");
		GS_SMODE1 = 0x1742880504;
		asm ("sync.l; sync.p");
		asm ("sync.l; sync.p");
		GS_SMODE2 = 0;
		asm("ei");
	}
	else { // BIOS
		SetGsCrt(ps2_mode[mode].interlaced, ps2_mode[mode].mode, 0); // ps2_mode[mode].interlaced);
	}

	asm("di");
	GS_PMODE = GS_SET_PMODE(1, 0, 1, 1, 0, 255);
	GS_BGCOLOUR = GS_RGBA(0, 0, 0, 0);
	GS_DISPLAY1 = GS_SET_DISPLAY_MODE(ps2_mode[mode]);
	asm("ei");

	_curDrawBuf = 0;

	_dmaPipe->setOrigin(ORIGIN_X, ORIGIN_Y);
	_dmaPipe->setConfig(1, 0, 1);
	_dmaPipe->setScissorRect(0, 0, _tvWidth - 1, _tvHeight - 1);
	_dmaPipe->setDrawBuffer(_frameBufPtr[_curDrawBuf], _tvPitch, GS_PSMCT24, 0);
	_dmaPipe->flush();

	_clutChanged = _screenChanged = _overlayChanged = true;

	clearPrintfOverlay();
	updateScreen();

	createAnimTextures();

	// create animation thread
	#ifdef __NEW_PS2SDK__
	ee_thread_t animThread;
	ee_thread_status_t thisThread;
	#else
	ee_thread_t animThread, thisThread;
	#endif
	ReferThreadStatus(GetThreadId(), &thisThread);

	_animStack = memalign(64, ANIM_STACK_SIZE);
	animThread.initial_priority = thisThread.current_priority - 3;
	animThread.stack      = _animStack;
	animThread.stack_size = ANIM_STACK_SIZE;
	animThread.func       = (void *)runAnimThread;
	animThread.gp_reg     = &_gp;

	_animTid = CreateThread(&animThread);
	assert(_animTid >= 0);
	StartThread(_animTid, this);
}

void Gs2dScreen::quit(void) {
	_systemQuit = true;
	#ifdef __NEW_PS2SDK__
	ee_thread_status_t statAnim;
	#else
	ee_thread_t statAnim;
	#endif
	do { // wait until thread called ExitThread()
		SignalSema(g_AnimSema);
		ReferThreadStatus(_animTid, &statAnim);
	} while (statAnim.status != 0x10);
	DeleteThread(_animTid);
	free(_animStack);
	_dmaPipe->waitForDma(); // wait for dmac and vblank for the last time
	while (g_DmacCmd || g_VblankCmd);

	sioprintf("kill handlers\n");
	DisableIntc(INT_VBLANK_START);
	DisableIntc(INT_VBLANK_END);
	DisableDmac(2);
	RemoveIntcHandler(INT_VBLANK_START, _vblankStartId);
	RemoveIntcHandler(INT_VBLANK_END, _vblankEndId);
	RemoveDmacHandler(2, _dmacId);

	DeleteSema(g_VblankSema);
	DeleteSema(g_DmacSema);
	DeleteSema(g_AnimSema);
}

void Gs2dScreen::createAnimTextures(void) {
	uint8 *buf = (uint8 *)memalign(64, 16 * 64);
	memset(buf, 0, 16 * 64);
	uint32 vramDest = _texPtrs[TEXT];
	for (int i = 0; i < 16; i++) {
		uint32 *destPos = (uint32 *)buf;
		for (int ch = 15; ch >= 0; ch--) {
			const uint32 *src = (const uint32 *)(_binaryData + ((_binaryPattern[i] >> ch) & 1) * 4 * 14);
			for (int line = 0; line < 14; line++)
				destPos[line << 4] = src[line];
			destPos++;
		}
		if (!(i & 1))
			_dmaPipe->uploadTex( vramDest, 128, 0, 0, GS_PSMT4HH, buf, 128, 16);
		else {
			_dmaPipe->uploadTex( vramDest, 128, 0, 0, GS_PSMT4HL, buf, 128, 16);
			vramDest += 128 * 16 * 4;
		}
		_dmaPipe->flush();
		_dmaPipe->waitForDma();
	}
	_dmaPipe->uploadTex(_clutPtrs[TEXT], 64, 0, 0, GS_PSMCT32, _binaryClut, 8, 2);
	_dmaPipe->flush();
	free(buf);
}

void Gs2dScreen::newScreenSize(uint16 width, uint16 height) {
	if ((width == _width) && (height == _height))
		return;

	WaitSema(g_DmacSema);
	WaitSema(g_VblankSema);

	_dmaPipe->flush();
	_width = width;
	_height = height;
	_pitch = (width + 127) & ~127;

	// malloc new buffers
	free(_screenBuf);
	free(_overlayBuf);
	_screenBuf = (uint8 *)memalign(64, _width * _height);
	_overlayBuf = (uint16 *)memalign(64, _width * _height * 2);
	memset(_screenBuf, 0, _width * height);
	memset(_overlayBuf, 0, _width * height * 2);
	memset(_clut, 0, 256 * sizeof(uint32));
	_clut[1] = GS_RGBA(0xC0, 0xC0, 0xC0, 0);

	// clear video ram
	_dmaPipe->uploadTex(_clutPtrs[MOUSE], 64, 0, 0, GS_PSMCT32, _clut, 16, 16);
	_dmaPipe->uploadTex(_clutPtrs[SCREEN], 64, 0, 0, GS_PSMCT32, _clut, 16, 16);
	_dmaPipe->uploadTex(_texPtrs[SCREEN], _width, 0, 0, GS_PSMCT16, _overlayBuf, _width, _height);
	_dmaPipe->flush();
	_dmaPipe->waitForDma();

	/*_clutChanged = */ _screenChanged = _overlayChanged = false;
	_clutChanged = true; // reload palette on scr change

	_texCoords[1].u = SCALE(_width);
	_texCoords[1].v = SCALE(_height);
	_mouseScaleX = (_tvWidth << 8) / _width;
	_mouseScaleY = (_tvHeight << 8) / _height;
	setMouseXy(_width / 2, _height / 2);

	SignalSema(g_VblankSema);
	SignalSema(g_DmacSema);
}

void Gs2dScreen::copyScreenRect(const uint8 *buf, int pitch, int x, int y, int w, int h) {
	if (x < 0) {
		w += x;
		buf -= x;
		x = 0;
	}
	if (y < 0) {
		h += y;
		buf -= y * pitch;
		y = 0;
	}
	if (x + w > _width)
		w = (int)_width - x;
	if (y + h > _height)
		h = (int)_height - y;

	if ((w > 0) && (h > 0)) {
		WaitSema(g_DmacSema);
		uint8 *dest = _screenBuf + y * _width + x;
		if ((w == pitch) && (pitch == _width))
			memcpy(dest, buf, w * h);
		else
			for (int cnt = 0; cnt < h; cnt++) {
				memcpy(dest, buf, w);
				buf += pitch;
				dest += _width;
			}
		_screenChanged = true;
		SignalSema(g_DmacSema);
	}
}

Graphics::Surface *Gs2dScreen::lockScreen() {
	WaitSema(g_DmacSema);

	// -not- _pitch; ! It's EE mem, not Tex
	_framebuffer.init(_width, _height, _width, _screenBuf, Graphics::PixelFormat::createFormatCLUT8());
	return &_framebuffer;
}

void Gs2dScreen::unlockScreen() {
	_screenChanged = true;
	SignalSema(g_DmacSema);
}

void Gs2dScreen::setPalette(const uint8 *pal, uint8 start, uint16 num) {
	assert(start + num <= 256);

	WaitSema(g_DmacSema);
	for (uint16 cnt = 0; cnt < num; cnt++) {
		uint16 dest = start + cnt;
		dest = (dest & 0xE7) | ((dest & 0x8) << 1) | ((dest & 0x10) >> 1); // rearrange like the GS expects it

		uint32 color = pal[0] | (pal[1] << 8) | (pal[2] << 16);
		_clut[dest] = color;
		pal += 3;
	}
	_clutChanged = true;
	SignalSema(g_DmacSema);
}

void Gs2dScreen::grabPalette(uint8 *pal, uint8 start, uint16 num) const {
	assert(start + num <= 256);
	for (uint16 cnt = 0; cnt < num; cnt++) {
		uint16 src = start + cnt;
		src = (src & 0xE7) | ((src & 0x8) << 1) | ((src & 0x10) >> 1);

		uint32 color = _clut[src];
		pal[0] = (color >>  0) & 0xFF;
		pal[1] = (color >>  8) & 0xFF;
		pal[2] = (color >> 16) & 0xFF;
		pal += 3;
	}
}

void Gs2dScreen::uploadToVram(void) {
	if (_clutChanged) {
		_clutChanged = false;
		uint32 tmp = _clut[_mTraCol];
		_clut[_mTraCol] = GS_RGBA(0, 0, 0, 0x80); // this mouse color is transparent
		_dmaPipe->uploadTex(_clutPtrs[MOUSE], 64, 0, 0, GS_PSMCT32, _clut, 16, 16);
		_dmaPipe->flush();
		_dmaPipe->waitForDma();
		_clut[_mTraCol] = tmp;

		_dmaPipe->uploadTex(_clutPtrs[SCREEN], 64, 0, 0, GS_PSMCT32, _clut, 16, 16);
	}

	if (_showOverlay) {
		if (_overlayChanged) {
			_dmaPipe->uploadTex(_texPtrs[SCREEN], _width, 0, 0, GS_PSMCT16, _overlayBuf, _width, _height);
			_overlayChanged = false;
		}
	} else {
		if (_screenChanged) {
			_dmaPipe->uploadTex(_texPtrs[SCREEN], _pitch, 0, 0, GS_PSMT8, _screenBuf, _width, _height);
			_screenChanged = false;
		}
	}
}

extern "C" void _ps2sdk_alloc_lock(void);
extern "C" void _ps2sdk_alloc_unlock(void);

void Gs2dScreen::updateScreen(void) {
	WaitSema(_screenSema);
	uploadToVram();
	if (!g_RunAnim) {
		_dmaPipe->flatRect(kFullScreen + 0, kFullScreen + 1, GS_RGBA(0, 0, 0, 0)); // clear screen

		if (_showOverlay) {
			_dmaPipe->setTex(_texPtrs[SCREEN], _width, TEX_POW, TEX_POW, GS_PSMCT16, 0, 0, 0, 0);
			_dmaPipe->textureRect(kFullScreen + 0, kFullScreen + 1, _texCoords + 0, _texCoords + 1);
		} else {
			_dmaPipe->setTex(_texPtrs[SCREEN], _pitch, TEX_POW, TEX_POW, GS_PSMT8, _clutPtrs[SCREEN], 0, 64, GS_PSMCT32);
			_dmaPipe->textureRect(_blitCoords + 0, _blitCoords + 1, _texCoords + 0, _texCoords + 1);
		}

		if (_showMouse) {
			GsVertex mouseCoords[2];
			mouseCoords[0].x = (((_mouseX - _hotSpotX) * _mouseScaleX + 8) >> 4) + ORIGIN_X;
			mouseCoords[0].y = (((_mouseY - _hotSpotY) * _mouseScaleY + 8) >> 4) + ORIGIN_Y;
			mouseCoords[1].x = mouseCoords[0].x + (((M_SIZE * _mouseScaleX) + 8) >> 4);
			mouseCoords[1].y = mouseCoords[0].y + (((M_SIZE * _mouseScaleY) + 8) >> 4);
			mouseCoords[0].z = mouseCoords[1].z = 0;

			_dmaPipe->setTex(_texPtrs[MOUSE], M_SIZE, M_POW, M_POW, GS_PSMT8H, _clutPtrs[MOUSE], 0, 64, GS_PSMCT32);
			_dmaPipe->textureRect(mouseCoords + 0, mouseCoords + 1, kMouseTex + 0, kMouseTex + 1);
		}

		_dmaPipe->setTex(_texPtrs[PRINTF], 3 * 128, TEX_POW, TEX_POW, GS_PSMT8H, _clutPtrs[TEXT], 0, 64, GS_PSMCT32);
		_dmaPipe->textureRect(kFullScreen + 0, kFullScreen + 1, kPrintTex + 0, kPrintTex + 1);

#if 0
		_ps2sdk_alloc_lock();
		uint32 heapTop = (uint32)ps2_sbrk(0);
		_ps2sdk_alloc_unlock();
		if (heapTop != (uint32)-1) {
			float yPos = (((float)heapTop) / (32 * 1024 * 1024)) * _tvHeight;
			GsVertex bottom = { SCALE(_tvWidth - 40) + ORIGIN_X, SCALE(_tvHeight) + ORIGIN_Y, 0 };
			GsVertex top = { SCALE(_tvWidth) + ORIGIN_X, 0, 0 };
			top.y = SCALE((uint16)(_tvHeight - yPos)) + ORIGIN_Y;
			_dmaPipe->flatRect(&bottom, &top, GS_RGBA(0x80, 0, 0, 0x40));
		}
#endif

		WaitSema(g_DmacSema);	// wait for dma transfer, if there's one running
		WaitSema(g_VblankSema); // wait if there's already an image waiting for vblank

		g_DmacCmd = GS_SET_DISPFB(_frameBufPtr[_curDrawBuf], _tvPitch, GS_PSMCT24); // put it here for dmac/vblank handler
		_dmaPipe->flush();
		_curDrawBuf ^= 1;
		_dmaPipe->setDrawBuffer(_frameBufPtr[_curDrawBuf], _tvPitch, GS_PSMCT24, 0);
	} else
		_dmaPipe->flush();
	SignalSema(_screenSema);
}

void Gs2dScreen::showOverlay(void) {
	_showOverlay = true;
	clearOverlay();
}

void Gs2dScreen::hideOverlay(void) {
	_screenChanged = true;
	_showOverlay = false;
}

Graphics::PixelFormat Gs2dScreen::getOverlayFormat(void) {
	return _overlayFormat;
	// return Graphics::createPixelFormat<1555>();
}

int16 Gs2dScreen::getOverlayWidth(void) {
	return _width; // _videoMode.overlayWidth;
}

int16 Gs2dScreen::getOverlayHeight(void) {
	return _height; // _videoMode.overlayHeight;
}

void Gs2dScreen::setShakePos(int shakeXOffset, int shakeYOffset) {
	_shakeXOffset = (shakeXOffset * _mouseScaleX) >> 8;
	_shakeYOffset = (shakeYOffset * _mouseScaleY) >> 8;
	_blitCoords[0].x = SCALE(_shakeXOffset) + ORIGIN_X;
	_blitCoords[0].y = SCALE(_shakeYOffset) + ORIGIN_Y;
	_blitCoords[1].x = SCALE(_tvWidth + _shakeXOffset) + ORIGIN_X;
	_blitCoords[1].y = SCALE(_tvHeight + _shakeYOffset) + ORIGIN_Y;
}

void Gs2dScreen::copyPrintfOverlay(const uint8 *buf) {
	assert(!((uint32)buf & 63));
	_dmaPipe->uploadTex(_texPtrs[PRINTF], 3 * 128, 0, 0, GS_PSMT8H, buf, 320, 200);
	_dmaPipe->flush();
	_dmaPipe->waitForDma();
}

void Gs2dScreen::clearPrintfOverlay(void) {
	uint8 *tmpBuf = (uint8 *)memalign(64, 320 * 200);
	memset(tmpBuf, 4, 320 * 200);
	_dmaPipe->uploadTex(_texPtrs[PRINTF], 3 * 128, 0, 0, GS_PSMT8H, tmpBuf, 320, 200);
	_dmaPipe->flush();
	_dmaPipe->waitForDma();
	free(tmpBuf);
}

void Gs2dScreen::copyOverlayRect(const byte *buf, uint16 pitch, uint16 x, uint16 y, uint16 w, uint16 h) {
	WaitSema(g_DmacSema);

	// warning("_overlayBuf [dst] = %x", _overlayBuf);
	// warning("buf [src] = %x", buf);

	// warning("pitch=%d _width=%d - x=%d y=%d w=%d h=%d",
	//	pitch, _width, x, y, w, h);

	if (x >= 65535) x=0;
	if (y >= 65535) y=0;

	_overlayChanged = true;
	uint16 *dest = _overlayBuf + y * _width + x;
	for (uint32 cnt = 0; cnt < h; cnt++) {
		memcpy(dest, buf, w * 2);
		dest += _width;
		buf += pitch;
	}
	SignalSema(g_DmacSema);
}

void Gs2dScreen::clearOverlay(void) {
	WaitSema(g_DmacSema);
	_overlayChanged = true;
	// first convert our clut to 16 bit RGBA for the overlay...
	uint16 palette[256];
	for (uint32 cnt = 0; cnt < 256; cnt++) {
		uint32 rgba = _clut[(cnt & 0xE7) | ((cnt & 0x8) << 1) | ((cnt & 0x10) >> 1)];
		palette[cnt] = ((rgba >> 3) & 0x1F) | (((rgba >> 11) & 0x1F) << 5) | (((rgba >> 19) & 0x1F) << 10);
	}
	// now copy the current screen over
	for (int cnt = 0; cnt < _width * _height; cnt++)
		_overlayBuf[cnt] = palette[_screenBuf[cnt]];
	SignalSema(g_DmacSema);
}

void Gs2dScreen::grabOverlay(byte *buf, uint16 pitch) {
	uint16 *src = _overlayBuf;
	for (uint32 cnt = 0; cnt < _height; cnt++) {
		memcpy(buf, src, _width * 2);
		buf += pitch;
		src += _width;
	}
}

void Gs2dScreen::setMouseOverlay(const uint8 *buf, uint16 width, uint16 height, uint16 hotSpotX, uint16 hotSpotY, uint8 transpCol) {
	assert((width <= M_SIZE) && (height <= M_SIZE));

	_hotSpotX = hotSpotX;
	_hotSpotY = hotSpotY;
	if (_mTraCol != transpCol) {
		_mTraCol = transpCol;
		_clutChanged = true;
	}
	uint8 *bufCopy = (uint8 *)memalign(64, M_SIZE * M_SIZE); // make a copy to align to 64 bytes
	memset(bufCopy, _mTraCol, M_SIZE * M_SIZE);
	for (int cnt = 0; cnt < height; cnt++)
		memcpy(bufCopy + cnt * M_SIZE, buf + cnt * width, width);

	_dmaPipe->uploadTex( _texPtrs[MOUSE], M_SIZE, 0, 0, GS_PSMT8H, bufCopy, M_SIZE, M_SIZE);
	_dmaPipe->flush();
	_dmaPipe->waitForDma(); // make sure all data has been transferred when we free bufCopy
	free(bufCopy);
}

void Gs2dScreen::showMouse(bool show) {
	_showMouse = show;
}

void Gs2dScreen::setMouseXy(int16 x, int16 y) {
	_mouseX = x;
	_mouseY = y;
}
/*
uint8 Gs2dScreen::tvMode(void) {
	return _tvMode;
}
*/
uint16 Gs2dScreen::getWidth(void) {
	return _width;
}

uint16 Gs2dScreen::getHeight(void) {
	return _height;
}

void Gs2dScreen::wantAnim(bool runIt) {
	g_RunAnim = runIt;
}

#define LINE_SPACE 20
#define SCRL_TIME 8
#define V 1000
#define Z_TRANSL 65

void Gs2dScreen::playAnim(void) {
	// animate zeros and ones while game accesses memory card, etc.
	g_RunAnim = false;
	float yPos   = 0.0;
	uint8 texSta = 0;
	float scrlSpeed = (_tvMode == TV_PAL) ? (_tvHeight / (SCRL_TIME * 50.0)) : (_tvHeight / (SCRL_TIME * 60.0));
	uint8 texMax = (_tvHeight / LINE_SPACE) + (ORG_Y / LINE_SPACE);
	TexVertex texNodes[4] = {
		{ SCALE(1),   SCALE(1) }, { SCALE(1),   SCALE(14) },
		{ SCALE(128), SCALE(1) }, { SCALE(128), SCALE(14) }
	};
	float angleStep = ((2 * M_PI) / _tvHeight);

	while (!_systemQuit) {
		do {
			WaitSema(g_AnimSema);
		} while ((!_systemQuit) && (!g_RunAnim));

		if (_systemQuit)
			break;

		if (PollSema(_screenSema) > 0) { // make sure no thread is currently drawing
			WaitSema(g_DmacSema);   // dma transfers have to be finished
			WaitSema(g_VblankSema); // wait for image, if there is one...

			// redraw the engine's last frame
			_dmaPipe->flatRect(kFullScreen + 0, kFullScreen + 1, GS_RGBA(0, 0, 0, 0)); // clear screen

			if (_showOverlay) {
				_dmaPipe->setTex(_texPtrs[SCREEN], _width, TEX_POW, TEX_POW, GS_PSMCT16, 0, 0, 0, 0);
				_dmaPipe->textureRect(kFullScreen + 0, kFullScreen + 1, _texCoords + 0, _texCoords + 1);
			} else {
				_dmaPipe->setTex(_texPtrs[SCREEN], _pitch, TEX_POW, TEX_POW, GS_PSMT8, _clutPtrs[SCREEN], 0, 64, GS_PSMCT32);
				_dmaPipe->textureRect(_blitCoords + 0, _blitCoords + 1, _texCoords + 0, _texCoords + 1);
			}

			_dmaPipe->setTex(_texPtrs[PRINTF], 3 * 128, TEX_POW, TEX_POW, GS_PSMT8H, _clutPtrs[TEXT], 0, 64, GS_PSMCT32);
			_dmaPipe->textureRect(kFullScreen + 0, kFullScreen + 1, kPrintTex + 0, kPrintTex + 1);

			if (_showMouse) {
				GsVertex mouseCoords[2];
				mouseCoords[0].x = (((_mouseX - _hotSpotX) * _mouseScaleX + 8) >> 4) + ORIGIN_X;
				mouseCoords[0].y = (((_mouseY - _hotSpotY) * _mouseScaleY + 8) >> 4) + ORIGIN_Y;
				mouseCoords[1].x = mouseCoords[0].x + (((M_SIZE * _mouseScaleX) + 8) >> 4);
				mouseCoords[1].y = mouseCoords[0].y + (((M_SIZE * _mouseScaleY) + 8) >> 4);
				mouseCoords[0].z = mouseCoords[1].z = 0;

				_dmaPipe->setTex(_texPtrs[MOUSE], M_SIZE, M_POW, M_POW, GS_PSMT8H, _clutPtrs[MOUSE], 0, 64, GS_PSMCT32);
				_dmaPipe->textureRect(mouseCoords + 0, mouseCoords + 1, kMouseTex + 0, kMouseTex + 1);
			}

			_dmaPipe->setAlphaBlend(SOURCE_COLOR, ZERO_COLOR, SOURCE_ALPHA, DEST_COLOR, 0);
			yPos -= scrlSpeed;
			if (yPos <= -LINE_SPACE) {
				yPos += LINE_SPACE;
				texSta++;
			}

			float drawY = yPos;

			for (int i = 0; i < texMax; i++) {
				uint8 texIdx = (texSta + i) & 0xF;

				float x[4] = { -64.0, -64.0, 64.0, 64.0 };
				float y[4];
				y[0] = y[2] = drawY - _tvHeight / 2 - LINE_SPACE / 2;
				y[1] = y[3] = y[0] + LINE_SPACE;
				float z[4];
				GsVertex nodes[4];

				float angle = M_PI / 2 + angleStep * drawY;
				float rotSin = sinf(angle);
				float rotCos = cosf(angle);
				for (int coord = 0; coord < 4; coord++) {
					z[coord] = rotCos * x[coord];
					x[coord] = rotSin * x[coord];

					nodes[coord].z = 0;
					nodes[coord].x = (uint16)(((V * x[coord]) / (z[coord] + V + Z_TRANSL)) * 16);
					nodes[coord].y = (uint16)(((V * y[coord]) / (z[coord] + V + Z_TRANSL)) * 16);
					nodes[coord].x += SCALE(_tvWidth - 80 + ORG_X);
					nodes[coord].y += SCALE(_tvHeight / 2 + ORG_Y);
				}

				uint32 texPtr = _texPtrs[TEXT] + 128 * 16 * 4 * (texIdx >> 1);
				if (texIdx & 1)
					_dmaPipe->setTex(texPtr, 128, 7, 4, GS_PSMT4HL, _clutPtrs[TEXT], 0, 64, GS_PSMCT32);
				else
					_dmaPipe->setTex(texPtr, 128, 7, 4, GS_PSMT4HH, _clutPtrs[TEXT], 0, 64, GS_PSMCT32);

				_dmaPipe->textureRect(nodes + 0, nodes + 1, nodes + 2, nodes + 3,
					texNodes + 0, texNodes + 1, texNodes + 2, texNodes + 3, GS_RGBA(0x80, 0x80, 0x80, 0x80));

				drawY += LINE_SPACE;
			}
			g_DmacCmd = GS_SET_DISPFB(_frameBufPtr[_curDrawBuf], _tvPitch, GS_PSMCT24); // put it here for dmac/vblank handler
			_dmaPipe->flush();
			_curDrawBuf ^= 1;
			_dmaPipe->setDrawBuffer(_frameBufPtr[_curDrawBuf], _tvPitch, GS_PSMCT24, 0);
			_dmaPipe->setAlphaBlend(DEST_COLOR, ZERO_COLOR, SOURCE_ALPHA, SOURCE_COLOR, 0);

			SignalSema(_screenSema);
		}
	}
	ExitThread();
}

void runAnimThread(Gs2dScreen *param) {
	param->playAnim();
}

// data for the animated zeros and ones...
const uint8 Gs2dScreen::_binaryData[4 * 14 * 2] = {
	// figure zero
	0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x00, 0x22, 0x22, 0x00, 0x31, 0x13,
	0x31, 0x13, 0x20, 0x02, 0x22, 0x02, 0x31, 0x13, 0x33, 0x13, 0x20, 0x02, 0x20, 0x02,
	0x31, 0x33, 0x31, 0x13, 0x20, 0x22, 0x20, 0x02, 0x31, 0x13, 0x31, 0x13, 0x00, 0x22,
	0x22, 0x00, 0x11, 0x11, 0x11, 0x11, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,
	// figure one
	0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x00, 0x20, 0x02, 0x00, 0x11, 0x33,
	0x13, 0x11, 0x22, 0x22, 0x02, 0x00, 0x11, 0x31, 0x13, 0x11, 0x00, 0x20, 0x02, 0x00,
	0x11, 0x31, 0x13, 0x11, 0x00, 0x20, 0x02, 0x00, 0x11, 0x31, 0x13, 0x11, 0x00, 0x20,
	0x02, 0x00, 0x11, 0x11, 0x11, 0x11, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11
};

const uint16 Gs2dScreen::_binaryPattern[16] = {
	0xD992, 0x344B, 0xA592, 0x110D,
	0x9234, 0x2326, 0x5199, 0xC8A6,
	0x4D29, 0x18B0, 0xA5AA, 0x2949,
	0x6DB3, 0xB2AA, 0x64A4, 0x3329
};

const uint32 Gs2dScreen::_binaryClut[16] __attribute__((aligned(64))) = {
	GS_RGBA(   0,    0,    0, 0x40),
	GS_RGBA(  50,   50,   50, 0x40),
	GS_RGBA( 204,  204, 0xFF, 0x40),
	GS_RGBA( 140,  140, 0xFF, 0x40),

	GS_RGBA(   0,    0,    0, 0x80), // scrPrintf: transparent
	GS_RGBA(   0,    0,    0, 0x20), // scrPrintf: semitransparent
	GS_RGBA(0xC0, 0xC0, 0xC0,    0), // scrPrintf: red
	GS_RGBA(0x16, 0x16, 0xF0,    0), // scrPrintf: blue

	GS_RGBA(0xFF, 0xFF, 0xFF, 0x80), GS_RGBA(0xFF, 0xFF, 0xFF, 0x80), // unused
	GS_RGBA(0xFF, 0xFF, 0xFF, 0x80), GS_RGBA(0xFF, 0xFF, 0xFF, 0x80),
	GS_RGBA(0xFF, 0xFF, 0xFF, 0x80), GS_RGBA(0xFF, 0xFF, 0xFF, 0x80),
	GS_RGBA(0xFF, 0xFF, 0xFF, 0x80), GS_RGBA(0xFF, 0xFF, 0xFF, 0x80)
};