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