aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/ps2/fileio.cpp
blob: fc3530690785edd86b4fcb0878492ca26f0599d0 (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* 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 "backends/platform/ps2/fileio.h"

#include <tamtypes.h>
#include <kernel.h>
#include <fileio.h>
#include <assert.h>
#include <string.h>
#include "backends/platform/ps2/asyncfio.h"
#include "engines/engine.h"
#include "common/file.h"
#include "eecodyvdfs.h"
#include "common/config-manager.h"
#include "backends/platform/ps2/ps2debug.h"
#include "backends/platform/ps2/systemps2.h"

#define __PS2_FILE_SEMA__ 1
// #define __PS2_FILE_DEBUG 1
// #define __PS2_CACHE_DEBUG__ 1

#define PS2_CACHE_MAX (128 * 1024)
#define PS2_CACHE_CHK (16 * 1024)

extern OSystem_PS2 *g_systemPs2;

uint32 _rseek;

AsyncFio fio;

Ps2File::Ps2File(void) {
	_fd = -1;
	_fileSize = 0;
	_filePos = 0;
	_cacheSize = 0;
	_cachePos = 0;
	_eof = false;

	// _cache = (uint8 *)malloc(PS2_CACHE_MAX);

	_cacheBuf = (uint8*)memalign(64, CACHE_SIZE);

	_cacheOpRunning = 0;
	_filePos = _physFilePos = _cachePos = 0;
	_fileSize = _bytesInCache = _cacheOfs = 0;
	_cacheOpRunning = false;
	_readBytesBlock = 0;
	_stream = true;

#ifdef __PS2_FILE_SEMA__
	ee_sema_t newSema;
	newSema.init_count = 1;
	newSema.max_count = 1;
	_sema = CreateSema(&newSema);
	assert(_sema >= 0);
#endif
}

Ps2File::~Ps2File(void) {
	if (_fd >= 0) {
		fio.close(_fd);
		uint32 r = fio.sync(_fd);
		printf("close [%d] - sync'd = %d\n", _fd, r);
	}

	// free(_cache);
	free(_cacheBuf);

#ifdef __PS2_FILE_SEMA__
	DeleteSema(_sema);
#endif
}

bool Ps2File::open(const char *name, int mode) {
	assert(_fd < 0);

	_fd = fio.open(name, mode);

	printf("open %s [%d]\n", name, _fd);

	if (_fd >= 0) {
		_fileSize = fio.seek(_fd, 0, SEEK_END);
		if (mode == O_RDONLY)
		// if (!(mode & O_APPEND))
			fio.seek(_fd, 0, SEEK_SET);

		printf("  _fileSize = %d\n", _fileSize);
		printf("  _filePos = %d\n", _filePos);

		return true;
	} else
		return false;
}

int32 Ps2File::tell(void) {
#ifdef __PS2_FILE_SEMA__
	WaitSema(_sema);
#endif
	int32 res = _filePos;
#ifdef __PS2_FILE_SEMA__
	SignalSema(_sema);
#endif
	return res;
}

int32 Ps2File::size(void) {
#ifdef __PS2_FILE_SEMA__
	WaitSema(_sema);
#endif
	int32 res = _fileSize;
#ifdef __PS2_FILE_SEMA__
	SignalSema(_sema);
#endif
	return res;
}

bool Ps2File::eof(void) {
#ifdef __PS2_FILE_SEMA__
	WaitSema(_sema);
#endif
	bool res = _eof; // (_filePos == _fileSize);
	// bool res = (_filePos >= _fileSize);
#ifdef __PS2_FILE_SEMA__
	SignalSema(_sema);

	// printf(" EOF [%d] : %d of %d  -> %d\n", _fd, _filePos, _fileSize, res);
#endif
	return res;
}

bool Ps2File::getErr(void) {
	return _eof;
}

void Ps2File::setErr(bool err) {
	_eof = err;
}

int Ps2File::seek(int32 offset, int origin) {
#ifdef __PS2_FILE_SEMA__
	WaitSema(_sema);
#endif
	_rseek = 0;
	int seekDest;
	int res = -1;
	switch (origin) {
		case SEEK_SET:
			seekDest = offset;
			break;
		case SEEK_CUR:
			seekDest = _filePos + offset;
			break;
		case SEEK_END:
			seekDest = _fileSize + offset;
			break;
		default:
			seekDest = -1;
			break;
	}
	if ((seekDest >= 0) && (seekDest <= (int)_fileSize)) {
		// _rseek = fio.sync(_fd);
		_filePos = seekDest;
		// fio.seek(_fd, _filePos, SEEK_SET);
		// fio.sync(_fd);
		// _cacheSize = 0;
		_eof = false;
		res = 0;
	}
	else _eof = true;

	// printf("seek [%d]  %d  %d\n", _fd, offset, origin);
	// printf("  res = %d\n", res);

#ifdef __PS2_FILE_SEMA__
	SignalSema(_sema);
#endif

	return res;
}

void Ps2File::cacheReadAhead(void) {
	if (_cacheOpRunning) {
		// there's already some cache read running
		if (fio.poll(_fd)) // did it finish?
			cacheReadSync(); // yes.
	}
	if ((!_cacheOpRunning) && ((_readBytesBlock >= CACHE_READ_THRESHOLD) || _stream) && fio.fioAvail()) {
		// the engine seems to do sequential reads and there are no other I/Os going on. read ahead.
		uint32 cachePosEnd = _cachePos + _bytesInCache;

		if (_cachePos > _filePos)
			return; // there was a seek in the meantime, don't cache.
		if (cachePosEnd - _filePos >= CACHE_FILL_MIN)
			return; // cache is full enough.
		if (cachePosEnd == _fileSize)
			return; // can't read beyond EOF.

		assert(cachePosEnd < _fileSize);

		if (_cachePos + _bytesInCache <= _filePos) {
			_cacheOfs = _bytesInCache = 0;
			_cachePos = cachePosEnd = _filePos & ~READ_ALIGN_MASK;
			assert(_filePos == _physFilePos);
		} else {
            		uint32 cacheDiff = _filePos - _cachePos;
			assert(_bytesInCache >= cacheDiff);
			cacheDiff &= ~READ_ALIGN_MASK;
			_bytesInCache -= cacheDiff;
			_cachePos += cacheDiff;
			_cacheOfs = (_cacheOfs + cacheDiff) % CACHE_SIZE;
		}

		if (_physFilePos != cachePosEnd) {
			sioprintf("unexpected _physFilePos %d cache %d %d\n", _physFilePos, _cacheOfs, _bytesInCache);
			// assert(!(cachePosEnd & READ_ALIGN_MASK)); // romeo
			_physFilePos = fio.seek(_fd, cachePosEnd, SEEK_SET);
			if (_physFilePos != cachePosEnd) {
				sioprintf("cache seek error: seek to %d instead of %d, fs = %d\n", _physFilePos, cachePosEnd, _fileSize);
				return;
			}
		}

		uint32 cacheDest = (_cacheOfs + _bytesInCache) % CACHE_SIZE;
		uint32 cacheRead = CACHE_SIZE - _bytesInCache;
		if (cacheDest + cacheRead > CACHE_SIZE)
			cacheRead = CACHE_SIZE - cacheDest;
		if (cacheRead > MAX_READ_STEP)
			cacheRead = MAX_READ_STEP;

		assert((!(cacheRead & READ_ALIGN_MASK)) && cacheRead);

		_cacheOpRunning = true;
		fio.read(_fd, _cacheBuf + cacheDest, cacheRead);
	}
}

void Ps2File::cacheReadSync(void) {
	if (_cacheOpRunning) {
		int res = fio.sync(_fd);
		assert(res >= 0);
		_bytesInCache += res;
		_physFilePos += res;
		_cacheOpRunning = false;
	}
}

uint32 Ps2File::read(void *dest, uint32 len) {
	// uint32 r=0, d=0, ds=0, sz=0;
#ifdef __PS2_FILE_SEMA__
	WaitSema(_sema);
#endif

#ifdef __PS2_FILE_DEBUG__
	printf("read (1) : _filePos = %d\n", _filePos);
	printf("read (1) : _cachePos = %d\n", _cachePos);
#endif

	if (_filePos >= _fileSize) {
		_eof = true;
#ifdef __PS2_FILE_SEMA__
    SignalSema(_sema);
#endif
		return 0;
	}

	if ((_filePos+len) > _fileSize) {
		len = _fileSize-_filePos;
		_eof = true;
	}

	uint8 *destBuf = (uint8*)dest;
	if ((_filePos < _cachePos) || (_filePos + len > _cachePos + _bytesInCache))
		cacheReadSync(); // we have to read from CD, sync cache.

	while (len && (_filePos != _fileSize)) {
		if ((_filePos >= _cachePos) && (_filePos < _cachePos + _bytesInCache)) { // read from cache
			uint32 staPos = (_cacheOfs + (_filePos - _cachePos)) % CACHE_SIZE;
			uint32 cpyLen = _bytesInCache - (_filePos - _cachePos);
			if (cpyLen > len)
				cpyLen = len;
			if (staPos + cpyLen > CACHE_SIZE)
				cpyLen = CACHE_SIZE - staPos;

			assert(cpyLen);
			memcpy(destBuf, _cacheBuf + staPos, cpyLen);
			_filePos += cpyLen;
			destBuf += cpyLen;
			_readBytesBlock += len;
			len -= cpyLen;
		} else { // cache miss
			assert(!_cacheOpRunning);
			if (_physFilePos != _filePos) {
				if ((_filePos < _physFilePos) || (_filePos > _physFilePos + (CACHE_SIZE / 2)))
					_readBytesBlock = 0; // reset cache hit count

				_physFilePos = _filePos & ~READ_ALIGN_MASK;
				if (fio.seek(_fd, _physFilePos, SEEK_SET) != (int)_physFilePos)
					break; // read beyond EOF
			}

			int doRead = len + (_filePos - _physFilePos);
			doRead = (doRead + READ_ALIGN_MASK) & ~READ_ALIGN_MASK;

			if (doRead > MAX_READ_STEP)
				doRead = MAX_READ_STEP;
			if (doRead < 2048)
				doRead = 2048;

			fio.read(_fd, _cacheBuf, doRead);
			_cachePos = _physFilePos;
			_cacheOfs = 0;
			_bytesInCache = fio.sync(_fd);
			_physFilePos += _bytesInCache;
			if (!_bytesInCache)
				break; // EOF
		}
	}
	cacheReadAhead();
	SignalSema(_sema);
	return destBuf - (uint8*)dest;
}

uint32 Ps2File::write(const void *src, uint32 len) {
	uint32 w;
#ifdef __PS2_FILE_SEMA__
	WaitSema(_sema);
#endif
	_cacheSize = 0;

	w = fio.sync(_fd);
	assert(w==0);

	fio.seek(_fd, _filePos, SEEK_SET);
	fio.write(_fd, src, len);

	w = fio.sync(_fd);

#ifdef __PS2_FILE_SEMA__
	SignalSema(_sema);
#endif

	if (w) {
		_filePos += w;
		if (w < len)
			_eof = true;
		return w;
	}

	return 0;
}

FILE *ps2_fopen(const char *fname, const char *mode) {
	Ps2File *file = new Ps2File();
	int _mode = O_RDONLY;

	printf("fopen(%s, %s)\n", fname, mode);

	if (mode[0] == 'r' && mode [1] == 'w')
		_mode = O_RDWR;
	else if (mode[0] == 'w')
		_mode = O_WRONLY | O_CREAT;
	else if (mode[0] == 'a')
		_mode = O_RDWR | O_CREAT | O_APPEND;

	if (file->open(fname, _mode))
		return (FILE *)file;
	else
		return NULL;
}

int ps2_fclose(FILE *stream) {
	Ps2File *file = (Ps2File*)stream;

	delete file;

	return 0;
}

int ps2_fseek(FILE *stream, long offset, int origin) {
	return ((Ps2File*)stream)->seek(offset, origin);
}

uint32 ps2_ftell(FILE *stream) {
	return ((Ps2File*)stream)->tell();
}

int ps2_feof(FILE *stream) {
	return ((Ps2File*)stream)->eof();
}

size_t ps2_fread(void *buf, size_t r, size_t n, FILE *stream) {
	assert(r != 0);
	return ((Ps2File*)stream)->read(buf, r * n) / r;
}

int ps2_fgetc(FILE *stream) {
	uint8 temp;
	if (((Ps2File*)stream)->read(&temp, 1))
		return temp;
	else
		return EOF;
}

size_t ps2_fwrite(const void *buf, size_t r, size_t n, FILE *stream) {
	assert(r != 0);
	return ((Ps2File*)stream)->write(buf, r * n) / r;
}

int ps2_fputc(int c, FILE *stream) {
	if (((Ps2File*)stream)->write(&c, 1) == 1)
		return c;
	else
		return -1;
}

int ps2_fputs(const char *s, FILE *stream) {
	int len = strlen(s);
	if (ps2_fwrite(s, 1, len, stream) == (size_t)len)
		return len;
	else
		return EOF;
}

int ps2_fprintf(FILE *pOut, const char *zFormat, ...) {
	va_list ap;
	char resStr[2048];

	va_start(ap,zFormat);
	int res = vsnprintf(resStr, 2048, zFormat, ap);
	va_end(ap);
	if ((pOut == stderr) || (pOut == stdout)) {
		printf("%s", resStr);
		sioprintf("%s", resStr);
	} else
		res = ps2_fwrite(resStr, 1, res, pOut);

	return res;
}

int ps2_fflush(FILE *stream) {
	// printf("fflush not implemented\n");
	return 0;
}

int ps2_ferror(FILE *stream) {
	int err = ((Ps2File*)stream)->getErr();

	if (err)
		printf("ferror -> %d\n", err);

	return err;
}

void ps2_clearerr(FILE *stream) {
	((Ps2File*)stream)->setErr(false);
}