-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIO.C
452 lines (367 loc) · 9.93 KB
/
IO.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
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
/***************************************************************
* *
* *
* FILE: IO functions *
* *
* *
***************************************************************/
#include "incs.h"
#ifdef PSX
#include "..\..\global\striker.def" // defs for PSX striker
#include "..\..\striker\frontend\mod.def"
#include "datafile.h"
#include "frontend.h"
#include "hardware.h"
#include "mem.h"
#include "utils.h"
#include "io.h"
#endif
#if CD_LOAD == YES
#define BIGFILE "\\DATAFILE.DAT"
#else
#define BIGFILE "\\datafile.dat"
#endif
/***************************************************************
* *
* Variables *
* *
***************************************************************/
FILE_HEADER file_header;
LONG hd_fp;
CdlFILE cd_fp;
LONG io_status = FCLOSED;
BYTE datafile_buffer[2048]; //storage for datafile.dat containing
//offset to sector for each file
/************************************************************
* *
* FUNCTION: Load a chunk of data from HD/CD to memory. *
* *
* PASSED: Pointer to filename, *
* which file in big file to read, *
* buffer to load to, *
* max size of buffer, *
* file control. (eg open it:read it:close it) *
* *
* RETURNS: Size of file. (0=error) *
* *
* NOTES: With this method we can cut down on file *
* opening times as the file only gets opened *
* once and we seek to each data lump position *
* within it. *
* buffer must be at least 2k *
* *
************************************************************/
ULONG read_io_data( BYTE *filename, LONG index, BYTE *buffer, ULONG buffsize, BYTE control )
{
ULONG size = 0;
LONG n = 0;
#if CD_LOAD == NO
size = read_hd_data( filename, index, buffer, buffsize, control );
#elif CD_LOAD == YES
while( !size )
{
size = read_cd_data( filename, index, buffer, buffsize, control );
if( !size )
{
for(n=0 ; n<30*5 ; n++)
VSync(2);
print_error( E_READSAM, NULL, -1 );
}
}
#endif
return( size );
}
/************************************************************
* *
* FUNCTION: Load a chunk of data from HD to memory. *
* *
* PASSED: Pointer to filename, *
* which file in big file to read, *
* buffer to load to, *
* max size of buffer, *
* file control. (eg open it:read it:close it) *
* *
* RETURNS: Size of file. (0=error) *
* *
* NOTES: With this method we can cut down on file *
* opening times as the file only gets opened *
* once and we seek to each data lump position *
* within it. *
* *
************************************************************/
ULONG read_hd_data( BYTE *filename, LONG index, BYTE *buffer, ULONG buffsize, BYTE control )
{
#if CD_LOAD == NO
ULONG size = 0;
UBYTE newname[100];
ULONG n=0;
FILE_HEADER *ptr;
// Open big file //
//---------------//
if( control & OPEN_IT )
{
// strcpy( newname, "c:\\shell\\front\\src\\object\\" );
strcpy( newname, filename );
// strcat( newname, filename );
hd_fp = PCopen( newname, READ, 0 );
if( hd_fp == -1 )
{
PCclose( hd_fp );
print_error( E_CANT_FIND, newname, -1 );
return(0);
}
io_status = FOPENED;
// Clear all file header info //
#if 0
file_header.numfiles = -1;
for(n=0 ; n<MAX_FILES ; n++)
{
file_header.file[n].seek_pos =
file_header.file[n].size = -1;
}
#endif
// Read big file header into RAM for later use //
PClseek( hd_fp, 0, FROM_START );
PCread( hd_fp, buffer, 2048 );
ptr = (FILE_HEADER *) buffer;
file_header.numfiles = ptr->numfiles;
for(n=0 ; n<file_header.numfiles ; n++)
{
file_header.file[n].seek_pos = ptr->file[n].seek_pos;
file_header.file[n].size = ptr->file[n].size;
}
size = 2048;
}
// Read piece of file into memory //
//--------------------------------//
if( control & READ_IT )
{
if( file_header.file[index].size > buffsize )
{
#if DEBUG
printf( "filenum %d too big; filesize=%d bytes ; buffer=%d bytes",index,file_header.file[index].size,buffsize );
#endif
return(0);
}
PClseek( hd_fp, file_header.file[index].seek_pos, FROM_START );
PCread( hd_fp, buffer, file_header.file[index].size );
size = file_header.file[index].size;
// printf("loaded %dk\n", size/1024);
}
// Close big file //
//----------------//
if( control & CLOSE_IT )
{
PCclose( hd_fp );
io_status = FCLOSED;
size = file_header.file[index].size;
}
return(size);
#elif CD_LOAD == YES
return(0);
#endif
}
/*---------------------------------------------------------*/
/* */
/* FUNCTION: Load a chunk of data from CD to memory. */
/* */
/* PASSED: Pointer to filename, */
/* which file in big file to read, */
/* buffer to load to, */
/* max size of buffer, */
/* file control. (eg open it:read it:close it) */
/* */
/* RETURNS: Size of file. (0=error) */
/* */
/* NOTES: With this method we can cut down on file */
/* opening times as the file only gets opened */
/* once and we seek to each data lump position */
/* within it. */
/* buffer must be at least 2k */
/* */
/*---------------------------------------------------------*/
ULONG read_cd_data( BYTE *filename, LONG index, BYTE *buffer, ULONG buffsize, BYTE control )
{
#if CD_LOAD == YES
CdlFILE temp;
CdlFILE *fp = &cd_fp;
ULONG size = 0;
UBYTE newname[100];
FILE_HEADER *ptr;
ULONG n;
// Open big file //
//---------------//
if( control & OPEN_IT )
{
strcpy( newname, filename );
strcat( newname, ";1" );
if( !CdSearchFile(fp, newname) )
{
#if DEBUG
printf( "cant find filenum %d", newname );
#endif
return(0);
}
io_status = FOPENED;
// Clear all file header info //
#if 0
file_header.numfiles = -1;
for(n=0 ; n<MAX_FILES ; n++)
{
file_header.file[n].seek_pos =
file_header.file[n].size = -1;
}
#endif
// Read big file header into RAM for later use //
if( !get_cd_data(buffer, fp, 2048) )
return(0);
ptr = (FILE_HEADER *) buffer;
file_header.numfiles = ptr->numfiles;
for(n=0 ; n<file_header.numfiles ; n++)
{
file_header.file[n].seek_pos = ptr->file[n].seek_pos;
file_header.file[n].size = ptr->file[n].size;
}
size = 2048;
}
// Read piece of file into memory //
//--------------------------------//
if( control & READ_IT )
{
if( io_status == FCLOSED )
{
#if DEBUG
printf( "file not opened\n" );
#endif
return(0);
}
if( file_header.file[index].size > buffsize )
{
n = file_header.file[index].size;
#if DEBUG
printf( "filenum %d too big; filesize=%d bytes ; buffer=%d bytes",index,file_header.file[index].size,buffsize );
#endif
return(0);
}
temp.pos.minute = cd_fp.pos.minute;
temp.pos.second = cd_fp.pos.second;
temp.pos.sector = cd_fp.pos.sector;
temp.pos.track = cd_fp.pos.track;
n = file_header.file[index].seek_pos / 2048;
n += CdPosToInt( &cd_fp.pos );
CdIntToPos( n, (CdlLOC *) &temp );
if( !get_cd_data(buffer, &temp, file_header.file[index].size) )
size = 0;
else
{
size = file_header.file[index].size;
}
}
// Close big file //
//----------------//
if( control & CLOSE_IT )
{
io_status = FCLOSED;
size = file_header.file[index].size;
}
return(size);
#elif CD_LOAD == NO
return(0);
#endif
}
/*---------------------------------------------------------*/
/* */
/* FUNCTION: Read in data from CD. */
/* */
/* PASSED: buffer to load to, */
/* pointer to file structure with seek pos in, */
/* size of file in bytes. */
/* */
/* RETURNS: Size of file. (0=error : 1=ok) */
/* */
/*---------------------------------------------------------*/
ULONG get_cd_data( BYTE *buffer, CdlFILE *fp, ULONG size )
{
#if CD_LOAD == YES
LONG n,i;
size = (size+2047) / 2048;
if( !CdControl(CdlSeekL, (UBYTE *) &fp->pos, 0))
{
print_error( E_SEEK, NULL, -1 );
return(0);
}
while( (n=CdSync(1,0)) != CdlComplete )
{
if( n == CdlDiskError ) return(0);
for(i=0 ; i<3 ; i++) VSync(2);
// Vsync(0);
}
if( !CdRead(size, (ULONG *) buffer, CdlModeSpeed) )
{
print_error( E_READ, NULL, -1 );
return(0);
}
while( (n=CdReadSync(1,0)) > 0 )
{
for(i=0 ; i<3 ; i++) VSync(2);
}
if( n < 0 ) return(0);
#endif
return(1);
}
/*
Please note..
Elvis
a
t
shit
*/
/******************************************
* *
* Functions utilising IO functions above *
* *
******************************************/
void open_datafile()
{
//**** Open the large datafile ****
// CdInit();
//#if CD_LOAD == YES
// CdInit();
//#else
// PCinit();
//#endif
read_io_data ( BIGFILE, NULL, datafile_buffer, 0xffffff, OPEN_IT );
}
void read_datafile ( WORD file_num, BYTE *addr, LONG max_size )
{
LONG size;
//**** Read a file from within the large datafile ****
size = read_io_data ( BIGFILE, file_num, addr, max_size, READ_IT );
// printf("loading file %d\n",file_num );
}
void close_datafile()
{
//**** Close the large datafile ****
read_io_data ( BIGFILE, NULL, datafile_buffer, 0xffffff, CLOSE_IT );
}
void *read_datafile_alloc ( WORD file_num )
{
//**** Load a file from the datafile into an allocated area of memory ****
BYTE *buffer;
ULONG file_size;
// printf("loading...\n");
file_size = file_header.file[file_num].size;
buffer = allocate_mem ( 0, file_size );
if ( buffer )
{
read_datafile ( file_num, buffer, 1000000 );
}
else
{
#if DEBUG
printf("*** Could not allocate %d bytes for filenum %d' ***\n",file_size,file_num );
#endif
}
return buffer;
}