1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include <time.h>
24 #include <psptypes.h>
25 #include <psprtc.h>
26 
27 #include "common/scummsys.h"
28 #include "backends/platform/psp/rtc.h"
29 
30 //#define __PSP_DEBUG_FUNCS__	/* For debugging function calls */
31 //#define __PSP_DEBUG_PRINT__	/* For debug printouts */
32 
33 #include "backends/platform/psp/trace.h"
34 
35 
36 // Class PspRtc ---------------------------------------------------------------
37 namespace Common {
38 DECLARE_SINGLETON(PspRtc);
39 }
40 
init()41 void PspRtc::init() {						// init our starting ticks
42 	uint32 ticks[2];
43 	sceRtcGetCurrentTick((u64 *)ticks);
44 
45 	_startMillis = ticks[0]/1000;
46 	_startMicros = ticks[0];
47 	//_lastMillis = ticks[0]/1000;	//debug - only when we don't subtract startMillis
48 }
49 
50 #define MS_LOOP_AROUND 4294967				/* We loop every 2^32 / 1000 = 71 minutes */
51 #define MS_LOOP_CHECK  60000				/* Threading can cause weird mixups without this */
52 
53 // Note that after we fill up 32 bits ie 50 days we'll loop back to 0, which may cause
54 // unpredictable results
getMillis(bool skipRecord)55 uint32 PspRtc::getMillis(bool skipRecord) {
56 	uint32 ticks[2];
57 
58 	sceRtcGetCurrentTick((u64 *)ticks);		// can introduce weird thread delays
59 
60 	uint32 millis = ticks[0]/1000;
61 	millis -= _startMillis;					// get ms since start of program
62 
63 	if ((int)_lastMillis - (int)millis > MS_LOOP_CHECK) {		// we must have looped around
64 		if (_looped == false) {					// check to make sure threads do this once
65 			_looped = true;
66 			_milliOffset += MS_LOOP_AROUND;		// add the needed offset
67 			PSP_DEBUG_PRINT("looping around. last ms[%d], curr ms[%d]\n", _lastMillis, millis);
68 		}
69 	} else {
70 		_looped = false;
71 	}
72 
73 	_lastMillis = millis;
74 
75 	return millis + _milliOffset;
76 }
77 
getMicros()78 uint32 PspRtc::getMicros() {
79 	uint32 ticks[2];
80 
81 	sceRtcGetCurrentTick((u64 *)ticks);
82 	ticks[0] -= _startMicros;
83 
84 	return ticks[0];
85 }
86