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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2005-2006 The ScummVM project
*
* 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/ps2/fileio.h"
#include <tamtypes.h>
#include <kernel.h>
#include <fileio.h>
#include <assert.h>
#include <string.h>
#include <cdvd_rpc.h>
#include "backends/ps2/asyncfio.h"
#include "base/engine.h"
#include "common/file.h"
#define CACHE_SIZE (2048 * 32)
#define MAX_READ_STEP (2048 * 16)
#define MAX_CACHED_FILES 6
#define CACHE_READ_THRESHOLD (16 * 2048)
#define CACHE_FILL_MIN (2048 * 24)
#define READ_ALIGN 64 // align all reads to the size of an EE cache line
#define READ_ALIGN_MASK (READ_ALIGN - 1)
extern void sioprintf(const char *zFormat, ...);
AsyncFio fio;
Ps2File::Ps2File(int64 cacheId) {
_cacheId = cacheId;
}
Ps2File::~Ps2File(void) {
}
class Ps2ReadFile : public Ps2File {
public:
Ps2ReadFile(int64 cacheId, bool stream);
virtual ~Ps2ReadFile(void);
virtual bool open(const char *name);
virtual uint32 read(void *dest, uint32 len);
virtual uint32 write(const void *src, uint32 len);
virtual uint32 tell(void);
virtual uint32 size(void);
virtual int seek(int32 offset, int origin);
virtual bool eof(void);
private:
void cacheReadAhead(void);
void cacheReadSync(void);
int _fd, _sema;
uint8 *_cacheBuf;
bool _cacheOpRunning;
uint32 _filePos, _physFilePos, _cachePos;
uint32 _fileSize, _bytesInCache, _cacheOfs;
uint32 _readBytesBlock;
bool _stream;
};
Ps2ReadFile::Ps2ReadFile(int64 cacheId, bool stream) : Ps2File(cacheId) {
_fd = -1;
_cacheBuf = (uint8*)memalign(64, CACHE_SIZE);
_cacheOpRunning = 0;
_filePos = _physFilePos = _cachePos = 0;
_fileSize = _bytesInCache = _cacheOfs = 0;
_cacheOpRunning = false;
_readBytesBlock = 0;
_stream = stream;
ee_sema_t newSema;
newSema.init_count = 1;
newSema.max_count = 1;
_sema = CreateSema(&newSema);
assert(_sema >= 0);
}
Ps2ReadFile::~Ps2ReadFile(void) {
if (_cacheOpRunning)
cacheReadSync();
free(_cacheBuf);
if (_fd >= 0)
fio.close(_fd);
DeleteSema(_sema);
}
bool Ps2ReadFile::open(const char *name) {
assert(_fd < 0);
_fd = fio.open(name, O_RDONLY);
if (_fd >= 0) {
_fileSize = fio.seek(_fd, 0, SEEK_END);
fio.seek(_fd, 0, SEEK_SET);
return true;
} else
return false;
}
uint32 Ps2ReadFile::tell(void) {
WaitSema(_sema);
uint32 res = _filePos;
SignalSema(_sema);
return res;
}
uint32 Ps2ReadFile::size(void) {
WaitSema(_sema);
uint32 res = _fileSize;
SignalSema(_sema);
return res;
}
bool Ps2ReadFile::eof(void) {
WaitSema(_sema);
bool res = (_filePos == _fileSize);
SignalSema(_sema);
return res;
}
int Ps2ReadFile::seek(int32 offset, int origin) {
WaitSema(_sema);
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)) {
_filePos = seekDest;
res = 0;
}
SignalSema(_sema);
return res;
}
void Ps2ReadFile::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", _physFilePos, _cacheOfs, _bytesInCache);
assert(!(cachePosEnd & READ_ALIGN_MASK));
_physFilePos = fio.seek(_fd, cachePosEnd, SEEK_SET);
if (_physFilePos != cachePosEnd) {
sioprintf("cache seek error: seek to %d instead of %d, fs = %d", _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 Ps2ReadFile::cacheReadSync(void) {
if (_cacheOpRunning) {
int res = fio.sync(_fd);
assert(res >= 0);
_bytesInCache += res;
_physFilePos += res;
_cacheOpRunning = false;
}
}
uint32 Ps2ReadFile::read(void *dest, uint32 len) {
WaitSema(_sema);
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 Ps2ReadFile::write(const void *src, uint32 len) {
sioprintf("write request on Ps2ReadFile!");
SleepThread();
return 0;
}
class Ps2WriteFile : public Ps2File {
public:
Ps2WriteFile(int64 cacheId);
virtual ~Ps2WriteFile(void);
virtual bool open(const char *name);
virtual uint32 read(void *dest, uint32 len);
virtual uint32 write(const void *src, uint32 len);
virtual uint32 tell(void);
virtual uint32 size(void);
virtual int seek(int32 offset, int origin);
virtual bool eof(void);
private:
int _fd;
uint8 *_cacheBuf;
uint32 _filePos, _bytesInCache;
};
Ps2WriteFile::Ps2WriteFile(int64 cacheId) : Ps2File(cacheId) {
_fd = -1;
_cacheBuf = (uint8*)malloc(CACHE_SIZE);
_filePos = _bytesInCache = 0;
}
Ps2WriteFile::~Ps2WriteFile(void) {
if ((_fd >= 0) && (_bytesInCache)) {
fio.write(_fd, _cacheBuf, _bytesInCache);
int wrRes = fio.sync(_fd);
if (wrRes != (int)_bytesInCache) // too late to return an error
printf("Cache flush on fclose(): Unable to write %d cached bytes to mc, only %d bytes written\n", _bytesInCache, wrRes);
}
if (_fd >= 0)
fio.close(_fd);
free(_cacheBuf);
}
bool Ps2WriteFile::open(const char *name) {
_fd = fio.open(name, O_WRONLY | O_CREAT | O_TRUNC);
return (_fd >= 0);
}
uint32 Ps2WriteFile::read(void *dest, uint32 len) {
printf("ERROR: Read request on Ps2WriteFile\n");
SleepThread();
return 0;
}
uint32 Ps2WriteFile::write(const void *src, uint32 len) {
uint32 size = len;
uint8 *srcBuf = (uint8*)src;
while (size) {
uint32 doCpy = (len > CACHE_SIZE - _bytesInCache) ? (CACHE_SIZE - _bytesInCache) : len;
if (doCpy) {
memcpy(_cacheBuf + _bytesInCache, srcBuf, doCpy);
_bytesInCache += doCpy;
srcBuf += doCpy;
size -= doCpy;
}
if (_bytesInCache == CACHE_SIZE) {
fio.write(_fd, _cacheBuf, _bytesInCache);
if (fio.sync(_fd) != (int)_bytesInCache) {
printf("Unable to flush %d cached bytes to memory card!\n", _bytesInCache);
return 0;
}
_filePos += _bytesInCache;
_bytesInCache = 0;
}
}
return len;
}
uint32 Ps2WriteFile::tell(void) {
return _bytesInCache + _filePos;
}
uint32 Ps2WriteFile::size(void) {
return tell();
}
int Ps2WriteFile::seek(int32 offset, int origin) {
printf("Seek(%d/%d) request on Ps2WriteFile\n", offset, origin);
SleepThread();
return 0;
}
bool Ps2WriteFile::eof(void) {
return true;
}
struct TocNode {
char name[64];
TocNode *next, *sub;
bool isDir;
uint8 nameLen;
};
class TocManager {
public:
TocManager(void);
~TocManager(void);
void readEntries(const char *root);
int64 fileExists(const char *name);
bool haveEntries(void);
private:
void readDir(const char *path, TocNode **node, int level);
TocNode *_rootNode;
char _root[256];
uint8 _rootLen;
};
TocManager tocManager;
struct FioHandleCache {
Ps2File *file;
FioHandleCache *next, *prev;
};
static FioHandleCache *cacheListStart = NULL;
static FioHandleCache *cacheListEnd = NULL;
static int cacheListLen = 0;
static int openFileCount = 0;
static int cacheListSema = -1;
Ps2File *findInCache(int64 id);
FILE *ps2_fopen(const char *fname, const char *mode) {
if (cacheListSema == -1) {
ee_sema_t newSema;
newSema.init_count = 1;
newSema.max_count = 1;
cacheListSema = CreateSema(&newSema);
assert(cacheListSema >= 0);
}
if (!tocManager.haveEntries() && g_engine) // read the TOC the first time the engine opens a file
tocManager.readEntries(g_engine->getGameDataPath());
if (((mode[0] != 'r') && (mode[0] != 'w')) || ((mode[1] != '\0') && (mode[1] != 'b'))) {
printf("unsupported mode \"%s\" for file \"%s\"\n", mode, fname);
return NULL;
}
bool rdOnly = (mode[0] == 'r');
int64 cacheId = -1;
if (rdOnly && tocManager.haveEntries())
cacheId = tocManager.fileExists(fname);
if (cacheId != 0) {
Ps2File *file = findInCache(cacheId);
if (file)
return (FILE*)file;
if (rdOnly) {
bool isAudioFile = strstr(fname, ".bun") || strstr(fname, ".BUN") || strstr(fname, ".Bun");
file = new Ps2ReadFile(cacheId, isAudioFile);
} else
file = new Ps2WriteFile(cacheId);
if (file->open(fname)) {
openFileCount++;
return (FILE*)file;
} else
delete file;
}
return NULL;
}
void checkCacheListLen(void) {
while ((cacheListLen > MAX_CACHED_FILES) || ((openFileCount > 13) && cacheListLen)) {
assert(cacheListEnd && cacheListStart);
delete cacheListEnd->file;
if (cacheListEnd->prev) {
cacheListEnd->prev->next = NULL;
FioHandleCache *temp = cacheListEnd;
cacheListEnd = cacheListEnd->prev;
delete temp;
} else {
assert(cacheListEnd == cacheListStart);
assert(cacheListLen == 1);
delete cacheListEnd;
cacheListEnd = cacheListStart = NULL;
}
cacheListLen--;
openFileCount--;
}
}
int ps2_fclose(FILE *stream) {
Ps2File *file = (Ps2File*)stream;
if (file->_cacheId > 0) { // this is a file on the CD, could be smart to cache it
FioHandleCache *newHandle = new FioHandleCache;
newHandle->file = file;
file->seek(0, SEEK_SET);
WaitSema(cacheListSema);
if (!cacheListEnd) {
assert(!cacheListStart);
newHandle->prev = newHandle->next = NULL;
cacheListEnd = cacheListStart = newHandle;
} else {
assert(cacheListStart);
newHandle->prev = NULL;
newHandle->next = cacheListStart;
cacheListStart->prev = newHandle;
cacheListStart = newHandle;
}
cacheListLen++;
checkCacheListLen();
SignalSema(cacheListSema);
} else {
openFileCount--;
delete file;
}
return 0;
}
Ps2File *findInCache(int64 id) {
if (id <= 0)
return NULL;
WaitSema(cacheListSema);
FioHandleCache *node = cacheListStart;
while (node) {
if (node->file->_cacheId == id) {
if (node == cacheListStart)
cacheListStart = node->next;
if (node == cacheListEnd)
cacheListEnd = node->prev;
if (node->prev)
node->prev->next = node->next;
if (node->next)
node->next->prev = node->prev;
Ps2File *ret = node->file;
delete node;
cacheListLen--;
SignalSema(cacheListSema);
return ret;
} else
node = node->next;
}
SignalSema(cacheListSema);
return NULL;
}
int ps2_fseek(FILE *stream, long offset, int origin) {
return ((Ps2File*)stream)->seek(offset, origin);
}
uint32 ps2_ftell(FILE *stream) {
return ((Ps2File*)stream)->tell();
}
uint32 ps2_fsize(FILE *stream) {
return ((Ps2File*)stream)->size();
}
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;
}
char *ps2_fgets(char *buf, int n, FILE *stream) {
char *retVal = buf;
while (n--) {
if (n == 0)
*buf = '\0';
else {
char c = ps2_fgetc(stream);
if (c == EOF)
return NULL;
if ((c == '\r') || (c == '\n')) {
*buf++ = '\0';
return retVal;
}
*buf++ = c;
}
}
return retVal;
}
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;
}
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_fflush(FILE *stream) {
printf("fflush not implemented\n");
return 0;
}
TocManager::TocManager(void) {
_rootNode = NULL;
}
TocManager::~TocManager(void) {
// todo: write this...
}
bool TocManager::haveEntries(void) {
return _rootNode != NULL;
}
void TocManager::readEntries(const char *root) {
_rootLen = strlen(root);
strcpy(_root, root);
if (_root[_rootLen - 1] == '/') {
_rootLen--;
_root[_rootLen] = '\0';
}
readDir(_root, &_rootNode, 0);
}
#define MAX_DIR_ENTRIES 512
void TocManager::readDir(const char *path, TocNode **node, int level) {
if (level <= 2) { // we don't scan deeper than that
struct TocEntry tocEntries[MAX_DIR_ENTRIES];
int files = CDVD_GetDir(path + 5, NULL, CDVD_GET_FILES_AND_DIRS, tocEntries, MAX_DIR_ENTRIES, NULL);
for (int cnt = 0; cnt < files; cnt++) {
if (tocEntries[cnt].filename[0] != '.') { // skip '.' and '..'
*node = new TocNode;
(*node)->sub = (*node)->next = NULL;
(*node)->nameLen = strlen(tocEntries[cnt].filename);
memcpy((*node)->name, tocEntries[cnt].filename, (*node)->nameLen + 1);
if (tocEntries[cnt].fileProperties & 2) { // directory
(*node)->isDir = true;
char nextPath[256];
sprintf(nextPath, "%s/%s", path, tocEntries[cnt].filename);
readDir(nextPath, &((*node)->sub), level + 1);
} else
(*node)->isDir = false;
node = &((*node)->next);
}
}
}
}
int64 TocManager::fileExists(const char *name) {
if (((name[_rootLen] != '/') && (name[_rootLen] != '\0')) || (strnicmp(name, _root, _rootLen) != 0)) {
for (int i = 0; i < 8; i++)
if (name[i] == ':') // we don't know the content of other drives,
return -1; // assume file exists
else if ((name[i] == '/') || (name[i] == '\\'))
return 0; // does not exists (this is probably ScummVM trying the 'current directory'.)
return 0;
}
uint8 nameLen = strlen(name);
name += _rootLen + 1;
nameLen -= _rootLen + 1;
TocNode *node = _rootNode;
int64 retId = 1;
while (node) {
if (((name[node->nameLen] == '/') || (name[node->nameLen] == '\0')) && (strnicmp(name, node->name, node->nameLen) == 0)) {
name += node->nameLen;
nameLen -= node->nameLen;
if (node->isDir) {
if (nameLen) {
name++; // skip '/'
nameLen--;
node = node->sub;
retId <<= 10;
} else
return 0; // can't open a directory with fopen()
} else {
if (nameLen == 0)
return retId; // ok, found
else
return 0; // here's a file, but there's something left in the path
}
} else {
node = node->next;
retId++;
}
}
return 0; // does not exist
}
|