aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/psp
diff options
context:
space:
mode:
authorTony Puccinelli2010-08-10 23:21:08 +0000
committerTony Puccinelli2010-08-10 23:21:08 +0000
commit682807f0e916b189c69b60765418ee1ccc327cbf (patch)
treee8b220b934f6b14d4dac290181fce0c1b209579a /backends/platform/psp
parent83f1531cb8fa5ba43c246143ecdde2cb1d140a8e (diff)
parentfffec23a02cc88ed8daba0a3b50007b7e220c075 (diff)
downloadscummvm-rg350-682807f0e916b189c69b60765418ee1ccc327cbf.tar.gz
scummvm-rg350-682807f0e916b189c69b60765418ee1ccc327cbf.tar.bz2
scummvm-rg350-682807f0e916b189c69b60765418ee1ccc327cbf.zip
merged trunk into branch, reverted Cruise Singleton changes
svn-id: r51961
Diffstat (limited to 'backends/platform/psp')
-rw-r--r--backends/platform/psp/Makefile16
-rw-r--r--backends/platform/psp/README.PSP2
-rw-r--r--backends/platform/psp/README.PSP.in2
-rw-r--r--backends/platform/psp/audio.cpp150
-rw-r--r--backends/platform/psp/audio.h69
-rw-r--r--backends/platform/psp/cursor.cpp4
-rw-r--r--backends/platform/psp/default_display_client.h4
-rw-r--r--backends/platform/psp/display_client.cpp34
-rw-r--r--backends/platform/psp/display_manager.cpp51
-rw-r--r--backends/platform/psp/display_manager.h34
-rw-r--r--backends/platform/psp/input.cpp4
-rw-r--r--backends/platform/psp/memory.cpp457
-rw-r--r--backends/platform/psp/memory.h61
-rw-r--r--backends/platform/psp/module.mk16
-rw-r--r--backends/platform/psp/mp3.cpp487
-rw-r--r--backends/platform/psp/mp3.h121
-rw-r--r--backends/platform/psp/osys_psp.cpp106
-rw-r--r--backends/platform/psp/osys_psp.h15
-rw-r--r--backends/platform/psp/powerman.cpp352
-rw-r--r--backends/platform/psp/powerman.h75
-rw-r--r--backends/platform/psp/psp.spec2
-rw-r--r--backends/platform/psp/psp_main.cpp14
-rw-r--r--backends/platform/psp/rtc.cpp87
-rw-r--r--backends/platform/psp/rtc.h50
-rw-r--r--backends/platform/psp/tests.cpp565
-rw-r--r--backends/platform/psp/tests.h36
-rw-r--r--backends/platform/psp/thread.cpp234
-rw-r--r--backends/platform/psp/thread.h112
-rw-r--r--backends/platform/psp/trace.cpp11
-rw-r--r--backends/platform/psp/trace.h12
30 files changed, 2677 insertions, 506 deletions
diff --git a/backends/platform/psp/Makefile b/backends/platform/psp/Makefile
index 754bc775f0..9c011220b8 100644
--- a/backends/platform/psp/Makefile
+++ b/backends/platform/psp/Makefile
@@ -64,7 +64,6 @@ ifeq ($(PSPSDK),)
$(error $$(PSPSDK) cannot be obtained.)
endif
-
# Variables for common Scummvm makefile
CXX = psp-g++
CXXFLAGS = -O3 -Wall -Wno-multichar -fno-exceptions -fno-rtti
@@ -124,16 +123,12 @@ CXXFLAGS += -pg -g
LDFLAGS += -pg
endif
-# SDL Libs and Flags
-SDLFLAGS := $(shell $(PSPBIN)/sdl-config --cflags)
-SDLLIBS := $(shell $(PSPBIN)/sdl-config --libs)
# PSP LIBS
PSPLIBS = -lpspprof -lpspvfpu -lpspdebug -lpspgu -lpspge -lpspdisplay -lpspctrl -lpspsdk \
-lpsputility -lpspuser -lpsppower -lpsphprm -lpspsdk -lpsprtc -lpspaudio -lpspkernel
# Add in PSPSDK includes and libraries.
-CXXFLAGS += $(SDLFLAGS)
-LIBS += -lpng -lSDL -lz $(findstring -lGL,$(SDLLIBS)) -lstdc++ -lc -lm $(filter -L%,$(SDLLIBS)) $(PSPLIBS)
+LIBS += -lpng -lz -lstdc++ -lc -lm $(PSPLIBS)
OBJS := powerman.o \
psp_main.o \
@@ -147,8 +142,15 @@ OBJS := powerman.o \
cursor.o \
trace.o \
psploader.o \
- pspkeyboard.o
+ pspkeyboard.o \
+ audio.o \
+ thread.o \
+ rtc.o \
+ mp3.o \
+ tests.o
+
+BACKEND := psp
# Include common Scummvm makefile
include $(srcdir)/Makefile.common
diff --git a/backends/platform/psp/README.PSP b/backends/platform/psp/README.PSP
index 45f31483f7..f4d5bae6d6 100644
--- a/backends/platform/psp/README.PSP
+++ b/backends/platform/psp/README.PSP
@@ -99,8 +99,6 @@ To build ScummVM for PSP you need:
Note: This usually gets installed by the PSP toolchain,
so you don't have to do it manually.
-- SDL (svn co svn://svn.pspdev.org/psp/trunk/SDL)
-
- zlib (svn co svn://svn.pspdev.org/psp/trunk/zlib)
- libPNG (svn co svn://svn.pspdev.org/psp/trunk/libpng)
diff --git a/backends/platform/psp/README.PSP.in b/backends/platform/psp/README.PSP.in
index d9f75974f1..2d53fd3b47 100644
--- a/backends/platform/psp/README.PSP.in
+++ b/backends/platform/psp/README.PSP.in
@@ -99,8 +99,6 @@ To build ScummVM for PSP you need:
Note: This usually gets installed by the PSP toolchain,
so you don't have to do it manually.
-- SDL (svn co svn://svn.pspdev.org/psp/trunk/SDL)
-
- zlib (svn co svn://svn.pspdev.org/psp/trunk/zlib)
- libPNG (svn co svn://svn.pspdev.org/psp/trunk/libpng)
diff --git a/backends/platform/psp/audio.cpp b/backends/platform/psp/audio.cpp
new file mode 100644
index 0000000000..14691befee
--- /dev/null
+++ b/backends/platform/psp/audio.cpp
@@ -0,0 +1,150 @@
+/* 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$
+ * $Id$
+ *
+ */
+
+#include <pspthreadman.h>
+#include <pspaudio.h>
+
+#include "common/scummsys.h"
+#include "backends/platform/psp/audio.h"
+
+//#define __PSP_DEBUG_FUNCS__ /* For debugging function calls */
+//#define __PSP_DEBUG_PRINT__ /* For debug printouts */
+
+#include "backends/platform/psp/trace.h"
+
+bool PspAudio::open(uint32 freq, uint32 numOfChannels, uint32 numOfSamples, callbackFunc callback, void *userData) {
+ DEBUG_ENTER_FUNC();
+ if (_init) {
+ PSP_ERROR("audio device already initialized\n");
+ return true;
+ }
+
+ PSP_DEBUG_PRINT("freq[%d], numOfChannels[%d], numOfSamples[%d], callback[%p], userData[%x]\n",
+ freq, numOfChannels, numOfSamples, callback, (uint32)userData);
+
+ numOfSamples = PSP_AUDIO_SAMPLE_ALIGN(numOfSamples);
+ uint32 bufLen = numOfSamples * numOfChannels * NUM_BUFFERS * sizeof(uint16);
+
+ PSP_DEBUG_PRINT("total buffer size[%d]\n", bufLen);
+
+ _buffers[0] = (byte *)memalign(64, bufLen);
+ if (!_buffers[0]) {
+ PSP_ERROR("failed to allocate memory for audio buffers\n");
+ return false;
+ }
+ memset(_buffers[0], 0, bufLen); // clean the buffer
+
+ // Fill in the rest of the buffer pointers
+ byte *pBuffer = _buffers[0];
+ for (int i = 1; i < NUM_BUFFERS; i++) {
+ pBuffer += numOfSamples * numOfChannels * sizeof(uint16);
+ _buffers[i] = pBuffer;
+ }
+
+ // Reserve a HW channel for our audio
+ _pspChannel = sceAudioChReserve(PSP_AUDIO_NEXT_CHANNEL, numOfSamples, numOfChannels == 2 ? PSP_AUDIO_FORMAT_STEREO : PSP_AUDIO_FORMAT_MONO);
+ if (_pspChannel < 0) {
+ PSP_ERROR("failed to reserve audio channel\n");
+ return false;
+ }
+
+ PSP_DEBUG_PRINT("reserved channel[%d] for audio\n", _pspChannel);
+
+ // Save our data
+ _numOfChannels = numOfChannels;
+ _numOfSamples = numOfSamples;
+ _bufferSize = numOfSamples * numOfChannels * sizeof(uint16); // should be the right size to send the app
+ _callback = callback;
+ _userData = userData;
+ _bufferToFill = 0;
+ _bufferToPlay = 0;
+
+ _init = true;
+ _paused = true; // start in paused mode
+
+ threadCreateAndStart("audioThread", PRIORITY_AUDIO_THREAD, STACK_AUDIO_THREAD); // start the consumer thread
+
+ return true;
+}
+
+// The real thread function
+void PspAudio::threadFunction() {
+ assert(_callback);
+ PSP_DEBUG_PRINT_FUNC("audio thread started\n");
+
+ while (_init) { // Keep looping so long as we haven't been told to stop
+ if (_paused)
+ PSP_DEBUG_PRINT("audio thread paused\n");
+ while (_paused) { // delay until we stop pausing
+ PspThread::delayMicros(100000); // 100ms
+ if (!_paused)
+ PSP_DEBUG_PRINT("audio thread unpaused\n");
+ }
+
+ PSP_DEBUG_PRINT("remaining samples[%d]\n", _remainingSamples);
+
+ PSP_DEBUG_PRINT("filling buffer[%d]\n", _bufferToFill);
+ _callback(_userData, _buffers[_bufferToFill], _bufferSize); // ask mixer to fill in data
+ nextBuffer(_bufferToFill);
+
+ PSP_DEBUG_PRINT("playing buffer[%d].\n", _bufferToPlay);
+ playBuffer();
+ nextBuffer(_bufferToPlay);
+ } // while _init
+
+ // destroy everything
+ free(_buffers[0]);
+ sceAudioChRelease(_pspChannel);
+ PSP_DEBUG_PRINT("audio thread exiting. ****************************\n");
+}
+
+// Much faster than using %, especially with conditional moves (MIPS)
+inline void PspAudio::nextBuffer(int &bufferIdx) {
+ DEBUG_ENTER_FUNC();
+ bufferIdx++;
+ if (bufferIdx >= NUM_BUFFERS)
+ bufferIdx = 0;
+}
+
+// Don't do it with blocking
+inline bool PspAudio::playBuffer() {
+ DEBUG_ENTER_FUNC();
+ int ret;
+ if (_numOfChannels == 1)
+ ret = sceAudioOutputBlocking(_pspChannel, PSP_AUDIO_VOLUME_MAX, _buffers[_bufferToPlay]);
+ else
+ ret = sceAudioOutputPannedBlocking(_pspChannel, PSP_AUDIO_VOLUME_MAX, PSP_AUDIO_VOLUME_MAX, _buffers[_bufferToPlay]);
+
+ if (ret < 0) {
+ PSP_ERROR("failed to output audio. Error[%d]\n", ret);
+ return false;
+ }
+ return true;
+}
+
+void PspAudio::close() {
+ PSP_DEBUG_PRINT("close has been called ***************\n");
+ _init = false;
+}
diff --git a/backends/platform/psp/audio.h b/backends/platform/psp/audio.h
new file mode 100644
index 0000000000..07f70cec7d
--- /dev/null
+++ b/backends/platform/psp/audio.h
@@ -0,0 +1,69 @@
+/* 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$
+ * $Id$
+ *
+ */
+
+#ifndef PSP_AUDIO_H
+#define PSP_AUDIO_H
+
+#include "backends/platform/psp/thread.h"
+
+class PspAudio : public PspThreadable {
+public:
+ enum {
+ NUM_BUFFERS = 2,
+ FREQUENCY = 44100 /* only frequency we allow */
+ };
+ typedef void (* callbackFunc)(void *userData, byte *samples, int len); // audio callback to call
+ PspAudio() : _pspChannel(0),
+ _numOfChannels(0), _numOfSamples(0), _callback(0),
+ _bufferToPlay(0), _bufferToFill(0),
+ _init(false), _paused(true) {
+ for (int i=0; i<NUM_BUFFERS; i++)
+ _buffers[i] = 0;
+ }
+ ~PspAudio() { close(); }
+ bool playBuffer();
+ void nextBuffer(int &bufferIdx);
+ bool open(uint32 freq, uint32 numOfChannels, uint32 numOfSamples, callbackFunc callback, void *userData);
+ void close();
+ uint32 getFrequency() { return FREQUENCY; }
+ void pause() { _paused = true; }
+ void unpause() { _paused = false; }
+ virtual void threadFunction(); // actual audio thread
+
+private:
+ int _pspChannel; // chosen hardware output channel
+ uint32 _numOfChannels; // 1 for mono; 2 for stereo
+ uint32 _numOfSamples;
+ callbackFunc _callback; // the callback to call between outputting audio
+ void *_userData; // userData to send with callback
+ byte *_buffers[NUM_BUFFERS];
+ int _bufferToPlay; // the next buffer to output
+ int _bufferToFill;
+ int _bufferSize;
+ bool _init; // flag for initialization
+ bool _paused;
+};
+
+#endif /* PSP_AUDIO_H */
diff --git a/backends/platform/psp/cursor.cpp b/backends/platform/psp/cursor.cpp
index ae3b8f0050..cf879e095a 100644
--- a/backends/platform/psp/cursor.cpp
+++ b/backends/platform/psp/cursor.cpp
@@ -18,8 +18,8 @@
* 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.h $
- * $Id: osys_psp.h 46120 2009-11-24 10:33:30Z Bluddy $
+ * $URL$
+ * $Id$
*
*/
diff --git a/backends/platform/psp/default_display_client.h b/backends/platform/psp/default_display_client.h
index 716e6fcc35..2e33632eb1 100644
--- a/backends/platform/psp/default_display_client.h
+++ b/backends/platform/psp/default_display_client.h
@@ -18,8 +18,8 @@
* 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/trace.h $
- * $Id: trace.h 44276 2009-09-23 16:11:23Z joostp $
+ * $URL$
+ * $Id$
*
*/
diff --git a/backends/platform/psp/display_client.cpp b/backends/platform/psp/display_client.cpp
index 90c41e796d..6360772c96 100644
--- a/backends/platform/psp/display_client.cpp
+++ b/backends/platform/psp/display_client.cpp
@@ -18,8 +18,8 @@
* 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 46126 2009-11-24 14:18:46Z fingolfin $
+ * $URL$
+ * $Id$
*
*/
@@ -340,11 +340,17 @@ void Buffer::copyFromRect(const byte *buf, uint32 pitch, int destX, int destY, u
if (pitch == realWidthInBytes && pitch == recWidthInBytes) {
//memcpy(dst, buf, _pixelFormat.pixelsToBytes(recHeight * recWidth));
- Copier::copy(dst, buf, _pixelFormat.pixelsToBytes(recHeight * recWidth), &_pixelFormat);
+ if (_pixelFormat.swapRB)
+ PspMemory::fastSwap(dst, buf, _pixelFormat.pixelsToBytes(recHeight * recWidth), _pixelFormat);
+ else
+ PspMemory::fastCopy(dst, buf, _pixelFormat.pixelsToBytes(recHeight * recWidth));
} else {
do {
//memcpy(dst, buf, recWidthInBytes);
- Copier::copy(dst, buf, recWidthInBytes, &_pixelFormat);
+ if (_pixelFormat.swapRB)
+ PspMemory::fastSwap(dst, buf, recWidthInBytes, _pixelFormat);
+ else
+ PspMemory::fastCopy(dst, buf, recWidthInBytes);
buf += pitch;
dst += realWidthInBytes;
} while (--recHeight);
@@ -363,7 +369,10 @@ void Buffer::copyToArray(byte *dst, int pitch) {
do {
//memcpy(dst, src, sourceWidthInBytes);
- Copier::copy(dst, src, sourceWidthInBytes, &_pixelFormat);
+ if (_pixelFormat.swapRB)
+ PspMemory::fastSwap(dst, src, sourceWidthInBytes, _pixelFormat);
+ else
+ PspMemory::fastCopy(dst, src, sourceWidthInBytes);
src += realWidthInBytes;
dst += pitch;
} while (--h);
@@ -686,17 +695,18 @@ void GuRenderer::fillVertices(Vertex *vertices) {
uint32 gapX = _useGlobalScaler ? (PSP_SCREEN_WIDTH - outputWidth) >> 1 : 0;
uint32 gapY = _useGlobalScaler ? (PSP_SCREEN_HEIGHT - outputHeight) >> 1 : 0;
+ // Save scaled offset on screen
+ float scaledOffsetOnScreenX = scaleSourceToOutputX(_offsetOnScreen.x);
+ float scaledOffsetOnScreenY = scaleSourceToOutputY(_offsetOnScreen.y);
+
float imageStartX, imageStartY, imageEndX, imageEndY;
- imageStartX = gapX + (scaleSourceToOutputX(_maxTextureOffset.x));
- imageStartY = gapY;
-
- imageStartX += scaleSourceToOutputX(_offsetOnScreen.x);
- imageStartY += scaleSourceToOutputY(_offsetOnScreen.y);
+ imageStartX = gapX + scaledOffsetOnScreenX + (scaleSourceToOutputX(_maxTextureOffset.x));
+ imageStartY = gapY + scaledOffsetOnScreenY;
if (_fullScreen) { // shortcut
- imageEndX = PSP_SCREEN_WIDTH - gapX;
- imageEndY = PSP_SCREEN_HEIGHT - gapY;
+ imageEndX = PSP_SCREEN_WIDTH - gapX + scaledOffsetOnScreenX;
+ imageEndY = PSP_SCREEN_HEIGHT - gapY + scaledOffsetOnScreenY; // needed for screen shake
} else { /* !fullScreen */
imageEndX = imageStartX + scaleSourceToOutputX(_drawSize.width);
imageEndY = imageStartY + scaleSourceToOutputY(_drawSize.height);
diff --git a/backends/platform/psp/display_manager.cpp b/backends/platform/psp/display_manager.cpp
index 0982512a86..544e5a1b25 100644
--- a/backends/platform/psp/display_manager.cpp
+++ b/backends/platform/psp/display_manager.cpp
@@ -18,8 +18,8 @@
* 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 47541 2010-01-25 01:39:44Z lordhoto $
+ * $URL$
+ * $Id$
*
*/
@@ -64,37 +64,24 @@ const OSystem::GraphicsMode DisplayManager::_supportedModes[] = {
void MasterGuRenderer::setupCallbackThread() {
DEBUG_ENTER_FUNC();
- int thid = sceKernelCreateThread("displayCbThread", guCallbackThread, 0x11, 4*1024, THREAD_ATTR_USER, 0);
- PSP_DEBUG_PRINT("Display CB thread id is %x\n", thid);
-
- // We want to pass the pointer to this, but we'll have to take address of this so use a little trick
- MasterGuRenderer *_this = this;
-
- if (thid >= 0) {
- sceKernelStartThread(thid, sizeof(uint32 *), &_this);
- } else
- PSP_ERROR("failed to create display callback thread\n");
+ // start the thread that updates the display
+ threadCreateAndStart("DisplayCbThread", PRIORITY_DISPLAY_THREAD, STACK_DISPLAY_THREAD);
}
-// thread that reacts to the callback
-int MasterGuRenderer::guCallbackThread(SceSize, void *__this) {
+// this function gets called by PspThread when starting the new thread
+void MasterGuRenderer::threadFunction() {
DEBUG_ENTER_FUNC();
- // Dereferenced the copied value which was this
- MasterGuRenderer *_this = *(MasterGuRenderer **)__this;
-
// Create the callback. It should always get the pointer to MasterGuRenderer
- _this->_callbackId = sceKernelCreateCallback("Display Callback", guCallback, _this);
- if (_this->_callbackId < 0) {
- PSP_ERROR("failed to create display callback\n");
- return -1;
+ _callbackId = sceKernelCreateCallback("Display Callback", guCallback, this);
+ if (_callbackId < 0) {
+ PSP_ERROR("failed to create display callback\n");
}
PSP_DEBUG_PRINT("created callback. Going to sleep\n");
- sceKernelSleepThreadCB(); // sleep until we get a callback
- return 0;
+ sceKernelSleepThreadCB(); // sleep until we get a callback
}
// This callback is called when the render is finished. It swaps the buffers
@@ -310,27 +297,29 @@ void DisplayManager::calculateScaleParams() {
}
}
-void DisplayManager::renderAll() {
+// return true if we really rendered or no dirty. False otherwise
+bool DisplayManager::renderAll() {
DEBUG_ENTER_FUNC();
#ifdef USE_DISPLAY_CALLBACK
if (!_masterGuRenderer.isRenderFinished()) {
PSP_DEBUG_PRINT("Callback render not finished.\n");
- return;
+ return false; // didn't render
}
#endif /* USE_DISPLAY_CALLBACK */
- if (!isTimeToUpdate())
- return;
-
+ // This is cheaper than checking time, so we do it first
if (!_screen->isDirty() &&
(!_overlay->isDirty()) &&
(!_cursor->isDirty()) &&
(!_keyboard->isDirty())) {
PSP_DEBUG_PRINT("Nothing dirty\n");
- return;
+ return true; // nothing to render
}
+ if (!isTimeToUpdate())
+ return false; // didn't render
+
PSP_DEBUG_PRINT("screen[%s], overlay[%s], cursor[%s], keyboard[%s]\n",
_screen->isDirty() ? "true" : "false",
_overlay->isDirty() ? "true" : "false",
@@ -360,6 +349,8 @@ void DisplayManager::renderAll() {
_keyboard->setClean();
_masterGuRenderer.guPostRender();
+
+ return true; // rendered successfully
}
inline bool DisplayManager::isTimeToUpdate() {
@@ -374,7 +365,7 @@ inline bool DisplayManager::isTimeToUpdate() {
return true;
}
-Common::List<Graphics::PixelFormat> DisplayManager::getSupportedPixelFormats() {
+Common::List<Graphics::PixelFormat> DisplayManager::getSupportedPixelFormats() const {
Common::List<Graphics::PixelFormat> list;
// In order of preference
diff --git a/backends/platform/psp/display_manager.h b/backends/platform/psp/display_manager.h
index 5864f39b36..72f252faae 100644
--- a/backends/platform/psp/display_manager.h
+++ b/backends/platform/psp/display_manager.h
@@ -18,18 +18,20 @@
* 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 47541 2010-01-25 01:39:44Z lordhoto $
+ * $URL$
+ * $Id$
*
*/
#ifndef PSP_DISPLAY_MAN_H
#define PSP_DISPLAY_MAN_H
+#include "backends/platform/psp/thread.h"
+
/**
* Class used only by DisplayManager to start/stop GU rendering
*/
-class MasterGuRenderer {
+class MasterGuRenderer : public PspThreadable {
public:
MasterGuRenderer() : _lastRenderTime(0), _renderFinished(true), _callbackId(-1) {}
void guInit();
@@ -37,15 +39,15 @@ public:
void guPostRender();
void guShutDown();
bool isRenderFinished() { return _renderFinished; }
- void setupCallbackThread();
+ void setupCallbackThread();
private:
+ virtual void threadFunction(); // for the display callback thread
static uint32 _displayList[];
- uint32 _lastRenderTime; // For measuring rendering
+ uint32 _lastRenderTime; // For measuring rendering time
void guProgramDisplayBufferSizes();
- static int guCallbackThread(SceSize, void *); // for the graphics callbacks
- static int guCallback(int, int, void *__this);
- bool _renderFinished;
- int _callbackId;
+ static int guCallback(int, int, void *__this); // for the display callback
+ bool _renderFinished; // for sync with render callback
+ int _callbackId; // to keep track of render callback
};
class Screen;
@@ -68,7 +70,7 @@ public:
~DisplayManager();
void init();
- void renderAll();
+ bool renderAll(); // return true if rendered or nothing dirty. False otherwise
bool setGraphicsMode(int mode);
bool setGraphicsMode(const char *name);
int getGraphicsMode() const { return _graphicsMode; }
@@ -83,12 +85,12 @@ public:
void setSizeAndPixelFormat(uint width, uint height, const Graphics::PixelFormat *format);
// Getters
- float getScaleX() { return _displayParams.scaleX; }
- float getScaleY() { return _displayParams.scaleY; }
- uint32 getOutputWidth() { return _displayParams.screenOutput.width; }
- uint32 getOutputHeight() { return _displayParams.screenOutput.height; }
- uint32 getOutputBitsPerPixel() { return _displayParams.outputBitsPerPixel; }
- Common::List<Graphics::PixelFormat> getSupportedPixelFormats();
+ float getScaleX() const { return _displayParams.scaleX; }
+ float getScaleY() const { return _displayParams.scaleY; }
+ uint32 getOutputWidth() const { return _displayParams.screenOutput.width; }
+ uint32 getOutputHeight() const { return _displayParams.screenOutput.height; }
+ uint32 getOutputBitsPerPixel() const { return _displayParams.outputBitsPerPixel; }
+ Common::List<Graphics::PixelFormat> getSupportedPixelFormats() const;
private:
struct GlobalDisplayParams {
diff --git a/backends/platform/psp/input.cpp b/backends/platform/psp/input.cpp
index 4fe7cb3f92..2a91ce455a 100644
--- a/backends/platform/psp/input.cpp
+++ b/backends/platform/psp/input.cpp
@@ -18,8 +18,8 @@
* 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 43618 2009-08-21 22:44:49Z joostp $
+ * $URL$
+ * $Id$
*
*/
diff --git a/backends/platform/psp/memory.cpp b/backends/platform/psp/memory.cpp
index e134a7d0f4..29d0482d9a 100644
--- a/backends/platform/psp/memory.cpp
+++ b/backends/platform/psp/memory.cpp
@@ -35,129 +35,408 @@
#include "backends/platform/psp/trace.h"
-void Copier::copy(byte *dst, const byte *src, uint32 bytes, PSPPixelFormat *format /* = NULL */) {
+//#define TEST_MEMORY_COPY
+
+extern "C" {
+
+#ifdef TEST_MEMORY_COPY /* we won't be able to run in this case b/c of printouts */
+extern void *__real_memcpy(void *dst, void *src, size_t bytes);
+#endif
+
+void *__wrap_memcpy(void *dst, void *src, size_t bytes) {
+#ifdef TEST_MEMORY_COPY /* we won't be able to run in this case */
+ return __real_memcpy(dst, src, bytes);
+#else
+ PspMemory::fastCopy((byte *)dst, (byte *)src, bytes);
+ return dst;
+#endif
+}
+
+}
+
+void PspMemory::copy(byte *dst, const byte *src, uint32 bytes) {
DEBUG_ENTER_FUNC();
- uint32 prefixDst = (((uint32)dst) & 0x3);
- prefixDst = prefixDst ? 4 - prefixDst : 0; // prefix only if we have address % 4 != 0
- uint32 prefixSrc = (((uint32)src) & 0x3);
- prefixSrc = prefixSrc ? 4 - prefixSrc : 0; // prefix only if we have address % 4 != 0
- uint32 *dst32, *src32;
- bool swapRB = format ? format->swapRB : false; // take swap value from pixelformat if it's given
-#ifdef __PSP_DEBUG_PRINT__
+#ifdef TEST_MEMORY_COPY
uint32 debugBytes = bytes;
const byte *debugDst = dst, *debugSrc = src;
#endif
- uint32 words, remainingBytes;
- //PSP_DEBUG_PRINT("dst[%p], src[%p], bytes[%d], swap[%s], prefixDst[%u], prefixSrc[%u]\n", dst, src, bytes, swapRB ? "true" : "false", prefixDst, prefixSrc);
+ PSP_DEBUG_PRINT("copy(): dst[%p], src[%p], bytes[%d]\n", dst, src, bytes);
- if (prefixDst || prefixSrc) { // we're not aligned to word boundaries
- if (prefixDst != prefixSrc) { // worst case: we can never be aligned. this mode is highly inefficient. try to get engines not to use this mode too much
- PSP_DEBUG_PRINT("misaligned copy of %u bytes from %p to %p\n", bytes, src, dst);
- if ((prefixDst & 1) || (prefixSrc & 1))
- copy8(dst, src, bytes); // no swap is possible on 8 bit
- else
- copy16((uint16 *)dst, (uint16 *)src, bytes, format);
-
- goto test;
- }
-
- // Do the prefix: the part to get us aligned
- if (prefixDst & 1) { // byte
- copy8(dst, src, prefixDst); // no swap available
- } else { // short
- copy16((uint16 *)dst, (uint16 *)src, prefixDst, format);
- }
- if (bytes > prefixDst) // check that we can afford to subtract from bytes
- bytes -= prefixDst;
- else {
+ // align the destination pointer first
+ uint32 prefixDst = (((uint32)dst) & 0x3);
+
+ if (prefixDst) {
+ prefixDst = 4 - prefixDst; // prefix only if we have address % 4 != 0
+ PSP_DEBUG_PRINT("prefixDst[%d]\n", prefixDst);
+
+ bytes -= prefixDst; // remember we assume bytes >= 4
+
+ if (bytes < MIN_AMOUNT_FOR_COMPLEX_COPY) { // check if it's worthwhile to continue
+ copy8(dst, src, bytes + prefixDst);
+#ifdef TEST_MEMORY_COPY
+ testCopy(debugDst, debugSrc, debugBytes);
+#endif
return;
}
- dst32 = (uint32 *)(dst + prefixDst);
- src32 = (uint32 *)(src + prefixSrc);
- } else { // We're aligned to word boundaries
- dst32 = (uint32 *)dst;
- src32 = (uint32 *)src;
+
+ while (prefixDst--) {
+ *dst++ = *src++;
+ }
}
-
- words = bytes >> 2;
- remainingBytes = bytes & 0x3;
-
- if (swapRB) { // need to swap
- for (; words > 0; words--) {
- *dst32 = format->swapRedBlue32(*src32);
- dst32++;
- src32++;
- }
- } else { // no swapping
- for (; words > 0; words--) {
- *dst32 = *src32;
- dst32++;
- src32++;
- }
+
+ // check the source pointer alignment now
+ uint32 alignSrc = (((uint32)src) & 0x3);
+
+ if (alignSrc) { // we'll need to realign our reads
+ copy32Misaligned((uint32 *)dst, src, bytes, alignSrc);
+ } else {
+ copy32Aligned((uint32 *)dst, (uint32 *)src, bytes);
}
- // Do any remaining bytes
- if (remainingBytes) {
- if (remainingBytes & 1) // we have bytes left
- copy8((byte *)dst32, (byte *)src32, remainingBytes);
- else // 16bits left
- copy16((uint16*)dst32, (uint16 *)src32, remainingBytes, format);
- }
+#ifdef TEST_MEMORY_COPY
+ testCopy(debugDst, debugSrc, debugBytes);
+#endif
+}
-test:
- // debug
-#ifdef __PSP_DEBUG_PRINT__
+void PspMemory::testCopy(const byte *debugDst, const byte *debugSrc, uint32 debugBytes) {
+
bool mismatch = false;
+ PSP_INFO_PRINT("testing fastCopy...");
for (uint32 i = 0; i < debugBytes; i++) {
if (debugDst[i] != debugSrc[i]) {
- if (mismatch == false) {
- PSP_DEBUG_PRINT_SAMELN("mismatch in copy:\n");
- PSP_DEBUG_PRINT("dst[%p], src[%p], bytes[%u], swap[%s], prefixDst[%u], prefixSrc[%u]\n", debugDst, debugSrc, debugBytes, swapRB ? "true" : "false", prefixDst, prefixSrc);
+ if (!mismatch) {
+ PSP_INFO_PRINT("**** mismatch in copy! ****\n");
+ PSP_INFO_PRINT("dst[%p], src[%p], bytes[%u]\n", debugDst, debugSrc, debugBytes);
mismatch = true;
}
- PSP_DEBUG_PRINT_SAMELN("%x!=%x ", debugSrc[i], debugDst[i]);
+ PSP_INFO_PRINT("[%d]%x!=%x ", i, debugSrc[i], debugDst[i]);
}
}
- if (mismatch)
- PSP_DEBUG_PRINT("\n");
+ if (mismatch) {
+ PSP_INFO_PRINT("\n");
+ } else {
+ PSP_INFO_PRINT("ok\n");
+ }
+}
+
+//
+// used to swap red and blue
+void PspMemory::swap(uint16 *dst16, const uint16 *src16, uint32 bytes, PSPPixelFormat &format) {
+ DEBUG_ENTER_FUNC();
+
+#ifdef TEST_MEMORY_COPY
+ uint32 debugBytes = bytes;
+ const uint16 *debugDst = dst16, *debugSrc = src16;
#endif
+
+ // align the destination pointer first
+ uint32 prefixDst = (((uint32)dst16) & 0x3); // for swap, we can only have 2 or 0 as our prefix
+
+ if (prefixDst) {
+ bytes -= prefixDst; // remember we assume bytes > 4
+ *dst16++ = format.swapRedBlue16(*src16++);
+
+ if (bytes < MIN_AMOUNT_FOR_COMPLEX_COPY) { // check if it's worthwhile to continue
+ swap16(dst16, src16, bytes, format);
+
+#ifdef TEST_MEMORY_COPY
+ testSwap(debugDst, debugSrc, debugBytes, format);
+#endif
+ return;
+ }
+ }
+
+ // check the source pointer alignment now
+ uint32 alignSrc = (((uint32)src16) & 0x3);
+
+ if (alignSrc) { // we'll need to realign our reads
+ PSP_DEBUG_PRINT("misaligned copy of %u bytes from %p to %p\n", bytes, src16, dst16);
+ swap32Misaligned((uint32 *)dst16, src16, bytes, format);
+ } else {
+ swap32Aligned((uint32 *)dst16, (const uint32 *)src16, bytes, format);
+ }
+
+#ifdef TEST_MEMORY_COPY
+ testSwap(debugDst, debugSrc, debugBytes, format);
+#endif
+
+}
+
+void PspMemory::testSwap(const uint16 *debugDst, const uint16 *debugSrc, uint32 debugBytes, PSPPixelFormat &format) {
+
+ bool mismatch = false;
+ PSP_INFO_PRINT("testing fastSwap...");
+
+ uint32 shorts = debugBytes >> 1;
+
+ for (uint32 i = 0; i < shorts; i++) {
+ if (debugDst[i] != format.swapRedBlue16(debugSrc[i])) {
+ if (!mismatch) {
+ PSP_INFO_PRINT("**** mismatch in swap! ****\n");
+ PSP_INFO_PRINT("dst[%p], src[%p], bytes[%u]\n", debugDst, debugSrc, debugBytes);
+ mismatch = true;
+ }
+ PSP_INFO_PRINT("[%d]%x!=%x ", i<<1, format.swapRedBlue16(debugSrc[i]), debugDst[i]);
+ }
+ }
+ if (mismatch) {
+ PSP_INFO_PRINT("\n");
+ } else {
+ PSP_INFO_PRINT("ok\n");
+ }
+}
+
+
+void PspMemory::copy32Aligned(uint32 *dst32, const uint32 *src32, uint32 bytes) {
+ PSP_DEBUG_PRINT("copy32Aligned(): dst32[%p], src32[%p], bytes[%d]\n", dst32, src32, bytes);
+
+ int words8 = bytes >> 5;
+
+ // try blocks of 8 words at a time
+ if (words8) {
+ while (words8--) {
+ uint32 a, b, c, d;
+ a = src32[0];
+ b = src32[1];
+ c = src32[2];
+ d = src32[3];
+ dst32[0] = a;
+ dst32[1] = b;
+ dst32[2] = c;
+ dst32[3] = d;
+ a = src32[4];
+ b = src32[5];
+ c = src32[6];
+ d = src32[7];
+ dst32[4] = a;
+ dst32[5] = b;
+ dst32[6] = c;
+ dst32[7] = d;
+ dst32 += 8;
+ src32 += 8;
+ }
+ }
+
+ int words4 = (bytes & 0x1F) >> 4;
+
+ // try blocks of 4 words at a time
+ if (words4) {
+ uint32 a, b, c, d;
+ a = src32[0];
+ b = src32[1];
+ c = src32[2];
+ d = src32[3];
+ dst32[0] = a;
+ dst32[1] = b;
+ dst32[2] = c;
+ dst32[3] = d;
+ dst32 += 4;
+ src32 += 4;
+ }
+
+ int bytesLeft = (bytes & 0xF); // only look at bytes left after we did the above
+ int wordsLeft = bytesLeft >> 2;
+
+ // now just do single words
+ while (wordsLeft) {
+ *dst32++ = *src32++;
+ wordsLeft--;
+ }
- return; // So we have something to jump to with the label
+ bytesLeft = bytes & 0x3; // get remaining bytes
+
+ PSP_DEBUG_PRINT("bytesLeft[%d]\n", bytesLeft);
+
+ byte *dst = (byte *)dst32;
+ byte *src = (byte *)src32;
+
+ while (bytesLeft--) {
+ *dst++ = *src++;
+ }
}
-inline void Copier::copy8(byte *dst, const byte *src, uint32 bytes) {
- for (; bytes > 0; bytes--) {
- *dst = *src;
- dst++;
- src++;
+void PspMemory::swap32Aligned(uint32 *dst32, const uint32 *src32, uint32 bytes, PSPPixelFormat &format) {
+ DEBUG_ENTER_FUNC();
+ int words4 = bytes >> 4;
+
+ // try blocks of 4 words at a time
+ while (words4--) {
+ uint32 a, b, c, d;
+ a = format.swapRedBlue32(src32[0]);
+ b = format.swapRedBlue32(src32[1]);
+ c = format.swapRedBlue32(src32[2]);
+ d = format.swapRedBlue32(src32[3]);
+ dst32[0] = a;
+ dst32[1] = b;
+ dst32[2] = c;
+ dst32[3] = d;
+ dst32 += 4;
+ src32 += 4;
+ }
+
+ uint32 bytesLeft = bytes & 0xF;
+ uint32 words = bytesLeft >> 2;
+
+ // now just do words
+ while (words--) {
+ *dst32++ = format.swapRedBlue32(*src32++);
+ }
+
+ bytesLeft = bytes & 0x3;
+
+ if (bytesLeft) { // for swap, can only be 1 short left
+ *((uint16 *)dst32) = format.swapRedBlue16(*((uint16 *)src32));
}
}
-inline void Copier::copy16(uint16 *dst, const uint16 *src, uint32 bytes, PSPPixelFormat *format /* = NULL */) {
- uint32 shorts = bytes >> 1;
- uint32 remainingBytes = bytes & 1;
- bool swapRB = format ? format->swapRB : false;
- if (swapRB) {
- for (; shorts > 0 ; shorts--) {
- *dst = format->swapRedBlue16(*src);
- dst++;
- src++;
+// More challenging -- need to shift
+// Assume dst is aligned
+void PspMemory::copy32Misaligned(uint32 *dst32, const byte *src, uint32 bytes, uint32 alignSrc) {
+ PSP_DEBUG_PRINT("copy32Misaligned: dst32[%p], src[%p], bytes[%d], alignSrc[%d]\n", dst32, src, bytes, alignSrc);
+
+ uint32 *src32 = (uint32 *)(((uint32)src) & 0xFFFFFFFC); // remove misalignment
+ uint32 shiftValue, lastShiftValue;
+
+ switch (alignSrc) {
+ case 1:
+ shiftValue = 8;
+ lastShiftValue = 24;
+ break;
+ case 2:
+ shiftValue = 16;
+ lastShiftValue = 16;
+ break;
+ default: /* 3 */
+ shiftValue = 24;
+ lastShiftValue = 8;
+ break;
+ }
+
+ uint32 dstWord, srcWord;
+
+ // Try to do groups of 4 words
+ uint32 words4 = bytes >> 4;
+
+ srcWord = *src32; // preload 1st word so we read ahead
+
+ for (; words4; words4--) {
+ dstWord = srcWord >> shiftValue;
+ srcWord = src32[1];
+ dstWord |= srcWord << lastShiftValue;
+ dst32[0] = dstWord;
+ dstWord = srcWord >> shiftValue;
+ srcWord = src32[2];
+ dstWord |= srcWord << lastShiftValue;
+ dst32[1] = dstWord;
+ dstWord = srcWord >> shiftValue;
+ srcWord = src32[3];
+ dstWord |= srcWord << lastShiftValue;
+ dst32[2] = dstWord;
+ dstWord = srcWord >> shiftValue;
+ srcWord = src32[4];
+ dstWord |= srcWord << lastShiftValue;
+ dst32[3] = dstWord;
+ src32 += 4;
+ dst32 += 4;
+ }
+
+ uint32 words = (bytes & 0xF) >> 2; // now get remaining words
+
+ // we read one word ahead of what we write
+ // setup the first read
+
+ for (; words ;words--) {
+ dstWord = srcWord >> shiftValue;
+ srcWord = src32[1]; // we still go one ahead
+ src32++;
+ dstWord |= srcWord << lastShiftValue;
+ *dst32++ = dstWord;
+ }
+
+ uint32 bytesLeft = bytes & 3; // and remaining bytes
+
+ if (bytesLeft) {
+ byte *dst8 = (byte *)dst32;
+ byte *src8 = ((byte *)src32) + ((uint32)src & 0x3); // get exact location we should be at
+
+ for(; bytesLeft; bytesLeft--) {
+ *dst8++ = *src8++;
}
- } else {
- for (; shorts > 0 ; shorts--) {
- *dst = *src;
- dst++;
- src++;
+ }
+}
+
+// More challenging -- need to shift
+// We assume dst is aligned
+void PspMemory::swap32Misaligned(uint32 *dst32, const uint16 *src16, uint32 bytes, PSPPixelFormat &format) {
+ DEBUG_ENTER_FUNC();
+
+ const uint32 shiftValue = 16;
+ uint32 *src32 = (uint32 *)(((uint32)src16) & 0xFFFFFFFC); // remove misalignment
+
+ // Try to do groups of 4 words
+ uint32 words4 = bytes >> 4;
+ uint32 srcWord = src32[0]; // preload
+
+ while (words4--) {
+ uint32 dstWord = srcWord >> shiftValue;
+ srcWord = src32[1];
+ dstWord |= srcWord << shiftValue;
+ dst32[0] = format.swapRedBlue32(dstWord);
+ dstWord = srcWord >> shiftValue;
+ srcWord = src32[2];
+ dstWord |= srcWord << shiftValue;
+ dst32[1] = format.swapRedBlue32(dstWord);
+ dstWord = srcWord >> shiftValue;
+ srcWord = src32[3];
+ dstWord |= srcWord << shiftValue;
+ dst32[2] = format.swapRedBlue32(dstWord);
+ dstWord = srcWord >> shiftValue;
+ srcWord = src32[4];
+ dstWord |= srcWord << shiftValue;
+ dst32[3] = format.swapRedBlue32(dstWord);
+ src32 += 4;
+ dst32 += 4;
+ }
+
+ uint32 words = (bytes & 0xF) >> 2;
+
+ // we read one word ahead of what we write
+ // setup the first read
+ if (words) {
+ //srcWord = *src32++; // don't need this. already loaded
+ src32++; // we already have the value loaded in
+
+ while (words--) {
+ uint32 dstWord = srcWord >> shiftValue;
+ srcWord = *src32++;
+ dstWord |= srcWord << shiftValue;
+ *dst32++ = format.swapRedBlue32(dstWord);
}
}
- if (remainingBytes)
- *(byte *)dst = *(byte *)src;
+
+ uint32 bytesLeft = bytes & 3;
+
+ if (bytesLeft) { // for swap, can only be 1 short left
+ *((uint16 *)dst32) = format.swapRedBlue16((uint16)(srcWord >> shiftValue));
+ }
}
+inline void PspMemory::copy16(uint16 *dst16, const uint16 *src16, uint32 bytes) {
+ PSP_DEBUG_PRINT("copy16(): dst16[%p], src16[%p], bytes[%d]\n", dst16, src16, bytes);
+
+ uint32 shorts = bytes >> 1;
+ uint32 remainingBytes = bytes & 1;
+
+ for (; shorts > 0 ; shorts--) {
+ *dst16++ = *src16++;
+ }
+ if (remainingBytes)
+ *(byte *)dst16 = *(byte *)src16;
+}
// Class VramAllocator -----------------------------------
diff --git a/backends/platform/psp/memory.h b/backends/platform/psp/memory.h
index a198095090..793bc94888 100644
--- a/backends/platform/psp/memory.h
+++ b/backends/platform/psp/memory.h
@@ -27,17 +27,68 @@
#ifndef PSP_MEMORY_H
#define PSP_MEMORY_H
+#include "backends/platform/psp/psppixelformat.h"
+#include "common/list.h"
+
#define UNCACHED(x) ((byte *)(((uint32)(x)) | 0x40000000)) /* make an uncached access */
#define CACHED(x) ((byte *)(((uint32)(x)) & 0xBFFFFFFF)) /* make an uncached access into a cached one */
+#define MIN_AMOUNT_FOR_COMPLEX_COPY 8
+#define MIN_AMOUNT_FOR_MISALIGNED_COPY 8
+
+//#define __PSP_DEBUG_PRINT__
+
+#include "backends/platform/psp/trace.h"
+
/**
* Class that does memory copying and swapping if needed
*/
-class Copier {
-public:
- static void copy(byte *dst, const byte *src, uint32 bytes, PSPPixelFormat *format = NULL);
- static void copy8(byte *dst, const byte *src, uint32 bytes);
- static void copy16(uint16 *dst, const uint16 *src, uint32 bytes, PSPPixelFormat *format = NULL);
+class PspMemory {
+private:
+ static void testCopy(const byte *debugDst, const byte *debugSrc, uint32 debugBytes);
+ static void testSwap(const uint16 *debugDst, const uint16 *debugSrc, uint32 debugBytes, PSPPixelFormat &format);
+ static void copy(byte *dst, const byte *src, uint32 bytes);
+ static void swap(uint16 *dst16, const uint16 *src16, uint32 bytes, PSPPixelFormat &format);
+ static void copy32Aligned(uint32 *dst32, const uint32 *src32, uint32 bytes);
+ static void swap32Aligned(uint32 *dst32, const uint32 *src32, uint32 bytes, PSPPixelFormat &format);
+ static void copy32Misaligned(uint32 *dst32, const byte *src, uint32 bytes, uint32 alignSrc);
+ static void swap32Misaligned(uint32 *dst32, const uint16 *src16, uint32 bytes, PSPPixelFormat &format);
+ static void copy16(uint16 *dst, const uint16 *src, uint32 bytes);
+
+ // For swapping, we know that we have multiples of 16 bits
+ static void swap16(uint16 *dst16, const uint16 *src16, uint32 bytes, PSPPixelFormat &format) {
+ PSP_DEBUG_PRINT("swap16 called with dst16[%p], src16[%p], bytes[%d]\n", dst16, src16, bytes);
+ uint32 shorts = bytes >> 1;
+
+ while (shorts--) {
+ *dst16++ = format.swapRedBlue16(*src16++);
+ }
+ }
+
+ static void copy8(byte *dst, const byte *src, uint32 bytes) {
+ PSP_DEBUG_PRINT("copy8 called with dst[%p], src[%p], bytes[%d]\n", dst, src, bytes);
+ while (bytes--) {
+ *dst++ = *src++;
+ }
+ }
+
+public:
+ // This is the interface to the outside world
+ static void fastCopy(byte *dst, const byte *src, uint32 bytes) {
+ if (bytes < MIN_AMOUNT_FOR_COMPLEX_COPY) {
+ copy8(dst, src, bytes);
+ } else { // go to more powerful copy
+ copy(dst, src, bytes);
+ }
+ }
+
+ static void fastSwap(byte *dst, const byte *src, uint32 bytes, PSPPixelFormat &format) {
+ if (bytes < MIN_AMOUNT_FOR_COMPLEX_COPY * 2) {
+ swap16((uint16 *)dst, (uint16 *)src, bytes, format);
+ } else { // go to more powerful copy
+ swap((uint16 *)dst, (uint16 *)src, bytes, format);
+ }
+ }
};
/**
diff --git a/backends/platform/psp/module.mk b/backends/platform/psp/module.mk
index f7191fe14f..4652189ab4 100644
--- a/backends/platform/psp/module.mk
+++ b/backends/platform/psp/module.mk
@@ -12,10 +12,14 @@ MODULE_OBJS := powerman.o \
cursor.o \
trace.o \
psploader.o \
- pspkeyboard.o
+ pspkeyboard.o \
+ audio.o \
+ thread.o \
+ rtc.o \
+ mp3.o \
+ tests.o
-MODULE_DIRS += \
- backends/platform/psp/
-
-# We don't use the rules.mk here on purpose
-OBJS := $(addprefix $(MODULE)/, $(MODULE_OBJS)) $(OBJS)
+# We don't use rules.mk but rather manually update OBJS and MODULE_DIRS.
+MODULE_OBJS := $(addprefix $(MODULE)/, $(MODULE_OBJS))
+OBJS := $(MODULE_OBJS) $(OBJS)
+MODULE_DIRS += $(sort $(dir $(MODULE_OBJS)))
diff --git a/backends/platform/psp/mp3.cpp b/backends/platform/psp/mp3.cpp
new file mode 100644
index 0000000000..e25891396b
--- /dev/null
+++ b/backends/platform/psp/mp3.cpp
@@ -0,0 +1,487 @@
+/* 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$
+ * $Id$
+ *
+ */
+
+
+#include "common/debug.h"
+#include "common/stream.h"
+#include "common/util.h"
+#include "common/singleton.h"
+#include "common/mutex.h"
+
+#include "sound/audiostream.h"
+
+#include <pspaudiocodec.h>
+#include <psputility_modules.h>
+#include <pspthreadman.h>
+#include <pspsysmem.h>
+#include <pspmodulemgr.h>
+#include <psputility_avmodules.h>
+#include <mad.h>
+#include "backends/platform/psp/mp3.h"
+
+//#define DISABLE_PSP_MP3 // to make us use the regular MAD decoder instead
+
+//#define __PSP_DEBUG_FUNCS__ /* For debugging the stack */
+//#define __PSP_DEBUG_PRINT__
+#include "backends/platform/psp/trace.h"
+
+//#define PRINT_BUFFERS /* to debug MP3 buffers */
+
+namespace Audio {
+
+class Mp3PspStream;
+
+bool Mp3PspStream::_decoderInit = false; // has the decoder been initialized
+#ifdef DISABLE_PSP_MP3
+bool Mp3PspStream::_decoderFail = true; // pretend the decoder failed
+#else
+bool Mp3PspStream::_decoderFail = false; // has the decoder failed to load
+#endif
+
+bool Mp3PspStream::initDecoder() {
+ DEBUG_ENTER_FUNC();
+
+ if (_decoderInit) {
+ PSP_ERROR("Already initialized!");
+ return true;
+ }
+
+ // Based on PSP firmware version, we need to do different things to do Media Engine processing
+ uint32 firmware = sceKernelDevkitVersion();
+ PSP_DEBUG_PRINT("Firmware version 0x%x\n", firmware);
+ if (firmware == 0x01050001){
+ if (!loadStartAudioModule((char *)(void *)"flash0:/kd/me_for_vsh.prx",
+ PSP_MEMORY_PARTITION_KERNEL)) {
+ PSP_ERROR("failed to load me_for_vsh.prx. ME cannot start.\n");
+ _decoderFail = true;
+ return false;
+ }
+ if (!loadStartAudioModule((char *)(void *)"flash0:/kd/audiocodec.prx", PSP_MEMORY_PARTITION_KERNEL)) {
+ PSP_ERROR("failed to load audiocodec.prx. ME cannot start.\n");
+ _decoderFail = true;
+ return false;
+ }
+ } else {
+ if (sceUtilityLoadAvModule(PSP_AV_MODULE_AVCODEC) < 0) {
+ PSP_ERROR("failed to load AVCODEC module. ME cannot start.\n");
+ _decoderFail = true;
+ return false;
+ }
+ }
+
+ PSP_DEBUG_PRINT("Using PSP's ME for MP3\n"); // important to know this is happening
+
+ _decoderInit = true;
+ return true;
+}
+
+bool Mp3PspStream::stopDecoder() {
+ DEBUG_ENTER_FUNC();
+
+ if (!_decoderInit)
+ return true;
+
+ // Based on PSP firmware version, we need to do different things to do Media Engine processing
+ if (sceKernelDevkitVersion() == 0x01050001){
+/* if (!unloadAudioModule("flash0:/kd/me_for_vsh.prx", PSP_MEMORY_PARTITION_KERNEL) ||
+ !unloadAudioModule("flash0:/kd/audiocodec.prx", PSP_MEMORY_PARTITION_KERNEL) {
+ PSP_ERROR("failed to unload audio module\n");
+ return false;
+ }
+*/
+ }else{
+ if (sceUtilityUnloadModule(PSP_MODULE_AV_AVCODEC) < 0) {
+ PSP_ERROR("failed to unload avcodec module\n");
+ return false;
+ }
+ }
+
+ _decoderInit = false;
+ return true;
+}
+
+//Load a PSP audio module
+bool Mp3PspStream::loadStartAudioModule(const char *modname, int partition){
+ DEBUG_ENTER_FUNC();
+
+ SceKernelLMOption option;
+ SceUID modid;
+
+ memset(&option, 0, sizeof(option));
+ option.size = sizeof(option);
+ option.mpidtext = partition;
+ option.mpiddata = partition;
+ option.position = 0;
+ option.access = 1;
+
+ modid = sceKernelLoadModule(modname, 0, &option);
+ if (modid < 0) {
+ PSP_ERROR("Failed to load module %s. Got error 0x%x\n", modname, modid);
+ return false;
+ }
+
+ int ret = sceKernelStartModule(modid, 0, NULL, NULL, NULL);
+ if (ret < 0) {
+ PSP_ERROR("Failed to start module %s. Got error 0x%x\n", modname, ret);
+ return false;
+ }
+ return true;
+}
+
+// TODO: make parallel function for unloading the 1.50 modules
+
+Mp3PspStream::Mp3PspStream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag dispose) :
+ _inStream(inStream),
+ _disposeAfterUse(dispose),
+ _pcmLength(0),
+ _posInFrame(0),
+ _state(MP3_STATE_INIT),
+ _length(0, 1000),
+ _sampleRate(0),
+ _totalTime(mad_timer_zero) {
+
+ DEBUG_ENTER_FUNC();
+
+ assert(_decoderInit); // must be initialized by now
+
+ // let's leave the buffer guard -- who knows, it may be good?
+ memset(_buf, 0, sizeof(_buf));
+ memset(_codecInBuffer, 0, sizeof(_codecInBuffer));
+
+ initStream(); // init needed stuff for the stream
+
+ while (_state != MP3_STATE_EOS)
+ findValidHeader(); // get a first header so we can read basic stuff
+
+ _sampleRate = _header.samplerate; // copy it before it gets destroyed
+
+ _length = Timestamp(mad_timer_count(_totalTime, MAD_UNITS_MILLISECONDS), getRate());
+
+ //initStreamME(); // init the stuff needed for the ME to work
+
+ deinitStream();
+ //releaseStreamME();
+
+ _state = MP3_STATE_INIT;
+}
+
+int Mp3PspStream::initStream() {
+ DEBUG_ENTER_FUNC();
+
+ if (_state != MP3_STATE_INIT)
+ deinitStream();
+
+ // Init MAD
+ mad_stream_init(&_stream);
+ mad_header_init(&_header);
+
+ // Reset the stream data
+ _inStream->seek(0, SEEK_SET);
+ _totalTime = mad_timer_zero;
+ _posInFrame = 0;
+
+ // Update state
+ _state = MP3_STATE_READY;
+
+ // Read the first few sample bytes into the buffer
+ readMP3DataIntoBuffer();
+
+ return true;
+}
+
+bool Mp3PspStream::initStreamME() {
+ // The following will eventually go into the thread
+ sceAudiocodecReleaseEDRAM(_codecParams); // do we need this?
+
+ memset(_codecParams, 0, sizeof(_codecParams));
+
+ // Init the MP3 hardware
+ int ret = 0;
+ ret = sceAudiocodecCheckNeedMem(_codecParams, 0x1002);
+ if (ret < 0) {
+ PSP_ERROR("failed to init MP3 ME module. sceAudiocodecCheckNeedMem returned 0x%x.\n", ret);
+ return false;
+ }
+ PSP_DEBUG_PRINT("sceAudiocodecCheckNeedMem returned %d\n", ret);
+ ret = sceAudiocodecGetEDRAM(_codecParams, 0x1002);
+ if (ret < 0) {
+ PSP_ERROR("failed to init MP3 ME module. sceAudiocodecGetEDRAM returned 0x%x.\n", ret);
+ return false;
+ }
+ PSP_DEBUG_PRINT("sceAudioCodecGetEDRAM returned %d\n", ret);
+
+ PSP_DEBUG_PRINT("samplerate[%d]\n", _sampleRate);
+ _codecParams[10] = _sampleRate;
+
+ ret = sceAudiocodecInit(_codecParams, 0x1002);
+ if (ret < 0) {
+ PSP_ERROR("failed to init MP3 ME module. sceAudiocodecInit returned 0x%x.\n", ret);
+ return false;
+ }
+
+ return true;
+}
+
+Mp3PspStream::~Mp3PspStream() {
+ DEBUG_ENTER_FUNC();
+
+ deinitStream();
+ releaseStreamME(); // free the memory used for this stream
+
+ if (_disposeAfterUse == DisposeAfterUse::YES)
+ delete _inStream;
+}
+
+void Mp3PspStream::deinitStream() {
+ DEBUG_ENTER_FUNC();
+
+ if (_state == MP3_STATE_INIT)
+ return;
+
+ // Deinit MAD
+ mad_header_finish(&_header);
+ mad_stream_finish(&_stream);
+
+ _state = MP3_STATE_EOS;
+}
+
+void Mp3PspStream::releaseStreamME() {
+ sceAudiocodecReleaseEDRAM(_codecParams);
+}
+
+void Mp3PspStream::decodeMP3Data() {
+ DEBUG_ENTER_FUNC();
+
+ do {
+ if (_state == MP3_STATE_INIT) {
+ initStream();
+ initStreamME();
+ }
+
+ if (_state == MP3_STATE_EOS)
+ return;
+
+ findValidHeader(); // seach for next valid header
+
+ while (_state == MP3_STATE_READY) {
+ _stream.error = MAD_ERROR_NONE;
+
+ uint32 frame_size = _stream.next_frame - _stream.this_frame;
+ uint32 samplesPerFrame = _header.layer == MAD_LAYER_III ? 576 : 1152; // Varies by layer
+ // calculate frame size -- try
+ //uint32 calc_frame_size = ((144 * _header.bitrate) / 22050) + (_header.flags & MAD_FLAG_PADDING ? 1 : 0);
+
+ // Get stereo/mono
+ uint32 multFactor = 1;
+ if (_header.mode != MAD_MODE_SINGLE_CHANNEL) // mono - x2 for 16bit
+ multFactor *= 2; // stereo - x4 for 16bit
+
+ PSP_DEBUG_PRINT("MP3 frame size[%d]. Samples[%d]. Multfactor[%d] pad[%d]\n", frame_size, samplesPerFrame, multFactor, _header.flags & MAD_FLAG_PADDING);
+ memcpy(_codecInBuffer, _stream.this_frame, frame_size); // we need it aligned
+
+ // set up parameters for ME
+ _codecParams[6] = (unsigned long)_codecInBuffer;
+ _codecParams[8] = (unsigned long)_pcmSamples;
+ _codecParams[7] = frame_size;
+ _codecParams[9] = samplesPerFrame * multFactor; // x2 for stereo
+
+ // debug
+#ifdef PRINT_BUFFERS
+ PSP_DEBUG_PRINT("mp3 frame:\n");
+ for (int i=0; i < (int)frame_size; i++) {
+ PSP_DEBUG_PRINT_SAMELN("%x ", _codecInBuffer[i]);
+ }
+ PSP_DEBUG_PRINT("\n");
+#endif
+ // Decode the next frame
+ // This function blocks. We'll want to put it in a thread
+ int ret = sceAudiocodecDecode(_codecParams, 0x1002);
+ if (ret < 0) {
+ PSP_INFO_PRINT("failed to decode MP3 data in ME. sceAudiocodecDecode returned 0x%x\n", ret);
+ // handle error here
+ }
+
+#ifdef PRINT_BUFFERS
+ PSP_DEBUG_PRINT("PCM frame:\n");
+ for (int i=0; i < (int)_codecParams[9]; i+=2) { // changed from i+=2
+ PSP_DEBUG_PRINT_SAMELN("%d ", (int16)_pcmSamples[i]);
+ }
+ PSP_DEBUG_PRINT("\n");
+#endif
+ _pcmLength = samplesPerFrame;
+ _posInFrame = 0;
+ break;
+ }
+ } while (_state != MP3_STATE_EOS && _stream.error == MAD_ERROR_BUFLEN);
+
+ if (_stream.error != MAD_ERROR_NONE) // catch EOS
+ _state = MP3_STATE_EOS;
+}
+
+void Mp3PspStream::readMP3DataIntoBuffer() {
+ DEBUG_ENTER_FUNC();
+
+ uint32 remaining = 0;
+
+ // Give up immediately if we already used up all data in the stream
+ if (_inStream->eos()) {
+ _state = MP3_STATE_EOS;
+ return;
+ }
+
+ if (_stream.next_frame) {
+ // If there is still data in the MAD stream, we need to preserve it.
+ // Note that we use memmove, as we are reusing the same buffer,
+ // and hence the data regions we copy from and to may overlap.
+ remaining = _stream.bufend - _stream.next_frame;
+ assert(remaining < BUFFER_SIZE); // Paranoia check
+ memmove(_buf, _stream.next_frame, remaining); // TODO: may want another buffer
+ }
+
+ // Try to read the next block
+ uint32 size = _inStream->read(_buf + remaining, BUFFER_SIZE - remaining);
+ if (size <= 0) {
+ _state = MP3_STATE_EOS;
+ return;
+ }
+
+ // Feed the data we just read into the stream decoder
+ _stream.error = MAD_ERROR_NONE;
+ mad_stream_buffer(&_stream, _buf, size + remaining); // just setup the pointers
+}
+
+bool Mp3PspStream::seek(const Timestamp &where) {
+ DEBUG_ENTER_FUNC();
+
+ if (where == _length) {
+ _state = MP3_STATE_EOS;
+ return true;
+ } else if (where > _length) {
+ return false;
+ }
+
+ const uint32 time = where.msecs();
+
+ mad_timer_t destination;
+ mad_timer_set(&destination, time / 1000, time % 1000, 1000);
+
+ // Check if we need to rewind
+ if (_state != MP3_STATE_READY || mad_timer_compare(destination, _totalTime) < 0) {
+ initStream();
+ initStreamME();
+ }
+
+ // The ME will need clear data no matter what once we seek?
+ //if (mad_timer_compare(destination, _totalTime) > 0 && _state != MP3_STATE_EOS)
+ // initStreamME();
+
+ // Skip ahead
+ while (mad_timer_compare(destination, _totalTime) > 0 && _state != MP3_STATE_EOS)
+ findValidHeader();
+
+ return (_state != MP3_STATE_EOS);
+}
+
+// Seek in the stream, finding the next valid header
+void Mp3PspStream::findValidHeader() {
+ DEBUG_ENTER_FUNC();
+
+ if (_state != MP3_STATE_READY)
+ return;
+
+ // If necessary, load more data into the stream decoder
+ if (_stream.error == MAD_ERROR_BUFLEN)
+ readMP3DataIntoBuffer();
+
+ while (_state != MP3_STATE_EOS) {
+ _stream.error = MAD_ERROR_NONE;
+
+ // Decode the next header.
+ if (mad_header_decode(&_header, &_stream) == -1) {
+ if (_stream.error == MAD_ERROR_BUFLEN) {
+ readMP3DataIntoBuffer(); // Read more data
+ continue;
+ } else if (MAD_RECOVERABLE(_stream.error)) {
+ debug(6, "MP3PSPStream: Recoverable error in mad_header_decode (%s)", mad_stream_errorstr(&_stream));
+ continue;
+ } else {
+ warning("MP3PSPStream: Unrecoverable error in mad_header_decode (%s)", mad_stream_errorstr(&_stream));
+ break;
+ }
+ }
+
+ // Sum up the total playback time so far
+ mad_timer_add(&_totalTime, _header.duration);
+ break;
+ }
+
+ if (_stream.error != MAD_ERROR_NONE)
+ _state = MP3_STATE_EOS;
+}
+
+int Mp3PspStream::readBuffer(int16 *buffer, const int numSamples) {
+ DEBUG_ENTER_FUNC();
+
+ int samples = 0;
+#ifdef PRINT_BUFFERS
+ int16 *debugBuffer = buffer;
+#endif
+
+ // Keep going as long as we have input available
+ while (samples < numSamples && _state != MP3_STATE_EOS) {
+ const int len = MIN(numSamples, samples + (int)(_pcmLength - _posInFrame) * MAD_NCHANNELS(&_header));
+
+ while (samples < len) {
+ *buffer++ = _pcmSamples[_posInFrame << 1];
+ samples++;
+ if (MAD_NCHANNELS(&_header) == 2) {
+ *buffer++ = _pcmSamples[(_posInFrame << 1) + 1];
+ samples++;
+ }
+ _posInFrame++; // always skip an extra sample since ME always outputs stereo
+ }
+
+ //memcpy(buffer, &_pcmSamples[_posInFrame], len << 1); // 16 bits
+ //_posInFrame += len; // next time we start from the middle
+
+ if (_posInFrame >= _pcmLength) {
+ // We used up all PCM data in the current frame -- read & decode more
+ decodeMP3Data();
+ }
+ }
+
+#ifdef PRINT_BUFFERS
+ PSP_INFO_PRINT("buffer:\n");
+ for (int i = 0; i<numSamples; i++)
+ PSP_INFO_PRINT("%d ", debugBuffer[i]);
+ PSP_INFO_PRINT("\n\n");
+#endif
+
+ return samples;
+}
+
+} // End of namespace Audio
+
+
diff --git a/backends/platform/psp/mp3.h b/backends/platform/psp/mp3.h
new file mode 100644
index 0000000000..029b3e498c
--- /dev/null
+++ b/backends/platform/psp/mp3.h
@@ -0,0 +1,121 @@
+/* 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$
+ * $Id$
+ *
+ */
+
+#ifndef SOUND_MP3_PSP_H
+#define SOUND_MP3_PSP_H
+
+#include "common/types.h"
+#include "common/scummsys.h"
+
+namespace Common {
+ class SeekableReadStream;
+}
+
+namespace Audio {
+
+class AudioStream;
+class SeekableAudioStream;
+
+class Mp3PspStream : public SeekableAudioStream {
+protected:
+ enum State {
+ MP3_STATE_INIT, // Need to init the decoder
+ MP3_STATE_READY, // ready for processing data
+ MP3_STATE_EOS // end of data reached (may need to loop)
+ };
+
+ #define MAX_SAMPLES_PER_FRAME 2048 * 2
+ int16 _pcmSamples[MAX_SAMPLES_PER_FRAME] __attribute__((aligned(64))); // samples to output PCM data into
+ byte _codecInBuffer[3072] __attribute__((aligned(64))); // the codec always needs alignment
+ unsigned long _codecParams[65]__attribute__((aligned(64))); // TODO: change to struct
+
+ Common::SeekableReadStream *_inStream;
+ DisposeAfterUse::Flag _disposeAfterUse;
+
+ uint32 _pcmLength; // how many pcm samples we have (/2 for mono)
+
+ uint _posInFrame; // position in frame
+ State _state; // what state the stream is in
+
+ Timestamp _length;
+ uint32 _sampleRate;
+
+ mad_timer_t _totalTime;
+ mad_stream _stream; //
+ mad_header _header; // This is all we need from libmad
+
+ static bool _decoderInit; // has the decoder been initialized
+ static bool _decoderFail; // has the decoder failed to load
+
+ enum {
+ BUFFER_SIZE = 5 * 8192
+ };
+
+ // This buffer contains a slab of input data
+ byte _buf[BUFFER_SIZE + MAD_BUFFER_GUARD];
+
+ void decodeMP3Data();
+ void readMP3DataIntoBuffer();
+
+ static bool loadStartAudioModule(const char *modname, int partition);
+ int initStream();
+ void findValidHeader();
+ void deinitStream();
+
+ // to init and uninit ME decoder
+ static bool initDecoder();
+ static bool stopDecoder();
+
+ // ME functions for stream
+ bool initStreamME();
+ void releaseStreamME();
+
+public:
+ Mp3PspStream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag dispose);
+ ~Mp3PspStream();
+
+ // This function avoids having to create streams when it's not possible
+ static inline bool isOkToCreateStream() {
+ if (_decoderFail) // fatal failure
+ return false;
+ if (!_decoderInit) // if we're not initialized
+ if (!initDecoder()) // check if we failed init
+ return false;
+ return true;
+ }
+
+ int readBuffer(int16 *buffer, const int numSamples);
+
+ bool endOfData() const { return _state == MP3_STATE_EOS; }
+ bool isStereo() const { return MAD_NCHANNELS(&_header) == 2; }
+ int getRate() const { return _sampleRate; }
+
+ bool seek(const Timestamp &where);
+ Timestamp getLength() const { return _length; }
+};
+
+} // End of namespace Audio
+
+#endif // #ifndef SOUND_MP3_PSP_H
diff --git a/backends/platform/psp/osys_psp.cpp b/backends/platform/psp/osys_psp.cpp
index 58d98bc219..b09d9c0c00 100644
--- a/backends/platform/psp/osys_psp.cpp
+++ b/backends/platform/psp/osys_psp.cpp
@@ -37,6 +37,7 @@
#include "backends/platform/psp/psppixelformat.h"
#include "backends/platform/psp/osys_psp.h"
#include "backends/platform/psp/powerman.h"
+#include "backends/platform/psp/rtc.h"
#include "backends/saves/psp/psp-saves.h"
#include "backends/timer/default/default-timer.h"
@@ -48,7 +49,6 @@
#include "backends/platform/psp/trace.h"
-
#define SAMPLES_PER_SEC 44100
static int timer_handler(int t) {
@@ -57,10 +57,6 @@ static int timer_handler(int t) {
return t;
}
-void OSystem_PSP::initSDL() {
- SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER);
-}
-
OSystem_PSP::~OSystem_PSP() {}
#define PSP_SCREEN_WIDTH 480
@@ -69,6 +65,9 @@ OSystem_PSP::~OSystem_PSP() {}
void OSystem_PSP::initBackend() {
DEBUG_ENTER_FUNC();
+ // Instantiate real time clock
+ PspRtc::instance();
+
_cursor.enableCursorPalette(false);
_cursor.setXY(PSP_SCREEN_WIDTH >> 1, PSP_SCREEN_HEIGHT >> 1); // Mouse in the middle of the screen
@@ -84,8 +83,6 @@ void OSystem_PSP::initBackend() {
_inputHandler.setKeyboard(&_keyboard);
_inputHandler.init();
- initSDL();
-
_savefile = new PSPSaveFileManager;
_timer = new DefaultTimerManager();
@@ -122,11 +119,13 @@ int OSystem_PSP::getDefaultGraphicsMode() const {
bool OSystem_PSP::setGraphicsMode(int mode) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
return _displayManager.setGraphicsMode(mode);
}
bool OSystem_PSP::setGraphicsMode(const char *name) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
return _displayManager.setGraphicsMode(name);
}
@@ -141,7 +140,7 @@ Graphics::PixelFormat OSystem_PSP::getScreenFormat() const {
return _screen.getScummvmPixelFormat();
}
-Common::List<Graphics::PixelFormat> OSystem_PSP::getSupportedFormats() {
+Common::List<Graphics::PixelFormat> OSystem_PSP::getSupportedFormats() const {
return _displayManager.getSupportedPixelFormats();
}
@@ -149,6 +148,7 @@ Common::List<Graphics::PixelFormat> OSystem_PSP::getSupportedFormats() {
void OSystem_PSP::initSize(uint width, uint height, const Graphics::PixelFormat *format) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
_displayManager.setSizeAndPixelFormat(width, height, format);
_cursor.setVisible(false);
@@ -167,6 +167,7 @@ int16 OSystem_PSP::getHeight() {
void OSystem_PSP::setPalette(const byte *colors, uint start, uint num) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
_screen.setPartialPalette(colors, start, num);
_cursor.setScreenPalette(colors, start, num);
_cursor.clearKeyColor();
@@ -174,6 +175,7 @@ void OSystem_PSP::setPalette(const byte *colors, uint start, uint num) {
void OSystem_PSP::setCursorPalette(const byte *colors, uint start, uint num) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
_cursor.setCursorPalette(colors, start, num);
_cursor.enableCursorPalette(true);
_cursor.clearKeyColor(); // Do we need this?
@@ -181,37 +183,43 @@ void OSystem_PSP::setCursorPalette(const byte *colors, uint start, uint num) {
void OSystem_PSP::disableCursorPalette(bool disable) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
_cursor.enableCursorPalette(!disable);
}
void OSystem_PSP::copyRectToScreen(const byte *buf, int pitch, int x, int y, int w, int h) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
_screen.copyFromRect(buf, pitch, x, y, w, h);
}
Graphics::Surface *OSystem_PSP::lockScreen() {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
return _screen.lockAndGetForEditing();
}
void OSystem_PSP::unlockScreen() {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
// The screen is always completely updated anyway, so we don't have to force a full update here.
_screen.unlock();
}
void OSystem_PSP::updateScreen() {
DEBUG_ENTER_FUNC();
- _displayManager.renderAll();
+ _pendingUpdate = !_displayManager.renderAll(); // if we didn't update, we have a pending update
}
void OSystem_PSP::setShakePos(int shakeOffset) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
_screen.setShakePos(shakeOffset);
}
void OSystem_PSP::showOverlay() {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
_overlay.setVisible(true);
_cursor.setLimits(_overlay.getWidth(), _overlay.getHeight());
_cursor.useGlobalScaler(false); // mouse with overlay is 1:1
@@ -219,6 +227,7 @@ void OSystem_PSP::showOverlay() {
void OSystem_PSP::hideOverlay() {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
_overlay.setVisible(false);
_cursor.setLimits(_screen.getWidth(), _screen.getHeight());
_cursor.useGlobalScaler(true); // mouse needs to be scaled with screen
@@ -226,6 +235,7 @@ void OSystem_PSP::hideOverlay() {
void OSystem_PSP::clearOverlay() {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
_overlay.clearBuffer();
}
@@ -236,6 +246,7 @@ void OSystem_PSP::grabOverlay(OverlayColor *buf, int pitch) {
void OSystem_PSP::copyRectToOverlay(const OverlayColor *buf, int pitch, int x, int y, int w, int h) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
_overlay.copyFromRect(buf, pitch, x, y, w, h);
}
@@ -254,6 +265,8 @@ void OSystem_PSP::grabPalette(byte *colors, uint start, uint num) {
bool OSystem_PSP::showMouse(bool v) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
+
PSP_DEBUG_PRINT("%s\n", v ? "true" : "false");
bool last = _cursor.isVisible();
_cursor.setVisible(v);
@@ -263,11 +276,14 @@ bool OSystem_PSP::showMouse(bool v) {
void OSystem_PSP::warpMouse(int x, int y) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
_cursor.setXY(x, y);
}
void OSystem_PSP::setMouseCursor(const byte *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, int cursorTargetScale, const Graphics::PixelFormat *format) {
DEBUG_ENTER_FUNC();
+ _pendingUpdate = false;
+
PSP_DEBUG_PRINT("pbuf[%p], w[%u], h[%u], hotspot:X[%d], Y[%d], keycolor[%d], scale[%d], pformat[%p]\n", buf, w, h, hotspotX, hotspotY, keycolor, cursorTargetScale, format);
if (format) {
PSP_DEBUG_PRINT("format: bpp[%d], rLoss[%d], gLoss[%d], bLoss[%d], aLoss[%d], rShift[%d], gShift[%d], bShift[%d], aShift[%d]\n", format->bytesPerPixel, format->rLoss, format->gLoss, format->bLoss, format->aLoss, format->rShift, format->gShift, format->bShift, format->aShift);
@@ -287,39 +303,54 @@ bool OSystem_PSP::pollEvent(Common::Event &event) {
// Pausing the engine is a necessary fix for games that use the timer for music synchronization
// recovering many hours later causes the game to crash. We're polling without mutexes since it's not critical to
// get it right now.
-
PowerMan.pollPauseEngine();
+ // A hack:
+ // Check if we have a pending update that we missed for some reason (FPS throttling for example)
+ // Time between event polls is usually 5-10ms, so waiting for 4 calls before checking to update the screen should be fine
+ if (_pendingUpdate) {
+ _pendingUpdateCounter++;
+
+ if (_pendingUpdateCounter >= 4) {
+ PSP_DEBUG_PRINT("servicing pending update\n");
+ updateScreen();
+ if (!_pendingUpdate) // we handled the update
+ _pendingUpdateCounter = 0;
+ }
+ } else
+ _pendingUpdateCounter = 0; // reset the counter, no pending
+
return _inputHandler.getAllInputs(event);
}
-
uint32 OSystem_PSP::getMillis() {
- return SDL_GetTicks();
+ return PspRtc::instance().getMillis();
}
void OSystem_PSP::delayMillis(uint msecs) {
- SDL_Delay(msecs);
+ PspThread::delayMillis(msecs);
}
void OSystem_PSP::setTimerCallback(TimerProc callback, int interval) {
- SDL_SetTimer(interval, (SDL_TimerCallback)callback);
+ _pspTimer.setCallback((PspTimer::CallbackFunc)callback);
+ _pspTimer.setIntervalMs(interval);
+ _pspTimer.start();
}
OSystem::MutexRef OSystem_PSP::createMutex(void) {
- return (MutexRef)SDL_CreateMutex();
+ return (MutexRef) new PspMutex(true); // start with a full mutex
}
void OSystem_PSP::lockMutex(MutexRef mutex) {
- SDL_mutexP((SDL_mutex *)mutex);
+ ((PspMutex *)mutex)->lock();
}
void OSystem_PSP::unlockMutex(MutexRef mutex) {
- SDL_mutexV((SDL_mutex *)mutex);
+ ((PspMutex *)mutex)->unlock();
}
void OSystem_PSP::deleteMutex(MutexRef mutex) {
- SDL_DestroyMutex((SDL_mutex *)mutex);
+ delete (PspMutex *)mutex;
}
void OSystem_PSP::mixCallback(void *sys, byte *samples, int len) {
@@ -331,8 +362,6 @@ void OSystem_PSP::mixCallback(void *sys, byte *samples, int len) {
}
void OSystem_PSP::setupMixer(void) {
- SDL_AudioSpec desired;
- SDL_AudioSpec obtained;
// Determine the desired output sampling frequency.
uint32 samplesPerSec = 0;
@@ -349,38 +378,21 @@ void OSystem_PSP::setupMixer(void) {
while (samples * 16 > samplesPerSec * 2)
samples >>= 1;
- memset(&desired, 0, sizeof(desired));
- desired.freq = samplesPerSec;
- desired.format = AUDIO_S16SYS;
- desired.channels = 2;
- desired.samples = samples;
- desired.callback = mixCallback;
- desired.userdata = this;
-
assert(!_mixer);
- if (SDL_OpenAudio(&desired, &obtained) != 0) {
- warning("Could not open audio: %s", SDL_GetError());
- _mixer = new Audio::MixerImpl(this, samplesPerSec);
- assert(_mixer);
- _mixer->setReady(false);
- } else {
- // Note: This should be the obtained output rate, but it seems that at
- // least on some platforms SDL will lie and claim it did get the rate
- // even if it didn't. Probably only happens for "weird" rates, though.
- samplesPerSec = obtained.freq;
-
- // Create the mixer instance and start the sound processing
- _mixer = new Audio::MixerImpl(this, samplesPerSec);
- assert(_mixer);
- _mixer->setReady(true);
-
- SDL_PauseAudio(0);
+
+ if (!_audio.open(samplesPerSec, 2, samples, mixCallback, this)) {
+ PSP_ERROR("failed to open audio\n");
+ return;
}
+ samplesPerSec = _audio.getFrequency(); // may have been changed by audio system
+ _mixer = new Audio::MixerImpl(this, samplesPerSec);
+ assert(_mixer);
+ _mixer->setReady(true);
+ _audio.unpause();
}
void OSystem_PSP::quit() {
- SDL_CloseAudio();
- SDL_Quit();
+ _audio.close();
sceKernelExitGame();
}
diff --git a/backends/platform/psp/osys_psp.h b/backends/platform/psp/osys_psp.h
index 8c5b40dcdf..5721296c94 100644
--- a/backends/platform/psp/osys_psp.h
+++ b/backends/platform/psp/osys_psp.h
@@ -38,8 +38,9 @@
#include "backends/platform/psp/pspkeyboard.h"
#include "backends/platform/psp/display_manager.h"
#include "backends/platform/psp/input.h"
-
-#include <SDL.h>
+#include "backends/platform/psp/audio.h"
+#include "backends/timer/psp/timer.h"
+#include "backends/platform/psp/thread.h"
class OSystem_PSP : public BaseBackend {
private:
@@ -47,6 +48,8 @@ private:
Common::SaveFileManager *_savefile;
Audio::MixerImpl *_mixer;
Common::TimerManager *_timer;
+ bool _pendingUpdate; // save an update we couldn't perform
+ uint32 _pendingUpdateCounter; // prevent checking for pending update too often, in a cheap way
// All needed sub-members
Screen _screen;
@@ -55,11 +58,11 @@ private:
DisplayManager _displayManager;
PSPKeyboard _keyboard;
InputHandler _inputHandler;
-
- void initSDL();
+ PspAudio _audio;
+ PspTimer _pspTimer;
public:
- OSystem_PSP() : _savefile(0), _mixer(0), _timer(0) {}
+ OSystem_PSP() : _savefile(0), _mixer(0), _timer(0), _pendingUpdate(false), _pendingUpdateCounter(0) {}
~OSystem_PSP();
static OSystem *instance();
@@ -79,7 +82,7 @@ public:
int getGraphicsMode() const;
#ifdef USE_RGB_COLOR
virtual Graphics::PixelFormat getScreenFormat() const;
- virtual Common::List<Graphics::PixelFormat> getSupportedFormats();
+ virtual Common::List<Graphics::PixelFormat> getSupportedFormats() const;
#endif
// Screen size
diff --git a/backends/platform/psp/powerman.cpp b/backends/platform/psp/powerman.cpp
index df8da12f6d..eaadad16c5 100644
--- a/backends/platform/psp/powerman.cpp
+++ b/backends/platform/psp/powerman.cpp
@@ -23,16 +23,16 @@
*
*/
-//#define __PSP_DEBUG_FUNCS__ /* can put this locally too */
-//#define __PSP_DEBUG_PRINT__
-#include "backends/platform/psp/trace.h"
-
#include <psppower.h>
#include <pspthreadman.h>
#include "backends/platform/psp/powerman.h"
#include "engine.h"
+//#define __PSP_DEBUG_FUNCS__ /* can put this locally too */
+//#define __PSP_DEBUG_PRINT__
+#include "backends/platform/psp/trace.h"
+
DECLARE_SINGLETON(PowerManager)
// Function to debug the Power Manager (we have no output to screen)
@@ -47,68 +47,30 @@ inline void PowerManager::debugPM() {
* Constructor
*
********************************************/
-PowerManager::PowerManager() {
- DEBUG_ENTER_FUNC();
-
- _flagMutex = NULL; /* Init mutex handle */
- _listMutex = NULL; /* Init mutex handle */
- _condSuspendable = NULL; /* Init condition variable */
- _condPM = NULL;
-
- _condSuspendable = SDL_CreateCond();
- if (_condSuspendable <= 0) {
- PSP_ERROR("Couldn't create Suspendable condition variable\n");
- }
-
- _condPM = SDL_CreateCond();
- if (_condPM <= 0) {
- PSP_ERROR("Couldn't create PM condition variable\n");
- }
-
- _flagMutex = SDL_CreateMutex();
- if (_flagMutex <= 0) {
- PSP_ERROR("Couldn't create flag Mutex\n");
- }
-
- _listMutex = SDL_CreateMutex();
- if (_listMutex <= 0) {
- PSP_ERROR("Couldn't create list Mutex\n");
- }
-
- _suspendFlag = false;
- _criticalCounter = 0; // How many are in the critical section
- _pauseFlag = 0;
- _pauseFlagOld = 0;
- _pauseClientState = 0;
- _listCounter = 0;
- PMStatusSet(kInitDone);
- _error = 0;
-}
+PowerManager::PowerManager() : _pauseFlag(false), _pauseFlagOld(false), _pauseClientState(UNPAUSED),
+ _suspendFlag(false), _flagMutex(true), _listMutex(true),
+ _criticalCounter(0), _listCounter(0), _error(0), _PMStatus(kInitDone) {}
/*******************************************
*
* Function to register to be notified when suspend/resume time comes
*
********************************************/
-int PowerManager::registerSuspend(Suspendable *item) {
+bool PowerManager::registerForSuspend(Suspendable *item) {
DEBUG_ENTER_FUNC();
// Register in list
debugPM();
- if (SDL_mutexP(_listMutex) != 0) {
- PSP_ERROR("Couldn't lock _listMutex[%p]\n", _listMutex);
- }
+ _listMutex.lock();
_suspendList.push_front(item);
_listCounter++;
- if (SDL_mutexV(_listMutex) != 0) {
- PSP_ERROR("Couldn't unlock _listMutex[%p]\n", _listMutex);
- }
+ _listMutex.unlock();
debugPM();
- return 0;
+ return true;
}
/*******************************************
@@ -116,26 +78,20 @@ int PowerManager::registerSuspend(Suspendable *item) {
* Function to unregister to be notified when suspend/resume time comes
*
********************************************/
-int PowerManager::unregisterSuspend(Suspendable *item) {
+bool PowerManager::unregisterForSuspend(Suspendable *item) {
DEBUG_ENTER_FUNC();
debugPM();
// Unregister from stream list
- if (SDL_mutexP(_listMutex) != 0) {
- PSP_ERROR("Couldn't unlock _listMutex[%p]\n", _listMutex);
- }
-
+ _listMutex.lock();
+
_suspendList.remove(item);
_listCounter--;
- if (SDL_mutexV(_listMutex) != 0) {
- PSP_ERROR("Couldn't unlock _listMutex[%p]\n", _listMutex);
- }
+ _listMutex.unlock();
- PSP_DEBUG_PRINT("Out of unregisterSuspend\n");
debugPM();
-
- return 0;
+ return true;
}
/*******************************************
@@ -144,21 +100,7 @@ int PowerManager::unregisterSuspend(Suspendable *item) {
*
********************************************/
PowerManager::~PowerManager() {
- DEBUG_ENTER_FUNC();
-
- PMStatusSet(kDestroyPM);
-
- SDL_DestroyCond(_condSuspendable);
- _condSuspendable = 0;
-
- SDL_DestroyCond(_condPM);
- _condPM = 0;
-
- SDL_DestroyMutex(_flagMutex);
- _flagMutex = 0;
-
- SDL_DestroyMutex(_listMutex);
- _listMutex = 0;
+ _PMStatus = kDestroyPM;
}
/*******************************************
@@ -171,114 +113,92 @@ PowerManager::~PowerManager() {
*
********************************************/
void PowerManager::pollPauseEngine() {
-
+ DEBUG_ENTER_FUNC();
+
+
bool pause = _pauseFlag; // We copy so as not to have multiple values
- if ((pause != _pauseFlagOld) && g_engine) { // Check to see if we have an engine
- if (pause && _pauseClientState == PowerManager::Unpaused) {
- _pauseClientState = PowerManager::Pausing; // Tell PM we're in the middle of pausing
- g_engine->pauseEngine(true);
- PSP_DEBUG_PRINT_FUNC("Pausing engine\n");
- _pauseClientState = PowerManager::Paused; // Tell PM we're done pausing
- } else if (!pause && _pauseClientState == PowerManager::Paused) {
- g_engine->pauseEngine(false);
- PSP_DEBUG_PRINT_FUNC("Unpausing for resume\n");
- _pauseClientState = PowerManager::Unpaused; // Tell PM we're in the middle of pausing
+ if (pause != _pauseFlagOld) {
+ if (g_engine) { // Check to see if we have an engine
+ if (pause && _pauseClientState == UNPAUSED) {
+ _pauseClientState = PAUSING; // Tell PM we're in the middle of pausing
+ g_engine->pauseEngine(true);
+ PSP_DEBUG_PRINT_FUNC("Pausing engine\n");
+ _pauseClientState = PAUSED; // Tell PM we're done pausing
+ } else if (!pause && _pauseClientState == PAUSED) {
+ g_engine->pauseEngine(false);
+ PSP_DEBUG_PRINT_FUNC("Unpausing for resume\n");
+ _pauseClientState = UNPAUSED; // Tell PM we're unpaused
+ }
}
-
_pauseFlagOld = pause;
}
}
/*******************************************
*
-* Function to be called by threads wanting to block on the PSP entering suspend
-* Use this for small critical sections where you can easily restore the previous state.
-*
-********************************************/
-int PowerManager::blockOnSuspend() {
- return beginCriticalSection(true);
-}
-
-/*******************************************
-*
* Function to block on a suspend, then start a non-suspendable critical section
* Use this for large or REALLY critical critical-sections.
* Make sure to call endCriticalSection or the PSP won't suspend.
+* returns true if blocked, false if not blocked
********************************************/
-int PowerManager::beginCriticalSection(bool justBlock) {
+bool PowerManager::beginCriticalSection() {
DEBUG_ENTER_FUNC();
- int ret = NotBlocked;
-
- if (SDL_mutexP(_flagMutex) != 0) {
- PSP_ERROR("PowerManager::blockOnSuspend(): Couldn't lock flagMutex[%p]\n", _flagMutex);
- ret = Error;
- }
+ bool ret = false;
+
+ _flagMutex.lock();
// Check the access flag
- if (_suspendFlag == true) {
- PSP_DEBUG_PRINT("We're being blocked!\n");
- debugPM();
- ret = Blocked;
+ if (_suspendFlag) {
+ ret = true;
- // If it's true, we wait for a signal to continue
- if (SDL_CondWait(_condSuspendable, _flagMutex) != 0) {
- PSP_DEBUG_PRINT("PowerManager::blockOnSuspend(): Couldn't wait on cond[%p]\n", _condSuspendable);
- }
+ PSP_DEBUG_PRINT("I got blocked. ThreadId[%x]\n", sceKernelGetThreadId());
+ debugPM();
+
+ _threadSleep.wait(_flagMutex);
- PSP_DEBUG_PRINT("We got blocked!!\n");
+ PSP_DEBUG_PRINT_FUNC("I got released. ThreadId[%x]\n", sceKernelGetThreadId());
debugPM();
}
// Now prevent the PM from suspending until we're done
- if (justBlock == false)
- _criticalCounter++;
+ _criticalCounter++;
- if (SDL_mutexV(_flagMutex) != 0) {
- PSP_ERROR("PowerManager::blockOnSuspend(): Couldn't unlock flagMutex[%p]\n", _flagMutex);
- ret = Error;
- }
+ _flagMutex.unlock();
return ret;
}
-int PowerManager::endCriticalSection() {
+// returns success = true
+void PowerManager::endCriticalSection() {
DEBUG_ENTER_FUNC();
- int ret = 0;
- if (SDL_mutexP(_flagMutex) != 0) {
- PSP_ERROR("PowerManager::endCriticalSection(): Couldn't lock flagMutex[%p]\n", _flagMutex);
- ret = Error;
- }
+ _flagMutex.lock();
// We're done with our critical section
_criticalCounter--;
if (_criticalCounter <= 0) {
- if (_suspendFlag == true) { // If the PM is sleeping, this flag must be set
- PSP_DEBUG_PRINT("Unblocked thread waking up the PM.\n");
- debugPM();
-
- SDL_CondBroadcast(_condPM);
-
- PSP_DEBUG_PRINT("Woke up the PM\n");
- debugPM();
+ if (_suspendFlag) { // If the PM is sleeping, this flag must be set
+ PSP_DEBUG_PRINT_FUNC("PM is asleep. Waking it up.\n");
+ debugPM();
+
+ _pmSleep.releaseAll();
+
+ PSP_DEBUG_PRINT_FUNC("Woke up the PM\n");
+
+ debugPM();
}
if (_criticalCounter < 0) { // Check for bad usage of critical sections
- PSP_ERROR("Critical counter[%d]\n", _criticalCounter);
+ PSP_ERROR("Critical counter[%d]!!!\n", _criticalCounter);
debugPM();
}
}
- if (SDL_mutexV(_flagMutex) != 0) {
- PSP_ERROR("Couldn't unlock flagMutex[%p]\n", _flagMutex);
- ret = Error;
- }
-
- return ret;
+ _flagMutex.unlock();
}
/*******************************************
@@ -286,90 +206,77 @@ int PowerManager::endCriticalSection() {
* Callback function to be called to put every Suspendable to suspend
*
********************************************/
-int PowerManager::suspend() {
+void PowerManager::suspend() {
DEBUG_ENTER_FUNC();
- int ret = 0;
- if (_pauseFlag) return ret; // Very important - make sure we only suspend once
+ if (_pauseFlag)
+ return; // Very important - make sure we only suspend once
- scePowerLock(0); // Critical to make sure PSP doesn't suspend before we're done
+ scePowerLock(0); // Also critical to make sure PSP doesn't suspend before we're done
// The first stage of suspend is pausing the engine if possible. We don't want to cause files
// to block, or we might not get the engine to pause. On the other hand, we might wait for polling
// and it'll never happen. We also want to do this w/o mutexes (for speed) which is ok in this case.
_pauseFlag = true;
- PMStatusSet(kWaitForClientPause);
+ _PMStatus = kWaitForClientPause;
// Now we wait, giving the engine thread some time to find our flag.
- for (int i = 0; i < 10 && _pauseClientState == Unpaused; i++)
- sceKernelDelayThread(50000); // We wait 50 msec x 10 times = 0.5 seconds
+ for (int i = 0; i < 10 && _pauseClientState == UNPAUSED; i++)
+ PspThread::delayMicros(50000); // We wait 50 msec x 10 times = 0.5 seconds
- if (_pauseClientState == Pausing) { // Our event has been acknowledged. Let's wait until the client is done.
- PMStatusSet(kWaitForClientToFinishPausing);
+ if (_pauseClientState == PAUSING) { // Our event has been acknowledged. Let's wait until the client is done.
+ _PMStatus = kWaitForClientToFinishPausing;
- while (_pauseClientState != Paused)
- sceKernelDelayThread(50000); // We wait 50 msec at a time
+ while (_pauseClientState != PAUSED)
+ PspThread::delayMicros(50000); // We wait 50 msec at a time
}
- // It's possible that the polling thread missed our pause event, but there's nothing we can do about that.
- // We can't know if there's polling going on or not. It's usually not a critical thing anyway.
+ // It's possible that the polling thread missed our pause event, but there's
+ // nothing we can do about that.
+ // We can't know if there's polling going on or not.
+ // It's usually not a critical thing anyway.
- PMStatusSet(kGettingFlagMutexSuspend);
+ _PMStatus = kGettingFlagMutexSuspend;
// Now we set the suspend flag to true to cause reading threads to block
+ _flagMutex.lock();
- if (SDL_mutexP(_flagMutex) != 0) {
- PSP_ERROR("Couldn't lock flagMutex[%p]\n", _flagMutex);
- _error = Error;
- ret = Error;
- }
-
- PMStatusSet(kGotFlagMutexSuspend);
+ _PMStatus = kGotFlagMutexSuspend;
_suspendFlag = true;
// Check if anyone is in a critical section. If so, we'll wait for them
if (_criticalCounter > 0) {
- PMStatusSet(kWaitCritSectionSuspend);
- SDL_CondWait(_condPM, _flagMutex);
- PMStatusSet(kDoneWaitingCritSectionSuspend);
- }
+ _PMStatus = kWaitCritSectionSuspend;
+
+ _pmSleep.wait(_flagMutex);
+
+ _PMStatus = kDoneWaitingCritSectionSuspend;
+ }
+
+ _flagMutex.unlock();
- if (SDL_mutexV(_flagMutex) != 0) {
- PSP_ERROR("Couldn't unlock flagMutex[%p]\n", _flagMutex);
- _error = Error;
- ret = Error;
- }
-
- PMStatusSet(kGettingListMutexSuspend);
+ _PMStatus = kGettingListMutexSuspend;
// Loop over list, calling suspend()
- if (SDL_mutexP(_listMutex) != 0) {
- PSP_ERROR("Couldn't lock listMutex[%p]\n", _listMutex);
- _error = Error;
- ret = Error;
- }
- PMStatusSet(kIteratingListSuspend);
+ _listMutex.lock();
+
+ _PMStatus = kIteratingListSuspend;
// Iterate
Common::List<Suspendable *>::iterator i;
for (i = _suspendList.begin(); i != _suspendList.end(); ++i) {
(*i)->suspend();
}
+ _PMStatus = kDoneIteratingListSuspend;
- PMStatusSet(kDoneIteratingListSuspend);
-
- if (SDL_mutexV(_listMutex) != 0) {
- PSP_ERROR("Couldn't unlock listMutex[%p]\n", _listMutex);
- _error = Error;
- ret = Error;
- }
- PMStatusSet(kDoneSuspend);
+ _listMutex.unlock();
+ _PMStatus = kDoneSuspend;
scePowerUnlock(0); // Allow the PSP to go to sleep now
-
- return ret;
+
+ _PMStatus = kDonePowerUnlock;
}
/*******************************************
@@ -377,24 +284,26 @@ int PowerManager::suspend() {
* Callback function to resume every Suspendable
*
********************************************/
-int PowerManager::resume() {
+void PowerManager::resume() {
DEBUG_ENTER_FUNC();
- int ret = 0;
+
+ _PMStatus = kBeginResume;
// Make sure we can't get another suspend
scePowerLock(0);
- if (!_pauseFlag) return ret; // Make sure we can only resume once
+ _PMStatus = kCheckingPauseFlag;
+
+ if (!_pauseFlag)
+ return; // Make sure we can only resume once
- PMStatusSet(kGettingListMutexResume);
+ _PMStatus = kGettingListMutexResume;
// First we notify our Suspendables. Loop over list, calling resume()
- if (SDL_mutexP(_listMutex) != 0) {
- PSP_ERROR("Couldn't lock listMutex[%p]\n", _listMutex);
- _error = Error;
- ret = Error;
- }
- PMStatusSet(kIteratingListResume);
+ _listMutex.lock();
+
+ _PMStatus = kIteratingListResume;
+
// Iterate
Common::List<Suspendable *>::iterator i = _suspendList.begin();
@@ -402,46 +311,31 @@ int PowerManager::resume() {
(*i)->resume();
}
- PMStatusSet(kDoneIteratingListResume);
+ _PMStatus = kDoneIteratingListResume;
- if (SDL_mutexV(_listMutex) != 0) {
- PSP_ERROR("Couldn't unlock listMutex[%p]\n", _listMutex);
- _error = Error;
- ret = Error;
- }
-
- PMStatusSet(kGettingFlagMutexResume);
+ _listMutex.unlock();
+
+ _PMStatus = kGettingFlagMutexResume;
// Now we set the suspend flag to false
- if (SDL_mutexP(_flagMutex) != 0) {
- PSP_ERROR("Couldn't lock flagMutex %p\n", _flagMutex);
- _error = Error;
- ret = Error;
- }
- PMStatusSet(kGotFlagMutexResume);
+ _flagMutex.lock();
+
+ _PMStatus = kGotFlagMutexResume;
_suspendFlag = false;
- PMStatusSet(kSignalSuspendedThreadsResume);
+ _PMStatus = kSignalSuspendedThreadsResume;
- // Signal the other threads to wake up
- if (SDL_CondBroadcast(_condSuspendable) != 0) {
- PSP_ERROR("Couldn't broadcast condition[%p]\n", _condSuspendable);
- _error = Error;
- ret = Error;
- }
- PMStatusSet(kDoneSignallingSuspendedThreadsResume);
+ // Signal the threads to wake up
+ _threadSleep.releaseAll();
+
+ _PMStatus = kDoneSignallingSuspendedThreadsResume;
- if (SDL_mutexV(_flagMutex) != 0) {
- PSP_ERROR("Couldn't unlock flagMutex[%p]\n", _flagMutex);
- _error = Error;
- ret = Error;
- }
- PMStatusSet(kDoneResume);
+ _flagMutex.unlock();
+
+ _PMStatus = kDoneResume;
- _pauseFlag = false; // Signal engine to unpause
+ _pauseFlag = false; // Signal engine to unpause -- no mutex needed
scePowerUnlock(0); // Allow new suspends
-
- return ret;
}
diff --git a/backends/platform/psp/powerman.h b/backends/platform/psp/powerman.h
index af3134adee..5f09bc7794 100644
--- a/backends/platform/psp/powerman.h
+++ b/backends/platform/psp/powerman.h
@@ -26,8 +26,7 @@
#ifndef POWERMAN_H
#define POWERMAN_H
-#include <SDL/SDL_thread.h>
-#include <SDL/SDL_mutex.h>
+#include "backends/platform/psp/thread.h"
#include "common/singleton.h"
#include "common/list.h"
@@ -53,12 +52,12 @@ class PowerManager: public Common::Singleton<PowerManager> {
public:
int blockOnSuspend(); /* block if suspending */
- int beginCriticalSection(bool justBlock = false); /* Use a critical section to block (if suspend was already pressed) */
- int endCriticalSection(); /* and to prevent the PSP from suspending in a particular section */
- int registerSuspend(Suspendable *item); /* register to be called to suspend/resume */
- int unregisterSuspend(Suspendable *item); /* remove from suspend/resume list */
- int suspend(); /* callback to have all items in list suspend */
- int resume(); /* callback to have all items in list resume */
+ bool beginCriticalSection(); /* Use a critical section to block (if suspend was already pressed) */
+ void endCriticalSection(); /* and to prevent the PSP from suspending in a particular section */
+ bool registerForSuspend(Suspendable *item); /* register to be called to suspend/resume */
+ bool unregisterForSuspend(Suspendable *item); /* remove from suspend/resume list */
+ void suspend(); /* callback to have all items in list suspend */
+ void resume(); /* callback to have all items in list resume */
// Functions for pausing the engine
void pollPauseEngine(); /* Poll whether the engine should be paused */
@@ -69,11 +68,9 @@ public:
};
enum PauseState {
- Unpaused = 0,
- PauseEvent,
- UnpauseEvent,
- Pausing,
- Paused
+ UNPAUSED = 0,
+ PAUSING,
+ PAUSED
};
private:
@@ -81,34 +78,38 @@ private:
PowerManager();
~PowerManager();
- Common::List<Suspendable *> _suspendList; /* list to register in */
+ Common::List<Suspendable *> _suspendList; // list to register in
- volatile bool _pauseFlag; /* For pausing, which is before suspending */
- volatile bool _pauseFlagOld; /* Save the last state of the flag while polling */
- volatile int _pauseClientState; /* Pause state of the target */
+ volatile bool _pauseFlag; // For pausing, which is before suspending
+ volatile bool _pauseFlagOld; // Save the last state of the flag while polling
+ volatile PauseState _pauseClientState; // Pause state of the target
- volatile bool _suspendFlag; /* protected variable */
- SDL_mutex *_flagMutex; /* mutex to access access flag */
- SDL_mutex *_listMutex; /* mutex to access Suspendable list */
- SDL_cond *_condSuspendable; /* signal to synchronize accessing threads */
- SDL_cond *_condPM; /* signal to wake up the PM from a critical section */
- volatile int _criticalCounter; /* Counter of how many threads are in a critical section */
- int _error; /* error code - PM can't talk to us. For debugging */
+ volatile bool _suspendFlag; // protected variable
+ PspMutex _flagMutex; // mutex to access access flag
+ PspMutex _listMutex; // mutex to access Suspendable list
+ PspCondition _threadSleep; // signal to synchronize accessing threads
+ PspCondition _pmSleep; // signal to wake up the PM from a critical section
+ volatile int _criticalCounter; // Counter of how many threads are in a critical section
+ int _error; // error code - PM can't talk to us. For debugging
+ volatile int _PMStatus; // What the PM is doing. for debugging
// States for PM to be in (used for debugging)
enum PMState {
- kInitDone = 1 ,
- kDestroyPM,
- kWaitForClientPause,
- kWaitForClientToFinishPausing,
- kGettingFlagMutexSuspend,
- kGotFlagMutexSuspend,
- kWaitCritSectionSuspend,
- kDoneWaitingCritSectionSuspend,
- kGettingListMutexSuspend,
- kIteratingListSuspend,
- kDoneIteratingListSuspend,
- kDoneSuspend,
+ kInitDone = 1,
+ kDestroyPM = 2,
+ kWaitForClientPause = 3,
+ kWaitForClientToFinishPausing = 4,
+ kGettingFlagMutexSuspend = 5,
+ kGotFlagMutexSuspend = 6,
+ kWaitCritSectionSuspend = 7,
+ kDoneWaitingCritSectionSuspend = 8,
+ kGettingListMutexSuspend = 9,
+ kIteratingListSuspend = 10,
+ kDoneIteratingListSuspend = 11,
+ kDoneSuspend = 12,
+ kDonePowerUnlock,
+ kBeginResume,
+ kCheckingPauseFlag,
kGettingListMutexResume,
kIteratingListResume,
kDoneIteratingListResume,
@@ -122,8 +123,6 @@ private:
volatile int _listCounter; /* How many people are in the list - just for debugging */
void debugPM(); /* print info about the PM */
- void PMStatusSet(PMState s) { _PMStatus = s; }
- volatile int _PMStatus; /* What the PM is doing */
public:
int getPMStatus() const { return _PMStatus; }
diff --git a/backends/platform/psp/psp.spec b/backends/platform/psp/psp.spec
index debdab3208..ac325b7fd6 100644
--- a/backends/platform/psp/psp.spec
+++ b/backends/platform/psp/psp.spec
@@ -1,3 +1,3 @@
%rename lib old_lib
*lib:
-%(old_lib) -lz -lstdc++ -lc -lm -lpspprof -lpspvfpu -lpspdebug -lpspgu -lpspge -lpspdisplay -lpspctrl -lpspsdk -lpsputility -lpspuser -lpsppower -lpsphprm -lpsprtc -lpspaudio -lpspkernel
+%(old_lib) -lz -lstdc++ -lc -lm -lpspprof -lpspvfpu -lpspdebug -lpspgu -lpspge -lpspdisplay -lpspctrl -lpspsdk -lpsputility -lpspuser -lpsppower -lpsphprm -lpsprtc -lpspaudio -lpspaudiocodec -lpspkernel
diff --git a/backends/platform/psp/psp_main.cpp b/backends/platform/psp/psp_main.cpp
index e568184990..dba9a8fc2b 100644
--- a/backends/platform/psp/psp_main.cpp
+++ b/backends/platform/psp/psp_main.cpp
@@ -39,10 +39,12 @@
#include <base/main.h>
#include <base/plugins.h>
#include "backends/platform/psp/powerman.h"
+#include "backends/platform/psp/thread.h"
#include "backends/plugins/psp/psp-provider.h"
#include "backends/platform/psp/psppixelformat.h"
#include "backends/platform/psp/osys_psp.h"
+#include "backends/platform/psp/tests.h" /* for unit/speed tests */
#include "backends/platform/psp/trace.h"
#ifdef ENABLE_PROFILING
@@ -109,12 +111,13 @@ int exit_callback(void) {
}
/* Function for handling suspend/resume */
-void power_callback(int , int powerinfo) {
+int power_callback(int , int powerinfo, void *) {
if (powerinfo & PSP_POWER_CB_POWER_SWITCH || powerinfo & PSP_POWER_CB_SUSPENDING) {
PowerMan.suspend();
} else if (powerinfo & PSP_POWER_CB_RESUME_COMPLETE) {
PowerMan.resume();
}
+ return 0;
}
/* Callback thread */
@@ -140,7 +143,7 @@ int CallbackThread(SceSize /*size*/, void *arg) {
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
- int thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, THREAD_ATTR_USER, 0);
+ int thid = sceKernelCreateThread("power_thread", CallbackThread, PRIORITY_POWER_THREAD, STACK_POWER_THREAD, THREAD_ATTR_USER, 0);
if (thid >= 0) {
sceKernelStartThread(thid, 0, 0);
}
@@ -167,6 +170,13 @@ int main(void) {
PluginManager::instance().addPluginProvider(new PSPPluginProvider());
#endif
+/* unit/speed tests */
+#if defined (PSP_ENABLE_UNIT_TESTS) || defined (PSP_ENABLE_SPEED_TESTS)
+ PSP_INFO_PRINT("running tests\n");
+ psp_tests();
+ sceKernelSleepThread(); // that's it. That's all we're doing
+#endif
+
int res = scummvm_main(argc, argv);
g_system->quit(); // TODO: Consider removing / replacing this!
diff --git a/backends/platform/psp/rtc.cpp b/backends/platform/psp/rtc.cpp
new file mode 100644
index 0000000000..57edea7e49
--- /dev/null
+++ b/backends/platform/psp/rtc.cpp
@@ -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];
+}
+
diff --git a/backends/platform/psp/rtc.h b/backends/platform/psp/rtc.h
new file mode 100644
index 0000000000..7c1a28474d
--- /dev/null
+++ b/backends/platform/psp/rtc.h
@@ -0,0 +1,50 @@
+/* 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 \ No newline at end of file
diff --git a/backends/platform/psp/tests.cpp b/backends/platform/psp/tests.cpp
new file mode 100644
index 0000000000..d1bdb9e640
--- /dev/null
+++ b/backends/platform/psp/tests.cpp
@@ -0,0 +1,565 @@
+/* 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 46126 2009-11-24 14:18:46Z fingolfin $
+ *
+ */
+
+// PSP speed and unit tests. Activate in tests.h
+// You may also want to build without any engines.
+
+#include "backends/platform/psp/tests.h"
+
+#if defined (PSP_ENABLE_UNIT_TESTS) || defined (PSP_ENABLE_SPEED_TESTS)
+
+#include "common/scummsys.h"
+#include <pspiofilemgr_fcntl.h>
+#include <pspiofilemgr_stat.h>
+#include <pspiofilemgr.h>
+#include <pspthreadman.h>
+#include <pspsdk.h>
+#include <psprtc.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <psputils.h>
+#include "backends/platform/psp/rtc.h"
+#include "backends/platform/psp/thread.h"
+#include "backends/platform/psp/memory.h"
+
+
+#define UNCACHED(x) ((byte *)(((uint32)(x)) | 0x40000000)) /* make an uncached access */
+#define CACHED(x) ((byte *)(((uint32)(x)) & 0xBFFFFFFF)) /* make an uncached access into a cached one */
+
+//#define __PSP_DEBUG_FUNCS__
+//#define __PSP_DEBUG_PRINT__
+
+// Results: (333Mhz/222Mhz)
+// Getting a tick: 1-2 us
+// Getting a time structure: 9/14us
+// ie. using a tick and just dividing by 1000 saves us time.
+
+#include "backends/platform/psp/trace.h"
+
+class PspSpeedTests {
+public:
+ void tickSpeed();
+ void getMicrosSpeed();
+ void seekSpeed();
+ void msReadSpeed();
+ void threadFunctionsSpeed();
+ void semaphoreSpeed();
+ static int threadFunc(SceSize args, void *argp);
+ void semaphoreManyThreadSpeed();
+ void fastCopySpeed();
+
+private:
+ enum {
+ MEMCPY_BUFFER_SIZE = 8192
+ };
+ static PspSemaphore _sem; // semaphore
+
+ void readAndTime(uint32 bytes, char *buffer, FILE *file);
+ void seekAndTime(int bytes, int origin, FILE *file);
+ void fastCopySpecificSize(byte *dst, byte *src, uint32 bytes);
+ void fastCopyDifferentSizes(byte *dst, byte *src);
+ int getThreadIdSpeed();
+ void getPrioritySpeed();
+ void changePrioritySpeed(int id, int priority);
+};
+
+PspSemaphore PspSpeedTests::_sem(0);
+
+void PspSpeedTests::tickSpeed() {
+ uint32 ticksPerSecond = sceRtcGetTickResolution();
+ PSP_INFO_PRINT("ticksPerSecond[%d]\n", ticksPerSecond);
+
+ uint32 currentTicks1[2];
+ uint32 currentTicks2[2];
+
+ sceRtcGetCurrentTick((u64 *)currentTicks1);
+ sceRtcGetCurrentTick((u64 *)currentTicks2);
+ PSP_INFO_PRINT("current tick[%x %x][%u %u]\n", currentTicks1[0], currentTicks1[1], currentTicks1[0], currentTicks1[1]);
+ PSP_INFO_PRINT("current tick[%x %x][%u %u]\n", currentTicks2[0], currentTicks2[1], currentTicks2[0], currentTicks2[1]);
+
+ pspTime time;
+ sceRtcSetTick(&time, (u64 *)currentTicks2);
+ PSP_INFO_PRINT("current tick in time, year[%d] month[%d] day[%d] hour[%d] minutes[%d] seconds[%d] us[%d]\n", time.year, time.month, time.day, time.hour, time.minutes, time.seconds, time.microseconds);
+
+ pspTime time1;
+ pspTime time2;
+ sceRtcGetCurrentClockLocalTime(&time1);
+ sceRtcGetCurrentClockLocalTime(&time2);
+ PSP_INFO_PRINT("time1, year[%d] month[%d] day[%d] hour[%d] minutes[%d] seconds[%d] us[%d]\n", time1.year, time1.month, time1.day, time1.hour, time1.minutes, time1.seconds, time1.microseconds);
+ PSP_INFO_PRINT("time2, year[%d] month[%d] day[%d] hour[%d] minutes[%d] seconds[%d] us[%d]\n", time2.year, time2.month, time2.day, time2.hour, time2.minutes, time2.seconds, time2.microseconds);
+}
+
+void PspSpeedTests::getMicrosSpeed() {
+ uint32 time1, time2, time3, time4;
+ time1 = PspRtc::instance().getMicros();
+ time2 = PspRtc::instance().getMicros();
+ time3 = PspRtc::instance().getMicros();
+ time4 = PspRtc::instance().getMicros();
+
+ PSP_INFO_PRINT("getMicros() times: %d, %d, %d\n", time4-time3, time3-time2, time2-time1);
+}
+
+void PspSpeedTests::readAndTime(uint32 bytes, char *buffer, FILE *file) {
+ uint32 time1 = PspRtc::instance().getMicros();
+ // test minimal read
+ fread(buffer, bytes, 1, file);
+ uint32 time2 = PspRtc::instance().getMicros();
+
+ PSP_INFO_PRINT("Reading %d byte takes %dus\n", bytes, time2-time1);
+}
+
+/*
+ 333MHz/222MHz
+ Reading 1 byte takes 2590us / 3167
+ Reading 10 byte takes 8us / 9
+ Reading 50 byte takes 8us / 11
+ Reading 100 byte takes 8us / 11
+ Reading 1000 byte takes 915us / 1131
+ Reading 2000 byte takes 1806us / 2,284
+ Reading 3000 byte takes 2697us / 3,374
+ Reading 5000 byte takes 4551us / 5,544
+ Reading 6000 byte takes 5356us / 6,676
+ Reading 7000 byte takes 6800us / 8,358
+ Reading 8000 byte takes 6794us / 8,454
+ Reading 9000 byte takes 6782us / 8,563
+ Reading 10000 byte takes 8497us / 10,631
+ Reading 30000 byte takes 25995us / 32,473
+ Reading 80000 byte takes 68457us / 85,291
+ Reading 100000 byte takes 85103us / 106,163
+*/
+// Function to test the impact of MS reads
+// These tests can't be done from shell - the cache screws them up
+void PspSpeedTests::msReadSpeed() {
+ FILE *file;
+ file = fopen("ms0:/psp/music/track1.mp3", "r");
+
+ char *buffer = (char *)malloc(2 * 1024 * 1024);
+
+ readAndTime(1, buffer, file);
+ readAndTime(10, buffer, file);
+ readAndTime(50, buffer, file);
+ readAndTime(100, buffer, file);
+ readAndTime(1000, buffer, file);
+ readAndTime(2000, buffer, file);
+ readAndTime(3000, buffer, file);
+ readAndTime(5000, buffer, file);
+ readAndTime(6000, buffer, file);
+ readAndTime(7000, buffer, file);
+ readAndTime(8000, buffer, file);
+ readAndTime(9000, buffer, file);
+ readAndTime(10000, buffer, file);
+ readAndTime(30000, buffer, file);
+ readAndTime(50000, buffer, file);
+ readAndTime(80000, buffer, file);
+ readAndTime(100000, buffer, file);
+
+ fclose(file);
+ free(buffer);
+}
+
+void PspSpeedTests::seekAndTime(int bytes, int origin, FILE *file) {
+ char buffer[1000];
+
+ uint32 time1 = PspRtc::instance().getMicros();
+ // test minimal read
+ fseek(file, bytes, origin);
+ uint32 time2 = PspRtc::instance().getMicros();
+
+ PSP_INFO_PRINT("Seeking %d byte from %d took %dus\n", bytes, origin, time2-time1);
+
+ time1 = PspRtc::instance().getMicros();
+ // test minimal read
+ fread(buffer, 1000, 1, file);
+ time2 = PspRtc::instance().getMicros();
+
+ PSP_INFO_PRINT("Reading 1000 bytes took %dus\n", time2-time1);
+}
+
+/*
+333MHz
+Seeking 0 byte from 0 took 946us
+Reading 1000 bytes took 1781us
+Seeking 5 byte from 0 took 6us
+Reading 1000 bytes took 19us
+Seeking 1000 byte from 0 took 5us
+Reading 1000 bytes took 913us
+Seeking 100 byte from 0 took 955us
+Reading 1000 bytes took 906us
+Seeking 10000 byte from 0 took 963us
+Reading 1000 bytes took 905us
+Seeking -5 byte from 1 took 1022us
+Reading 1000 bytes took 949us
+Seeking -100 byte from 1 took 1040us
+Reading 1000 bytes took 907us
+Seeking 100 byte from 1 took 1044us
+Reading 1000 bytes took 930us
+Seeking 0 byte from 2 took 7211us
+Reading 1000 bytes took 80us
+Seeking 10000 byte from 2 took 3636us
+Reading 1000 bytes took 110us
+*/
+
+void PspSpeedTests::seekSpeed() {
+ FILE *file;
+ file = fopen("ms0:/psp/music/track1.mp3", "r");
+
+ seekAndTime(0, SEEK_SET, file);
+ seekAndTime(5, SEEK_SET, file);
+ seekAndTime(1000, SEEK_SET, file);
+ seekAndTime(100, SEEK_SET, file);
+ seekAndTime(10000, SEEK_SET, file);
+ seekAndTime(-5, SEEK_CUR, file);
+ seekAndTime(-100, SEEK_CUR, file);
+ seekAndTime(100, SEEK_CUR, file);
+ seekAndTime(0, SEEK_END, file);
+ seekAndTime(-10000, SEEK_END, file);
+
+ fclose(file);
+}
+
+// 222: 5-7us
+int PspSpeedTests::getThreadIdSpeed() {
+ uint32 time1 = PspRtc::instance().getMicros();
+ int threadId = sceKernelGetThreadId();
+ uint32 time2 = PspRtc::instance().getMicros();
+
+ PSP_INFO_PRINT("Getting thread ID %d took %dus\n", threadId, time2-time1);
+
+ return threadId;
+}
+
+// 222: 4-5us
+void PspSpeedTests::getPrioritySpeed() {
+ uint32 time1 = PspRtc::instance().getMicros();
+ int priority = sceKernelGetThreadCurrentPriority();
+ uint32 time2 = PspRtc::instance().getMicros();
+
+ PSP_INFO_PRINT("Getting thread priority %d took %dus\n", priority, time2-time1);
+}
+
+// 222: 9-10us
+void PspSpeedTests::changePrioritySpeed(int id, int priority) {
+ uint32 time1 = PspRtc::instance().getMicros();
+ sceKernelChangeThreadPriority(id, priority);
+ uint32 time2 = PspRtc::instance().getMicros();
+
+ PSP_INFO_PRINT("Changing thread priority to %d for id %d took %dus\n", priority, id, time2-time1);
+}
+
+void PspSpeedTests::threadFunctionsSpeed() {
+ // very unscientific -- just ballpark
+ int id;
+ id = getThreadIdSpeed();
+ getThreadIdSpeed();
+ getPrioritySpeed();
+ getPrioritySpeed();
+ changePrioritySpeed(id, 30);
+ changePrioritySpeed(id, 35);
+ changePrioritySpeed(id, 25);
+
+ // test context switch time
+ for (int i=0; i<10; i++) {
+ uint time1 = PspRtc::instance().getMicros();
+ PspThread::delayMicros(0);
+ uint time2 = PspRtc::instance().getMicros();
+ PSP_INFO_PRINT("poll %d. context switch Time = %dus\n", i, time2-time1); // 10-15us
+ }
+}
+
+void PspSpeedTests::semaphoreSpeed() {
+ PspSemaphore sem(1);
+
+ uint32 time1 = PspRtc::instance().getMicros();
+
+ sem.take();
+
+ uint32 time2 = PspRtc::instance().getMicros();
+
+ PSP_INFO_PRINT("taking semaphore took %d us\n", time2-time1); // 10us
+
+ uint32 time3 = PspRtc::instance().getMicros();
+
+ sem.give();
+
+ uint32 time4 = PspRtc::instance().getMicros();
+ PSP_INFO_PRINT("releasing semaphore took %d us\n", time4-time3); //10us-55us
+}
+
+int PspSpeedTests::threadFunc(SceSize args, void *argp) {
+ PSP_INFO_PRINT("thread %x created.\n", sceKernelGetThreadId());
+
+ _sem.take();
+
+ PSP_INFO_PRINT("grabbed semaphore. Quitting thread\n");
+
+ return 0;
+}
+
+void PspSpeedTests::semaphoreManyThreadSpeed() {
+
+ // create 4 threads
+ for (int i=0; i<4; i++) {
+ int thid = sceKernelCreateThread("my_thread", PspSpeedTests::threadFunc, 0x18, 0x10000, THREAD_ATTR_USER, NULL);
+ sceKernelStartThread(thid, 0, 0);
+ }
+
+ PSP_INFO_PRINT("main thread. created threads\n");
+
+ uint32 threads = _sem.numOfWaitingThreads();
+ while (threads < 4) {
+ threads = _sem.numOfWaitingThreads();
+ PSP_INFO_PRINT("main thread: waiting threads[%d]\n", threads);
+ }
+
+ PSP_INFO_PRINT("main: semaphore value[%d]\n", _sem.getValue());
+ PSP_INFO_PRINT("main thread: waiting threads[%d]\n", _sem.numOfWaitingThreads());
+
+ _sem.give(4);
+}
+
+void PspSpeedTests::fastCopySpecificSize(byte *dst, byte *src, uint32 bytes) {
+ uint32 time1, time2;
+ uint32 fastcopyTime, memcpyTime;
+ const int iterations = 2000;
+ int intc;
+
+ intc = pspSdkDisableInterrupts();
+
+ time1 = PspRtc::instance().getMicros();
+ for (int i=0; i<iterations; i++) {
+ PspMemory::fastCopy(dst, src, bytes);
+ }
+ time2 = PspRtc::instance().getMicros();
+
+ pspSdkEnableInterrupts(intc);
+
+ fastcopyTime = time2-time1;
+
+ intc = pspSdkDisableInterrupts();
+
+ time1 = PspRtc::instance().getMicros();
+ for (int i=0; i<iterations; i++) {
+ memcpy(dst, src, bytes);
+ }
+ time2 = PspRtc::instance().getMicros();
+
+ pspSdkEnableInterrupts(intc);
+
+ memcpyTime = time2-time1;
+
+ PSP_INFO_PRINT("%d bytes. memcpy[%d], fastcopy[%d]\n", bytes, memcpyTime, fastcopyTime);
+}
+
+void PspSpeedTests::fastCopyDifferentSizes(byte *dst, byte *src) {
+ PSP_INFO_PRINT("\nsrc[%p], dst[%p]\n", src, dst);
+ fastCopySpecificSize(dst, src, 1);
+ fastCopySpecificSize(dst, src, 2);
+ fastCopySpecificSize(dst, src, 3);
+ fastCopySpecificSize(dst, src, 4);
+ fastCopySpecificSize(dst, src, 5);
+ fastCopySpecificSize(dst, src, 8);
+ fastCopySpecificSize(dst, src, 10);
+ fastCopySpecificSize(dst, src, 16);
+ fastCopySpecificSize(dst, src, 32);
+ fastCopySpecificSize(dst, src, 50);
+ fastCopySpecificSize(dst, src, 100);
+ fastCopySpecificSize(dst, src, 500);
+ fastCopySpecificSize(dst, src, 1024);
+ fastCopySpecificSize(dst, src, 2048);
+}
+
+void PspSpeedTests::fastCopySpeed() {
+ PSP_INFO_PRINT("running fastCopy speed test\n");
+
+ uint32 *bufferSrc32 = (uint32 *)memalign(16, MEMCPY_BUFFER_SIZE);
+ uint32 *bufferDst32 = (uint32 *)memalign(16, MEMCPY_BUFFER_SIZE);
+
+ // fill buffer 1
+ for (int i=0; i<MEMCPY_BUFFER_SIZE/4; i++)
+ bufferSrc32[i] = i | (((MEMCPY_BUFFER_SIZE/4)-i)<<16);
+
+ // print buffer
+ for (int i=0; i<50; i++)
+ PSP_INFO_PRINT("%x ", bufferSrc32[i]);
+ PSP_INFO_PRINT("\n");
+
+ byte *bufferSrc = ((byte *)bufferSrc32);
+ byte *bufferDst = ((byte *)bufferDst32);
+
+ PSP_INFO_PRINT("\n\ndst and src cached: -----------------\n");
+ fastCopyDifferentSizes(bufferDst, bufferSrc);
+ fastCopyDifferentSizes(bufferDst+1, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst+1, bufferSrc);
+
+ PSP_INFO_PRINT("\n\ndst cached, src uncached: -----------------\n");
+ bufferSrc = UNCACHED(bufferSrc);
+ fastCopyDifferentSizes(bufferDst, bufferSrc);
+ fastCopyDifferentSizes(bufferDst+1, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst+1, bufferSrc);
+
+ PSP_INFO_PRINT("\n\ndst uncached, src uncached: --------------\n");
+ bufferDst = UNCACHED(bufferDst);
+ fastCopyDifferentSizes(bufferDst, bufferSrc);
+ fastCopyDifferentSizes(bufferDst+1, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst+1, bufferSrc);
+
+ PSP_INFO_PRINT("\n\ndst uncached, src cached: -------------------\n");
+ bufferSrc = CACHED(bufferSrc);
+ fastCopyDifferentSizes(bufferDst, bufferSrc);
+ fastCopyDifferentSizes(bufferDst+1, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst+1, bufferSrc);
+
+
+ free(bufferSrc32);
+ free(bufferDst32);
+}
+
+//-------Unit Tests -------------------------------
+
+class PspUnitTests {
+public:
+ void testFastCopy();
+
+private:
+ enum {
+ MEMCPY_BUFFER_SIZE = 8192
+ };
+
+ void fastCopySpecificSize(byte *dst, byte *src, uint32 bytes, bool swap = false);
+ void fastCopyDifferentSizes(byte *dst, byte *src, bool swap = false);
+};
+
+void PspUnitTests::testFastCopy() {
+ PSP_INFO_PRINT("running fastcopy unit test ***********\n");
+ PSP_INFO_PRINT("this test requires the test flag to be on in fastCopy\n\n");
+
+ uint32 *bufferSrc32 = (uint32 *)memalign(16, MEMCPY_BUFFER_SIZE);
+ uint32 *bufferDst32 = (uint32 *)memalign(16, MEMCPY_BUFFER_SIZE);
+
+ // fill buffer 1
+ for (int i=0; i<MEMCPY_BUFFER_SIZE/4; i++)
+ bufferSrc32[i] = i | (((MEMCPY_BUFFER_SIZE/4)-i)<<16);
+
+ // print buffer
+ for (int i=0; i<50; i++)
+ PSP_INFO_PRINT("%x ", bufferSrc32[i]);
+ PSP_INFO_PRINT("\n");
+
+ byte *bufferSrc = ((byte *)bufferSrc32);
+ byte *bufferDst = ((byte *)bufferDst32);
+
+ fastCopyDifferentSizes(bufferDst, bufferSrc, true);
+ fastCopyDifferentSizes(bufferDst+1, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst+2, bufferSrc+2, true);
+ fastCopyDifferentSizes(bufferDst+3, bufferSrc+3);
+ fastCopyDifferentSizes(bufferDst, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst, bufferSrc+2, true);
+ fastCopyDifferentSizes(bufferDst+2, bufferSrc, true);
+ fastCopyDifferentSizes(bufferDst, bufferSrc+3);
+ fastCopyDifferentSizes(bufferDst+1, bufferSrc+2);
+ fastCopyDifferentSizes(bufferDst+1, bufferSrc+3);
+ fastCopyDifferentSizes(bufferDst+2, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst+2, bufferSrc+3);
+ fastCopyDifferentSizes(bufferDst+3, bufferSrc+1);
+ fastCopyDifferentSizes(bufferDst+3, bufferSrc+2);
+
+ free(bufferSrc32);
+ free(bufferDst32);
+}
+
+void PspUnitTests::fastCopyDifferentSizes(byte *dst, byte *src, bool swap) {
+ fastCopySpecificSize(dst, src, 1);
+ fastCopySpecificSize(dst, src, 2, swap);
+ fastCopySpecificSize(dst, src, 4, swap);
+ fastCopySpecificSize(dst, src, 6, swap);
+ fastCopySpecificSize(dst, src, 8, swap);
+ fastCopySpecificSize(dst, src, 9);
+ fastCopySpecificSize(dst, src, 10, swap);
+ fastCopySpecificSize(dst, src, 11);
+ fastCopySpecificSize(dst, src, 12, swap);
+ fastCopySpecificSize(dst, src, 13);
+ fastCopySpecificSize(dst, src, 14, swap);
+ fastCopySpecificSize(dst, src, 15);
+ fastCopySpecificSize(dst, src, 16, swap);
+ fastCopySpecificSize(dst, src, 17);
+ fastCopySpecificSize(dst, src, 18, swap);
+ fastCopySpecificSize(dst, src, 19);
+ fastCopySpecificSize(dst, src, 20, swap);
+ fastCopySpecificSize(dst, src, 32, swap);
+ fastCopySpecificSize(dst, src, 33);
+ fastCopySpecificSize(dst, src, 34, swap);
+ fastCopySpecificSize(dst, src, 35);
+ fastCopySpecificSize(dst, src, 36, swap);
+ fastCopySpecificSize(dst, src, 50, swap);
+ fastCopySpecificSize(dst, src, 100, swap);
+ fastCopySpecificSize(dst, src, 500, swap);
+ fastCopySpecificSize(dst, src, 1000, swap);
+}
+
+void PspUnitTests::fastCopySpecificSize(byte *dst, byte *src, uint32 bytes, bool swap) {
+ memset(dst, 0, bytes);
+ PspMemory::fastCopy(dst, src, bytes);
+
+ if (swap) { // test swap also
+ memset(dst, 0, bytes);
+
+ // pixelformat for swap
+ PSPPixelFormat format;
+ format.set(PSPPixelFormat::Type_4444, true);
+
+ PspMemory::fastSwap(dst, src, bytes, format);
+ }
+}
+
+void psp_tests() {
+ PSP_INFO_PRINT("in tests\n");
+
+#ifdef PSP_ENABLE_SPEED_TESTS
+ // Speed tests
+ PspSpeedTests speedTests;
+ speedTests.tickSpeed();
+ speedTests.getMicrosSpeed();
+ speedTests.msReadSpeed();
+ speedTests.seekSpeed();
+ speedTests.msReadSpeed();
+ speedTests.threadFunctionsSpeed();
+ speedTests.semaphoreSpeed();
+ speedTests.semaphoreManyThreadSpeed();
+ speedTests.fastCopySpeed();
+#endif
+
+#ifdef PSP_ENABLE_UNIT_TESTS
+ // Unit tests
+ PspUnitTests unitTests;
+
+ unitTests.testFastCopy();
+#endif
+}
+
+#endif /* (PSP_ENABLE_UNIT_TESTS) || defined (PSP_ENABLE_SPEED_TESTS) */ \ No newline at end of file
diff --git a/backends/platform/psp/tests.h b/backends/platform/psp/tests.h
new file mode 100644
index 0000000000..1518acfb4c
--- /dev/null
+++ b/backends/platform/psp/tests.h
@@ -0,0 +1,36 @@
+/* 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/psp_main.cpp $
+ * $Id: psp_main.cpp 49155 2010-05-23 11:48:21Z Bluddy $
+ *
+ */
+
+#ifndef _PSP_TESTS_H_
+#define _PSP_TESTS_H_
+
+//#define PSP_ENABLE_UNIT_TESTS // run unit tests
+//#define PSP_ENABLE_SPEED_TESTS // run speed tests
+
+#if defined (PSP_ENABLE_UNIT_TESTS) || defined (PSP_ENABLE_SPEED_TESTS)
+void psp_tests();
+#endif
+
+#endif /* _PSP_TESTS_H_ */ \ No newline at end of file
diff --git a/backends/platform/psp/thread.cpp b/backends/platform/psp/thread.cpp
new file mode 100644
index 0000000000..e757c2f575
--- /dev/null
+++ b/backends/platform/psp/thread.cpp
@@ -0,0 +1,234 @@
+/* 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$
+ * $Id$
+ *
+ */
+
+#include <pspthreadman.h>
+
+#include "backends/platform/psp/thread.h"
+#include "backends/platform/psp/trace.h"
+
+// Class PspThreadable --------------------------------------------------
+// Inherit this to create C++ threads easily
+
+bool PspThreadable::threadCreateAndStart(const char *threadName, int priority, int stackSize, bool useVfpu /*= false*/) {
+ DEBUG_ENTER_FUNC();
+
+ if (_threadId != -1) {
+ PSP_ERROR("thread already created!\n");
+ return false;
+ }
+
+ _threadId = sceKernelCreateThread(threadName, __threadCallback, priority, stackSize, THREAD_ATTR_USER, 0); // add VFPU support
+
+ if (_threadId < 0) {
+ PSP_ERROR("failed to create %s thread. Error code %d\n", threadName, _threadId);
+ return false;
+ }
+
+ // We want to pass the pointer to this, but we'll have to take address of this so use a little trick
+ PspThreadable *_this = this;
+
+ if (sceKernelStartThread(_threadId, sizeof(uint32 *), &_this) < 0) {
+ PSP_ERROR("failed to start %s thread id[%d]\n", threadName, _threadId);
+ return false;
+ }
+
+ PSP_DEBUG_PRINT("Started %s thread with id[%x]\n", threadName, _threadId);
+
+ return true;
+}
+
+// Callback function to be called by PSP kernel
+int PspThreadable::__threadCallback(SceSize, void *__this) {
+ DEBUG_ENTER_FUNC();
+
+ PspThreadable *_this = *(PspThreadable **)__this; // Dereference the copied value which was 'this'
+
+ _this->threadFunction(); // call the virtual function
+
+ return 0;
+}
+
+// PspThread class
+// Utilities to access general thread functions
+
+void PspThread::delayMillis(uint32 ms) {
+ sceKernelDelayThread(ms * 1000);
+}
+
+void PspThread::delayMicros(uint32 us) {
+ sceKernelDelayThread(us);
+}
+
+// Class PspSemaphore ------------------------------------------------
+//#define __PSP_DEBUG_FUNCS__ /* For debugging function calls */
+//#define __PSP_DEBUG_PRINT__ /* For debug printouts */
+
+#include "backends/platform/psp/trace.h"
+
+PspSemaphore::PspSemaphore(int initialValue, int maxValue/*=255*/) {
+ DEBUG_ENTER_FUNC();
+ _handle = 0;
+ _handle = (uint32)sceKernelCreateSema("ScummVM Sema", 0 /* attr */,
+ initialValue, maxValue,
+ 0 /*option*/);
+ if (!_handle)
+ PSP_ERROR("failed to create semaphore.\n");
+}
+
+PspSemaphore::~PspSemaphore() {
+ DEBUG_ENTER_FUNC();
+ if (_handle)
+ if (sceKernelDeleteSema((SceUID)_handle) < 0)
+ PSP_ERROR("failed to delete semaphore.\n");
+}
+
+int PspSemaphore::numOfWaitingThreads() {
+ DEBUG_ENTER_FUNC();
+ SceKernelSemaInfo info;
+ info.numWaitThreads = 0;
+
+ if (sceKernelReferSemaStatus((SceUID)_handle, &info) < 0)
+ PSP_ERROR("failed to retrieve semaphore info for handle %d\n", _handle);
+
+ return info.numWaitThreads;
+}
+
+int PspSemaphore::getValue() {
+ DEBUG_ENTER_FUNC();
+ SceKernelSemaInfo info;
+ info.currentCount = 0;
+
+ if (sceKernelReferSemaStatus((SceUID)_handle, &info) < 0)
+ PSP_ERROR("failed to retrieve semaphore info for handle %d\n", _handle);
+
+ return info.currentCount;
+}
+
+bool PspSemaphore::pollForValue(int value) {
+ DEBUG_ENTER_FUNC();
+ if (sceKernelPollSema((SceUID)_handle, value) < 0)
+ return false;
+
+ return true;
+}
+
+// false: timeout or error
+bool PspSemaphore::takeWithTimeOut(uint32 timeOut) {
+ DEBUG_ENTER_FUNC();
+
+ uint32 *pTimeOut = 0;
+ if (timeOut)
+ pTimeOut = &timeOut;
+
+ if (sceKernelWaitSema(_handle, 1, pTimeOut) < 0) // we always wait for 1
+ return false;
+ return true;
+}
+
+bool PspSemaphore::give(int num /*=1*/) {
+ DEBUG_ENTER_FUNC();
+
+ if (sceKernelSignalSema((SceUID)_handle, num) < 0)
+ return false;
+ return true;
+}
+
+// Class PspMutex ------------------------------------------------------------
+
+bool PspMutex::lock() {
+ DEBUG_ENTER_FUNC();
+ int threadId = sceKernelGetThreadId();
+ bool ret = true;
+
+ if (_ownerId == threadId) {
+ _recursiveCount++;
+ } else {
+ ret = _semaphore.take();
+ _ownerId = threadId;
+ _recursiveCount = 0;
+ }
+ return ret;
+}
+
+bool PspMutex::unlock() {
+ DEBUG_ENTER_FUNC();
+ int threadId = sceKernelGetThreadId();
+ bool ret = true;
+
+ if (_ownerId != threadId) {
+ PSP_ERROR("attempt to unlock mutex by thread[%x] as opposed to owner[%x]\n",
+ threadId, _ownerId);
+ return false;
+ }
+
+ if (_recursiveCount) {
+ _recursiveCount--;
+ } else {
+ _ownerId = 0;
+ ret = _semaphore.give(1);
+ }
+ return ret;
+}
+
+// Class PspCondition -------------------------------------------------
+
+// Release all threads waiting on the condition
+void PspCondition::releaseAll() {
+ _mutex.lock();
+ if (_waitingThreads > _signaledThreads) { // we have signals to issue
+ int numWaiting = _waitingThreads - _signaledThreads; // threads we haven't signaled
+ _signaledThreads = _waitingThreads;
+
+ _waitSem.give(numWaiting);
+ _mutex.unlock();
+ for (int i=0; i<numWaiting; i++) // wait for threads to tell us they're awake
+ _doneSem.take();
+ } else {
+ _mutex.unlock();
+ }
+}
+
+// Mutex must be taken before entering wait
+void PspCondition::wait(PspMutex &externalMutex) {
+ _mutex.lock();
+ _waitingThreads++;
+ _mutex.unlock();
+
+ externalMutex.unlock(); // must unlock external mutex
+
+ _waitSem.take(); // sleep on the wait semaphore
+
+ // let the signaling thread know we're done
+ _mutex.lock();
+ if (_signaledThreads > 0 ) {
+ _doneSem.give(); // let the thread know
+ _signaledThreads--;
+ }
+ _waitingThreads--;
+ _mutex.unlock();
+
+ externalMutex.lock(); // must lock external mutex here for continuation
+}
+
diff --git a/backends/platform/psp/thread.h b/backends/platform/psp/thread.h
new file mode 100644
index 0000000000..44394af40a
--- /dev/null
+++ b/backends/platform/psp/thread.h
@@ -0,0 +1,112 @@
+/* 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$
+ * $Id$
+ *
+ */
+
+#ifndef PSP_THREAD_H
+#define PSP_THREAD_H
+
+#include <pspthreadman.h>
+#include "common/scummsys.h"
+
+// class to inherit for creating threads
+class PspThreadable {
+protected:
+ int _threadId;
+ virtual void threadFunction() = 0; // this function will be called when the thread starts
+public:
+ PspThreadable() : _threadId(-1) {} // constructor
+ virtual ~PspThreadable() {} // destructor
+ static int __threadCallback(SceSize, void *__this); // used to get called by sceKernelStartThread() Don't override
+ bool threadCreateAndStart(const char *threadName, int priority, int stackSize, bool useVfpu = false);
+};
+
+// class for thread utils
+class PspThread {
+public:
+ // static functions
+ static void delayMillis(uint32 ms); // delay the current thread
+ static void delayMicros(uint32 us);
+};
+
+class PspSemaphore {
+private:
+ uint32 _handle;
+public:
+ PspSemaphore(int initialValue, int maxValue=255);
+ ~PspSemaphore();
+ bool take() { return takeWithTimeOut(0); }
+ bool takeWithTimeOut(uint32 timeOut);
+ bool give(int num=1);
+ bool pollForValue(int value); // check for a certain value
+ int numOfWaitingThreads();
+ int getValue();
+};
+
+class PspMutex {
+private:
+ PspSemaphore _semaphore;
+ int _recursiveCount;
+ int _ownerId;
+public:
+ PspMutex(bool initialValue) : _semaphore(initialValue ? 1 : 0, 255), _recursiveCount(0), _ownerId(0) {} // initial, max value
+ bool lock();
+ bool unlock();
+ bool poll() { return _semaphore.pollForValue(1); }
+ int numOfWaitingThreads() { return _semaphore.numOfWaitingThreads(); }
+ bool getValue() { return (bool)_semaphore.getValue(); }
+};
+
+class PspCondition {
+private:
+ PspMutex _mutex;
+ int _waitingThreads;
+ int _signaledThreads;
+ PspSemaphore _waitSem;
+ PspSemaphore _doneSem;
+public:
+ PspCondition() : _mutex(true), _waitingThreads(0), _signaledThreads(0),
+ _waitSem(0), _doneSem(0) {}
+ void wait(PspMutex &externalMutex);
+ void releaseAll();
+};
+
+enum ThreadPriority {
+ PRIORITY_MAIN_THREAD = 36,
+ PRIORITY_TIMER_THREAD = 30,
+ PRIORITY_AUDIO_THREAD = 25, // must be higher than timer or we get stuttering
+ PRIORITY_POWER_THREAD = 20, // quite a light thread
+ PRIORITY_DISPLAY_THREAD = 17 // very light thread for callbacks only
+};
+
+enum StackSizes {
+ STACK_DEFAULT = 4 * 1024,
+ STACK_AUDIO_THREAD = 16 * 1024,
+ STACK_TIMER_THREAD = 32 * 1024,
+ STACK_DISPLAY_THREAD = 2 * 1024,
+ STACK_POWER_THREAD = 4 * 1024
+};
+
+#endif /* PSP_THREADS_H */
+
+
diff --git a/backends/platform/psp/trace.cpp b/backends/platform/psp/trace.cpp
index 4bf5450177..7bac6534da 100644
--- a/backends/platform/psp/trace.cpp
+++ b/backends/platform/psp/trace.cpp
@@ -30,8 +30,9 @@
#include <stdio.h>
int psp_debug_indent = 0;
+bool firstWriteToFile = true;
-void PSPDebugTrace(bool alsoToScreen, const char *format, ...) {
+void PspDebugTrace(bool alsoToScreen, const char *format, ...) {
va_list opt;
char buffer[2048];
int bufsz;
@@ -41,8 +42,12 @@ void PSPDebugTrace(bool alsoToScreen, const char *format, ...) {
bufsz = vsnprintf(buffer, (size_t) sizeof(buffer), format, opt);
va_end(opt);
- //fd = fopen("MS0:/SCUMMTRACE.TXT", "ab");
- fd = fopen("SCUMMTRACE.TXT", "ab");
+ if (firstWriteToFile) {
+ fd = fopen("SCUMMTRACE.TXT", "wb"); // erase the file the first time we write
+ firstWriteToFile = false;
+ } else {
+ fd = fopen("SCUMMTRACE.TXT", "ab");
+ }
if (fd == 0)
return;
diff --git a/backends/platform/psp/trace.h b/backends/platform/psp/trace.h
index 1aad0f6781..625aa60772 100644
--- a/backends/platform/psp/trace.h
+++ b/backends/platform/psp/trace.h
@@ -30,17 +30,19 @@
#include "common/str.h"
+#define __PSP_PRINT_TO_FILE_AND_SCREEN__
+
/* Choose to print to file/screen/both */
#ifdef __PSP_PRINT_TO_FILE__
- #define __PSP_PRINT__(format,...) PSPDebugTrace(false, format, ## __VA_ARGS__)
+ #define __PSP_PRINT__(format,...) PspDebugTrace(false, format, ## __VA_ARGS__)
#elif defined __PSP_PRINT_TO_FILE_AND_SCREEN__
- #define __PSP_PRINT__(format,...) PSPDebugTrace(true, format, ## __VA_ARGS__)
+ #define __PSP_PRINT__(format,...) PspDebugTrace(true, format, ## __VA_ARGS__)
#else /* default - print to screen */
#define __PSP_PRINT__(format,...) fprintf(stderr, format, ## __VA_ARGS__)
#endif /* PSP_PRINT_TO_FILE/SCREEN */
-/* Error function */
-#define PSP_ERROR(format,...) __PSP_PRINT__("Error in %s: " format, __PRETTY_FUNCTION__, ## __VA_ARGS__)
+/* Error function - always print to file as well */
+#define PSP_ERROR(format,...) PspDebugTrace(true, "Error in %s: " format, __PRETTY_FUNCTION__, ## __VA_ARGS__)
/* Do the indent */
#define __PSP_INDENT__ for(int _i=psp_debug_indent; _i>0; _i--) \
@@ -52,7 +54,7 @@
#define PSP_INFO_PRINT_INDENT(format,...) { __PSP_INDENT__; \
__PSP_PRINT__(format, ## __VA_ARGS__); }
-void PSPDebugTrace(bool alsoToScreen, const char *format, ...);
+void PspDebugTrace(bool alsoToScreen, const char *format, ...);
extern int psp_debug_indent;