ZuluSCSI_cdrom.cpp 49 KB

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