Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/mednadisc/cdrom/CDUtility.cpp
2 views
1
/* Mednafen - Multi-system Emulator
2
*
3
* Subchannel Q CRC Code: Copyright (C) 1998 Andreas Mueller <[email protected]>
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
*/
19
20
#include <string.h>
21
#include <assert.h>
22
23
#include "emuware/emuware.h"
24
#include "CDUtility.h"
25
#include "dvdisaster.h"
26
#include "lec.h"
27
28
#include <assert.h>
29
// Kill_LEC_Correct();
30
31
32
namespace CDUtility
33
{
34
35
// lookup table for crc calculation
36
static uint16 subq_crctab[256] =
37
{
38
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108,
39
0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 0x1231, 0x0210,
40
0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, 0x9339, 0x8318, 0xB37B,
41
0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, 0x2462, 0x3443, 0x0420, 0x1401,
42
0x64E6, 0x74C7, 0x44A4, 0x5485, 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE,
43
0xF5CF, 0xC5AC, 0xD58D, 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6,
44
0x5695, 0x46B4, 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D,
45
0xC7BC, 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
46
0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, 0x5AF5,
47
0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, 0xDBFD, 0xCBDC,
48
0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, 0x6CA6, 0x7C87, 0x4CE4,
49
0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD,
50
0xAD2A, 0xBD0B, 0x8D68, 0x9D49, 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13,
51
0x2E32, 0x1E51, 0x0E70, 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A,
52
0x9F59, 0x8F78, 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E,
53
0xE16F, 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
54
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, 0x02B1,
55
0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, 0xB5EA, 0xA5CB,
56
0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, 0x34E2, 0x24C3, 0x14A0,
57
0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xA7DB, 0xB7FA, 0x8799, 0x97B8,
58
0xE75F, 0xF77E, 0xC71D, 0xD73C, 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657,
59
0x7676, 0x4615, 0x5634, 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9,
60
0xB98A, 0xA9AB, 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882,
61
0x28A3, 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
62
0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, 0xFD2E,
63
0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, 0x7C26, 0x6C07,
64
0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, 0xEF1F, 0xFF3E, 0xCF5D,
65
0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 0x4E55, 0x5E74,
66
0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
67
};
68
69
70
static uint8 scramble_table[2352 - 12];
71
72
static bool CDUtility_Inited = false;
73
74
static void InitScrambleTable(void)
75
{
76
unsigned cv = 1;
77
78
for(unsigned i = 12; i < 2352; i++)
79
{
80
unsigned char z = 0;
81
82
for(int b = 0; b < 8; b++)
83
{
84
z |= (cv & 1) << b;
85
86
int feedback = ((cv >> 1) & 1) ^ (cv & 1);
87
cv = (cv >> 1) | (feedback << 14);
88
}
89
90
scramble_table[i - 12] = z;
91
}
92
93
//for(int i = 0; i < 2352 - 12; i++)
94
// printf("0x%02x, ", scramble_table[i]);
95
}
96
97
void CDUtility_Init(void)
98
{
99
if(!CDUtility_Inited)
100
{
101
Init_LEC_Correct();
102
103
InitScrambleTable();
104
105
CDUtility_Inited = true;
106
}
107
}
108
109
void encode_mode0_sector(uint32 aba, uint8 *sector_data)
110
{
111
CDUtility_Init();
112
113
lec_encode_mode0_sector(aba, sector_data);
114
}
115
116
void encode_mode1_sector(uint32 aba, uint8 *sector_data)
117
{
118
CDUtility_Init();
119
120
lec_encode_mode1_sector(aba, sector_data);
121
}
122
123
void encode_mode2_sector(uint32 aba, uint8 *sector_data)
124
{
125
CDUtility_Init();
126
127
lec_encode_mode2_sector(aba, sector_data);
128
}
129
130
void encode_mode2_form1_sector(uint32 aba, uint8 *sector_data)
131
{
132
CDUtility_Init();
133
134
lec_encode_mode2_form1_sector(aba, sector_data);
135
}
136
137
void encode_mode2_form2_sector(uint32 aba, uint8 *sector_data)
138
{
139
CDUtility_Init();
140
141
lec_encode_mode2_form2_sector(aba, sector_data);
142
}
143
144
bool edc_check(const uint8 *sector_data, bool xa)
145
{
146
CDUtility_Init();
147
148
return(CheckEDC(sector_data, xa));
149
}
150
151
bool edc_lec_check_and_correct(uint8 *sector_data, bool xa)
152
{
153
CDUtility_Init();
154
155
return(ValidateRawSector(sector_data, xa));
156
}
157
158
159
bool subq_check_checksum(const uint8 *SubQBuf)
160
{
161
uint16 crc = 0;
162
uint16 stored_crc = 0;
163
164
stored_crc = SubQBuf[0xA] << 8;
165
stored_crc |= SubQBuf[0xB];
166
167
for(int i = 0; i < 0xA; i++)
168
crc = subq_crctab[(crc >> 8) ^ SubQBuf[i]] ^ (crc << 8);
169
170
crc = ~crc;
171
172
return(crc == stored_crc);
173
}
174
175
void subq_generate_checksum(uint8 *buf)
176
{
177
uint16 crc = 0;
178
179
for(int i = 0; i < 0xA; i++)
180
crc = subq_crctab[(crc >> 8) ^ buf[i]] ^ (crc << 8);
181
182
// Checksum
183
buf[0xa] = ~(crc >> 8);
184
buf[0xb] = ~(crc);
185
}
186
187
void subq_deinterleave(const uint8 *SubPWBuf, uint8 *qbuf)
188
{
189
memset(qbuf, 0, 0xC);
190
191
for(int i = 0; i < 96; i++)
192
{
193
qbuf[i >> 3] |= ((SubPWBuf[i] >> 6) & 0x1) << (7 - (i & 0x7));
194
}
195
}
196
197
198
// Deinterleaves 96 bytes of subchannel P-W data from 96 bytes of interleaved subchannel PW data.
199
void subpw_deinterleave(const uint8 *in_buf, uint8 *out_buf)
200
{
201
assert(in_buf != out_buf);
202
203
memset(out_buf, 0, 96);
204
205
for(unsigned ch = 0; ch < 8; ch++)
206
{
207
for(unsigned i = 0; i < 96; i++)
208
{
209
out_buf[(ch * 12) + (i >> 3)] |= ((in_buf[i] >> (7 - ch)) & 0x1) << (7 - (i & 0x7));
210
}
211
}
212
213
}
214
215
// Interleaves 96 bytes of subchannel P-W data from 96 bytes of uninterleaved subchannel PW data.
216
void subpw_interleave(const uint8 *in_buf, uint8 *out_buf)
217
{
218
assert(in_buf != out_buf);
219
220
for(unsigned d = 0; d < 12; d++)
221
{
222
for(unsigned bitpoodle = 0; bitpoodle < 8; bitpoodle++)
223
{
224
uint8 rawb = 0;
225
226
for(unsigned ch = 0; ch < 8; ch++)
227
{
228
rawb |= ((in_buf[ch * 12 + d] >> (7 - bitpoodle)) & 1) << (7 - ch);
229
}
230
out_buf[(d << 3) + bitpoodle] = rawb;
231
}
232
}
233
}
234
235
// NOTES ON LEADOUT AREA SYNTHESIS
236
//
237
// I'm not trusting that the "control" field for the TOC leadout entry will always be set properly, so | the control fields for the last track entry
238
// and the leadout entry together before extracting the D2 bit. Audio track->data leadout is fairly benign though maybe noisy(especially if we ever implement
239
// data scrambling properly), but data track->audio leadout could break things in an insidious manner for the more accurate drive emulation code).
240
//
241
void subpw_synth_leadout_lba(const TOC& toc, const int32 lba, uint8* SubPWBuf)
242
{
243
uint8 buf[0xC];
244
uint32 lba_relative;
245
uint32 ma, sa, fa;
246
uint32 m, s, f;
247
248
lba_relative = lba - toc.tracks[100].lba;
249
250
f = (lba_relative % 75);
251
s = ((lba_relative / 75) % 60);
252
m = (lba_relative / 75 / 60);
253
254
fa = (lba + 150) % 75;
255
sa = ((lba + 150) / 75) % 60;
256
ma = ((lba + 150) / 75 / 60);
257
258
uint8 adr = 0x1; // Q channel data encodes position
259
uint8 control = toc.tracks[100].control;
260
261
if(toc.tracks[toc.last_track].valid)
262
control |= toc.tracks[toc.last_track].control & 0x4;
263
else if(toc.disc_type == DISC_TYPE_CD_I)
264
control |= 0x4;
265
266
memset(buf, 0, 0xC);
267
buf[0] = (adr << 0) | (control << 4);
268
buf[1] = 0xAA;
269
buf[2] = 0x01;
270
271
// Track relative MSF address
272
buf[3] = U8_to_BCD(m);
273
buf[4] = U8_to_BCD(s);
274
buf[5] = U8_to_BCD(f);
275
276
buf[6] = 0; // Zerroooo
277
278
// Absolute MSF address
279
buf[7] = U8_to_BCD(ma);
280
buf[8] = U8_to_BCD(sa);
281
buf[9] = U8_to_BCD(fa);
282
283
subq_generate_checksum(buf);
284
285
for(int i = 0; i < 96; i++)
286
SubPWBuf[i] = (((buf[i >> 3] >> (7 - (i & 0x7))) & 1) ? 0x40 : 0x00) | 0x80;
287
}
288
289
void synth_leadout_sector_lba(uint8 mode, const TOC& toc, const int32 lba, uint8* out_buf)
290
{
291
memset(out_buf, 0, 2352 + 96);
292
subpw_synth_leadout_lba(toc, lba, out_buf + 2352);
293
294
if(out_buf[2352 + 1] & 0x40)
295
{
296
if(mode == 0xFF)
297
{
298
if(toc.disc_type == DISC_TYPE_CD_XA || toc.disc_type == DISC_TYPE_CD_I)
299
mode = 0x02;
300
else
301
mode = 0x01;
302
}
303
304
switch(mode)
305
{
306
default:
307
encode_mode0_sector(LBA_to_ABA(lba), out_buf);
308
break;
309
310
case 0x01:
311
encode_mode1_sector(LBA_to_ABA(lba), out_buf);
312
break;
313
314
case 0x02:
315
out_buf[12 + 6] = 0x20;
316
out_buf[12 + 10] = 0x20;
317
encode_mode2_form2_sector(LBA_to_ABA(lba), out_buf);
318
break;
319
}
320
}
321
}
322
323
// ISO/IEC 10149:1995 (E): 20.2
324
//
325
void subpw_synth_udapp_lba(const TOC& toc, const int32 lba, const int32 lba_subq_relative_offs, uint8* SubPWBuf)
326
{
327
uint8 buf[0xC];
328
uint32 lba_relative;
329
uint32 ma, sa, fa;
330
uint32 m, s, f;
331
332
if(lba < -150 || lba >= 0)
333
printf("[BUG] subpw_synth_udapp_lba() lba out of range --- %d\n", lba);
334
335
{
336
int32 lba_tmp = lba + lba_subq_relative_offs;
337
338
if(lba_tmp < 0)
339
lba_relative = 0 - 1 - lba_tmp;
340
else
341
lba_relative = lba_tmp - 0;
342
}
343
344
f = (lba_relative % 75);
345
s = ((lba_relative / 75) % 60);
346
m = (lba_relative / 75 / 60);
347
348
fa = (lba + 150) % 75;
349
sa = ((lba + 150) / 75) % 60;
350
ma = ((lba + 150) / 75 / 60);
351
352
uint8 adr = 0x1; // Q channel data encodes position
353
uint8 control;
354
355
if(toc.disc_type == DISC_TYPE_CD_I && toc.first_track > 1)
356
control = 0x4;
357
else if(toc.tracks[toc.first_track].valid)
358
control = toc.tracks[toc.first_track].control;
359
else
360
control = 0x0;
361
362
memset(buf, 0, 0xC);
363
buf[0] = (adr << 0) | (control << 4);
364
buf[1] = U8_to_BCD(toc.first_track);
365
buf[2] = U8_to_BCD(0x00);
366
367
// Track relative MSF address
368
buf[3] = U8_to_BCD(m);
369
buf[4] = U8_to_BCD(s);
370
buf[5] = U8_to_BCD(f);
371
372
buf[6] = 0; // Zerroooo
373
374
// Absolute MSF address
375
buf[7] = U8_to_BCD(ma);
376
buf[8] = U8_to_BCD(sa);
377
buf[9] = U8_to_BCD(fa);
378
379
subq_generate_checksum(buf);
380
381
for(int i = 0; i < 96; i++)
382
SubPWBuf[i] = (((buf[i >> 3] >> (7 - (i & 0x7))) & 1) ? 0x40 : 0x00) | 0x80;
383
}
384
385
void synth_udapp_sector_lba(uint8 mode, const TOC& toc, const int32 lba, int32 lba_subq_relative_offs, uint8* out_buf)
386
{
387
memset(out_buf, 0, 2352 + 96);
388
subpw_synth_udapp_lba(toc, lba, lba_subq_relative_offs, out_buf + 2352);
389
390
if(out_buf[2352 + 1] & 0x40)
391
{
392
if(mode == 0xFF)
393
{
394
if(toc.disc_type == DISC_TYPE_CD_XA || toc.disc_type == DISC_TYPE_CD_I)
395
mode = 0x02;
396
else
397
mode = 0x01;
398
}
399
400
switch(mode)
401
{
402
default:
403
encode_mode0_sector(LBA_to_ABA(lba), out_buf);
404
break;
405
406
case 0x01:
407
encode_mode1_sector(LBA_to_ABA(lba), out_buf);
408
break;
409
410
case 0x02:
411
out_buf[12 + 6] = 0x20;
412
out_buf[12 + 10] = 0x20;
413
encode_mode2_form2_sector(LBA_to_ABA(lba), out_buf);
414
break;
415
}
416
}
417
}
418
419
#if 0
420
bool subq_extrapolate(const uint8 *subq_input, int32 position_delta, uint8 *subq_output)
421
{
422
assert(subq_check_checksum(subq_input));
423
424
425
subq_generate_checksum(subq_output);
426
}
427
#endif
428
429
void scrambleize_data_sector(uint8 *sector_data)
430
{
431
for(unsigned i = 12; i < 2352; i++)
432
sector_data[i] ^= scramble_table[i - 12];
433
}
434
435
}
436
437