Debian Codegraph @

mednafen/cdrom/CDUtility.h

raw symbols SCIP
1 #ifndef __MDFN_CDROM_CDUTILITY_H
2 #define __MDFN_CDROM_CDUTILITY_H
3
4 namespace CDUtility
5 {
6 // Call once at app startup before creating any threads that could potentially cause re-entrancy to these functions.
7 // It will also be called automatically if needed for the first time a function in this namespace that requires
8 // the initialization function to be called is called, for potential
9 // usage in constructors of statically-declared objects.
10 void CDUtility_Init(void);
11
12 // Quick definitions here:
13 //
14 // ABA - Absolute block address, synonymous to absolute MSF
15 // aba = (m_a * 60 * 75) + (s_a * 75) + f_a
16 //
17 // LBA - Logical block address(related: data CDs are required to have a pregap of 2 seconds, IE 150 frames/sectors)
18 // lba = aba - 150
19
20
21 enum
22 {
23 ADR_NOQINFO = 0x00,
24 ADR_CURPOS = 0x01,
25 ADR_MCN = 0x02,
26 ADR_ISRC = 0x03
27 };
28
29
30 struct TOC_Track
31 {
32 uint8 adr;
33 uint8 control;
34 uint32 lba;
35 };
36
37 // SubQ control field flags.
38 enum
39 {
40 SUBQ_CTRLF_PRE = 0x01, // With 50/15us pre-emphasis.
41 SUBQ_CTRLF_DCP = 0x02, // Digital copy permitted.
42 SUBQ_CTRLF_DATA = 0x04, // Data track.
43 SUBQ_CTRLF_4CH = 0x08, // 4-channel CD-DA.
44 };
45
46 enum
47 {
48 DISC_TYPE_CDDA_OR_M1 = 0x00,
49 DISC_TYPE_CD_I = 0x10,
50 DISC_TYPE_CD_XA = 0x20
51 };
52
53 struct TOC
54 {
55 INLINE TOC()
56 {
57 Clear();
58 }
59
60 INLINE void Clear(void)
61 {
62 first_track = last_track = 0;
63 disc_type = 0;
64
65 memset(tracks, 0, sizeof(tracks)); // FIXME if we change TOC_Track to non-POD type.
66 }
67
68 INLINE int FindTrackByLBA(uint32 LBA)
69 {
70 for(int32 track = first_track; track <= (last_track + 1); track++)
71 {
72 if(track == (last_track + 1))
73 {
74 if(LBA < tracks[100].lba)
75 return(track - 1);
76 }
77 else
78 {
79 if(LBA < tracks[track].lba)
80 return(track - 1);
81 }
82 }
83 return(0);
84 }
85
86 uint8 first_track;
87 uint8 last_track;
88 uint8 disc_type;
89 TOC_Track tracks[100 + 1]; // [0] is unused, [100] is for the leadout track.
90 // Also, for convenience, tracks[last_track + 1] will always refer
91 // to the leadout track(even if last_track < 99, IE the leadout track details are duplicated).
92 };
93
94 //
95 // Address conversion functions.
96 //
97 static INLINE uint32 AMSF_to_ABA(int32 m_a, int32 s_a, int32 f_a)
98 {
99 return(f_a + 75 * s_a + 75 * 60 * m_a);
100 }
101
102 static INLINE void ABA_to_AMSF(uint32 aba, uint8 *m_a, uint8 *s_a, uint8 *f_a)
103 {
104 *m_a = aba / 75 / 60;
105 *s_a = (aba - *m_a * 75 * 60) / 75;
106 *f_a = aba - (*m_a * 75 * 60) - (*s_a * 75);
107 }
108
109 static INLINE int32 ABA_to_LBA(uint32 aba)
110 {
111 return(aba - 150);
112 }
113
114 static INLINE uint32 LBA_to_ABA(int32 lba)
115 {
116 return(lba + 150);
117 }
118
119 static INLINE int32 AMSF_to_LBA(uint8 m_a, uint8 s_a, uint8 f_a)
120 {
121 return(ABA_to_LBA(AMSF_to_ABA(m_a, s_a, f_a)));
122 }
123
124 static INLINE void LBA_to_AMSF(int32 lba, uint8 *m_a, uint8 *s_a, uint8 *f_a)
125 {
126 ABA_to_AMSF(LBA_to_ABA(lba), m_a, s_a, f_a);
127 }
128
129 //
130 // BCD conversion functions
131 //
132 static INLINE bool BCD_is_valid(uint8 bcd_number)
133 {
134 if((bcd_number & 0xF0) >= 0xA0)
135 return(false);
136
137 if((bcd_number & 0x0F) >= 0x0A)
138 return(false);
139
140 return(true);
141 }
142
143 static INLINE uint8 BCD_to_U8(uint8 bcd_number)
144 {
145 return( ((bcd_number >> 4) * 10) + (bcd_number & 0x0F) );
146 }
147
148 static INLINE uint8 U8_to_BCD(uint8 num)
149 {
150 return( ((num / 10) << 4) + (num % 10) );
151 }
152
153 // should always perform the conversion, even if the bcd number is invalid.
154 static INLINE bool BCD_to_U8_check(uint8 bcd_number, uint8 *out_number)
155 {
156 *out_number = BCD_to_U8(bcd_number);
157
158 if(!BCD_is_valid(bcd_number))
159 return(false);
160
161 return(true);
162 }
163
164 //
165 // Sector data encoding functions(to full 2352 bytes raw sector).
166 //
167 // sector_data must be able to contain at least 2352 bytes.
168 void encode_mode0_sector(uint32 aba, uint8 *sector_data);
169 void encode_mode1_sector(uint32 aba, uint8 *sector_data); // 2048 bytes of user data at offset 16
170 void encode_mode2_sector(uint32 aba, uint8 *sector_data); // 2336 bytes of user data at offset 16
171 void encode_mode2_form1_sector(uint32 aba, uint8 *sector_data); // 2048+8 bytes of user data at offset 16
172 void encode_mode2_form2_sector(uint32 aba, uint8 *sector_data); // 2324+8 bytes of user data at offset 16
173
174
175 // out_buf must be able to contain 2352+96 bytes.
176 // "mode" is only used if(toc.tracks[100].control & 0x4)
177 void synth_leadout_sector_lba(const uint8 mode, const TOC& toc, const int32 lba, uint8* out_buf);
178
179 //
180 // User data error detection and correction
181 //
182
183 // Check EDC of a mode 1 or mode 2 form 1 sector.
184 // Returns "true" if checksum is ok(matches).
185 // Returns "false" if checksum mismatch.
186 // sector_data should contain 2352 bytes of raw sector data.
187 bool edc_check(const uint8 *sector_data, bool xa);
188
189 // Check EDC and L-EC data of a mode 1 or mode 2 form 1 sector, and correct bit errors if any exist.
190 // Returns "true" if errors weren't detected, or they were corrected succesfully.
191 // Returns "false" if errors couldn't be corrected.
192 // sector_data should contain 2352 bytes of raw sector data.
193 bool edc_lec_check_and_correct(uint8 *sector_data, bool xa);
194
195 //
196 // Subchannel(Q in particular) functions
197 //
198
199 // Returns false on checksum mismatch, true on match.
200 bool subq_check_checksum(const uint8 *subq_buf);
201
202 // Calculates the checksum of Q subchannel data(not including the checksum bytes of course ;)) from subq_buf, and stores it into the appropriate position
203 // in subq_buf.
204 void subq_generate_checksum(uint8 *subq_buf);
205
206 // Deinterleaves 12 bytes of subchannel Q data from 96 bytes of interleaved subchannel PW data.
207 void subq_deinterleave(const uint8 *subpw_buf, uint8 *subq_buf);
208
209 // Deinterleaves 96 bytes of subchannel P-W data from 96 bytes of interleaved subchannel PW data.
210 void subpw_deinterleave(const uint8 *in_buf, uint8 *out_buf);
211
212 // Interleaves 96 bytes of subchannel P-W data from 96 bytes of uninterleaved subchannel PW data.
213 void subpw_interleave(const uint8 *in_buf, uint8 *out_buf);
214
215 // Extrapolates Q subchannel current position data from subq_input, with frame/sector delta position_delta, and writes to subq_output.
216 // Only valid for ADR_CURPOS.
217 // subq_input must pass subq_check_checksum().
218 // TODO
219 //void subq_extrapolate(const uint8 *subq_input, int32 position_delta, uint8 *subq_output);
220
221 // (De)Scrambles data sector.
222 void scrambleize_data_sector(uint8 *sector_data);
223 }
224
225 #endif