aboutsummaryrefslogtreecommitdiff
path: root/backends/fs/psp/psp-stream.cpp
blob: 10b80a0639a83fcc5b5ef5990309f65bc28e143c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* 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$
 *
 */
#ifdef __PSP__

#include <pspiofilemgr.h>

#include "backends/platform/psp/powerman.h"
#include "backends/fs/psp/psp-stream.h"

#define MIN2(a,b) ((a < b) ? a : b)
#define MIN3(a,b,c) ( (a < b) ? (a < c ? a : c) : (b < c ? b : c) )

//#define __PSP_PRINT_TO_FILE__ /* For debugging suspend stuff, we have no screen output */
//#define __PSP_DEBUG_FUNCS__ 	/* For debugging function calls */
//#define __PSP_DEBUG_PRINT__	/* For debug printouts */

#include "backends/platform/psp/trace.h"

//#define DEBUG_BUFFERS					/* to see the contents of the buffers being read */

#ifdef DEBUG_BUFFERS
void printBuffer(byte *ptr, uint32 len) {
	uint32 printLen = len <= 10 ? len : 10;

	for (int i = 0; i < printLen; i++) {
		PSP_INFO_PRINT("%x ", ptr[i]);
	}

	if (len > 10) {
		PSP_INFO_PRINT("... ");
		for (int i = len - 10; i < len; i++)
			PSP_INFO_PRINT("%x ", ptr[i]);
	}

	PSP_INFO_PRINT("\n");
}
#endif

// Class PspIoStream ------------------------------------------------

PspIoStream::PspIoStream(const Common::String &path, bool writeMode)
		: _handle(0), _path(path), _fileSize(0), _writeMode(writeMode),
		  _physicalPos(0), _pos(0), _eos(false),	_error(false),
		  _errorSuspend(0), _errorSource(0), _errorPos(0), _errorHandle(0), _suspendCount(0) {
	DEBUG_ENTER_FUNC();

	//assert(!path.empty());	// do we need this?
}

PspIoStream::~PspIoStream() {
	DEBUG_ENTER_FUNC();

	if (PowerMan.beginCriticalSection())
		PSP_DEBUG_PRINT_FUNC("suspended\n");

	PowerMan.unregisterForSuspend(this); 			// Unregister with powermanager to be suspended
													// Must do this before fclose() or resume() will reopen.
	sceIoClose(_handle);

	PowerMan.endCriticalSection();
}

/* Function to open the file pointed to by the path.
 *
 */
void *PspIoStream::open() {
	DEBUG_ENTER_FUNC();

	if (PowerMan.beginCriticalSection()) {
		// No need to open? Just return the _handle resume() already opened
		PSP_DEBUG_PRINT_FUNC("suspended\n");
	}

	_handle = sceIoOpen(_path.c_str(), _writeMode ? PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC : PSP_O_RDONLY, 0777);
	if (!_handle) {
		_error = true;
		_handle = NULL;
	}

	// Get the file size. This way is much faster than going to the end of the file and back
	SceIoStat stat;
	sceIoGetstat(_path.c_str(), &stat);
	_fileSize = *((uint32 *)(void *)&stat.st_size);	// 4GB file (32 bits) is big enough for us

	PSP_DEBUG_PRINT("%s filesize[%d]\n", _path.c_str(), _fileSize);

	PowerMan.registerForSuspend(this);	 // Register with the powermanager to be suspended

	PowerMan.endCriticalSection();

	return (void *)_handle;
}

bool PspIoStream::err() const {
	DEBUG_ENTER_FUNC();

	if (_error)	// We dump since no printing to screen with suspend callback
		PSP_ERROR("mem_error[%d], source[%d], suspend error[%d], pos[%d],"
				  "_errorPos[%d], _errorHandle[%p], suspendCount[%d]\n",
		          _error, _errorSource, _errorSuspend, _pos,
				  _errorPos, _errorHandle, _suspendCount);

	return _error;
}

void PspIoStream::clearErr() {
	_error = false;
}

bool PspIoStream::eos() const {
	return _eos;
}

int32 PspIoStream::pos() const {
	return _pos;
}

int32 PspIoStream::size() const {
	return _fileSize;
}

bool PspIoStream::physicalSeekFromCur(int32 offset) {

	int ret = sceIoLseek32(_handle, offset, PSP_SEEK_CUR);

	if (ret < 0) {
		_error = true;
		PSP_ERROR("failed to seek in file[%s] to [%x]. Error[%x]\n", _path.c_str(), offset, ret);
		return false;
	}
	_physicalPos += offset;
	return true;
}

bool PspIoStream::seek(int32 offs, int whence) {
	DEBUG_ENTER_FUNC();
	PSP_DEBUG_PRINT_FUNC("offset[0x%x], whence[%d], _pos[0x%x], _physPos[0x%x]\n", offs, whence, _pos, _physicalPos);
	_eos = false;

	int32 posToSearchFor = 0;
	switch (whence) {
	case SEEK_CUR:
		posToSearchFor = _pos;
		break;
	case SEEK_END:
		posToSearchFor = _fileSize;
		break;
	}
	posToSearchFor += offs;

	// Check for bad values
	if (posToSearchFor < 0) {
		_error = true;
		return false;
	} else if (posToSearchFor > _fileSize) {
		_error = true;
		_eos = true;
		return false;
	}

	_pos = posToSearchFor;

	return true;
}

uint32 PspIoStream::read(void *ptr, uint32 len) {
	DEBUG_ENTER_FUNC();
	PSP_DEBUG_PRINT_FUNC("filename[%s], len[0x%x], ptr[%p], _pos[%x], _physPos[%x]\n", _path.c_str(), len, ptr, _pos, _physicalPos);

	if (_error || _eos || len <= 0)
		return 0;

	uint32 lenRemainingInFile = _fileSize - _pos;

	// check for getting EOS
	if (len > lenRemainingInFile) {
		len = lenRemainingInFile;
		_eos = true;
	}

	if (PowerMan.beginCriticalSection())
		PSP_DEBUG_PRINT_FUNC("suspended\n");

	// check if we need to seek
	if (_pos != _physicalPos)
		PSP_DEBUG_PRINT("seeking from %x to %x\n", _physicalPos, _pos);
		if (!physicalSeekFromCur(_pos - _physicalPos)) {
			_error = true;
			return 0;
		}

	int ret = sceIoRead(_handle, ptr, len);

	PowerMan.endCriticalSection();

	_physicalPos += ret;	// Update position
	_pos = _physicalPos;

	if (ret != (int)len) {	// error
		PSP_ERROR("sceIoRead returned [0x%x] instead of len[0x%x]\n", ret, len);
		_error = true;
		_errorSource = 4;
	}
	return ret;
}

uint32 PspIoStream::write(const void *ptr, uint32 len) {
	DEBUG_ENTER_FUNC();
	PSP_DEBUG_PRINT_FUNC("filename[%s], len[0x%x], ptr[%p], _pos[%x], _physPos[%x]\n", _path.c_str(), len, ptr, _pos, _physicalPos);

	if (!len || _error)		// we actually get some calls with len == 0!
		return 0;

	_eos = false;			// we can't have eos with write

	if (PowerMan.beginCriticalSection())
		PSP_DEBUG_PRINT_FUNC("suspended\n");

	// check if we need to seek
	if (_pos != _physicalPos)
		if (!physicalSeekFromCur(_pos - _physicalPos)) {
			_error = true;
			return 0;
		}

	int ret = sceIoWrite(_handle, ptr, len);

	PowerMan.endCriticalSection();

	if (ret != (int)len) {
		_error = true;
		_errorSource = 5;
		PSP_ERROR("sceIoWrite returned[0x%x] instead of len[0x%x]\n", ret, len);
	}

	_physicalPos += ret;
	_pos = _physicalPos;

	if (_pos > _fileSize)
		_fileSize = _pos;

	return ret;
}

bool PspIoStream::flush() {
	return true;
}

// For the PSP, since we're building in suspend support, we moved opening
// the actual file to an open function since we need an actual PspIoStream object to suspend.
//
PspIoStream *PspIoStream::makeFromPath(const Common::String &path, bool writeMode) {
	DEBUG_ENTER_FUNC();
	PspIoStream *stream = new PspIoStream(path, writeMode);

	if (stream->open() <= 0) {
		delete stream;
		stream = 0;
	}

	return stream;
}

/*
 *  Function to suspend the IO stream (called by PowerManager)
 *  we can have no output here
 */
int PspIoStream::suspend() {
	DEBUG_ENTER_FUNC();
	_suspendCount++;

	if (_handle > 0 && _pos < 0) {	/* check for error */
		_errorSuspend = SuspendError;
		_errorPos = _pos;
		_errorHandle = _handle;
	}

	if (_handle > 0) {
		sceIoClose(_handle);		// close our file descriptor
		_handle = 0xFFFFFFFF;		// Set handle to non-null invalid value so makeFromPath doesn't return error
	}

	return 0;
}

/*
 *  Function to resume the IO stream (called by Power Manager)
 */
int PspIoStream::resume() {
	DEBUG_ENTER_FUNC();
	int ret = 0;
	_suspendCount--;

	// We reopen our file descriptor
	_handle = sceIoOpen(_path.c_str(), _writeMode ? PSP_O_RDWR | PSP_O_CREAT : PSP_O_RDONLY, 0777); 	// open
	if (_handle <= 0) {
		_errorSuspend = ResumeError;
		_errorPos = _pos;
	}

	// Resume our previous position if needed
	if (_handle > 0 && _pos > 0) {
		ret = sceIoLseek32(_handle, _pos, PSP_SEEK_SET);

		_physicalPos = _pos;

		if (ret < 0) {		// Check for problem
			_errorSuspend = ResumeError;
			_errorPos = _pos;
			_errorHandle = _handle;
		}
	}
	return ret;
}

#endif /* __PSP__ */