aboutsummaryrefslogtreecommitdiff
path: root/backends/ps2/systemps2.cpp
blob: 10cf6496ff2aa2cc8fc41bdbadb9e1a761820046 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2005 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#include "common/stdafx.h"
#include <kernel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sifrpc.h>
#include <loadfile.h>
#include <malloc.h>
#include <assert.h>
#include <iopcontrol.h>
#include <iopheap.h>
#include <osd_config.h>
#include "common/scummsys.h"
#include "../intern.h"
#include "base/engine.h"
#include "backends/ps2/systemps2.h"
#include "backends/ps2/Gs2dScreen.h"
#include "backends/ps2/ps2input.h"
#include <sjpcm.h>
#include <cdvd_rpc.h>
#include "backends/ps2/savefile.h"
#include "common/file.h"
#include "backends/ps2/sysdefs.h"
#include <libmc.h>
#include "backends/ps2/cd.h"
#include <sio.h>
#include <fileXio_rpc.h>
#include "graphics/surface.h"
#include "graphics/font.h"

#define TIMER_STACK_SIZE (1024 * 32)
#define SOUND_STACK_SIZE (1024 * 32)
#define SMP_PER_BLOCK 800
#define FROM_BCD(a) ((a >> 4) * 10 + (a & 0xF))
#define BUS_CLOCK (150 * 1000 * 1000) // 150 Mhz Bus clock
#define CLK_DIVIS 5859	// the timer IRQ handler gets called (BUS_CLOCK / 256) / CLK_DIVIS times per second (~100 times)

#ifdef USE_PS2LINK
#define IRX_PREFIX "host:"
#define IRX_SUFFIX
#else
#define IRX_PREFIX "cdrom0:\\"
#define IRX_SUFFIX ";1"
#endif

static int g_TimerThreadSema = -1, g_SoundThreadSema = -1;
static int g_MainWaitSema = -1, g_TimerWaitSema = -1;
static volatile int32 g_MainWakeUp = 0, g_TimerWakeUp = 0;
static volatile uint64 msecCount = 0;

OSystem_PS2 *g_systemPs2 = NULL;

void readRtcTime(void);

int gBitFormat = 555;

namespace Graphics {
	extern const NewFont g_sysfont;
};

void sioprintf(const char *zFormat, ...) {
	va_list ap;
	char resStr[2048];

	va_start(ap,zFormat);
	int res = vsnprintf(resStr, 2048, zFormat, ap);
	va_end(ap);

	sio_puts(resStr);
}

OSystem *OSystem_PS2_create(void) {
	if (!g_systemPs2)
		g_systemPs2 = new OSystem_PS2();
	return g_systemPs2;
}

extern "C" int scummvm_main(int argc, char *argv[]);

extern "C" int main(int argc, char *argv[]) {
	SifInitRpc(0);
#ifndef USE_PS2LINK // reset the IOP if this is a CD build
	cdvdInit(CDVD_EXIT);
	cdvdExit();
	SifExitIopHeap();
	SifLoadFileExit();
	SifExitRpc();
	sio_puts("Resetting IOP.");
	SifIopReset("rom0:UDNL rom0:EELOADCNF",0);
	while (!SifIopSync())
		;
	sio_puts("IOP synced.");
	SifInitRpc(0);
	SifLoadFileInit();
    cdvdInit(CDVD_INIT_NOWAIT);
#endif

	ee_thread_t thisThread;
	int tid = GetThreadId();
	ReferThreadStatus(tid, &thisThread);

	sioprintf("Thread Start Priority = %d\n", thisThread.current_priority);
	if ((thisThread.current_priority < 5) || (thisThread.current_priority > 80)) {
		/* Depending on the way ScummVM is run, we may get here with different
		   thread priorities.
		   The PS2 BIOS executes it with priority = 0, ps2link uses priority 64.
		   Don't know about NapLink, etc.
		   The priority doesn't matter too much, but we need to be at least at prio 3,
		   so we can have the timer thread run at prio 2 and the sound thread at prio 1	*/
		sioprintf("Changing thread priority");
		int res = ChangeThreadPriority(tid, 20);
		sioprintf("Result = %d", res);
	}

	sioprintf("Creating system");
	/* The OSystem has to be created before we enter ScummVM's main.
	   It sets up the memory card, etc. */
	OSystem_PS2_create();

	sioprintf("init done. starting ScummVM.");
	return scummvm_main(argc, argv);
}

s32 timerInterruptHandler(s32 cause) {
	msecCount += (((uint64)256 * CLK_DIVIS) << 32) / (BUS_CLOCK / 1000);
	T0_MODE = 0xDC2; // same value as in initialization.

	iSignalSema(g_SoundThreadSema);
	iSignalSema(g_TimerThreadSema);

	if (g_MainWakeUp) {
		g_MainWakeUp -= 10;
		if (g_MainWakeUp <= 0) {
			iSignalSema(g_MainWaitSema);
			g_MainWakeUp = 0;
		}
	}
	if (g_TimerWakeUp) {
		g_TimerWakeUp -= 10;
		if (g_TimerWakeUp <= 0) {
			iSignalSema(g_TimerWaitSema);
			g_TimerWakeUp = 0;
		}
	}
	return 0;
}

void systemTimerThread(OSystem_PS2 *system) {
	system->timerThread();
}

void systemSoundThread(OSystem_PS2 *system) {
	system->soundThread();
}

OSystem_PS2::OSystem_PS2(void) {
	sioprintf("OSystem_PS2 constructor\n");

	_soundStack = _timerStack = NULL;
	_scummTimerProc = NULL;
	_scummSoundProc = NULL;
	_scummSoundParam = NULL;

	_screen = new Gs2dScreen(320, 200, TV_DONT_CARE);
	_width = 320;
	_height = 200;

	sioprintf("Initializing timer\n");
	initTimer();

	_screen->wantAnim(true);

	char errorStr[256];
	if (!loadModules(errorStr))
		fatalError(errorStr);

	sioprintf("Initializing SjPCM");
	if (SjPCM_Init(0) < 0)
		fatalError("SjPCM Bind failed");

	if (CDVD_Init() != 0)
		fatalError("CDVD_Init failed");

	_mouseVisible = false;

	sioprintf("reading RTC");
	readRtcTime();

	sioprintf("Initializing FXIO");
	if (fileXioInit() < 0)
		fatalError("Can't init fileXio");

	fileXioSetBlockMode(FXIO_NOWAIT);

	sioprintf("Starting SavefileManager");
	_saveManager = new Ps2SaveFileManager(this, _screen);

	_soundBufL = (int16*)malloc(SMP_PER_BLOCK * sizeof(int16));
	_soundBufR = (int16*)malloc(SMP_PER_BLOCK * sizeof(int16));

	sioprintf("Initializing ps2Input");
	_input = new Ps2Input(this, _useMouse, _useKbd);

	_screen->wantAnim(false);
}

OSystem_PS2::~OSystem_PS2(void) {
}

void OSystem_PS2::initTimer(void) {
	// first setup the two threads that get activated by the timer:
	// the timerthread and the soundthread
	ee_sema_t threadSema;
	threadSema.init_count = 0;
	threadSema.max_count = 255;
	g_TimerThreadSema = CreateSema(&threadSema);
	g_SoundThreadSema = CreateSema(&threadSema);
	assert((g_TimerThreadSema >= 0) && (g_SoundThreadSema >= 0));

	ee_thread_t timerThread, soundThread, thisThread;
	ReferThreadStatus(GetThreadId(), &thisThread);

	_timerStack = (uint8*)malloc(TIMER_STACK_SIZE);
	_soundStack = (uint8*)malloc(SOUND_STACK_SIZE);

	// give timer thread a higher priority than main thread
	timerThread.initial_priority = thisThread.current_priority - 1;
	timerThread.stack            = _timerStack;
	timerThread.stack_size       = TIMER_STACK_SIZE;
	timerThread.func             = (void *)systemTimerThread;
	//timerThread.gp_reg         = _gp; // _gp is always NULL.. broken linkfile?
	asm("move %0, $gp\n": "=r"(timerThread.gp_reg));

	// soundthread's priority is higher than main- and timerthread
	soundThread.initial_priority = thisThread.current_priority - 2;
	soundThread.stack            = _soundStack;
	soundThread.stack_size       = SOUND_STACK_SIZE;
	soundThread.func             = (void *)systemSoundThread;
	asm("move %0, $gp\n": "=r"(soundThread.gp_reg));

	_timerTid = CreateThread(&timerThread);
	_soundTid = CreateThread(&soundThread);

	assert((_timerTid >= 0) && (_soundTid >= 0));

	StartThread(_timerTid, this);
	StartThread(_soundTid, this);

	// these semaphores are used for OSystem::delay()
	threadSema.init_count = 0;
	threadSema.max_count = 1;
	g_MainWaitSema = CreateSema(&threadSema);
	g_TimerWaitSema = CreateSema(&threadSema);
	assert((g_MainWaitSema >= 0) && (g_TimerWaitSema >= 0));

	// threads done, start the interrupt handler
	AddIntcHandler( INT_TIMER0, timerInterruptHandler, 0); // 0=first handler
	EnableIntc(INT_TIMER0);
	T0_HOLD = 0;
	T0_COUNT = 0;
	T0_COMP = CLK_DIVIS; // (busclock / 256) / 5859 = ~ 100.0064
	T0_MODE = TIMER_MODE( 2, 0, 0, 0, 1, 1, 1, 0, 1, 1);
}

void OSystem_PS2::timerThread(void) {
	while (1) {
		WaitSema(g_TimerThreadSema);
		if (_scummTimerProc)
			_scummTimerProc(0);
	}
}

void OSystem_PS2::soundThread(void) {
	ee_sema_t soundSema;
	soundSema.init_count = 1;
	soundSema.max_count = 1;
	_soundSema = CreateSema(&soundSema);
	assert(_soundSema >= 0);

	int bufferedSamples = 0;
	int cycles = 0;
	while (1) {
		WaitSema(g_SoundThreadSema);

		if (!(cycles & 31))
			bufferedSamples = SjPCM_Buffered();
		else
			bufferedSamples -= 480;
		cycles++;

		WaitSema(_soundSema);
		if (_scummSoundProc) {
			if (bufferedSamples <= 8 * SMP_PER_BLOCK) {
				// we have to produce more samples, call sound mixer
				// the scratchpad at 0x70000000 is used as temporary soundbuffer
				_scummSoundProc(_scummSoundParam, (uint8*)0x70000000, SMP_PER_BLOCK * 2 * sizeof(int16));

				// demux data into 2 buffers, L and R
				 __asm__ (
					"move  $t2, %1\n\t"			// dest buffer right
					"move  $t3, %0\n\t"			// dest buffer left
					"lui   $t8, 0x7000\n\t"		// muxed buffer, fixed at 0x70000000
					"addiu $t9, $0, 100\n\t"	// number of loops
					"mtsab $0, 2\n\t"			// set qword shift = 2 byte

					"loop:\n\t"
					"  lq $t4,  0($t8)\n\t"		// load 8 muxed samples
					"  lq $t5, 16($t8)\n\t"		// load 8 more muxed samples

					"  qfsrv $t6, $0, $t4\n\t"	// shift right for second
					"  qfsrv $t7, $0, $t5\n\t"	// packing step (right channel)

					"  ppach $t4, $t5, $t4\n\t"	// combine left channel data
					"  ppach $t6, $t7, $t6\n\t"	// right channel data

					"  sq $t4, 0($t3)\n\t"		// write back
					"  sq $t6, 0($t2)\n\t"		//

					"  addiu $t9, -1\n\t"		// decrement loop counter
					"  addiu $t2, 16\n\t"		// increment pointers
					"  addiu $t3, 16\n\t"
					"  addiu $t8, 32\n\t"
					"  bnez  $t9, loop\n\t"		// loop
						:  // outputs
				 		: "r"(_soundBufL), "r"(_soundBufR)  // inputs
					//  : "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8", "$t9"  // destroyed
						: "$10", "$11", "$12", "$13", "$14", "$15", "$24", "$25"  // destroyed
				);
				// and feed it into the SPU
				SjPCM_Enqueue((short int*)_soundBufL, (short int*)_soundBufR, SMP_PER_BLOCK, 0);
				bufferedSamples += SMP_PER_BLOCK;
			}
		}
		SignalSema(_soundSema);
	}
}

char *irxModules[] = {
	"rom0:SIO2MAN",
	"rom0:MCMAN",
	"rom0:MCSERV",
	"rom0:PADMAN",
	"rom0:LIBSD",
#ifndef USE_PS2LINK
	IRX_PREFIX "IOMANX.IRX" IRX_SUFFIX,
#endif
	IRX_PREFIX "FILEXIO.IRX" IRX_SUFFIX,
	IRX_PREFIX "CDVD.IRX" IRX_SUFFIX,
	IRX_PREFIX "SJPCM.IRX" IRX_SUFFIX
};

bool OSystem_PS2::loadModules(char *errorStr) {

	_useHdd = _useMouse = _useKbd = false;

	int res;
	for (int i = 0; i < ARRAYSIZE(irxModules); i++) {
		if ((res = SifLoadModule(irxModules[i], 0, NULL)) < 0) {
			sprintf(errorStr, "Can't load module %s (%d)", irxModules[i], res);
			return false;
		}
	}
	printf("Modules loaded\n");
	// now try to load optional IRXs
	if ((res = SifLoadModule(IRX_PREFIX "USBD.IRX" IRX_SUFFIX, 0, NULL)) < 0)
		sioprintf("Cannot load module: USBD.IRX (%d)\n", res);
	else {
		if ((res = SifLoadModule(IRX_PREFIX "PS2MOUSE.IRX" IRX_SUFFIX, 0, NULL)) < 0)
			sioprintf("Cannot load module: PS2MOUSE.IRX (%d)\n", res);
		else
			_useMouse = true;
		if ((res = SifLoadModule(IRX_PREFIX "RPCKBD.IRX" IRX_SUFFIX, 0, NULL)) < 0)
			sioprintf("Cannot load module: RPCKBD.IRX (%d)\n", res);
		else
			_useKbd = true;
	}
	sioprintf("Modules: UsbMouse %sloaded, UsbKbd %sloaded, Hdd %sloaded.", _useMouse ? "" : "not ", _useKbd ? "" : "not ", _useHdd ? "" : "not ");
	return true;
}

void OSystem_PS2::initSize(uint width, uint height, int overscale) {
	printf("initializing new size: (%d/%d)...", width, height);
	_screen->newScreenSize(width, height);
	_width = width;
	_height = height;
	_screen->setMouseXy(width / 2, height / 2);
	_input->newRange(0, 0, width - 1, height - 1);
	_input->warpTo(width / 2, height / 2);

	_oldMouseX = width / 2;
	_oldMouseY = height / 2;
	printf("done\n");
}

void OSystem_PS2::setPalette(const byte *colors, uint start, uint num) {
	_screen->setPalette((const uint32*)colors, (uint8)start, (uint16)num);
}

void OSystem_PS2::grabPalette(byte *colors, uint start, uint num) {
	_screen->grabPalette((uint32*)colors, (uint8)start, (uint16)num);
}

void OSystem_PS2::copyRectToScreen(const byte *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 = _width - x;
	if (y + h > _height)
		h = _height - y;
	if ((w > 0) && (h > 0))
		_screen->copyScreenRect((const uint8*)buf, (uint16)pitch, (uint16)x, (uint16)y, (uint16)w, (uint16)h);
}

void OSystem_PS2::updateScreen(void) {
	_screen->updateScreen();
}

uint32 OSystem_PS2::getMillis(void) {
	return (uint32)(msecCount >> 32);
}

void OSystem_PS2::delayMillis(uint msecs) {
	if (msecs == 0)
		return;

	int tid = GetThreadId();
	if (tid == _soundTid) {
		sioprintf("ERROR: delayMillis() from sound thread!");
		return;
	}

	if (tid == _timerTid) {
		g_TimerWakeUp = (int32)msecs;
		WaitSema(g_TimerWaitSema);
	} else {
		g_MainWakeUp = (int32)msecs;
		WaitSema(g_MainWaitSema);
	}
}

void OSystem_PS2::setTimerCallback(OSystem::TimerProc callback, int interval) {
	if (callback && (interval != 10))
		sioprintf("unhandled timer interval: %d\n", interval);
	_scummTimerProc = callback;
}

int OSystem_PS2::getOutputSampleRate(void) const {
	return 48000;
}

bool OSystem_PS2::setSoundCallback(SoundProc proc, void *param) {
	assert(proc != NULL);

	WaitSema(_soundSema);
    _scummSoundProc = proc;
	_scummSoundParam = param;
	SjPCM_Play();
	SignalSema(_soundSema);
	return true;
}

void OSystem_PS2::clearSoundCallback(void) {
	WaitSema(_soundSema);
	_scummSoundProc = NULL;
	_scummSoundParam = NULL;
	SjPCM_Pause();
	SignalSema(_soundSema);
}

Common::SaveFileManager *OSystem_PS2::getSavefileManager(void) {
	return _saveManager;
}

OSystem::MutexRef OSystem_PS2::createMutex(void) {
	ee_sema_t newSema;
	newSema.init_count = 1;
	newSema.max_count = 1;
	int resSema = CreateSema(&newSema);
	if (resSema < 0)
		printf("createMutex: unable to create Semaphore.\n");
	return (MutexRef)resSema;
}

void OSystem_PS2::lockMutex(MutexRef mutex) {
	WaitSema((int)mutex);
}

void OSystem_PS2::unlockMutex(MutexRef mutex) {
	SignalSema((int)mutex);
}

void OSystem_PS2::deleteMutex(MutexRef mutex) {
	DeleteSema((int)mutex);
}

void OSystem_PS2::setShakePos(int shakeOffset) {
	_screen->setShakePos(shakeOffset);
}

bool OSystem_PS2::showMouse(bool visible) {
	bool retVal = _mouseVisible;
	_screen->showMouse(visible);
	_mouseVisible = visible;
	return retVal;
}

void OSystem_PS2::warpMouse(int x, int y) {
	_input->warpTo((uint16)x, (uint16)y);
	_screen->setMouseXy(x, y);
}

void OSystem_PS2::setMouseCursor(const byte *buf, uint w, uint h, int hotspot_x, int hotspot_y, byte keycolor, int cursorTargetScale) {
	_screen->setMouseOverlay(buf, w, h, hotspot_x, hotspot_y, keycolor);
}

bool OSystem_PS2::openCD(int drive) {
	return false;
}

bool OSystem_PS2::pollCD(void) {
	return false;
}

void OSystem_PS2::playCD(int track, int num_loops, int start_frame, int duration) {
}

void OSystem_PS2::stopCD(void) {
}

void OSystem_PS2::updateCD(void) {
}

void OSystem_PS2::showOverlay(void) {
	_screen->showOverlay();
}

void OSystem_PS2::hideOverlay(void) {
	_screen->hideOverlay();
}

void OSystem_PS2::clearOverlay(void) {
	_screen->clearOverlay();
}

void OSystem_PS2::grabOverlay(OverlayColor *buf, int pitch) {
	_screen->grabOverlay((uint16*)buf, (uint16)pitch);
}

void OSystem_PS2::copyRectToOverlay(const OverlayColor *buf, int pitch, int x, int y, int w, int h) {
	_screen->copyOverlayRect((uint16*)buf, (uint16)pitch, (uint16)x, (uint16)y, (uint16)w, (uint16)h);
}

const OSystem::GraphicsMode OSystem_PS2::_graphicsMode = { NULL, NULL, 0 };

const OSystem::GraphicsMode *OSystem_PS2::getSupportedGraphicsModes(void) const {
    return &_graphicsMode;
}

bool OSystem_PS2::setGraphicsMode(int mode) {
	return (mode == 0);
}

int OSystem_PS2::getGraphicsMode(void) const {
	return 0;
}

int OSystem_PS2::getDefaultGraphicsMode(void) const {
	return 0;
}

bool OSystem_PS2::pollEvent(Event &event) {
	bool res = _input->pollEvent(&event);
	if (res && (event.type == EVENT_MOUSEMOVE))
		_screen->setMouseXy(event.mouse.x, event.mouse.y);
	return res;
}

OverlayColor OSystem_PS2::RGBToColor(uint8 r, uint8 g, uint8 b) {
	return (r >> 3) | ((g >> 3) << 5) | ((b >> 3) << 10);
}

void OSystem_PS2::colorToRGB(OverlayColor color, uint8 &r, uint8 &g, uint8 &b) {
	r = (color & 0x1F) << 3;
	g = ((color >> 5) & 0x1F) << 3;
	b = ((color >> 10) & 0x1F) << 3;
}

int16 OSystem_PS2::getHeight(void) {
	return _height;
}

int16 OSystem_PS2::getWidth(void) {
	return _width;
}

void OSystem_PS2::quit(void) {
	printf("OSystem_PS2::quit\n");
	clearSoundCallback();
	setTimerCallback(NULL, 0);
	SleepThread();
}

void OSystem_PS2::fatalError(char *errorStr) {
	sioprintf("ERROR: %s", errorStr);
	printf("ERROR: %s\n", errorStr);
	Graphics::Surface surf;
	surf.create(300, 200, 1);
	Common::String str(errorStr);
	Graphics::g_sysfont.drawString(&surf, str, 0, 0, 300, 0xFF);

	uint32 palette[256];
	palette[0] = 0x00400000;
	for (int i = 1; i < 256; i++)
		palette[i] = 0xFFFFFFFF;

	_screen->setPalette(palette, 0, 256);
	_screen->hideOverlay();
	_screen->wantAnim(false);

	_screen->copyScreenRect((uint8*)surf.getBasePtr(0, 0), surf.pitch, 10, 10, 300, 100);
	_screen->updateScreen();
	SleepThread();
}

static uint32 g_timeSecs;
static int	  g_day, g_month, g_year;
static uint32 g_lastTimeCheck;

void buildNewDate(int dayDiff) {
	static int daysPerMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (((g_year % 4) == 0) && (((g_year % 100) != 0) || ((g_year % 1000) == 0)))
		daysPerMonth[1] = 29;
	else
		daysPerMonth[1] = 28;

	if (dayDiff == -1) {
		g_day--;
		if (g_day == 0) {
			g_month--;
			if (g_month == 0) {
				g_year--;
				g_month = 12;
			}
			g_day = daysPerMonth[g_month - 1];
		}
	} else if (dayDiff == 1) {
		g_day++;
		if (g_day > daysPerMonth[g_month - 1]) {
			g_day = 1;
			g_month++;
			if (g_month > 12) {
				g_month = 1;
				g_year++;
			}
		}
	}
}

#define SECONDS_PER_DAY (24 * 60 * 60)

void readRtcTime(void) {
	struct CdClock cdClock;
	CDVD_ReadClock(&cdClock);
	g_lastTimeCheck = (uint32)(msecCount >> 32);

	if (cdClock.stat) {
		printf("Unable to read RTC time, EC: %d\n", cdClock.stat);
		g_day = g_month = 1;
		g_year = 0;
		g_timeSecs = 0;
	} else {
		int gmtOfs = configGetTimezone();
		if (configIsDaylightSavingEnabled())
			gmtOfs += 60;

		int timeSecs = (FROM_BCD(cdClock.hour) * 60 + FROM_BCD(cdClock.minute)) * 60 + FROM_BCD(cdClock.second);
		timeSecs -= 9 * 60 * 60; // minus 9 hours, JST -> GMT conversion
		timeSecs += gmtOfs * 60; // GMT -> timezone the user selected

		g_day = FROM_BCD(cdClock.day);
		g_month = FROM_BCD(cdClock.month);
		g_year = FROM_BCD(cdClock.year);

		if (timeSecs < 0) {
			buildNewDate(-1);
			timeSecs += SECONDS_PER_DAY;
		} else if (timeSecs >= SECONDS_PER_DAY) {
			buildNewDate(+1);
			timeSecs -= SECONDS_PER_DAY;
		}

		g_timeSecs = (uint32)timeSecs;
	}

	sioprintf("Time: %d:%02d:%02d - %d.%d.%4d", g_timeSecs / (60 * 60), (g_timeSecs / 60) % 60, g_timeSecs % 60,
		g_day, g_month, g_year + 2000);
}

extern time_t time(time_t *p) {
	time_t blah;
	memset(&blah, 0, sizeof(time_t));
	return blah;
}

extern struct tm *localtime(const time_t *p) {
	uint32 currentSecs = g_timeSecs + ((msecCount >> 32) - g_lastTimeCheck) / 1000;
	if (currentSecs >= SECONDS_PER_DAY) {
		buildNewDate(+1);
		g_timeSecs -= SECONDS_PER_DAY;
		currentSecs = g_timeSecs + ((msecCount >> 32) - g_lastTimeCheck) / 1000;
	}

	static struct tm retStruct;
	memset(&retStruct, 0, sizeof(retStruct));

	retStruct.tm_hour = currentSecs / (60 * 60);
	retStruct.tm_min  = (currentSecs / 60) % 60;
	retStruct.tm_sec  = currentSecs % 60;
	retStruct.tm_year = g_year + 100;
	retStruct.tm_mday = g_day;
	retStruct.tm_mon  = g_month;
	// tm_wday, tm_yday and tm_isdst are zero for now
    return &retStruct;
}