/
sdfileio.c
321 lines (261 loc) · 7.22 KB
/
sdfileio.c
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
/****************************************************************************
* NeoCD Redux 0.1
* NeoGeo CD Emulator
* Copyright (C) 2007 softdev
****************************************************************************/
/****************************************************************************
* SD FileIO
*
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <ogc/mutex.h>
#include <sys/dir.h>
#include <fat.h>
#include "neocdredux.h"
#include "fileio.h"
#include <dirent.h>
/* Generic File I/O */
#define MAXFILES 32
static FILE *sdfsfiles[MAXFILES];
static GENHANDLER sdhandler;
static u32 sdmutex = 0;
#define MAXDIRENTRIES 0x4000
static char *direntries[MAXDIRENTRIES];
char msg[128];
#ifndef HW_RVL
extern const DISC_INTERFACE* WKF_slot;
#endif
extern const DISC_INTERFACE* IDEA_slot;
extern const DISC_INTERFACE* IDEB_slot;
int have_ROM;
/****************************************************************************
* SDFindFree
****************************************************************************/
int SDFindFree( void )
{
int i;
for( i = 0; i < MAXFILES; i++ )
{
if ( sdfsfiles[i] == NULL )
return i;
}
return -1;
}
/****************************************************************************
* SDfopen
****************************************************************************/
static u32
SDfopen (const char *filename, const char *mode)
{
/* No writing allowed */
if ( strstr(mode,"w") )
{
return 0;
}
/* Open for reading */
int handle = SDFindFree();
if ( handle == -1 )
{
sprintf(msg,"OUT OF HANDLES!");
ActionScreen(msg);
return 0;
}
while ( LWP_MutexLock( sdmutex ) );
sdfsfiles[handle] = fopen(filename, mode);
if ( sdfsfiles[handle] == NULL )
{
LWP_MutexUnlock( sdmutex );
return 0;
}
LWP_MutexUnlock( sdmutex );
return handle | 0x8000;
}
/****************************************************************************
* SDfclose
****************************************************************************/
static int
SDfclose (u32 fp)
{
while ( LWP_MutexLock( sdmutex ) );
if( sdfsfiles[fp & 0x7FFF] != NULL )
{
fclose(sdfsfiles[fp & 0x7FFF]);
sdfsfiles[fp & 0x7FFF] = NULL;
LWP_MutexUnlock( sdmutex );
return 1;
}
LWP_MutexUnlock( sdmutex );
return 0;
}
/****************************************************************************
* SDfread
****************************************************************************/
static u32
SDfread (char *buf, int block, int len, u32 fp)
{
int handle = ( fp & 0x7FFF );
if( sdfsfiles[handle] == NULL )
return 0;
while ( LWP_MutexLock ( sdmutex ) );
int bytesdone = fread(buf, block, len, sdfsfiles[handle]);
LWP_MutexUnlock( sdmutex );
return bytesdone;
}
/****************************************************************************
* SDSeek
****************************************************************************/
static int
SDfseek (u32 fp, int where, int whence)
{
int handle = ( fp & 0x7FFF );
if ( sdfsfiles[handle] == NULL )
{
sprintf(msg,"SEEK : Invalid Handle %d", handle);
ActionScreen(msg);
return -1;
}
return fseek(sdfsfiles[handle], where, whence );
}
/****************************************************************************
* SDftell
*
* Return current position
****************************************************************************/
static int
SDftell (u32 fp)
{
int handle = ( fp & 0x7FFF );
if ( sdfsfiles[handle] == NULL )
{
sprintf(msg,"FTELL : Invalid Handle %d", handle);
ActionScreen(msg);
return -1;
}
return ftell(sdfsfiles[handle]);
}
static void
SDfcloseall (void)
{
int i;
while ( LWP_MutexLock( sdmutex ) );
for( i = 0; i < MAXFILES; i++ )
{
if ( sdfsfiles[i] != NULL )
fclose(sdfsfiles[i]);
}
LWP_MutexUnlock( sdmutex );
}
void SortListing( int max )
{
int top,seek;
char *t;
for( top = 0; top < max - 1; top++ )
{
for( seek = top + 1; seek < max; seek++ )
{
if ( stricmp(direntries[top], direntries[seek]) > 0 )
{
t = direntries[top];
direntries[top] = direntries[seek];
direntries[seek] = t;
}
}
}
}
/****************************************************************************
* SDgetdir
****************************************************************************/
static int SDgetdir( char *thisdir )
{
DIR *dirs = NULL;
static int count = 0;
int i;
unsigned int *p;
struct dirent *entry;
for( i = 0; i < count; i++ )
free(direntries[i]);
count = 0;
dirs = opendir(thisdir);
if ( dirs != NULL )
{
entry = readdir(dirs);
while ( entry != NULL )
{
/* Only get subdirectories */
if (entry->d_type == DT_DIR)
if (strcmp(entry->d_name,".") &&
strcmp(entry->d_name,".."))
{
direntries[count++] = strdup(entry->d_name);
}
if ( count == MAXDIRENTRIES ) break;
entry = readdir(dirs);
}
}
if ( count > 1 )
SortListing(count);
memcpy(dirbuffer, &count, 4);
p = (unsigned int *)(dirbuffer + 4);
for ( i = 0; i < count; i++ )
{
memcpy(&p[i], &direntries[i], 4);
}
return count;
}
/****************************************************************************
* SDmount
****************************************************************************/
static void SDmount()
{
memset (basedir, 0, 1024);
memset (scratchdir, 0, 1024);
memset (dirbuffer, 0, 0x10000);
memset (root_dir, 0, 10);
// Define DIR search location by Device type
if (use_IDE) {
if ( IDEA_slot->startup() && fatMountSimple("IDEA", IDEA_slot) ) sprintf(root_dir,"IDEA:");
else if ( IDEB_slot->startup() && fatMountSimple("IDEB", IDEB_slot) ) sprintf(root_dir,"IDEB:");
else { ActionScreen ("IDE-EXI not initialized"); return; }
}
#ifdef HW_RVL
else if (use_SD) sprintf(root_dir,"sd:"); // search this DIR first
else if (use_USB) sprintf(root_dir,"usb:");
#else
else if (use_WKF) {
if ( WKF_slot->startup() && fatMountSimple("WKF", WKF_slot) ) sprintf(root_dir,"WKF:");
else { ActionScreen ("WKF not initialized"); return; }
}
#endif
// Test if DIR exists, else default to root DIR by Device
sprintf(basedir,"%s/neocd/roms/",root_dir);
DIR *dir = opendir(basedir);
if (!dir) sprintf(basedir,"%s/",root_dir);
else closedir(dir);
if ( !SDgetdir(basedir) )
return;
DirSelector();
// Do not show the banner if there is no title selected
if (have_ROM == 1) bannerscreen();
}
void
SD_SetHandler ()
{
/* Clear */
memset(&sdhandler, 0, sizeof(GENHANDLER));
memset(&sdfsfiles, 0, MAXFILES * 4);
memset(&direntries, 0, MAXDIRENTRIES * 4);
sdhandler.gen_fopen = SDfopen;
sdhandler.gen_fclose = SDfclose;
sdhandler.gen_fread = SDfread;
sdhandler.gen_fseek = SDfseek;
sdhandler.gen_ftell = SDftell;
sdhandler.gen_fcloseall = SDfcloseall;
sdhandler.gen_getdir = SDgetdir;
sdhandler.gen_mount = SDmount;
GEN_SetHandler (&sdhandler);
/* Set mutex */
LWP_MutexInit(&sdmutex, FALSE);
}