-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathCD.C
551 lines (461 loc) · 10.2 KB
/
CD.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
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
#include "CD.H"
#include "CONTROL.H"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <SYS/TYPES.H>
#include <LIBCD.H>
#include <LIBSPU.H>
#include <LIBETC.H>
//Number of XA files on disc (XA1-17.XA)
#define NUM_XA_FILES 17
#define XA_FILE_NAME "\\XA%d.XA;1"
#define CDS "CDS!" + __TIMESTAMP__ + " ."
unsigned short XATrackClip[] =//Probably used for XA audio start/end pos of each track
{
0xFF00, 0xFF00, 0xFE80, 0xFF00, 0xFF00, 0xFF00, 0xFF00, 0xFF80,
0xFF00, 0xFF00, 0xFF00, 0xFF00, 0xFF00, 0xFF00, 0xFF00, 0xFF00,
0xFE80, 0xFF00, 0xFF00, 0xFF00, 0xFF00, 0xFF00, 0xFF80, 0xFF80,
0xFE00, 0xFF00, 0xFF00, 0xFF00, 0xFF00, 0xFE00, 0xFF80, 0xFF80,
0xFF00, 0xFF00, 0xFF80, 0xFF80, 0xFF00, 0xFF00, 0xFF00, 0xFF80,
0xFF80, 0xFE00, 0xFF80, 0xFF00, 0xFF80, 0xFF80, 0xFF80, 0xFF80,
0xFF00, 0xFF00, 0xFF80, 0xFF80, 0xFF80, 0xFF80, 0xFF80, 0xFF00,
0xFE00, 0xFF00, 0xFF80, 0xFF80, 0xFF80, 0xFF80, 0xFF00, 0xFF80,
0xFE00, 0xFD80, 0xFD80, 0xFE80, 0xFE80, 0xFE80, 0xFF00, 0xFF80,
0xFA00, 0xFC00, 0xFC00, 0xFC00, 0xFD80, 0xFF00, 0xFE00, 0xFF80,
0xFE00, 0xFE00, 0xFD80, 0xFE80, 0xFF00, 0xFF00, 0xFF00, 0xFF00,
0xFA00, 0xFB00, 0xFC00, 0xFB80, 0xFC80, 0xFC80, 0xFF00, 0xFFC0,
0xFC00, 0xFD00, 0xFD00, 0xFD00, 0xFE80, 0xFE80, 0xFF00, 0xFF80,
0xF700, 0xF880, 0xF780, 0xFC80, 0xFE80, 0xFE80, 0xFD80, 0xFE80,
0xEF80, 0xF000, 0xF280, 0xF480, 0xF700, 0xFD80, 0xFD00, 0xFF80,
0xF880, 0xFD80, 0xFD00, 0xFE00, 0xFE80, 0xFF40, 0xFF80, 0xFF80,
0xD200, 0xD500, 0xD600, 0xD880, 0xD880, 0xDB80, 0xE900, 0xFE80
};
short XAFadeRate = 8;
int current_cd_mode = -1;
volatile int XACurPos;
volatile int XAEndPos;
volatile short XAVolume;
volatile short XAReqVolume;
volatile char XAFlag;
short XAMasterVolume;
short XATrack;
short XAReqTrack;
char XAWait;
static char XARepeat;
int XAStartPos;
static int XATrackList[17][2];
#if PLAY_FMVS
int dword_A5660[8];
#endif
//Holds all game data file positions or offsets in GAMEWAD.OBJ.
struct GAMEWAD_header gwHeader;
//LBA for the GAMEWAD.OBJ file on disc, this is set by InitNewCDSystem(), CDPosToInt() (See CD.C)
int gwLba = 0;
//Start sector for the current gamewad file entry.
static int cdStartSector = 0;
//Current sector for the gamewad file entry, updated as data is read from disk.
int cdCurrentSector = 0;
void CDDA_SetVolume(int nVolume)//5D7FC(<), 5DC78(<) (F) (*) (D)
{
#ifndef NO_SOUND
SpuCommonAttr attr;
attr.mask = SPU_COMMON_CDVOLL | SPU_COMMON_CDVOLR | SPU_COMMON_CDMIX;
attr.cd.volume.left = nVolume << 6;
attr.cd.volume.right = nVolume << 6;
attr.cd.mix = SPU_ON;
SpuSetCommonAttr(&attr);
#endif
}
void XAReplay()//5D838(<), 5DCB4(<) (F) (*) (D)
{
CdlLOC loc;
CdIntToPos(XAStartPos, &loc);
if (CdControl(CdlReadS, (unsigned char*)&loc, NULL) == 1)
{
XACurPos = XAStartPos;
}
return;
}
void cbvsync()//5D884(<), 5DD00(<) (F)
{
int ret;
unsigned char io[8];
int cnt;
switch (XAFlag)
{
case 1:
{
if (XAVolume == 0)
{
XAFlag++;
}
//5DF9C
break;
}
case 2:
{
XATrack = XAReqTrack;
if (XAReqTrack < 0)
{
cnt = XAReqTrack + 7;
}
else
{
cnt = XAReqTrack;
}
XAStartPos = XATrackList[cnt >> 3][0];
io[0] = 1;
XAEndPos = XATrackList[cnt >> 3][1] + (short)XATrackClip[XAReqTrack];
io[1] = XATrack & 7;
XACurPos = XAStartPos;
CdControlF(CdlSetfilter, &io[0]);
XAFlag++;
break;
}
case 3:
{
//loc_5D980
XAReplay();
XAReqVolume = XAMasterVolume;
XAFlag++;
break;
}
case 4:
{
//loc_5D9AC
if (XAVolume == XAMasterVolume)
{
XAFlag++;
XAWait = 60;
}
break;
}
case 5:
{
//loc_5D9E0
if (XAWait == 0)
{
XAFlag++;
}
XAWait--;
break;
}
case 6:
{
//loc_5DA18
if (((VSync(-1)) & 7) == 0)
{
ret = CdSync(1, &io[0]);
if (ret == 5)
{
XAReplay();
}
else if (ret == 2)
{
if (XACurPos >= XAEndPos)
{
if (XARepeat)
{
XAReplay();
}
else
{
if (CurrentAtmosphere == 0)
{
CdControlB(CdlPause, NULL, NULL);
XAFlag = 7;
}
else
{
//5DAB8
XAVolume = 0;
XARepeat = 1;
XAFlag = ret;
XAReqTrack = CurrentAtmosphere;
CDDA_SetVolume(0);
IsAtmospherePlaying = 1;
}
}
}
else
{
//5DAEC
if (CdLastCom() == CdlGetlocP)
{
cnt = CdPosToInt((CdlLOC*)&io[5]);
if (cnt > 0)
{
XACurPos = cnt;
}
}
CdControlF(CdlGetlocP, NULL);
}
}
}
break;
}
case 7:
break;
default:
break;
}
if (XAVolume < XAReqVolume)
{
XAVolume += XAFadeRate;
if (XAVolume >= XAReqVolume)
{
XAVolume = XAReqVolume;
}
CDDA_SetVolume(XAVolume);
}
else if (XAVolume > XAReqVolume)
{
XAVolume -= XAFadeRate;
if (XAVolume <= XAVolume)
{
XAVolume = XAReqVolume;
}
CDDA_SetVolume(XAVolume);
}
//loc_5DC00
return;
}
void S_CDPlay(short track, int mode)//5DC10(<), 5E08C(<) (F) (*) (D)
{
unsigned char param[4];
if (XATrack == -1)
{
param[0] = CdlModeSpeed | CdlModeRT | CdlModeSF;
CdControlB(CdlSetmode, ¶m[0], NULL);
VSync(3);
CdControlB(CdlPause, NULL, NULL);
DEL_ChangeCDMode(1);
}
if (XATrack != track && XAReqTrack != track)
{
XAReqTrack = track;
XARepeat = mode;
if (XAFlag != 0)
{
XAFlag = 1;
XAReqVolume = 0;
}
else
{
XAFlag = 2;
}
}
//loc_5DCBC
return;
}
void S_CDStop()//5DCD0(<), 5E14C(<) (F) (*) (D)
{
XAFlag = 0;
CdControlB(CdlPause, NULL, NULL);
XAReqTrack = -1;
XATrack = -1;
DEL_ChangeCDMode(0);
return;
}
void S_CDPause()//5DD14(<), 5E190(<) (F) (*) (D)
{
if (XATrack >= 0)
{
CdControlF(CdlPause, NULL);
}
return;
}
void S_CDRestart()//5DD40(<) (F) (*)
{
if (XATrack >= 0 && XAFlag != 7)
{
CdControlF(CdlReadS, NULL);
}
return;
}
void S_StartSyncedAudio(int nTrack)//5DD78(<), 5E1F4(<) (F) (*) (D)
{
IsAtmospherePlaying = 0;
S_CDPlay(nTrack, 0);
while (XAFlag < 4) {}
VSync(29);
return;
}
void CDDA_SetMasterVolume(int nVolume)//5DDC4(<), 5E240(<) (F) (*) (D)
{
XAMasterVolume = nVolume;
CDDA_SetVolume(nVolume);
}
void InitNewCDSystem()//5DDE8(<), 5E264(<) (F) (*) (D) (ND)
{
int i;
long local_wadfile_header[512];
CdlFILE fp;
char buf[80];
DEL_ChangeCDMode(0);
#if DISC_VERSION
CdSearchFile(&fp, GAMEWAD_FILENAME);//662F0
CdControlB(CdlSetloc, (unsigned char*)&fp, NULL);//6956C
CdRead(1, (unsigned long*)&local_wadfile_header[0], CdlModeSpeed); //69C4C
while (CdReadSync(1, 0) > 0)
{
VSync(0);
}
memcpy(&gwHeader, &local_wadfile_header[0], 512);//5F6AC
gwLba = CdPosToInt(&fp.pos);//66270
#endif
//loc_5E2E8
for (i = 0; i < NUM_XA_FILES; i++)
{
sprintf(&buf[0], XA_FILE_NAME, i + 1);
CdSearchFile(&fp, &buf[0]);
XATrackList[i][0] = CdPosToInt(&fp.pos);
XATrackList[i][1] = XATrackList[i][0] + ((fp.size + ((1 << CD_SECTOR_SHIFT) - 1)) >> CD_SECTOR_SHIFT);
}
XAFlag = 0;
XAVolume = 0;
XAReqTrack = -1;
XATrack = -1;
#if PLAY_FMVS
sub_5E708();
#endif
}
void DEL_ChangeCDMode(int mode)//5DEB0(<), 5E650 (F) (*) (D)
{
unsigned char param[4];
if (mode == 0)
{
if (current_cd_mode != 0)
{
current_cd_mode = 0;
param[0] = CdlModeSpeed;
CdControlB(CdlSetmode, ¶m[0], NULL);
VSync(3);
}
}
else if (mode == 1)
{
//loc_5DEF8
if (current_cd_mode != 1)
{
current_cd_mode = mode;
}
}
else if (mode == 2)
{
//loc_5DF20
if (current_cd_mode != 2)
{
current_cd_mode = mode;
param[0] = CdlModeSpeed;
CdControlB(CdlSetmode, ¶m[0], NULL);
VSync(3);
}
}
//loc_5DF58
return;
}
/*
* [FUNCTIONALITY] - DEL_CDFS_OpenFile.
* GAMEWAD_header is already in the memory at this point, we loaded it during InitNewCDSystem() (See CD.C)
* This method initialises the CD's read sector for a specific gamewad entry.
* File ID indices must match GAMEWAD.OBJ, see gw_files enum (GAMEWAD.H)
* Note: A File ID of "NONE" or 0 will initialise the reader position back to 0.
*
* [USAGE]
* @PARAM - [filenum] index into GAMEWAD_header.entries you wish to seek to.
* @RETURN - Filesize of the gamewad entry in bytes.
*/
int DEL_CDFS_OpenFile(int filenum /*$a0*/)//*, 5E3C0(<) (F) (D)
{
DEL_ChangeCDMode(0);
cdCurrentSector = cdStartSector = gwLba + (gwHeader.entries[filenum].fileOffset >> CD_SECTOR_SHIFT);
return gwHeader.entries[filenum].fileSize;
}
/*
* [FUNCTIONALITY] - DEL_CDFS_Read
* It is assumed that prior to calling this you have initialised the cd's
* reader's position to the file entry you wish to read see (DEL_CDFS_OpenFile)
* This method reads data from disc at it's last sectior/read position.
*
* [USAGE]
* @PARAM - [size] the number of bytes you wish to read [addr] the memory location the data is read to.
*/
int DEL_CDFS_Read(char* addr, int size)//*, 5E414(<) (F) (*) (D)
{
CdlFILE fp;
long numSectorsToRead;
long remainingDataToRead;
char buf[1 << CD_SECTOR_SHIFT];
DEL_ChangeCDMode(0);
remainingDataToRead = size & ((1 << CD_SECTOR_SHIFT) - 1);
numSectorsToRead = size >> CD_SECTOR_SHIFT;
if (numSectorsToRead != 0)
{
CdIntToPos(cdCurrentSector, &fp.pos);
CdControlB(CdlSetloc, (unsigned char*)&fp, NULL);
CdRead(numSectorsToRead, (unsigned long*)addr, CdlModeSpeed);
//loc_5E48C
while (CdReadSync(1, 0) > 0)
{
VSync(0);
}
cdCurrentSector += numSectorsToRead;
}//loc_5E4AC
if (remainingDataToRead != 0)
{
CdIntToPos(cdCurrentSector, &fp.pos);
CdControlB(CdlSetloc, (unsigned char*)&fp, NULL);
CdRead(1, (unsigned long*)&buf[0], CdlModeSpeed);
while (CdReadSync(1, 0) > 0)
{
VSync(0);
}
memcpy(&addr[numSectorsToRead << CD_SECTOR_SHIFT], &buf, remainingDataToRead);
cdCurrentSector++;
}
return 0;
}
/*
* [FUNCTIONALITY] - DEL_CDFS_Seek
* Seeks from the cd reader's current position.
* Note: Negative numbers will allow backwards traversal.
* [USAGE]
* @PARAM - [offset] the number of bytes you wish to seek (not in sectors).
*/
int DEL_CDFS_Seek(int offset /*$a0*/)//*, 5E54C(<) (F) (*) (D)
{
cdCurrentSector = cdStartSector + (offset >> CD_SECTOR_SHIFT);
return 0;
}
/*
* [FUNCTIONALITY] - FRIG_CD_POS_TO_CUR
* Updates the cd reader's start sector to current reader position.
*/
void FRIG_CD_POS_TO_CUR()//*, 5E564(<) (F) (*)
{
cdStartSector = cdCurrentSector;
}
#if PLAY_FMVS
void sub_5E708()
{
CdlFILE fp;
char buff[80];
int i;
//loc_5E738
for(i = 0; i < 8; i++)
{
sprintf(&buff[0], "\\FMV%d.STR;1", i);
if (CdSearchFile(&fp, &buff[0]))
{
//loc_5E764
dword_A5660[i] = CdPosToInt(&fp.pos);
}
else
{
dword_A5660[i] = -1;
}
}
}
#endif