BlueSCSI_cdrom.cpp 49 KB

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