ZuluSCSI_cdrom.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. /* Advanced CD-ROM drive emulation.
  2. * Adds a few capabilities on top of the SCSI2SD CD-ROM emulation:
  3. *
  4. * - bin/cue support for support of multiple tracks
  5. * - on the fly image switching
  6. *
  7. * SCSI2SD V6 - Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
  8. * ZuluSCSI™ - Copyright (c) 2023 Rabbit Hole Computing™
  9. *
  10. * This file is licensed under the GPL version 3 or any later version. 
  11. * It is derived from cdrom.c in SCSI2SD V6
  12. *
  13. * https://www.gnu.org/licenses/gpl-3.0.html
  14. * ----
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version. 
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. * GNU General Public License for more details. 
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  27. */
  28. #include <string.h>
  29. #include "ZuluSCSI_cdrom.h"
  30. #include "ZuluSCSI_log.h"
  31. #include "ZuluSCSI_config.h"
  32. #include <CUEParser.h>
  33. #include <assert.h>
  34. #ifdef ENABLE_AUDIO_OUTPUT
  35. #include "ZuluSCSI_audio.h"
  36. #endif
  37. extern "C" {
  38. #include <scsi.h>
  39. }
  40. /******************************************/
  41. /* Basic TOC generation without cue sheet */
  42. /******************************************/
  43. static const uint8_t SimpleTOC[] =
  44. {
  45. 0x00, // toc length, MSB
  46. 0x12, // toc length, LSB
  47. 0x01, // First track number
  48. 0x01, // Last track number,
  49. // TRACK 1 Descriptor
  50. 0x00, // reserved
  51. 0x14, // Q sub-channel encodes current position, Digital track
  52. 0x01, // Track 1,
  53. 0x00, // Reserved
  54. 0x00,0x00,0x00,0x00, // Track start sector (LBA)
  55. 0x00, // reserved
  56. 0x14, // Q sub-channel encodes current position, Digital track
  57. 0xAA, // Leadout Track
  58. 0x00, // Reserved
  59. 0x00,0x00,0x00,0x00, // Track start sector (LBA)
  60. };
  61. static const uint8_t LeadoutTOC[] =
  62. {
  63. 0x00, // toc length, MSB
  64. 0x0A, // toc length, LSB
  65. 0x01, // First track number
  66. 0x01, // Last track number,
  67. 0x00, // reserved
  68. 0x14, // Q sub-channel encodes current position, Digital track
  69. 0xAA, // Leadout Track
  70. 0x00, // Reserved
  71. 0x00,0x00,0x00,0x00, // Track start sector (LBA)
  72. };
  73. static const uint8_t SessionTOC[] =
  74. {
  75. 0x00, // toc length, MSB
  76. 0x0A, // toc length, LSB
  77. 0x01, // First session number
  78. 0x01, // Last session number,
  79. // TRACK 1 Descriptor
  80. 0x00, // reserved
  81. 0x14, // Q sub-channel encodes current position, Digital track
  82. 0x01, // First track number in last complete session
  83. 0x00, // Reserved
  84. 0x00,0x00,0x00,0x00 // LBA of first track in last session
  85. };
  86. static const uint8_t FullTOC[] =
  87. {
  88. 0x00, // 0: toc length, MSB
  89. 0x44, // 1: toc length, LSB
  90. 0x01, // 2: First session number
  91. 0x01, // 3: Last session number,
  92. // A0 Descriptor
  93. 0x01, // 4: session number
  94. 0x14, // 5: ADR/Control
  95. 0x00, // 6: TNO
  96. 0xA0, // 7: POINT
  97. 0x00, // 8: Min
  98. 0x00, // 9: Sec
  99. 0x00, // 10: Frame
  100. 0x00, // 11: Zero
  101. 0x01, // 12: First Track number.
  102. 0x00, // 13: Disc type 00 = Mode 1
  103. 0x00, // 14: PFRAME
  104. // A1
  105. 0x01, // 15: session number
  106. 0x14, // 16: ADR/Control
  107. 0x00, // 17: TNO
  108. 0xA1, // 18: POINT
  109. 0x00, // 19: Min
  110. 0x00, // 20: Sec
  111. 0x00, // 21: Frame
  112. 0x00, // 22: Zero
  113. 0x01, // 23: Last Track number
  114. 0x00, // 24: PSEC
  115. 0x00, // 25: PFRAME
  116. // A2
  117. 0x01, // 26: session number
  118. 0x14, // 27: ADR/Control
  119. 0x00, // 28: TNO
  120. 0xA2, // 29: POINT
  121. 0x00, // 30: Min
  122. 0x00, // 31: Sec
  123. 0x00, // 32: Frame
  124. 0x00, // 33: Zero
  125. 0x79, // 34: LEADOUT position BCD
  126. 0x59, // 35: leadout PSEC BCD
  127. 0x74, // 36: leadout PFRAME BCD
  128. // TRACK 1 Descriptor
  129. 0x01, // 37: session number
  130. 0x14, // 38: ADR/Control
  131. 0x00, // 39: TNO
  132. 0x01, // 40: Point
  133. 0x00, // 41: Min
  134. 0x00, // 42: Sec
  135. 0x00, // 43: Frame
  136. 0x00, // 44: Zero
  137. 0x00, // 45: PMIN
  138. 0x02, // 46: PSEC
  139. 0x00, // 47: PFRAME
  140. };
  141. static const uint8_t DiscInformation[] =
  142. {
  143. 0x00, // 0: disc info length, MSB
  144. 0x20, // 1: disc info length, LSB
  145. 0x0E, // 2: disc status (finalized, single session non-rewritable)
  146. 0x01, // 3: first track number
  147. 0x01, // 4: number of sessions (LSB)
  148. 0x01, // 5: first track in last session (LSB)
  149. 0x01, // 6: last track in last session (LSB)
  150. 0x00, // 7: format status (0x00 = non-rewritable, no barcode, no disc id)
  151. 0x00, // 8: disc type (0x00 = CD-ROM)
  152. 0x00, // 9: number of sessions (MSB)
  153. 0x00, // 10: first track in last session (MSB)
  154. 0x00, // 11: last track in last session (MSB)
  155. 0x00, // 12: disc ID (MSB)
  156. 0x00, // 13: .
  157. 0x00, // 14: .
  158. 0x00, // 15: disc ID (LSB)
  159. 0x00, // 16: last session lead-in start (MSB)
  160. 0x00, // 17: .
  161. 0x00, // 18: .
  162. 0x00, // 19: last session lead-in start (LSB)
  163. 0x00, // 20: last possible lead-out start (MSB)
  164. 0x00, // 21: .
  165. 0x00, // 22: .
  166. 0x00, // 23: last possible lead-out start (LSB)
  167. 0x00, // 24: disc bar code (MSB)
  168. 0x00, // 25: .
  169. 0x00, // 26: .
  170. 0x00, // 27: .
  171. 0x00, // 28: .
  172. 0x00, // 29: .
  173. 0x00, // 30: .
  174. 0x00, // 31: disc bar code (LSB)
  175. 0x00, // 32: disc application code
  176. 0x00, // 33: number of opc tables
  177. };
  178. // Convert logical block address to CD-ROM time
  179. static void LBA2MSF(int32_t LBA, uint8_t* MSF, bool relative)
  180. {
  181. if (!relative) {
  182. LBA += 150;
  183. }
  184. uint32_t ulba = LBA;
  185. if (LBA < 0) {
  186. ulba = LBA * -1;
  187. }
  188. MSF[2] = ulba % 75; // Frames
  189. uint32_t rem = ulba / 75;
  190. MSF[1] = rem % 60; // Seconds
  191. MSF[0] = rem / 60; // Minutes
  192. }
  193. // Convert logical block address to CD-ROM time in binary coded decimal format
  194. static void LBA2MSFBCD(int32_t LBA, uint8_t* MSF, bool relative)
  195. {
  196. LBA2MSF(LBA, MSF, relative);
  197. MSF[0] = ((MSF[0] / 10) << 4) | (MSF[0] % 10);
  198. MSF[1] = ((MSF[1] / 10) << 4) | (MSF[1] % 10);
  199. MSF[2] = ((MSF[2] / 10) << 4) | (MSF[2] % 10);
  200. }
  201. // Convert CD-ROM time to logical block address
  202. static int32_t MSF2LBA(uint8_t m, uint8_t s, uint8_t f, bool relative)
  203. {
  204. int32_t lba = (m * 60 + s) * 75 + f;
  205. if (!relative) lba -= 150;
  206. return lba;
  207. }
  208. static void doReadTOCSimple(bool MSF, uint8_t track, uint16_t allocationLength)
  209. {
  210. if (track == 0xAA)
  211. {
  212. // 0xAA requests only lead-out track information (reports capacity)
  213. uint32_t len = sizeof(LeadoutTOC);
  214. memcpy(scsiDev.data, LeadoutTOC, len);
  215. uint32_t capacity = getScsiCapacity(
  216. scsiDev.target->cfg->sdSectorStart,
  217. scsiDev.target->liveCfg.bytesPerSector,
  218. scsiDev.target->cfg->scsiSectors);
  219. // Replace start of leadout track
  220. if (MSF)
  221. {
  222. scsiDev.data[8] = 0;
  223. LBA2MSF(capacity, scsiDev.data + 9, false);
  224. }
  225. else
  226. {
  227. scsiDev.data[8] = capacity >> 24;
  228. scsiDev.data[9] = capacity >> 16;
  229. scsiDev.data[10] = capacity >> 8;
  230. scsiDev.data[11] = capacity;
  231. }
  232. if (len > allocationLength)
  233. {
  234. len = allocationLength;
  235. }
  236. scsiDev.dataLen = len;
  237. scsiDev.phase = DATA_IN;
  238. }
  239. else if (track <= 1)
  240. {
  241. // We only support track 1.
  242. // track 0 means "return all tracks"
  243. uint32_t len = sizeof(SimpleTOC);
  244. memcpy(scsiDev.data, SimpleTOC, len);
  245. if (MSF)
  246. {
  247. scsiDev.data[0x0A] = 0x02;
  248. }
  249. uint32_t capacity = getScsiCapacity(
  250. scsiDev.target->cfg->sdSectorStart,
  251. scsiDev.target->liveCfg.bytesPerSector,
  252. scsiDev.target->cfg->scsiSectors);
  253. // Replace start of leadout track
  254. if (MSF)
  255. {
  256. scsiDev.data[0x10] = 0;
  257. LBA2MSF(capacity, scsiDev.data + 0x11, false);
  258. }
  259. else
  260. {
  261. scsiDev.data[0x10] = capacity >> 24;
  262. scsiDev.data[0x11] = capacity >> 16;
  263. scsiDev.data[0x12] = capacity >> 8;
  264. scsiDev.data[0x13] = capacity;
  265. }
  266. if (len > allocationLength)
  267. {
  268. len = allocationLength;
  269. }
  270. scsiDev.dataLen = len;
  271. scsiDev.phase = DATA_IN;
  272. }
  273. else
  274. {
  275. scsiDev.status = CHECK_CONDITION;
  276. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  277. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  278. scsiDev.phase = STATUS;
  279. }
  280. }
  281. static void doReadSessionInfoSimple(bool msf, uint16_t allocationLength)
  282. {
  283. uint32_t len = sizeof(SessionTOC);
  284. memcpy(scsiDev.data, SessionTOC, len);
  285. if (msf)
  286. {
  287. scsiDev.data[0x0A] = 0x02;
  288. }
  289. if (len > allocationLength)
  290. {
  291. len = allocationLength;
  292. }
  293. scsiDev.dataLen = len;
  294. scsiDev.phase = DATA_IN;
  295. }
  296. static inline uint8_t
  297. fromBCD(uint8_t val)
  298. {
  299. return ((val >> 4) * 10) + (val & 0xF);
  300. }
  301. static void doReadFullTOCSimple(int convertBCD, uint8_t session, uint16_t allocationLength)
  302. {
  303. // We only support session 1.
  304. if (session > 1)
  305. {
  306. scsiDev.status = CHECK_CONDITION;
  307. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  308. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  309. scsiDev.phase = STATUS;
  310. }
  311. else
  312. {
  313. uint32_t len = sizeof(FullTOC);
  314. memcpy(scsiDev.data, FullTOC, len);
  315. if (convertBCD)
  316. {
  317. int descriptor = 4;
  318. while (descriptor < len)
  319. {
  320. int i;
  321. for (i = 0; i < 7; ++i)
  322. {
  323. scsiDev.data[descriptor + i] =
  324. fromBCD(scsiDev.data[descriptor + 4 + i]);
  325. }
  326. descriptor += 11;
  327. }
  328. }
  329. if (len > allocationLength)
  330. {
  331. len = allocationLength;
  332. }
  333. scsiDev.dataLen = len;
  334. scsiDev.phase = DATA_IN;
  335. }
  336. }
  337. void doReadDiscInformationSimple(uint16_t allocationLength)
  338. {
  339. uint32_t len = sizeof(DiscInformation);
  340. memcpy(scsiDev.data, DiscInformation, len);
  341. if (len > allocationLength)
  342. {
  343. len = allocationLength;
  344. }
  345. scsiDev.dataLen = len;
  346. scsiDev.phase = DATA_IN;
  347. }
  348. /*********************************/
  349. /* TOC generation from cue sheet */
  350. /*********************************/
  351. // Fetch track info based on LBA
  352. static void getTrackFromLBA(CUEParser &parser, uint32_t lba, CUETrackInfo *result)
  353. {
  354. // Track info in case we have no .cue file
  355. result->file_mode = CUEFile_BINARY;
  356. result->track_mode = CUETrack_MODE1_2048;
  357. result->sector_length = 2048;
  358. result->track_number = 1;
  359. const CUETrackInfo *tmptrack;
  360. while ((tmptrack = parser.next_track()) != NULL)
  361. {
  362. if (tmptrack->track_start <= lba)
  363. {
  364. *result = *tmptrack;
  365. }
  366. else
  367. {
  368. break;
  369. }
  370. }
  371. }
  372. // Format track info read from cue sheet into the format used by ReadTOC command.
  373. // Refer to T10/1545-D MMC-4 Revision 5a, "Response Format 0000b: Formatted TOC"
  374. static void formatTrackInfo(const CUETrackInfo *track, uint8_t *dest, bool use_MSF_time)
  375. {
  376. uint8_t control_adr = 0x14; // Digital track
  377. if (track->track_mode == CUETrack_AUDIO)
  378. {
  379. control_adr = 0x10; // Audio track
  380. }
  381. dest[0] = 0; // Reserved
  382. dest[1] = control_adr;
  383. dest[2] = track->track_number;
  384. dest[3] = 0; // Reserved
  385. if (use_MSF_time)
  386. {
  387. // Time in minute-second-frame format
  388. dest[4] = 0;
  389. LBA2MSF(track->data_start, &dest[5], false);
  390. }
  391. else
  392. {
  393. // Time as logical block address
  394. dest[4] = (track->data_start >> 24) & 0xFF;
  395. dest[5] = (track->data_start >> 16) & 0xFF;
  396. dest[6] = (track->data_start >> 8) & 0xFF;
  397. dest[7] = (track->data_start >> 0) & 0xFF;
  398. }
  399. }
  400. // Load data from CUE sheet for the given device,
  401. // using the second half of scsiDev.data buffer for temporary storage.
  402. // Returns false if no cue sheet or it could not be opened.
  403. static bool loadCueSheet(image_config_t &img, CUEParser &parser)
  404. {
  405. if (!img.cuesheetfile.isOpen())
  406. {
  407. return false;
  408. }
  409. // Use second half of scsiDev.data as the buffer for cue sheet text
  410. size_t halfbufsize = sizeof(scsiDev.data) / 2;
  411. char *cuebuf = (char*)&scsiDev.data[halfbufsize];
  412. img.cuesheetfile.seek(0);
  413. int len = img.cuesheetfile.read(cuebuf, halfbufsize);
  414. if (len <= 0)
  415. {
  416. return false;
  417. }
  418. cuebuf[len] = '\0';
  419. parser = CUEParser(cuebuf);
  420. return true;
  421. }
  422. static void doReadTOC(bool MSF, uint8_t track, uint16_t allocationLength)
  423. {
  424. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  425. CUEParser parser;
  426. if (!loadCueSheet(img, parser))
  427. {
  428. // No CUE sheet, use hardcoded data
  429. return doReadTOCSimple(MSF, track, allocationLength);
  430. }
  431. // Format track info
  432. uint8_t *trackdata = &scsiDev.data[4];
  433. int trackcount = 0;
  434. int firsttrack = -1;
  435. int lasttrack = -1;
  436. const CUETrackInfo *trackinfo;
  437. while ((trackinfo = parser.next_track()) != NULL)
  438. {
  439. if (firsttrack < 0) firsttrack = trackinfo->track_number;
  440. lasttrack = trackinfo->track_number;
  441. if (track <= trackinfo->track_number)
  442. {
  443. formatTrackInfo(trackinfo, &trackdata[8 * trackcount], MSF);
  444. trackcount += 1;
  445. }
  446. }
  447. // Format lead-out track info
  448. CUETrackInfo leadout = {};
  449. leadout.track_number = 0xAA;
  450. leadout.track_mode = CUETrack_MODE1_2048;
  451. leadout.data_start = img.scsiSectors;
  452. formatTrackInfo(&leadout, &trackdata[8 * trackcount], MSF);
  453. trackcount += 1;
  454. // Format response header
  455. uint16_t toc_length = 2 + trackcount * 8;
  456. scsiDev.data[0] = toc_length >> 8;
  457. scsiDev.data[1] = toc_length & 0xFF;
  458. scsiDev.data[2] = firsttrack;
  459. scsiDev.data[3] = lasttrack;
  460. if (track != 0xAA && trackcount < 2)
  461. {
  462. // Unknown track requested
  463. scsiDev.status = CHECK_CONDITION;
  464. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  465. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  466. scsiDev.phase = STATUS;
  467. }
  468. else
  469. {
  470. uint32_t len = 2 + toc_length;
  471. if (len > allocationLength)
  472. {
  473. len = allocationLength;
  474. }
  475. scsiDev.dataLen = len;
  476. scsiDev.phase = DATA_IN;
  477. }
  478. }
  479. static void doReadSessionInfo(bool msf, uint16_t allocationLength)
  480. {
  481. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  482. CUEParser parser;
  483. if (!loadCueSheet(img, parser))
  484. {
  485. // No CUE sheet, use hardcoded data
  486. return doReadSessionInfoSimple(msf, allocationLength);
  487. }
  488. uint32_t len = sizeof(SessionTOC);
  489. memcpy(scsiDev.data, SessionTOC, len);
  490. // Replace first track info in the session table
  491. // based on data from CUE sheet.
  492. const CUETrackInfo *trackinfo = parser.next_track();
  493. if (trackinfo)
  494. {
  495. formatTrackInfo(trackinfo, &scsiDev.data[4], false);
  496. }
  497. if (len > allocationLength)
  498. {
  499. len = allocationLength;
  500. }
  501. scsiDev.dataLen = len;
  502. scsiDev.phase = DATA_IN;
  503. }
  504. // Format track info read from cue sheet into the format used by ReadFullTOC command.
  505. // Refer to T10/1545-D MMC-4 Revision 5a, "Response Format 0010b: Raw TOC"
  506. static void formatRawTrackInfo(const CUETrackInfo *track, uint8_t *dest)
  507. {
  508. uint8_t control_adr = 0x14; // Digital track
  509. if (track->track_mode == CUETrack_AUDIO)
  510. {
  511. control_adr = 0x10; // Audio track
  512. }
  513. dest[0] = 0x01; // Session always 1
  514. dest[1] = control_adr;
  515. dest[2] = 0x00; // "TNO", always 0?
  516. dest[3] = track->track_number; // "POINT", contains track number
  517. // Next three are ATIME. The spec doesn't directly address how these
  518. // should be reported in the TOC, just giving a description of Q-channel
  519. // data from Red Book/ECMA-130. On all disks tested so far these are
  520. // given as 00/00/00.
  521. dest[4] = 0x00;
  522. dest[5] = 0x00;
  523. dest[6] = 0x00;
  524. dest[7] = 0; // HOUR
  525. LBA2MSFBCD(track->data_start, &dest[8], false);
  526. }
  527. static void doReadFullTOC(int convertBCD, uint8_t session, uint16_t allocationLength)
  528. {
  529. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  530. CUEParser parser;
  531. if (!loadCueSheet(img, parser))
  532. {
  533. // No CUE sheet, use hardcoded data
  534. return doReadFullTOCSimple(convertBCD, session, allocationLength);
  535. }
  536. // We only support session 1.
  537. if (session > 1)
  538. {
  539. scsiDev.status = CHECK_CONDITION;
  540. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  541. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  542. scsiDev.phase = STATUS;
  543. return;
  544. }
  545. // Take the beginning of the hardcoded TOC as base
  546. uint32_t len = 4 + 11 * 3; // Header, A0, A1, A2
  547. memcpy(scsiDev.data, FullTOC, len);
  548. // Add track descriptors
  549. int trackcount = 0;
  550. int firsttrack = -1;
  551. int lasttrack = -1;
  552. const CUETrackInfo *trackinfo;
  553. while ((trackinfo = parser.next_track()) != NULL)
  554. {
  555. if (firsttrack < 0) firsttrack = trackinfo->track_number;
  556. lasttrack = trackinfo->track_number;
  557. formatRawTrackInfo(trackinfo, &scsiDev.data[len]);
  558. trackcount += 1;
  559. len += 11;
  560. }
  561. // First and last track numbers
  562. scsiDev.data[12] = firsttrack;
  563. scsiDev.data[23] = lasttrack;
  564. // Leadout track position
  565. LBA2MSFBCD(img.scsiSectors, &scsiDev.data[34], false);
  566. // Correct the record length in header
  567. uint16_t toclen = len - 2;
  568. scsiDev.data[0] = toclen >> 8;
  569. scsiDev.data[1] = toclen & 0xFF;
  570. if (len > allocationLength)
  571. {
  572. len = allocationLength;
  573. }
  574. scsiDev.dataLen = len;
  575. scsiDev.phase = DATA_IN;
  576. }
  577. // SCSI-3 MMC Read Header command, seems to be deprecated in later standards.
  578. // Refer to ANSI X3.304-1997
  579. // The spec is vague, but based on experimentation with a Matshita drive this
  580. // command should return the sector header absolute time (see ECMA-130 21).
  581. // Given 2048-byte block sizes this effectively is 1:1 with the provided LBA.
  582. void doReadHeader(bool MSF, uint32_t lba, uint16_t allocationLength)
  583. {
  584. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  585. #if ENABLE_AUDIO_OUTPUT
  586. // terminate audio playback if active on this target (Annex C)
  587. audio_stop(img.scsiId & 7);
  588. #endif
  589. uint8_t mode = 1;
  590. CUEParser parser;
  591. if (loadCueSheet(img, parser))
  592. {
  593. // Search the track with the requested LBA
  594. CUETrackInfo trackinfo = {};
  595. getTrackFromLBA(parser, lba, &trackinfo);
  596. // Track mode (audio / data)
  597. if (trackinfo.track_mode == CUETrack_AUDIO)
  598. {
  599. scsiDev.data[0] = 0;
  600. }
  601. }
  602. scsiDev.data[0] = mode;
  603. scsiDev.data[1] = 0; // reserved
  604. scsiDev.data[2] = 0; // reserved
  605. scsiDev.data[3] = 0; // reserved
  606. // Track start
  607. if (MSF)
  608. {
  609. scsiDev.data[4] = 0;
  610. LBA2MSF(lba, &scsiDev.data[5], false);
  611. }
  612. else
  613. {
  614. scsiDev.data[4] = (lba >> 24) & 0xFF;
  615. scsiDev.data[5] = (lba >> 16) & 0xFF;
  616. scsiDev.data[6] = (lba >> 8) & 0xFF;
  617. scsiDev.data[7] = (lba >> 0) & 0xFF;
  618. }
  619. uint8_t len = 8;
  620. if (len > allocationLength)
  621. {
  622. len = allocationLength;
  623. }
  624. scsiDev.dataLen = len;
  625. scsiDev.phase = DATA_IN;
  626. }
  627. void doReadDiscInformation(uint16_t allocationLength)
  628. {
  629. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  630. CUEParser parser;
  631. if (!loadCueSheet(img, parser))
  632. {
  633. // No CUE sheet, use hardcoded data
  634. return doReadDiscInformationSimple(allocationLength);
  635. }
  636. // Take the hardcoded header as base
  637. uint32_t len = sizeof(DiscInformation);
  638. memcpy(scsiDev.data, DiscInformation, len);
  639. // Find first and last track number
  640. int firsttrack = -1;
  641. int lasttrack = -1;
  642. const CUETrackInfo *trackinfo;
  643. while ((trackinfo = parser.next_track()) != NULL)
  644. {
  645. if (firsttrack < 0) firsttrack = trackinfo->track_number;
  646. lasttrack = trackinfo->track_number;
  647. }
  648. scsiDev.data[3] = firsttrack;
  649. scsiDev.data[5] = firsttrack;
  650. scsiDev.data[6] = lasttrack;
  651. if (len > allocationLength)
  652. {
  653. len = allocationLength;
  654. }
  655. scsiDev.dataLen = len;
  656. scsiDev.phase = DATA_IN;
  657. }
  658. /****************************************/
  659. /* CUE sheet check at image load time */
  660. /****************************************/
  661. bool cdromValidateCueSheet(image_config_t &img)
  662. {
  663. CUEParser parser;
  664. if (!loadCueSheet(img, parser))
  665. {
  666. return false;
  667. }
  668. const CUETrackInfo *trackinfo;
  669. int trackcount = 0;
  670. while ((trackinfo = parser.next_track()) != NULL)
  671. {
  672. trackcount++;
  673. if (trackinfo->track_mode != CUETrack_AUDIO &&
  674. trackinfo->track_mode != CUETrack_MODE1_2048 &&
  675. trackinfo->track_mode != CUETrack_MODE1_2352)
  676. {
  677. logmsg("---- Warning: track ", trackinfo->track_number, " has unsupported mode ", (int)trackinfo->track_mode);
  678. }
  679. if (trackinfo->file_mode != CUEFile_BINARY)
  680. {
  681. logmsg("---- Unsupported CUE data file mode ", (int)trackinfo->file_mode);
  682. }
  683. }
  684. if (trackcount == 0)
  685. {
  686. logmsg("---- Opened cue sheet but no valid tracks found");
  687. return false;
  688. }
  689. logmsg("---- Cue sheet loaded with ", (int)trackcount, " tracks");
  690. return true;
  691. }
  692. /**************************************/
  693. /* Ejection and image switching logic */
  694. /**************************************/
  695. // Reinsert any ejected CDROMs on reboot
  696. void cdromReinsertFirstImage(image_config_t &img)
  697. {
  698. if (img.image_index > 0)
  699. {
  700. // Multiple images for this drive, force restart from first one
  701. dbgmsg("---- Restarting from first CD-ROM image");
  702. img.image_index = 9;
  703. cdromSwitchNextImage(img);
  704. }
  705. else if (img.ejected)
  706. {
  707. // Reinsert the single image
  708. dbgmsg("---- Closing CD-ROM tray");
  709. img.ejected = false;
  710. img.cdrom_events = 2; // New media
  711. }
  712. }
  713. // Check if we have multiple CD-ROM images to cycle when drive is ejected.
  714. bool cdromSwitchNextImage(image_config_t &img)
  715. {
  716. // Check if we have a next image to load, so that drive is closed next time the host asks.
  717. img.image_index++;
  718. char filename[MAX_FILE_PATH];
  719. int target_idx = img.scsiId & 7;
  720. if (!scsiDiskGetImageNameFromConfig(img, filename, sizeof(filename)))
  721. {
  722. img.image_index = 0;
  723. scsiDiskGetImageNameFromConfig(img, filename, sizeof(filename));
  724. }
  725. #ifdef ENABLE_AUDIO_OUTPUT
  726. // if in progress for this device, terminate audio playback immediately (Annex C)
  727. audio_stop(target_idx);
  728. // Reset position tracking for the new image
  729. audio_get_status_code(target_idx); // trash audio status code
  730. #endif
  731. if (filename[0] != '\0')
  732. {
  733. logmsg("Switching to next CD-ROM image for ", target_idx, ": ", filename);
  734. img.file.close();
  735. bool status = scsiDiskOpenHDDImage(target_idx, filename, target_idx, 0, 2048);
  736. if (status)
  737. {
  738. img.ejected = false;
  739. img.cdrom_events = 2; // New media
  740. return true;
  741. }
  742. }
  743. return false;
  744. }
  745. static void doGetEventStatusNotification(bool immed)
  746. {
  747. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  748. if (!immed)
  749. {
  750. // Asynchronous notification not supported
  751. scsiDev.status = CHECK_CONDITION;
  752. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  753. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  754. scsiDev.phase = STATUS;
  755. }
  756. else if (img.cdrom_events)
  757. {
  758. scsiDev.data[0] = 0;
  759. scsiDev.data[1] = 6; // EventDataLength
  760. scsiDev.data[2] = 0x04; // Media status events
  761. scsiDev.data[3] = 0x04; // Supported events
  762. scsiDev.data[4] = img.cdrom_events;
  763. scsiDev.data[5] = 0x01; // Power status
  764. scsiDev.data[6] = 0; // Start slot
  765. scsiDev.data[7] = 0; // End slot
  766. scsiDev.dataLen = 8;
  767. scsiDev.phase = DATA_IN;
  768. img.cdrom_events = 0;
  769. if (img.ejected)
  770. {
  771. // We are now reporting to host that the drive is open.
  772. // Simulate a "close" for next time the host polls.
  773. cdromSwitchNextImage(img);
  774. }
  775. }
  776. else
  777. {
  778. scsiDev.data[0] = 0;
  779. scsiDev.data[1] = 2; // EventDataLength
  780. scsiDev.data[2] = 0x00; // Media status events
  781. scsiDev.data[3] = 0x04; // Supported events
  782. scsiDev.dataLen = 4;
  783. scsiDev.phase = DATA_IN;
  784. }
  785. }
  786. /**************************************/
  787. /* CD-ROM audio playback */
  788. /**************************************/
  789. void cdromGetAudioPlaybackStatus(uint8_t *status, uint32_t *current_lba, bool current_only)
  790. {
  791. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  792. #ifdef ENABLE_AUDIO_OUTPUT
  793. if (status) {
  794. uint8_t target = img.scsiId & 7;
  795. if (current_only) {
  796. *status = audio_is_playing(target) ? 1 : 0;
  797. } else {
  798. *status = (uint8_t) audio_get_status_code(target);
  799. }
  800. }
  801. #else
  802. if (status) *status = 0; // audio status code for 'unsupported/invalid' and not-playing indicator
  803. #endif
  804. if (current_lba)
  805. {
  806. if (img.file.isOpen()) {
  807. *current_lba = img.file.position() / 2352;
  808. } else {
  809. *current_lba = 0;
  810. }
  811. }
  812. }
  813. static void doPlayAudio(uint32_t lba, uint32_t length)
  814. {
  815. #ifdef ENABLE_AUDIO_OUTPUT
  816. dbgmsg("------ CD-ROM Play Audio request at ", lba, " for ", length, " sectors");
  817. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  818. uint8_t target_id = img.scsiId & 7;
  819. // Per Annex C terminate playback immediately if already in progress on
  820. // the current target. Non-current targets may also get their audio
  821. // interrupted later due to hardware limitations
  822. audio_stop(img.scsiId & 7);
  823. // if transfer length is zero no audio playback happens.
  824. // don't treat as an error per SCSI-2; handle via short-circuit
  825. if (length == 0)
  826. {
  827. scsiDev.status = 0;
  828. scsiDev.phase = STATUS;
  829. return;
  830. }
  831. // if actual playback is requested perform steps to verify prior to playback
  832. CUEParser parser;
  833. if (loadCueSheet(img, parser))
  834. {
  835. CUETrackInfo trackinfo = {};
  836. getTrackFromLBA(parser, lba, &trackinfo);
  837. if (lba == 0xFFFFFFFF)
  838. {
  839. // request to start playback from 'current position'
  840. lba = img.file.position() / 2352;
  841. }
  842. // --- TODO --- determine proper track offset, software I tested with had a tendency
  843. // to ask for offsets that seem to hint at 2048 here, not the 2352 you'd assume.
  844. // Might be due to a mode page reporting something unexpected? Needs investigation.
  845. uint64_t offset = trackinfo.file_offset + 2048 * (lba - trackinfo.data_start);
  846. dbgmsg("------ Play audio CD: ", (int)length, " sectors starting at ", (int)lba,
  847. ", track number ", trackinfo.track_number, ", data offset in file ", (int)offset);
  848. if (trackinfo.track_mode != CUETrack_AUDIO)
  849. {
  850. dbgmsg("---- Host tried audio playback on track type ", (int)trackinfo.track_mode);
  851. scsiDev.status = CHECK_CONDITION;
  852. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  853. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  854. scsiDev.phase = STATUS;
  855. return;
  856. }
  857. // playback request appears to be sane, so perform it
  858. // see earlier note for context on the block length below
  859. if (!audio_play(target_id, &(img.file), offset, offset + length * 2048, false))
  860. {
  861. // Underlying data/media error? Fake a disk scratch, which should
  862. // be a condition most CD-DA players are expecting
  863. scsiDev.status = CHECK_CONDITION;
  864. scsiDev.target->sense.code = MEDIUM_ERROR;
  865. scsiDev.target->sense.asc = 0x1106; // CIRC UNRECOVERED ERROR
  866. scsiDev.phase = STATUS;
  867. return;
  868. }
  869. scsiDev.status = 0;
  870. scsiDev.phase = STATUS;
  871. }
  872. else
  873. {
  874. // virtual drive supports audio, just not with this disk image
  875. dbgmsg("---- Request to play audio on non-audio image");
  876. scsiDev.status = CHECK_CONDITION;
  877. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  878. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  879. scsiDev.phase = STATUS;
  880. }
  881. #else
  882. dbgmsg("---- Target does not support audio playback");
  883. // per SCSI-2, targets not supporting audio respond to zero-length
  884. // PLAY AUDIO commands with ILLEGAL REQUEST; this seems to be a check
  885. // performed by at least some audio playback software
  886. scsiDev.status = CHECK_CONDITION;
  887. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  888. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  889. scsiDev.phase = STATUS;
  890. #endif
  891. }
  892. static void doPauseResumeAudio(bool resume)
  893. {
  894. #ifdef ENABLE_AUDIO_OUTPUT
  895. logmsg("------ CD-ROM ", resume ? "resume" : "pause", " audio playback");
  896. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  897. uint8_t target_id = img.scsiId & 7;
  898. if (audio_is_playing(target_id))
  899. {
  900. audio_set_paused(target_id, !resume);
  901. scsiDev.status = 0;
  902. scsiDev.phase = STATUS;
  903. }
  904. else
  905. {
  906. scsiDev.status = CHECK_CONDITION;
  907. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  908. scsiDev.target->sense.asc = 0x2C00; // COMMAND SEQUENCE ERROR
  909. scsiDev.phase = STATUS;
  910. }
  911. #else
  912. dbgmsg("---- Target does not support audio pausing");
  913. scsiDev.status = CHECK_CONDITION;
  914. scsiDev.target->sense.code = ILLEGAL_REQUEST; // assumed from PLAY AUDIO(10)
  915. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  916. scsiDev.phase = STATUS;
  917. #endif
  918. }
  919. static void doStopAudio()
  920. {
  921. dbgmsg("------ CD-ROM Stop Audio request");
  922. #ifdef ENABLE_AUDIO_OUTPUT
  923. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  924. uint8_t target_id = img.scsiId & 7;
  925. audio_stop(target_id);
  926. #endif
  927. }
  928. static void doMechanismStatus(uint16_t allocation_length)
  929. {
  930. uint8_t *buf = scsiDev.data;
  931. uint8_t status;
  932. uint32_t lba;
  933. cdromGetAudioPlaybackStatus(&status, &lba, true);
  934. *buf++ = 0x00; // No fault state
  935. *buf++ = (status) ? 0x20 : 0x00; // Currently playing?
  936. *buf++ = (lba >> 16) & 0xFF;
  937. *buf++ = (lba >> 8) & 0xFF;
  938. *buf++ = (lba >> 0) & 0xFF;
  939. *buf++ = 0; // No CD changer
  940. *buf++ = 0;
  941. *buf++ = 0;
  942. int len = 8;
  943. if (len > allocation_length) len = allocation_length;
  944. scsiDev.dataLen = len;
  945. scsiDev.phase = DATA_IN;
  946. }
  947. /*******************************************/
  948. /* CD-ROM data reading in low level format */
  949. /*******************************************/
  950. static void doReadCD(uint32_t lba, uint32_t length, uint8_t sector_type,
  951. uint8_t main_channel, uint8_t sub_channel)
  952. {
  953. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  954. #if ENABLE_AUDIO_OUTPUT
  955. // terminate audio playback if active on this target (Annex C)
  956. audio_stop(img.scsiId & 7);
  957. #endif
  958. CUEParser parser;
  959. if (!loadCueSheet(img, parser)
  960. && (sector_type == 0 || sector_type == 2)
  961. && main_channel == 0x10 && sub_channel == 0)
  962. {
  963. // Simple case, return sector data directly
  964. scsiDiskStartRead(lba, length);
  965. return;
  966. }
  967. // Search the track with the requested LBA
  968. // Supplies dummy data if no cue sheet is active.
  969. CUETrackInfo trackinfo = {};
  970. getTrackFromLBA(parser, lba, &trackinfo);
  971. // Figure out the data offset in the file
  972. uint64_t offset = trackinfo.file_offset + trackinfo.sector_length * (lba - trackinfo.data_start);
  973. dbgmsg("------ Read CD: ", (int)length, " sectors starting at ", (int)lba,
  974. ", track number ", trackinfo.track_number, ", sector size ", (int)trackinfo.sector_length,
  975. ", main channel ", main_channel, ", sub channel ", sub_channel,
  976. ", data offset in file ", (int)offset);
  977. // Verify sector type
  978. if (sector_type != 0)
  979. {
  980. bool sector_type_ok = false;
  981. if (sector_type == 1 && trackinfo.track_mode == CUETrack_AUDIO)
  982. {
  983. sector_type_ok = true;
  984. }
  985. else if (sector_type == 2 && trackinfo.track_mode == CUETrack_MODE1_2048)
  986. {
  987. sector_type_ok = true;
  988. }
  989. if (!sector_type_ok)
  990. {
  991. dbgmsg("---- Failed sector type check, host requested ", (int)sector_type, " CUE file has ", (int)trackinfo.track_mode);
  992. scsiDev.status = CHECK_CONDITION;
  993. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  994. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  995. scsiDev.phase = STATUS;
  996. return;
  997. }
  998. }
  999. // Select fields to transfer
  1000. // Refer to table 351 in T10/1545-D MMC-4 Revision 5a
  1001. // Only the mandatory cases are supported.
  1002. int sector_length = 0;
  1003. int skip_begin = 0;
  1004. bool add_fake_headers = false;
  1005. if (main_channel == 0)
  1006. {
  1007. // No actual data requested, just sector type check or subchannel
  1008. sector_length = 0;
  1009. }
  1010. else if (trackinfo.track_mode == CUETrack_AUDIO)
  1011. {
  1012. // Transfer whole 2352 byte audio sectors from file to host
  1013. sector_length = 2352;
  1014. }
  1015. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && main_channel == 0x10)
  1016. {
  1017. // Transfer whole 2048 byte data sectors from file to host
  1018. sector_length = 2048;
  1019. }
  1020. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && (main_channel & 0xB8) == 0xB8)
  1021. {
  1022. // Transfer 2048 bytes of data from file and fake the headers
  1023. sector_length = 2048;
  1024. add_fake_headers = true;
  1025. dbgmsg("------ Host requested ECC data but image file lacks it, replacing with zeros");
  1026. }
  1027. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && main_channel == 0x10)
  1028. {
  1029. // Transfer the 2048 byte payload of data sector to host.
  1030. sector_length = 2048;
  1031. skip_begin = 16;
  1032. }
  1033. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && (main_channel & 0xB8) == 0xB8)
  1034. {
  1035. // Transfer whole 2352 byte data sector with ECC to host
  1036. sector_length = 2352;
  1037. }
  1038. else
  1039. {
  1040. dbgmsg("---- Unsupported channel request for track type ", (int)trackinfo.track_mode);
  1041. scsiDev.status = CHECK_CONDITION;
  1042. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1043. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1044. scsiDev.phase = STATUS;
  1045. return;
  1046. }
  1047. bool field_q_subchannel = false;
  1048. if (sub_channel == 2)
  1049. {
  1050. // Include position information in Q subchannel
  1051. field_q_subchannel = true;
  1052. }
  1053. else if (sub_channel != 0)
  1054. {
  1055. dbgmsg("---- Unsupported subchannel request");
  1056. scsiDev.status = CHECK_CONDITION;
  1057. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1058. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1059. scsiDev.phase = STATUS;
  1060. return;
  1061. }
  1062. scsiDev.phase = DATA_IN;
  1063. scsiDev.dataLen = 0;
  1064. scsiDev.dataPtr = 0;
  1065. scsiEnterPhase(DATA_IN);
  1066. // Use two buffers alternately for formatting sector data
  1067. uint32_t result_length = sector_length + (field_q_subchannel ? 16 : 0) + (add_fake_headers ? 304 : 0);
  1068. uint8_t *buf0 = scsiDev.data;
  1069. uint8_t *buf1 = scsiDev.data + result_length;
  1070. // Format the sectors for transfer
  1071. for (uint32_t idx = 0; idx < length; idx++)
  1072. {
  1073. platform_poll();
  1074. img.file.seek(offset + idx * trackinfo.sector_length + skip_begin);
  1075. // Verify that previous write using this buffer has finished
  1076. uint8_t *buf = ((idx & 1) ? buf1 : buf0);
  1077. uint8_t *bufstart = buf;
  1078. uint32_t start = millis();
  1079. while (!scsiIsWriteFinished(buf + result_length - 1) && !scsiDev.resetFlag)
  1080. {
  1081. if ((uint32_t)(millis() - start) > 5000)
  1082. {
  1083. logmsg("doReadCD() timeout waiting for previous to finish");
  1084. scsiDev.resetFlag = 1;
  1085. }
  1086. platform_poll();
  1087. }
  1088. if (scsiDev.resetFlag) break;
  1089. if (add_fake_headers)
  1090. {
  1091. // 12-byte data sector sync pattern
  1092. *buf++ = 0x00;
  1093. for (int i = 0; i < 10; i++)
  1094. {
  1095. *buf++ = 0xFF;
  1096. }
  1097. *buf++ = 0x00;
  1098. // 4-byte data sector header
  1099. LBA2MSFBCD(lba + idx, buf, false);
  1100. buf += 3;
  1101. *buf++ = 0x01; // Mode 1
  1102. }
  1103. if (sector_length > 0)
  1104. {
  1105. // User data
  1106. img.file.read(buf, sector_length);
  1107. buf += sector_length;
  1108. }
  1109. if (add_fake_headers)
  1110. {
  1111. // 288 bytes of ECC
  1112. memset(buf, 0, 288);
  1113. buf += 288;
  1114. }
  1115. if (field_q_subchannel)
  1116. {
  1117. // Formatted Q subchannel data
  1118. // Refer to table 354 in T10/1545-D MMC-4 Revision 5a
  1119. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14); // Control & ADR
  1120. *buf++ = trackinfo.track_number;
  1121. *buf++ = (lba + idx >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1122. LBA2MSF(lba + idx, buf, false); buf += 3;
  1123. *buf++ = 0;
  1124. LBA2MSF(lba + idx, buf, false); buf += 3;
  1125. *buf++ = 0; *buf++ = 0; // CRC (optional)
  1126. *buf++ = 0; *buf++ = 0; *buf++ = 0; // (pad)
  1127. *buf++ = 0; // No P subchannel
  1128. }
  1129. assert(buf == bufstart + result_length);
  1130. scsiStartWrite(bufstart, result_length);
  1131. }
  1132. scsiFinishWrite();
  1133. scsiDev.status = 0;
  1134. scsiDev.phase = STATUS;
  1135. }
  1136. static void doReadSubchannel(bool time, bool subq, uint8_t parameter, uint8_t track_number, uint16_t allocation_length)
  1137. {
  1138. uint8_t *buf = scsiDev.data;
  1139. if (parameter == 0x01)
  1140. {
  1141. uint8_t audiostatus;
  1142. uint32_t lba;
  1143. cdromGetAudioPlaybackStatus(&audiostatus, &lba, false);
  1144. dbgmsg("------ Get audio playback position: status ", (int)audiostatus, " lba ", (int)lba);
  1145. // Fetch current track info
  1146. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1147. CUEParser parser;
  1148. CUETrackInfo trackinfo = {};
  1149. loadCueSheet(img, parser);
  1150. getTrackFromLBA(parser, lba, &trackinfo);
  1151. // Request sub channel data at current playback position
  1152. *buf++ = 0; // Reserved
  1153. *buf++ = audiostatus;
  1154. int len;
  1155. if (subq)
  1156. {
  1157. len = 12;
  1158. *buf++ = 0; // Subchannel data length (MSB)
  1159. *buf++ = len; // Subchannel data length (LSB)
  1160. *buf++ = 0x01; // Subchannel data format
  1161. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14);
  1162. *buf++ = trackinfo.track_number;
  1163. *buf++ = (lba >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1164. if (time)
  1165. {
  1166. *buf++ = 0;
  1167. LBA2MSF(lba, buf, false);
  1168. dbgmsg("------ ABS M ", *buf, " S ", *(buf+1), " F ", *(buf+2));
  1169. buf += 3;
  1170. }
  1171. else
  1172. {
  1173. *buf++ = (lba >> 24) & 0xFF; // Absolute block address
  1174. *buf++ = (lba >> 16) & 0xFF;
  1175. *buf++ = (lba >> 8) & 0xFF;
  1176. *buf++ = (lba >> 0) & 0xFF;
  1177. }
  1178. int32_t relpos = (int32_t)lba - (int32_t)trackinfo.data_start;
  1179. if (time)
  1180. {
  1181. *buf++ = 0;
  1182. LBA2MSF(relpos, buf, true);
  1183. dbgmsg("------ REL M ", *buf, " S ", *(buf+1), " F ", *(buf+2));
  1184. buf += 3;
  1185. }
  1186. else
  1187. {
  1188. uint32_t urelpos = relpos;
  1189. *buf++ = (urelpos >> 24) & 0xFF; // Track relative position (may be negative)
  1190. *buf++ = (urelpos >> 16) & 0xFF;
  1191. *buf++ = (urelpos >> 8) & 0xFF;
  1192. *buf++ = (urelpos >> 0) & 0xFF;
  1193. }
  1194. }
  1195. else
  1196. {
  1197. len = 0;
  1198. *buf++ = 0;
  1199. *buf++ = 0;
  1200. }
  1201. len += 4;
  1202. if (len > allocation_length) len = allocation_length;
  1203. scsiDev.dataLen = len;
  1204. scsiDev.phase = DATA_IN;
  1205. }
  1206. else
  1207. {
  1208. dbgmsg("---- Unsupported subchannel request");
  1209. scsiDev.status = CHECK_CONDITION;
  1210. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1211. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1212. scsiDev.phase = STATUS;
  1213. return;
  1214. }
  1215. }
  1216. /**************************************/
  1217. /* CD-ROM command dispatching */
  1218. /**************************************/
  1219. // Handle direct-access scsi device commands
  1220. extern "C" int scsiCDRomCommand()
  1221. {
  1222. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1223. int commandHandled = 1;
  1224. uint8_t command = scsiDev.cdb[0];
  1225. if (command == 0x1B)
  1226. {
  1227. #if ENABLE_AUDIO_OUTPUT
  1228. // terminate audio playback if active on this target (Annex C)
  1229. audio_stop(img.scsiId & 7);
  1230. #endif
  1231. if ((scsiDev.cdb[4] & 2))
  1232. {
  1233. // CD-ROM load & eject
  1234. int start = scsiDev.cdb[4] & 1;
  1235. if (start)
  1236. {
  1237. dbgmsg("------ CDROM close tray");
  1238. img.ejected = false;
  1239. img.cdrom_events = 2; // New media
  1240. }
  1241. else
  1242. {
  1243. dbgmsg("------ CDROM open tray");
  1244. img.ejected = true;
  1245. img.cdrom_events = 3; // Media removal
  1246. }
  1247. }
  1248. else
  1249. {
  1250. // flow through to disk handler
  1251. commandHandled = 0;
  1252. }
  1253. }
  1254. else if (command == 0x43)
  1255. {
  1256. // CD-ROM Read TOC
  1257. bool MSF = (scsiDev.cdb[1] & 0x02);
  1258. uint8_t track = scsiDev.cdb[6];
  1259. uint16_t allocationLength =
  1260. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1261. scsiDev.cdb[8];
  1262. // Reject MMC commands for now, otherwise the TOC data format
  1263. // won't be understood.
  1264. // The "format" field is reserved for SCSI-2
  1265. uint8_t format = scsiDev.cdb[2] & 0x0F;
  1266. switch (format)
  1267. {
  1268. case 0: doReadTOC(MSF, track, allocationLength); break; // SCSI-2
  1269. case 1: doReadSessionInfo(MSF, allocationLength); break; // MMC2
  1270. case 2: doReadFullTOC(0, track, allocationLength); break; // MMC2
  1271. case 3: doReadFullTOC(1, track, allocationLength); break; // MMC2
  1272. default:
  1273. {
  1274. scsiDev.status = CHECK_CONDITION;
  1275. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1276. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1277. scsiDev.phase = STATUS;
  1278. }
  1279. }
  1280. }
  1281. else if (command == 0x44)
  1282. {
  1283. // CD-ROM Read Header
  1284. bool MSF = (scsiDev.cdb[1] & 0x02);
  1285. uint32_t lba = 0; // IGNORED for now
  1286. uint16_t allocationLength =
  1287. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1288. scsiDev.cdb[8];
  1289. doReadHeader(MSF, lba, allocationLength);
  1290. }
  1291. else if (command == 0x51)
  1292. {
  1293. uint16_t allocationLength =
  1294. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1295. scsiDev.cdb[8];
  1296. doReadDiscInformation(allocationLength);
  1297. }
  1298. else if (command == 0x4A)
  1299. {
  1300. // Get event status notifications (media change notifications)
  1301. bool immed = scsiDev.cdb[1] & 1;
  1302. doGetEventStatusNotification(immed);
  1303. }
  1304. else if (command == 0x45)
  1305. {
  1306. // PLAY AUDIO (10)
  1307. uint32_t lba =
  1308. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1309. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1310. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1311. scsiDev.cdb[5];
  1312. uint32_t blocks =
  1313. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1314. scsiDev.cdb[8];
  1315. doPlayAudio(lba, blocks);
  1316. }
  1317. else if (command == 0xA5)
  1318. {
  1319. // PLAY AUDIO (12)
  1320. uint32_t lba =
  1321. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1322. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1323. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1324. scsiDev.cdb[5];
  1325. uint32_t blocks =
  1326. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1327. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1328. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1329. scsiDev.cdb[9];
  1330. doPlayAudio(lba, blocks);
  1331. }
  1332. else if (command == 0x47)
  1333. {
  1334. // PLAY AUDIO (MSF)
  1335. uint32_t start = MSF2LBA(scsiDev.cdb[3], scsiDev.cdb[4], scsiDev.cdb[5], false);
  1336. uint32_t end = MSF2LBA(scsiDev.cdb[6], scsiDev.cdb[7], scsiDev.cdb[8], false);
  1337. uint32_t lba = start;
  1338. if (scsiDev.cdb[3] == 0xFF
  1339. && scsiDev.cdb[4] == 0xFF
  1340. && scsiDev.cdb[5] == 0xFF)
  1341. {
  1342. // request to start playback from 'current position'
  1343. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1344. lba = img.file.position() / 2352;
  1345. }
  1346. uint32_t length = end - lba;
  1347. doPlayAudio(lba, length);
  1348. }
  1349. else if (command == 0x4B)
  1350. {
  1351. // PAUSE/RESUME AUDIO
  1352. doPauseResumeAudio(scsiDev.cdb[8] & 1);
  1353. }
  1354. else if (command == 0xBD)
  1355. {
  1356. // Mechanism status
  1357. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[8]) << 8) + scsiDev.cdb[9];
  1358. doMechanismStatus(allocationLength);
  1359. }
  1360. else if (command == 0xBB)
  1361. {
  1362. // Set CD speed (just ignored)
  1363. scsiDev.status = 0;
  1364. scsiDev.phase = STATUS;
  1365. }
  1366. else if (command == 0xBE)
  1367. {
  1368. // ReadCD (in low level format)
  1369. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1370. uint32_t lba =
  1371. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1372. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1373. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1374. scsiDev.cdb[5];
  1375. uint32_t blocks =
  1376. (((uint32_t) scsiDev.cdb[6]) << 16) +
  1377. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1378. (((uint32_t) scsiDev.cdb[8]));
  1379. uint8_t main_channel = scsiDev.cdb[9];
  1380. uint8_t sub_channel = scsiDev.cdb[10];
  1381. doReadCD(lba, blocks, sector_type, main_channel, sub_channel);
  1382. }
  1383. else if (command == 0xB9)
  1384. {
  1385. // ReadCD MSF
  1386. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1387. uint32_t start = MSF2LBA(scsiDev.cdb[3], scsiDev.cdb[4], scsiDev.cdb[5], false);
  1388. uint32_t end = MSF2LBA(scsiDev.cdb[6], scsiDev.cdb[7], scsiDev.cdb[8], false);
  1389. uint8_t main_channel = scsiDev.cdb[9];
  1390. uint8_t sub_channel = scsiDev.cdb[10];
  1391. doReadCD(start, end - start, sector_type, main_channel, sub_channel);
  1392. }
  1393. else if (command == 0x42)
  1394. {
  1395. // Read subchannel data
  1396. bool time = (scsiDev.cdb[1] & 0x02);
  1397. bool subq = (scsiDev.cdb[2] & 0x40);
  1398. uint8_t parameter = scsiDev.cdb[3];
  1399. uint8_t track_number = scsiDev.cdb[6];
  1400. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];
  1401. doReadSubchannel(time, subq, parameter, track_number, allocationLength);
  1402. }
  1403. else if (command == 0x28)
  1404. {
  1405. // READ(10) for CDs (may need sector translation for cue file handling)
  1406. uint32_t lba =
  1407. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1408. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1409. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1410. scsiDev.cdb[5];
  1411. uint32_t blocks =
  1412. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1413. scsiDev.cdb[8];
  1414. doReadCD(lba, blocks, 0, 0x10, 0);
  1415. }
  1416. else if (command == 0xA8)
  1417. {
  1418. // READ(12) for CDs (may need sector translation for cue file handling)
  1419. uint32_t lba =
  1420. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1421. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1422. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1423. scsiDev.cdb[5];
  1424. uint32_t blocks =
  1425. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1426. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1427. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1428. scsiDev.cdb[9];
  1429. doReadCD(lba, blocks, 0, 0x10, 0);
  1430. }
  1431. else if (command == 0x4E)
  1432. {
  1433. // STOP PLAY/SCAN
  1434. doStopAudio();
  1435. scsiDev.status = 0;
  1436. scsiDev.phase = STATUS;
  1437. }
  1438. else if (command == 0x01)
  1439. {
  1440. // REZERO UNIT
  1441. // AppleCD Audio Player uses this as a nonstandard
  1442. // "stop audio playback" command
  1443. doStopAudio();
  1444. scsiDev.status = 0;
  1445. scsiDev.phase = STATUS;
  1446. }
  1447. else if (command == 0x0B || command == 0x2B)
  1448. {
  1449. // SEEK
  1450. // implement Annex C termination requirement and pass to disk handler
  1451. doStopAudio();
  1452. // this may need more specific handling, the Win9x player appears to
  1453. // expect a pickup move to the given LBA
  1454. commandHandled = 0;
  1455. }
  1456. else
  1457. {
  1458. commandHandled = 0;
  1459. }
  1460. return commandHandled;
  1461. }