PSP: moved RTC to singleton to allow usage by classes other than Osystem

svn-id: r50116
This commit is contained in:
Yotam Barnoy 2010-06-21 15:13:36 +00:00
parent 4a4fcb19dd
commit 65fe8d817c
8 changed files with 140 additions and 75 deletions

View file

@ -147,6 +147,7 @@ OBJS := powerman.o \
pspkeyboard.o \ pspkeyboard.o \
audio.o \ audio.o \
thread.o \ thread.o \
rtc.o \
mp3.o mp3.o
# Include common Scummvm makefile # Include common Scummvm makefile

View file

@ -15,6 +15,7 @@ MODULE_OBJS := powerman.o \
pspkeyboard.o \ pspkeyboard.o \
audio.o \ audio.o \
thread.o \ thread.o \
rtc.o \
mp3.o mp3.o
MODULE_DIRS += \ MODULE_DIRS += \

View file

@ -37,6 +37,7 @@
#include "backends/platform/psp/psppixelformat.h" #include "backends/platform/psp/psppixelformat.h"
#include "backends/platform/psp/osys_psp.h" #include "backends/platform/psp/osys_psp.h"
#include "backends/platform/psp/powerman.h" #include "backends/platform/psp/powerman.h"
#include "backends/platform/psp/rtc.h"
#include "backends/saves/psp/psp-saves.h" #include "backends/saves/psp/psp-saves.h"
#include "backends/timer/default/default-timer.h" #include "backends/timer/default/default-timer.h"
@ -64,6 +65,9 @@ OSystem_PSP::~OSystem_PSP() {}
void OSystem_PSP::initBackend() { void OSystem_PSP::initBackend() {
DEBUG_ENTER_FUNC(); DEBUG_ENTER_FUNC();
// Instantiate real time clock
PspRtc::instance();
_cursor.enableCursorPalette(false); _cursor.enableCursorPalette(false);
_cursor.setXY(PSP_SCREEN_WIDTH >> 1, PSP_SCREEN_HEIGHT >> 1); // Mouse in the middle of the screen _cursor.setXY(PSP_SCREEN_WIDTH >> 1, PSP_SCREEN_HEIGHT >> 1); // Mouse in the middle of the screen
@ -320,7 +324,7 @@ bool OSystem_PSP::pollEvent(Common::Event &event) {
} }
uint32 OSystem_PSP::getMillis() { uint32 OSystem_PSP::getMillis() {
return _pspRtc.getMillis(); return PspRtc::instance().getMillis();
} }
void OSystem_PSP::delayMillis(uint msecs) { void OSystem_PSP::delayMillis(uint msecs) {

View file

@ -60,7 +60,6 @@ private:
InputHandler _inputHandler; InputHandler _inputHandler;
PspAudio _audio; PspAudio _audio;
PspTimer _pspTimer; PspTimer _pspTimer;
PspRtc _pspRtc;
public: public:
OSystem_PSP() : _savefile(0), _mixer(0), _timer(0), _pendingUpdate(false), _pendingUpdateCounter(0) {} OSystem_PSP() : _savefile(0), _mixer(0), _timer(0), _pendingUpdate(false), _pendingUpdateCounter(0) {}

View file

@ -0,0 +1,87 @@
/* 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.
*
* $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/trunk/backends/platform/psp/osys_psp.cpp $
* $Id: osys_psp.cpp 49903 2010-06-16 09:04:27Z Bluddy $
*
*/
#include <time.h>
#include <psptypes.h>
#include <psprtc.h>
#include "common/scummsys.h"
#include "backends/platform/psp/rtc.h"
//#define __PSP_DEBUG_FUNCS__ /* For debugging function calls */
//#define __PSP_DEBUG_PRINT__ /* For debug printouts */
#include "backends/platform/psp/trace.h"
// Class PspRtc ---------------------------------------------------------------
DECLARE_SINGLETON(PspRtc)
void PspRtc::init() { // init our starting ticks
uint32 ticks[2];
sceRtcGetCurrentTick((u64 *)ticks);
_startMillis = ticks[0]/1000;
_startMicros = ticks[0];
//_lastMillis = ticks[0]/1000; //debug - only when we don't subtract startMillis
}
#define MS_LOOP_AROUND 4294967 /* We loop every 2^32 / 1000 = 71 minutes */
#define MS_LOOP_CHECK 60000 /* Threading can cause weird mixups without this */
// Note that after we fill up 32 bits ie 50 days we'll loop back to 0, which may cause
// unpredictable results
uint32 PspRtc::getMillis() {
uint32 ticks[2];
sceRtcGetCurrentTick((u64 *)ticks); // can introduce weird thread delays
uint32 millis = ticks[0]/1000;
millis -= _startMillis; // get ms since start of program
if ((int)_lastMillis - (int)millis > MS_LOOP_CHECK) { // we must have looped around
if (_looped == false) { // check to make sure threads do this once
_looped = true;
_milliOffset += MS_LOOP_AROUND; // add the needed offset
PSP_DEBUG_PRINT("looping around. last ms[%d], curr ms[%d]\n", _lastMillis, millis);
}
} else {
_looped = false;
}
_lastMillis = millis;
return millis + _milliOffset;
}
uint32 PspRtc::getMicros() {
uint32 ticks[2];
sceRtcGetCurrentTick((u64 *)ticks);
ticks[0] -= _startMicros;
return ticks[0];
}

View file

@ -0,0 +1,45 @@
/* 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.
*
* $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/trunk/backends/platform/psp/osys_psp.cpp $
* $Id: osys_psp.cpp 49903 2010-06-16 09:04:27Z Bluddy $
*
*/
#ifndef _PSP_RTC_H_
#define _PSP_RTC_H_
#include "common/singleton.h"
class PspRtc : public Common::Singleton<PspRtc>{
private:
uint32 _startMillis;
uint32 _startMicros;
uint32 _lastMillis;
uint32 _milliOffset; // to prevent looping around of millis
bool _looped; // make sure we only loop once - for threading
public:
PspRtc() : _startMillis(0), _startMicros(0), _lastMillis(0), _milliOffset(0), _looped(false) { init(); }
void init();
uint32 getMillis();
uint32 getMicros();
};
#endif

View file

@ -23,9 +23,6 @@
* *
*/ */
#include <time.h>
#include <psptypes.h>
#include <psprtc.h>
#include <pspthreadman.h> #include <pspthreadman.h>
#include "backends/platform/psp/thread.h" #include "backends/platform/psp/thread.h"
@ -192,57 +189,3 @@ void PspCondition::wait(PspMutex &externalMutex) {
externalMutex.lock(); // must lock external mutex here for continuation externalMutex.lock(); // must lock external mutex here for continuation
} }
//#define __PSP_DEBUG_FUNCS__ /* For debugging function calls */
//#define __PSP_DEBUG_PRINT__ /* For debug printouts */
#include "backends/platform/psp/trace.h"
// Class PspRtc ---------------------------------------------------------------
void PspRtc::init() { // init our starting ticks
uint32 ticks[2];
sceRtcGetCurrentTick((u64 *)ticks);
_startMillis = ticks[0]/1000;
_startMicros = ticks[0];
//_lastMillis = ticks[0]/1000; //debug - only when we don't subtract startMillis
}
#define MS_LOOP_AROUND 4294967 /* We loop every 2^32 / 1000 = 71 minutes */
#define MS_LOOP_CHECK 60000 /* Threading can cause weird mixups without this */
// Note that after we fill up 32 bits ie 50 days we'll loop back to 0, which may cause
// unpredictable results
uint32 PspRtc::getMillis() {
uint32 ticks[2];
sceRtcGetCurrentTick((u64 *)ticks); // can introduce weird thread delays
uint32 millis = ticks[0]/1000;
millis -= _startMillis; // get ms since start of program
if ((int)_lastMillis - (int)millis > MS_LOOP_CHECK) { // we must have looped around
if (_looped == false) { // check to make sure threads do this once
_looped = true;
_milliOffset += MS_LOOP_AROUND; // add the needed offset
PSP_DEBUG_PRINT("looping around. last ms[%d], curr ms[%d]\n", _lastMillis, millis);
}
} else {
_looped = false;
}
_lastMillis = millis;
return millis + _milliOffset;
}
uint32 PspRtc::getMicros() {
uint32 ticks[2];
sceRtcGetCurrentTick((u64 *)ticks);
ticks[0] -= _startMicros;
return ticks[0];
}

View file

@ -73,22 +73,7 @@ public:
PspCondition() : _mutex(true), _waitingThreads(0), _signaledThreads(0), PspCondition() : _mutex(true), _waitingThreads(0), _signaledThreads(0),
_waitSem(0), _doneSem(0) {} _waitSem(0), _doneSem(0) {}
void wait(PspMutex &externalMutex); void wait(PspMutex &externalMutex);
void releaseAll(); void releaseAll();
};
class PspRtc {
private:
uint32 _startMillis;
uint32 _startMicros;
uint32 _lastMillis;
uint32 _milliOffset; // to prevent looping around of millis
bool _looped; // make sure we only loop once
public:
PspRtc() : _startMillis(0), _startMicros(0), _lastMillis(0), _milliOffset(0), _looped(false) { init(); }
void init();
uint32 getMillis();
uint32 getMicros();
}; };
enum ThreadPriority { enum ThreadPriority {