[Scummvm-cvs-logs] CVS: scummvm/backends/ps2 READ_PS2.TXT,NONE,1.1.2.1 Gs2dScreen.cpp,1.5.2.2,1.5.2.3 Gs2dScreen.h,1.5.2.2,1.5.2.3 ps2input.cpp,1.4.2.2,1.4.2.3 ps2input.h,1.3.2.2,1.3.2.3 savefile.cpp,1.6.2.3,1.6.2.4 savefile.h,1.5.2.2,1.5.2.3 systemps2.cpp,1.8.2.2,1.8.2.3 systemps2.h,1.7.2.2,1.7.2.3

Robert Göffringmann lavosspawn at users.sourceforge.net
Wed Nov 9 11:46:03 CET 2005


Update of /cvsroot/scummvm/scummvm/backends/ps2
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22820/backends/ps2

Modified Files:
      Tag: branch-0-8-0
	Gs2dScreen.cpp Gs2dScreen.h ps2input.cpp ps2input.h 
	savefile.cpp savefile.h systemps2.cpp systemps2.h 
Added Files:
      Tag: branch-0-8-0
	READ_PS2.TXT 
Log Message:
ps2 changes

--- NEW FILE: READ_PS2.TXT ---
ScummVM-PS2 0.8.0 Release Candidate 1 Readme
============================================

You need a Playstation2 with a modchip of your choice in order to run ScummVM.

ScummVM expects the game data on the CD-R, other devices like USB sticks
 and HDD are currently unsupported.

In order to use a USB Mouse and USB Keyboard, you need Sony's USB driver,
 the USBD.IRX.  Copy it from a PS2 game that supports USB devices, like
 Eye Toy or Singstar. 
The demo disc that came with my PS2 also had it.
 Put it into the directory in which you extracted the .zip file.
You can also use ScummVM without the USB driver but you can only use the
 Gamepad in that case.


Creating a CD-R using CD-Tool
=============================
Copy any games you want on the CD over into the directory where you extracted
 the ZIP file. Don't copy any files directly into the directory of the
 ScummVM.ELF, those files are ignored. Instead, have each game in its own
 directory.
File- and directorynames should be DOS 8+3, if they aren't, they'll get
 renamed.

If you run windows, just double click the make_iso.bat.
Wait while it creates the ISO image and burn it with Nero
or any other CD writing software.

For Linux or Mac OS X binaries check http://www.nobis-crew.org/cd-tool/

Unfortunately, writing DVD images with cd-tool is currently broken and will
 crash when it reaches the 2GB limit. This problem will probably be fixed in
 the nearer future.


Notes
=====
- CD swapping doesn't work, unfortunately. So you should have all games
   on the CD you booted from.
- Curse of Monkey Island is unsupported. iMuse digital crashes often, 
   especially during Sea battles.
- Zlib is unsupported, so compressed SAN files won't work.
- The cd/dvd drive is a bottleneck, that's why you should compress the games
   with MP3 or Ogg/Vorbis using compress_scumm_sou / compress_scumm_bun /
   compress_sword1 / compress_sword2 etc, but *not* compress_san as it
   requires zlib. Besides, uncompressed SAN files play fine.
- I haven't had time to implement a virtual keyboard. If you don't have a USB
   keyboard, you can only enter '1's using the Select button.
- When you see '1's and '0's flying across your screen, it means ScummVM is
   accessing the Memory Card. Don't switch off or reset the console in that
   case, and don't remove the Memory Card either :P


GamePad
=======
Start Button         -   F5
Select Button        -   Figure 1
L1                   -   'n'
R1                   -   'y'
L2                   -   Numpad 0
Triangle             -   Escape
Square               -   Enter
Cross                -   Left mouse button
Circle               -   Right mouse button
Left analog stick    -   Mouse. Disabled when USB mouse is detected.


Index: Gs2dScreen.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/ps2/Gs2dScreen.cpp,v
retrieving revision 1.5.2.2
retrieving revision 1.5.2.3
diff -u -d -r1.5.2.2 -r1.5.2.3
--- Gs2dScreen.cpp	25 Oct 2005 06:56:28 -0000	1.5.2.2
+++ Gs2dScreen.cpp	9 Nov 2005 19:45:00 -0000	1.5.2.3
@@ -37,6 +37,8 @@
 	PRINTF
 };
 
+#define ANIM_STACK_SIZE (1024 * 32)
+
 #define DEFAULT_PAL_X		175
 #define DEFAULT_PAL_Y		60
 #define DEFAULT_NTSC_X		165
@@ -66,6 +68,7 @@
 };
 
 void sioprintf(const char *zFormat, ...);
+void runAnimThread(Gs2dScreen *param);
 
 int vblankStartHandler(int cause) {
 	// start of VBlank period
@@ -96,6 +99,7 @@
 
 Gs2dScreen::Gs2dScreen(uint16 width, uint16 height, TVMode tvMode) {
 
+	_systemQuit = false;
 	ee_sema_t newSema;
 	newSema.init_count = 1;
 	newSema.max_count = 1;
@@ -107,9 +111,9 @@
 	g_AnimSema = CreateSema(&newSema);
 	assert((g_VblankSema >= 0) && (g_DmacSema >= 0) && (_screenSema >= 0) && (g_AnimSema >= 0));
 
-	AddIntcHandler(INT_VBLANK_START, vblankStartHandler, 0);
-	AddIntcHandler(INT_VBLANK_END, vblankEndHandler, 0);
-	AddDmacHandler(2, dmacHandler, 0);
+	_vblankStartId = AddIntcHandler(INT_VBLANK_START, vblankStartHandler, 0);
+	_vblankEndId   = AddIntcHandler(INT_VBLANK_END, vblankEndHandler, 0);
+	_dmacId		   = AddDmacHandler(2, dmacHandler, 0);
 
 	_dmaPipe = new DmaPipe(0x2000);
 
@@ -127,7 +131,6 @@
 
 	memset(_screenBuf, 0, _width * _height);
 	memset(_clut, 0, 256 * sizeof(uint32));
-	//_clut[1] = GS_RGBA(0x16, 0x16, 0xF0, 0);
 	_clut[1] = GS_RGBA(0xC0, 0xC0, 0xC0, 0);
 	clearOverlay();
 
@@ -215,7 +218,46 @@
 	updateScreen();
 
 	createAnimTextures();
-	createAnimThread(this);
+	
+	// create anim thread
+	ee_thread_t animThread, thisThread;
+	ReferThreadStatus(GetThreadId(), &thisThread);
+
+	_animStack = malloc(ANIM_STACK_SIZE);
+	animThread.initial_priority = thisThread.current_priority - 3;
+	animThread.stack = _animStack;
+	animThread.stack_size = ANIM_STACK_SIZE;
+	animThread.func = (void *)runAnimThread;
+	asm("move %0, $gp\n": "=r"(animThread.gp_reg));
+
+	_animTid = CreateThread(&animThread);
+	assert(_animTid >= 0);
+	StartThread(_animTid, this);
+}
+
+void Gs2dScreen::quit(void) {
+	_systemQuit = true;
+	ee_thread_t statAnim;
+	do {	// wait until thread called ExitThread()
+		SignalSema(g_AnimSema);
+		ReferThreadStatus(_animTid, &statAnim);
+	} while (statAnim.status != 0x10);
+	DeleteThread(_animTid);
+	free(_animStack);
+	_dmaPipe->waitForDma();	// wait for dmac and vblank for the last time
+	while (g_DmacCmd || g_VblankCmd);
+
+	sioprintf("kill handlers");
+	DisableIntc(INT_VBLANK_START);
+	DisableIntc(INT_VBLANK_END);
+	DisableDmac(2);
+	RemoveIntcHandler(INT_VBLANK_START, _vblankStartId);
+	RemoveIntcHandler(INT_VBLANK_END, _vblankEndId);
+	RemoveDmacHandler(2, _dmacId);
+
+	DeleteSema(g_VblankSema);
+	DeleteSema(g_DmacSema);
+	DeleteSema(g_AnimSema);
 }
 
 void Gs2dScreen::createAnimTextures(void) {
@@ -567,10 +609,13 @@
 	};
 	float angleStep = ((2 * PI) / _tvHeight);
 
-	while (1) {
+	while (!_systemQuit) {
 		do {
 			WaitSema(g_AnimSema);
-		} while (!g_RunAnim);
+		} while ((!_systemQuit) && (!g_RunAnim));
+		
+		if (_systemQuit)
+			break;
 
 		if (PollSema(_screenSema) > 0) { // make sure no thread is currently drawing
 			WaitSema(g_DmacSema);   // dma transfers have to be finished
@@ -655,31 +700,13 @@
 			SignalSema(_screenSema);
 		}
 	}
+	ExitThread();
 }
 
 void runAnimThread(Gs2dScreen *param) {
 	param->animThread();
 }
 
-#define ANIM_STACK_SIZE (1024 * 32)
-
-void createAnimThread(Gs2dScreen *screen) {
-	ee_thread_t animThread, thisThread;
-	ReferThreadStatus(GetThreadId(), &thisThread);
-
-	animThread.initial_priority = thisThread.current_priority - 3;
-	animThread.stack = malloc(ANIM_STACK_SIZE);
-	animThread.stack_size = ANIM_STACK_SIZE;
-	animThread.func = (void *)runAnimThread;
-	asm("move %0, $gp\n": "=r"(animThread.gp_reg));
-
-	int tid = CreateThread(&animThread);
-	if (tid >= 0) {
-		StartThread(tid, screen);
-	} else
-		free(animThread.stack);
-}
-
 // data for the animated zeros and ones...
 const uint8 Gs2dScreen::_binaryData[4 * 14 * 2] = {
 	// figure zero

Index: Gs2dScreen.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/ps2/Gs2dScreen.h,v
retrieving revision 1.5.2.2
retrieving revision 1.5.2.3
diff -u -d -r1.5.2.2 -r1.5.2.3
--- Gs2dScreen.h	25 Oct 2005 06:56:28 -0000	1.5.2.2
+++ Gs2dScreen.h	9 Nov 2005 19:45:01 -0000	1.5.2.3
@@ -73,6 +73,8 @@
 
 	void animThread(void);
 	void wantAnim(bool runIt);
+
+	void quit(void);
 private:
 	void uploadToVram(void);
 	void createAnimTextures(void);
@@ -101,6 +103,10 @@
 	uint32 *_clut;
 
 	int _screenSema;
+	int _vblankStartId, _vblankEndId, _dmacId, _animTid;
+	void *_animStack;
+	volatile bool _systemQuit;
+
 	static const uint32 _binaryClut[16];
 	static const uint8  _binaryData[4 * 14 * 2];
 	static const uint16 _binaryPattern[16];

Index: ps2input.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/ps2/ps2input.cpp,v
retrieving revision 1.4.2.2
retrieving revision 1.4.2.3
diff -u -d -r1.4.2.2 -r1.4.2.3
--- ps2input.cpp	25 Oct 2005 06:56:28 -0000	1.4.2.2
+++ ps2input.cpp	9 Nov 2005 19:45:01 -0000	1.4.2.3
@@ -235,11 +235,11 @@
 			entry++;
 			buttonCode >>= 1;
 		}
-		if (_asciiCodes[entry] || _keyCodes[entry]) {
+		if (_padCodes[entry]) {
 			event->type = (down) ? OSystem::EVENT_KEYDOWN : OSystem::EVENT_KEYUP;
-			event->kbd.ascii = _asciiCodes[entry];
-			event->kbd.keycode = _keyCodes[entry];
-			event->kbd.flags = 0;
+			event->kbd.keycode = _padCodes[entry];
+			event->kbd.flags = _padFlags[entry];
+			event->kbd.ascii = mapKey(_padCodes[entry], _padFlags[entry]);
 			return true;
 		}
 	}
@@ -505,41 +505,42 @@
 	/* FF */	0
 };
 
-const int Ps2Input::_keyCodes[16] = {
-	49,	 // '1' - Select
-	0,	 //     - L3
-	0,	 //     - R3
-	286, // F5  - Start
-	0,	 //		- Up
-	0,	 //		- Right
-	0,	 //		- Down
-	0,	 //		- Left
-	0,	 //		- L2
-	0,	 //		- R2
-	0,	 //		- L1
-	0,	 //		- R1
-	27,	 // Esc - Triangle
-	0,	 //		- Circle
-	0,	 //		- Cross
-	0,	 //		- Square
+const int Ps2Input::_padCodes[16] = {
+	SDLK_1,		 // Select
+	0,			 // L3
+	0,			 // R3
+	SDLK_F5,	 // Start
+	0,			 //	Up
+	0,			 //	Right
+	0,			 //	Down
+	0,			 //	Left
+	SDLK_KP0,	 //	L2
+	0,			 //	R2
+	SDLK_n,		 //	L1
+	SDLK_y,		 //	R1
+	SDLK_ESCAPE, // Triangle
+	0,			 //	Circle  => Right mouse button
+	0,			 //	Cross	=> Left mouse button
+	SDLK_RETURN	 //	Square
 };
 
-const uint16 Ps2Input::_asciiCodes[16] = {
-	49,	 // '1' - Select
-	0,	 //     - L3
-	0,	 //     - R3
-	319, // F5  - Start
-	0,	 //		- Up
-	0,	 //		- Right
-	0,	 //		- Down
-	0,	 //		- Left
-	0,	 //		- L2
-	0,	 //		- R2
-	0,	 //		- L1
-	0,	 //		- R1
-	27,	 // Esc - Triangle
-	0,	 //		- Circle
-	0,	 //		- Cross
-	0,	 //		- Square
+const int Ps2Input::_padFlags[16] = {
+	0,			 // Select
+	0,			 // L3
+	0,			 // R3
+	0,			 // Start
+	0,			 //	Up
+	0,			 //	Right
+	0,			 //	Down
+	0,			 //	Left
+	0,			 //	L2
+	0,			 //	R2
+	0,			 //	L1
+	0,			 //	R1
+	0,			 // Triangle
+	0,			 //	Circle
+	0,			 //	Cross
+	0			 //	Square
 };
 
+

Index: ps2input.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/ps2/ps2input.h,v
retrieving revision 1.3.2.2
retrieving revision 1.3.2.3
diff -u -d -r1.3.2.2 -r1.3.2.3
--- ps2input.h	25 Oct 2005 06:56:28 -0000	1.3.2.2
+++ ps2input.h	9 Nov 2005 19:45:01 -0000	1.3.2.3
@@ -50,8 +50,7 @@
 
 	bool _mouseLoaded, _kbdLoaded;
 	int _keyFlags;
-	static const int _keyCodes[16];
-	static const uint16 _asciiCodes[16];
+	static const int _padCodes[16], _padFlags[16];
 	static const int _usbToSdlk[0x100];
 };
 

Index: savefile.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/ps2/savefile.cpp,v
retrieving revision 1.6.2.3
retrieving revision 1.6.2.4
diff -u -d -r1.6.2.3 -r1.6.2.4
--- savefile.cpp	26 Oct 2005 16:34:58 -0000	1.6.2.3
+++ savefile.cpp	9 Nov 2005 19:45:01 -0000	1.6.2.4
@@ -38,6 +38,8 @@
 #define SLOT 0
 // port 0, slot 0: memory card in first slot.
 
+void sioprintf(const char *zFormat, ...);
+
 class McAccess {
 public:
 	McAccess(int port, int slot);
@@ -260,21 +262,21 @@
 	_autoSaveSignal = CreateSema(&newSema);
 	_autoSaveBuf = NULL;
 	_autoSaveSize = 0;
+	_systemQuit = false;
 
 	ee_thread_t saveThread, thisThread;
 	ReferThreadStatus(GetThreadId(), &thisThread);
 
 	saveThread.initial_priority = thisThread.current_priority + 1;
 	saveThread.stack_size = 8 * 1024;
-	saveThread.stack = malloc(saveThread.stack_size);	
+	_autoSaveStack = malloc(saveThread.stack_size);	
+	saveThread.stack = _autoSaveStack;
 	saveThread.func = (void *)runSaveThread;
 	asm("move %0, $gp\n": "=r"(saveThread.gp_reg));
 
-	int tid = CreateThread(&saveThread);
-	if (tid >= 0) {
-		StartThread(tid, this);
-	} else
-		free(saveThread.stack);
+	_autoSaveTid = CreateThread(&saveThread);
+	assert(_autoSaveTid >= 0);
+	StartThread(_autoSaveTid, this);
 }
 
 Ps2SaveFileManager::~Ps2SaveFileManager(void) {
@@ -283,6 +285,7 @@
 void Ps2SaveFileManager::checkMainDirectory(void) {
 	// verify that the main directory (scummvm config + icon) exists
 	int ret, fd;
+	_mcNeedsUpdate = true;
 	ret = _mc->getDir("/ScummVM/*", 0, MAX_MC_ENTRIES, _mcDirList);
 	printf("/ScummVM/* res = %d\n", ret);
 	if (ret <= 0) { // assume directory doesn't exist
@@ -320,28 +323,31 @@
 }
 
 bool Ps2SaveFileManager::mcReadyForDir(const char *dir) {
-	if (_mcNeedsUpdate || ((_system->getMillis() - _mcCheckTime) > 1000) || !_mcPresent) {
+	if (_mcNeedsUpdate || ((_system->getMillis() - _mcCheckTime) > 2000) || !_mcPresent) {
 		// check if memory card was exchanged/removed in the meantime
 		int mcType, mcFree, mcFormat, mcResult;
 		mcResult = _mc->getInfo(&mcType, &mcFree, &mcFormat);
 		if (mcResult != 0) { // memory card was exchanged
 			_mcNeedsUpdate = true;
-			if (mcResult != -1) {
+			if (mcResult == -1) { // yes, it was exchanged
+				checkMainDirectory(); // make sure ScummVM dir and icon are there
+			} else { // no memorycard in slot or not formatted or something like that
 				_mcPresent = false;
 				printf("MC not found, error code %d\n", mcResult);
 				return false;
 			}
 		}
 		_mcPresent = true;
+		_mcCheckTime = _system->getMillis();
 	}
 	if (_mcNeedsUpdate || strcmp(_mcDirName, dir)) {
 		strcpy(_mcDirName, dir);
 		char dirStr[256];
 		sprintf(dirStr, "/ScummVM-%s/*", dir);
 		_mcEntries = _mc->getDir(dirStr, 0, MAX_MC_ENTRIES, _mcDirList);
-		return (_mcEntries >= 0);
-	} else
-		return true;
+		_mcNeedsUpdate = false;
+	}
+	return (_mcEntries >= 0);
 }
 
 Common::InSaveFile *Ps2SaveFileManager::openForLoading(const char *filename) {
@@ -359,9 +365,9 @@
 			sprintf(fullName, "/ScummVM-%s/%s", dir, name);
 			UclInSaveFile *file = new UclInSaveFile(fullName, _screen, _mc);
 			if (file) {
-				if (!file->ioFailed()) {
+				if (!file->ioFailed())
 					return file;
-				} else
+				else
 					delete file;
 			}
 		} else
@@ -396,7 +402,7 @@
 	if (_mcPresent) {
 		char fullPath[256];
 		sprintf(fullPath, "/ScummVM-%s/%s", dir, name);
-		if (strstr(filename, ".s00")) {
+		if (strstr(filename, ".s00") || strstr(filename, ".ASD") || strstr(filename, ".asd")) {
 			// this is an autosave
 			AutoSaveFile *file = new AutoSaveFile(this, fullPath);
 			return file;
@@ -517,7 +523,7 @@
 }
 
 void Ps2SaveFileManager::writeSaveNonblocking(char *name, void *buf, uint32 size) {
-	if (buf && size) {
+	if (buf && size && !_systemQuit) {
 		strcpy(_autoSaveName, name);
 		assert(!_autoSaveBuf);
 		_autoSaveBuf = (uint8*)malloc(size);
@@ -528,22 +534,41 @@
 }
 
 void Ps2SaveFileManager::saveThread(void) {
-	while (1) {
+	while (!_systemQuit) {
 		WaitSema(_autoSaveSignal);
-		assert((_autoSaveBuf != NULL) && _autoSaveSize);
-		UclOutSaveFile *outSave = new UclOutSaveFile(_autoSaveName, _system, _screen, _mc);
-		if (!outSave->ioFailed()) {
-			outSave->write(_autoSaveBuf, _autoSaveSize);
-			outSave->flush();
-		} else
-			_system->msgPrintf(5000, "Writing autosave to %s failed", _autoSaveName);
-		delete outSave;
-		free(_autoSaveBuf);
-		_autoSaveBuf = NULL;
-		_autoSaveSize = 0;
-		_mcNeedsUpdate = true; // we've created a file, mc will have to be updated
-		_screen->wantAnim(false);
+		if (_autoSaveBuf && _autoSaveSize) {
+			UclOutSaveFile *outSave = new UclOutSaveFile(_autoSaveName, _system, _screen, _mc);
+			if (!outSave->ioFailed()) {
+				outSave->write(_autoSaveBuf, _autoSaveSize);
+				outSave->flush();
+			}
+			if (outSave->ioFailed())
+				_system->msgPrintf(5000, "Writing autosave to %s failed", _autoSaveName);
+			delete outSave;
+			free(_autoSaveBuf);
+			_autoSaveBuf = NULL;
+			_autoSaveSize = 0;
+			_mcNeedsUpdate = true; // we've created a file, mc will have to be updated
+			_screen->wantAnim(false);
+		}
 	}
+	ExitThread();
+}
+
+void Ps2SaveFileManager::quit(void) {
+	_systemQuit = true;
+	ee_thread_t statSave, statThis;
+	ReferThreadStatus(GetThreadId(), &statThis);
+	int res = ChangeThreadPriority(_autoSaveTid, statThis.current_priority - 1);
+	sioprintf("SaveThread prio res: %d", res);
+
+	do {	// wait until thread called ExitThread()
+		SignalSema(_autoSaveSignal);
+		ReferThreadStatus(_autoSaveTid, &statSave);
+	} while (statSave.status != 0x10);
+	sioprintf("wait done");
+	DeleteThread(_autoSaveTid);
+    free(_autoSaveStack);
 }
 
 UclInSaveFile::UclInSaveFile(const char *filename, Gs2dScreen *screen, McAccess *mc) {

Index: savefile.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/ps2/savefile.h,v
retrieving revision 1.5.2.2
retrieving revision 1.5.2.3
diff -u -d -r1.5.2.2 -r1.5.2.3
--- savefile.h	25 Oct 2005 06:56:28 -0000	1.5.2.2
+++ savefile.h	9 Nov 2005 19:45:01 -0000	1.5.2.3
@@ -43,6 +43,7 @@
 
 	void writeSaveNonblocking(char *name, void *buf, uint32 size);
 	void saveThread(void);
+	void quit(void);
 private:
 	bool setupIcon(const char *dest, const char *ico, const char *descr1, const char *descr2);
 
@@ -56,7 +57,10 @@
 	OSystem_PS2 *_system;
 	McAccess	*_mc;
 
+	int _autoSaveTid;
 	int _autoSaveSignal;
+	void *_autoSaveStack;
+	volatile bool _systemQuit;
 	uint8 *_autoSaveBuf;
 	uint32 _autoSaveSize;
 	char _autoSaveName[256];

Index: systemps2.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/ps2/systemps2.cpp,v
retrieving revision 1.8.2.2
retrieving revision 1.8.2.3
diff -u -d -r1.8.2.2 -r1.8.2.3
--- systemps2.cpp	25 Oct 2005 06:56:28 -0000	1.8.2.2
+++ systemps2.cpp	9 Nov 2005 19:45:01 -0000	1.8.2.3
@@ -43,7 +43,7 @@
 #include "common/file.h"
 #include "backends/ps2/sysdefs.h"
 #include <libmc.h>
-#include <libhdd.h>
+#include <libpad.h>
 #include "backends/ps2/cd.h"
 #include <sio.h>
 #include <fileXio_rpc.h>
@@ -182,6 +182,7 @@
 	_scummSoundParam = NULL;
 	_printY = 0;
 	_msgClearTime = 0;
+	_systemQuit = false;
 
 	_screen = new Gs2dScreen(320, 200, TV_DONT_CARE);
 
@@ -287,7 +288,8 @@
 	assert((g_MainWaitSema >= 0) && (g_TimerWaitSema >= 0));
 
 	// threads done, start the interrupt handler
-	AddIntcHandler( INT_TIMER0, timerInterruptHandler, 0); // 0=first handler
+	_intrId = AddIntcHandler( INT_TIMER0, timerInterruptHandler, 0); // 0=first handler
+	assert(_intrId >= 0);
 	EnableIntc(INT_TIMER0);
 	T0_HOLD = 0;
 	T0_COUNT = 0;
@@ -296,11 +298,12 @@
 }
 
 void OSystem_PS2::timerThread(void) {
-	while (1) {
+	while (!_systemQuit) {
 		WaitSema(g_TimerThreadSema);
 		if (_scummTimerProc)
 			_scummTimerProc(0);
 	}
+	ExitThread();
 }
 
 void OSystem_PS2::soundThread(void) {
@@ -315,7 +318,7 @@
 
 	int bufferedSamples = 0;
 	int cycles = 0;
-	while (1) {
+	while (!_systemQuit) {
 		WaitSema(g_SoundThreadSema);
 
 		if (!(cycles & 31))
@@ -371,6 +374,9 @@
 		}
 		SignalSema(_soundSema);
 	}
+	free(soundBufL);
+	DeleteSema(_soundSema);
+	ExitThread();
 }
 
 const char *irxModules[] = {
@@ -396,6 +402,7 @@
 		if ((res = SifLoadModule(irxModules[i], 0, NULL)) < 0) {
 			msgPrintf(FOREVER, "Unable to load module %s, Error %d", irxModules[i], res);
 			_screen->wantAnim(false);
+			updateScreen();
 			SleepThread();
 		}
 	}
@@ -742,11 +749,33 @@
 }
 
 void OSystem_PS2::quit(void) {
-	printf("OSystem_PS2::quit\n");
-	_screen->wantAnim(false);
+	sioprintf("OSystem_PS2::quit");
 	clearSoundCallback();
 	setTimerCallback(NULL, 0);
-	SleepThread();
+	_screen->wantAnim(false);
+	_systemQuit = true;
+	ee_thread_t statSound, statTimer;
+	do {	// wait until both threads called ExitThread()
+		ReferThreadStatus(_timerTid, &statTimer);
+		ReferThreadStatus(_soundTid, &statSound);
+	} while ((statSound.status != 0x10) || (statTimer.status != 0x10));
+	DeleteThread(_timerTid);
+	DeleteThread(_soundTid);
+	free(_timerStack);
+	free(_soundStack);
+	DisableIntc(INT_TIMER0);
+	RemoveIntcHandler(INT_TIMER0, _intrId);
+
+	_saveManager->quit();
+	_screen->quit();
+
+	padEnd(); // stop pad library
+	cdvdInit(CDVD_EXIT);
+	cdvdExit();
+	SifExitIopHeap();
+	SifLoadFileExit();
+	SifExitRpc();
+	LoadExecPS2("cdrom0:\\SCUMMVM.ELF", 0, NULL); // resets the console and executes the ELF
 }
 
 static uint32 g_timeSecs;

Index: systemps2.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/ps2/systemps2.h,v
retrieving revision 1.7.2.2
retrieving revision 1.7.2.3
diff -u -d -r1.7.2.2 -r1.7.2.3
--- systemps2.h	25 Oct 2005 06:56:28 -0000	1.7.2.2
+++ systemps2.h	9 Nov 2005 19:45:01 -0000	1.7.2.3
@@ -128,7 +128,9 @@
 	Ps2Mutex	*_mutex;
 #endif
 	uint8		*_timerStack, *_soundStack;
-	int32		_timerTid, _soundTid;
+	int			_timerTid, _soundTid;
+	int			_intrId;
+	volatile bool _systemQuit;
 	static const GraphicsMode _graphicsMode;
 };
 





More information about the Scummvm-git-logs mailing list