BlueSCSI_cdrom.cpp 49 KB

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