BlueSCSI_cdrom.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  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 "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 uint8_t SimpleHeader[] =
  167. {
  168. 0x01, // 2048byte user data, L-EC in 288 byte aux field.
  169. 0x00, // reserved
  170. 0x00, // reserved
  171. 0x00, // reserved
  172. 0x00,0x00,0x00,0x00 // Track start sector (LBA or MSF)
  173. };
  174. static const uint8_t DiscInformation[] =
  175. {
  176. 0x00, // 0: disc info length, MSB
  177. 0x20, // 1: disc info length, LSB
  178. 0x0E, // 2: disc status (finalized, single session non-rewritable)
  179. 0x01, // 3: first track number
  180. 0x01, // 4: number of sessions (LSB)
  181. 0x01, // 5: first track in last session (LSB)
  182. 0x01, // 6: last track in last session (LSB)
  183. 0x00, // 7: format status (0x00 = non-rewritable, no barcode, no disc id)
  184. 0x00, // 8: disc type (0x00 = CD-ROM)
  185. 0x00, // 9: number of sessions (MSB)
  186. 0x00, // 10: first track in last session (MSB)
  187. 0x00, // 11: last track in last session (MSB)
  188. 0x00, // 12: disc ID (MSB)
  189. 0x00, // 13: .
  190. 0x00, // 14: .
  191. 0x00, // 15: disc ID (LSB)
  192. 0x00, // 16: last session lead-in start (MSB)
  193. 0x00, // 17: .
  194. 0x00, // 18: .
  195. 0x00, // 19: last session lead-in start (LSB)
  196. 0x00, // 20: last possible lead-out start (MSB)
  197. 0x00, // 21: .
  198. 0x00, // 22: .
  199. 0x00, // 23: last possible lead-out start (LSB)
  200. 0x00, // 24: disc bar code (MSB)
  201. 0x00, // 25: .
  202. 0x00, // 26: .
  203. 0x00, // 27: .
  204. 0x00, // 28: .
  205. 0x00, // 29: .
  206. 0x00, // 30: .
  207. 0x00, // 31: disc bar code (LSB)
  208. 0x00, // 32: disc application code
  209. 0x00, // 33: number of opc tables
  210. };
  211. // Convert logical block address to CD-ROM time in formatted TOC format
  212. static void LBA2MSF(uint32_t LBA, uint8_t* MSF)
  213. {
  214. MSF[0] = 0; // reserved.
  215. MSF[3] = LBA % 75; // Frames
  216. uint32_t rem = LBA / 75;
  217. MSF[2] = rem % 60; // Seconds
  218. MSF[1] = rem / 60; // Minutes
  219. }
  220. static void doReadTOCSimple(bool MSF, uint8_t track, uint16_t allocationLength)
  221. {
  222. if (track == 0xAA)
  223. {
  224. // 0xAA requests only lead-out track information (reports capacity)
  225. uint32_t len = sizeof(LeadoutTOC);
  226. memcpy(scsiDev.data, LeadoutTOC, len);
  227. uint32_t capacity = getScsiCapacity(
  228. scsiDev.target->cfg->sdSectorStart,
  229. scsiDev.target->liveCfg.bytesPerSector,
  230. scsiDev.target->cfg->scsiSectors);
  231. // Replace start of leadout track
  232. if (MSF)
  233. {
  234. LBA2MSF(capacity, scsiDev.data + 8);
  235. }
  236. else
  237. {
  238. scsiDev.data[8] = capacity >> 24;
  239. scsiDev.data[9] = capacity >> 16;
  240. scsiDev.data[10] = capacity >> 8;
  241. scsiDev.data[11] = capacity;
  242. }
  243. if (len > allocationLength)
  244. {
  245. len = allocationLength;
  246. }
  247. scsiDev.dataLen = len;
  248. scsiDev.phase = DATA_IN;
  249. }
  250. else if (track <= 1)
  251. {
  252. // We only support track 1.
  253. // track 0 means "return all tracks"
  254. uint32_t len = sizeof(SimpleTOC);
  255. memcpy(scsiDev.data, SimpleTOC, len);
  256. uint32_t capacity = getScsiCapacity(
  257. scsiDev.target->cfg->sdSectorStart,
  258. scsiDev.target->liveCfg.bytesPerSector,
  259. scsiDev.target->cfg->scsiSectors);
  260. // Replace start of leadout track
  261. if (MSF)
  262. {
  263. LBA2MSF(capacity, scsiDev.data + 0x10);
  264. }
  265. else
  266. {
  267. scsiDev.data[0x10] = capacity >> 24;
  268. scsiDev.data[0x11] = capacity >> 16;
  269. scsiDev.data[0x12] = capacity >> 8;
  270. scsiDev.data[0x13] = capacity;
  271. }
  272. if (len > allocationLength)
  273. {
  274. len = allocationLength;
  275. }
  276. scsiDev.dataLen = len;
  277. scsiDev.phase = DATA_IN;
  278. }
  279. else
  280. {
  281. scsiDev.status = CHECK_CONDITION;
  282. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  283. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  284. scsiDev.phase = STATUS;
  285. }
  286. }
  287. static void doReadSessionInfoSimple(uint8_t session, uint16_t allocationLength)
  288. {
  289. uint32_t len = sizeof(SessionTOC);
  290. memcpy(scsiDev.data, SessionTOC, len);
  291. if (len > allocationLength)
  292. {
  293. len = allocationLength;
  294. }
  295. scsiDev.dataLen = len;
  296. scsiDev.phase = DATA_IN;
  297. }
  298. static inline uint8_t
  299. fromBCD(uint8_t val)
  300. {
  301. return ((val >> 4) * 10) + (val & 0xF);
  302. }
  303. static void doReadFullTOCSimple(int convertBCD, uint8_t session, uint16_t allocationLength)
  304. {
  305. // We only support session 1.
  306. if (session > 1)
  307. {
  308. scsiDev.status = CHECK_CONDITION;
  309. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  310. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  311. scsiDev.phase = STATUS;
  312. }
  313. else
  314. {
  315. uint32_t len = sizeof(FullTOC);
  316. memcpy(scsiDev.data, FullTOC, len);
  317. if (convertBCD)
  318. {
  319. int descriptor = 4;
  320. while (descriptor < len)
  321. {
  322. int i;
  323. for (i = 0; i < 7; ++i)
  324. {
  325. scsiDev.data[descriptor + i] =
  326. fromBCD(scsiDev.data[descriptor + 4 + i]);
  327. }
  328. descriptor += 11;
  329. }
  330. }
  331. if (len > allocationLength)
  332. {
  333. len = allocationLength;
  334. }
  335. scsiDev.dataLen = len;
  336. scsiDev.phase = DATA_IN;
  337. }
  338. }
  339. void doReadHeaderSimple(bool MSF, uint32_t lba, uint16_t allocationLength)
  340. {
  341. uint32_t len = sizeof(SimpleHeader);
  342. memcpy(scsiDev.data, SimpleHeader, len);
  343. if (len > allocationLength)
  344. {
  345. len = allocationLength;
  346. }
  347. scsiDev.dataLen = len;
  348. scsiDev.phase = DATA_IN;
  349. }
  350. void doReadDiscInformationSimple(uint16_t allocationLength)
  351. {
  352. uint32_t len = sizeof(DiscInformation);
  353. memcpy(scsiDev.data, DiscInformation, len);
  354. if (len > allocationLength)
  355. {
  356. len = allocationLength;
  357. }
  358. scsiDev.dataLen = len;
  359. scsiDev.phase = DATA_IN;
  360. }
  361. /*********************************/
  362. /* TOC generation from cue sheet */
  363. /*********************************/
  364. // Fetch track info based on LBA
  365. static void getTrackFromLBA(CUEParser &parser, uint32_t lba, CUETrackInfo *result)
  366. {
  367. // Track info in case we have no .cue file
  368. result->file_mode = CUEFile_BINARY;
  369. result->track_mode = CUETrack_MODE1_2048;
  370. result->sector_length = 2048;
  371. result->track_number = 1;
  372. const CUETrackInfo *tmptrack;
  373. while ((tmptrack = parser.next_track()) != NULL)
  374. {
  375. if (tmptrack->track_start <= lba)
  376. {
  377. *result = *tmptrack;
  378. }
  379. else
  380. {
  381. break;
  382. }
  383. }
  384. }
  385. // Format track info read from cue sheet into the format used by ReadTOC command.
  386. // Refer to T10/1545-D MMC-4 Revision 5a, "Response Format 0000b: Formatted TOC"
  387. static void formatTrackInfo(const CUETrackInfo *track, uint8_t *dest, bool use_MSF_time)
  388. {
  389. uint8_t control_adr = 0x14; // Digital track
  390. if (track->track_mode == CUETrack_AUDIO)
  391. {
  392. control_adr = 0x10; // Audio track
  393. }
  394. dest[0] = 0; // Reserved
  395. dest[1] = control_adr;
  396. dest[2] = track->track_number;
  397. dest[3] = 0; // Reserved
  398. if (use_MSF_time)
  399. {
  400. // Time in minute-second-frame format
  401. LBA2MSF(track->data_start, &dest[4]);
  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 == 0 || track == trackinfo->track_number)
  454. {
  455. formatTrackInfo(trackinfo, &trackdata[8 * trackcount], MSF);
  456. trackcount += 1;
  457. }
  458. }
  459. // Format lead-out track info
  460. if (track == 0 || track == 0xAA)
  461. {
  462. CUETrackInfo leadout = {};
  463. leadout.track_number = 0xAA;
  464. leadout.track_mode = CUETrack_MODE1_2048;
  465. leadout.data_start = img.scsiSectors;
  466. formatTrackInfo(&leadout, &trackdata[8 * trackcount], MSF);
  467. trackcount += 1;
  468. }
  469. // Format response header
  470. uint16_t toc_length = 2 + trackcount * 8;
  471. scsiDev.data[0] = toc_length >> 8;
  472. scsiDev.data[1] = toc_length & 0xFF;
  473. scsiDev.data[2] = firsttrack;
  474. scsiDev.data[3] = lasttrack;
  475. if (trackcount == 0)
  476. {
  477. // Unknown track requested
  478. scsiDev.status = CHECK_CONDITION;
  479. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  480. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  481. scsiDev.phase = STATUS;
  482. }
  483. else
  484. {
  485. uint32_t len = 2 + toc_length;
  486. if (len > allocationLength)
  487. {
  488. len = allocationLength;
  489. }
  490. scsiDev.dataLen = len;
  491. scsiDev.phase = DATA_IN;
  492. }
  493. }
  494. static void doReadSessionInfo(uint8_t session, uint16_t allocationLength)
  495. {
  496. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  497. CUEParser parser;
  498. if (!loadCueSheet(img, parser))
  499. {
  500. // No CUE sheet, use hardcoded data
  501. return doReadSessionInfoSimple(session, allocationLength);
  502. }
  503. uint32_t len = sizeof(SessionTOC);
  504. memcpy(scsiDev.data, SessionTOC, len);
  505. // Replace first track info in the session table
  506. // based on data from CUE sheet.
  507. const CUETrackInfo *trackinfo = parser.next_track();
  508. if (trackinfo)
  509. {
  510. formatTrackInfo(trackinfo, &scsiDev.data[4], false);
  511. }
  512. if (len > allocationLength)
  513. {
  514. len = allocationLength;
  515. }
  516. scsiDev.dataLen = len;
  517. scsiDev.phase = DATA_IN;
  518. }
  519. // Convert logical block address to CD-ROM time in the raw TOC format
  520. static void LBA2MSFRaw(uint32_t LBA, uint8_t* MSF)
  521. {
  522. MSF[2] = LBA % 75; // Frames
  523. uint32_t rem = LBA / 75;
  524. MSF[1] = rem % 60; // Seconds
  525. MSF[0] = rem / 60; // Minutes
  526. }
  527. // Convert logical block address to CD-ROM time in binary coded decimal format
  528. static void LBA2MSFBCD(uint32_t LBA, uint8_t* MSF)
  529. {
  530. uint8_t fra = LBA % 75;
  531. uint32_t rem = LBA / 75;
  532. uint8_t sec = rem % 60;
  533. uint8_t min = rem / 60;
  534. MSF[0] = ((min / 10) << 4) | (min % 10);
  535. MSF[1] = ((sec / 10) << 4) | (sec % 10);
  536. MSF[2] = ((fra / 10) << 4) | (fra % 10);
  537. }
  538. // Format track info read from cue sheet into the format used by ReadFullTOC command.
  539. // Refer to T10/1545-D MMC-4 Revision 5a, "Response Format 0010b: Raw TOC"
  540. static void formatRawTrackInfo(const CUETrackInfo *track, uint8_t *dest)
  541. {
  542. uint8_t control_adr = 0x14; // Digital track
  543. if (track->track_mode == CUETrack_AUDIO)
  544. {
  545. control_adr = 0x10; // Audio track
  546. }
  547. dest[0] = 0x01; // Session always 1
  548. dest[1] = control_adr;
  549. dest[2] = 0x00; // "TNO", always 0?
  550. dest[3] = track->track_number; // "POINT", contains track number
  551. if (track->pregap_start > 0)
  552. {
  553. LBA2MSFRaw(track->pregap_start, &dest[4]);
  554. }
  555. else
  556. {
  557. LBA2MSFRaw(track->data_start, &dest[4]);
  558. }
  559. dest[7] = 0; // HOUR
  560. LBA2MSFBCD(track->data_start, &dest[8]);
  561. }
  562. static void doReadFullTOC(int convertBCD, uint8_t session, uint16_t allocationLength)
  563. {
  564. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  565. CUEParser parser;
  566. if (!loadCueSheet(img, parser))
  567. {
  568. // No CUE sheet, use hardcoded data
  569. return doReadFullTOCSimple(convertBCD, session, allocationLength);
  570. }
  571. // We only support session 1.
  572. if (session > 1)
  573. {
  574. scsiDev.status = CHECK_CONDITION;
  575. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  576. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  577. scsiDev.phase = STATUS;
  578. return;
  579. }
  580. // Take the beginning of the hardcoded TOC as base
  581. uint32_t len = 4 + 11 * 3; // Header, A0, A1, A2
  582. memcpy(scsiDev.data, FullTOC, len);
  583. // Add track descriptors
  584. int trackcount = 0;
  585. int firsttrack = -1;
  586. int lasttrack = -1;
  587. const CUETrackInfo *trackinfo;
  588. while ((trackinfo = parser.next_track()) != NULL)
  589. {
  590. if (firsttrack < 0) firsttrack = trackinfo->track_number;
  591. lasttrack = trackinfo->track_number;
  592. formatRawTrackInfo(trackinfo, &scsiDev.data[len]);
  593. trackcount += 1;
  594. len += 11;
  595. }
  596. // First and last track numbers
  597. scsiDev.data[12] = firsttrack;
  598. scsiDev.data[23] = lasttrack;
  599. // Leadout track position
  600. LBA2MSFBCD(img.scsiSectors, &scsiDev.data[34]);
  601. // Append recordable disc records b0 and c0 indicating non-recordable disc
  602. memcpy(scsiDev.data + len, &FullTOC[48], 22);
  603. len += 22;
  604. // Correct the record length in header
  605. uint16_t toclen = len - 2;
  606. scsiDev.data[0] = toclen >> 8;
  607. scsiDev.data[1] = toclen & 0xFF;
  608. if (len > allocationLength)
  609. {
  610. len = allocationLength;
  611. }
  612. scsiDev.dataLen = len;
  613. scsiDev.phase = DATA_IN;
  614. }
  615. // SCSI-3 MMC Read Header command, seems to be deprecated in later standards.
  616. // Refer to ANSI X3.304-1997
  617. void doReadHeader(bool MSF, uint32_t lba, uint16_t allocationLength)
  618. {
  619. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  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. log("---- Warning: track ", trackinfo->track_number, " has unsupported mode ", (int)trackinfo->track_mode);
  707. }
  708. if (trackinfo->file_mode != CUEFile_BINARY)
  709. {
  710. log("---- Unsupported CUE data file mode ", (int)trackinfo->file_mode);
  711. }
  712. }
  713. if (trackcount == 0)
  714. {
  715. log("---- Opened cue sheet but no valid tracks found");
  716. return false;
  717. }
  718. log("---- 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. debuglog("---- 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. debuglog("---- 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. if (filename[0] != '\0')
  755. {
  756. log("Switching to next CD-ROM image for ", target_idx, ": ", filename);
  757. img.file.close();
  758. bool status = scsiDiskOpenHDDImage(target_idx, filename, target_idx, 0, 2048);
  759. if (status)
  760. {
  761. img.ejected = false;
  762. img.cdrom_events = 2; // New media
  763. return true;
  764. }
  765. }
  766. return false;
  767. }
  768. static void doGetEventStatusNotification(bool immed)
  769. {
  770. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  771. if (!immed)
  772. {
  773. // Asynchronous notification not supported
  774. scsiDev.status = CHECK_CONDITION;
  775. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  776. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  777. scsiDev.phase = STATUS;
  778. }
  779. else if (img.cdrom_events)
  780. {
  781. scsiDev.data[0] = 0;
  782. scsiDev.data[1] = 6; // EventDataLength
  783. scsiDev.data[2] = 0x04; // Media status events
  784. scsiDev.data[3] = 0x04; // Supported events
  785. scsiDev.data[4] = img.cdrom_events;
  786. scsiDev.data[5] = 0x01; // Power status
  787. scsiDev.data[6] = 0; // Start slot
  788. scsiDev.data[7] = 0; // End slot
  789. scsiDev.dataLen = 8;
  790. scsiDev.phase = DATA_IN;
  791. img.cdrom_events = 0;
  792. if (img.ejected)
  793. {
  794. // We are now reporting to host that the drive is open.
  795. // Simulate a "close" for next time the host polls.
  796. cdromSwitchNextImage(img);
  797. }
  798. }
  799. else
  800. {
  801. scsiDev.data[0] = 0;
  802. scsiDev.data[1] = 2; // EventDataLength
  803. scsiDev.data[2] = 0x00; // Media status events
  804. scsiDev.data[3] = 0x04; // Supported events
  805. scsiDev.dataLen = 4;
  806. scsiDev.phase = DATA_IN;
  807. }
  808. }
  809. /**************************************/
  810. /* CD-ROM audio playback */
  811. /**************************************/
  812. typedef struct {
  813. uint32_t last_lba = 0;
  814. } mechanism_status_t;
  815. static mechanism_status_t mechanism_status[8];
  816. void cdromGetAudioPlaybackStatus(uint8_t *status, uint32_t *current_lba, bool current_only)
  817. {
  818. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  819. uint8_t target = img.scsiId & 7;
  820. #ifdef ENABLE_AUDIO_OUTPUT
  821. if (status) {
  822. if (current_only) {
  823. *status = audio_get_owner() == target ? 1 : 0;
  824. } else {
  825. *status = (uint8_t) audio_get_status_code(target);
  826. }
  827. }
  828. if (current_lba) *current_lba = mechanism_status[target].last_lba
  829. + audio_get_bytes_read(target) / 2352;
  830. #else
  831. if (status) *status = 0; // audio status code for 'unsupported/invalid' and not-playing indicator
  832. if (current_lba) *current_lba = mechanism_status[target].last_lba;
  833. #endif
  834. }
  835. static void doPlayAudio(uint32_t lba, uint32_t length)
  836. {
  837. #ifdef ENABLE_AUDIO_OUTPUT
  838. debuglog("------ CD-ROM Play Audio request at ", lba, " for ", length, " sectors");
  839. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  840. uint8_t target_id = img.scsiId & 7;
  841. // Per Annex C terminate playback immediately if already in progress on
  842. // the current target. Non-current targets may also get their audio
  843. // interrupted later due to hardware limitations
  844. if ((img.scsiId & 7) == audio_get_owner()) {
  845. audio_stop();
  846. }
  847. // if transfer length is zero no audio playback happens.
  848. // don't treat as an error per SCSI-2; handle via short-circuit
  849. if (length == 0)
  850. {
  851. scsiDev.status = 0;
  852. scsiDev.phase = STATUS;
  853. return;
  854. }
  855. // if actual playback is requested perform steps to verify prior to playback
  856. CUEParser parser;
  857. if (loadCueSheet(img, parser))
  858. {
  859. CUETrackInfo trackinfo = {};
  860. getTrackFromLBA(parser, lba, &trackinfo);
  861. if (lba == 0xFFFFFFFF)
  862. {
  863. // request to start playback from 'current position'
  864. lba = mechanism_status[target_id].last_lba + audio_get_bytes_read(target_id) / 2352;
  865. }
  866. // --- TODO --- determine proper track offset, software I tested with had a tendency
  867. // to ask for offsets that seem to hint at 2048 here, not the 2352 you'd assume.
  868. // Might be due to a mode page reporting something unexpected? Needs investigation.
  869. uint64_t offset = trackinfo.file_offset + 2048 * (lba - trackinfo.data_start);
  870. debuglog("------ Play audio CD: ", (int)length, " sectors starting at ", (int)lba,
  871. ", track number ", trackinfo.track_number, ", data offset in file ", (int)offset);
  872. if (trackinfo.track_mode != CUETrack_AUDIO)
  873. {
  874. debuglog("---- Host tried audio playback on track type ", (int)trackinfo.track_mode);
  875. scsiDev.status = CHECK_CONDITION;
  876. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  877. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  878. scsiDev.phase = STATUS;
  879. return;
  880. }
  881. // playback request appears to be sane, so perform it
  882. char filename[MAX_FILE_PATH];
  883. if (!img.file.name(filename, sizeof(filename)))
  884. {
  885. // No underlying file available?
  886. log("---- No filename for SCSI ID ", target_id);
  887. scsiDev.status = CHECK_CONDITION;
  888. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  889. scsiDev.target->sense.asc = 0x0000;
  890. scsiDev.phase = STATUS;
  891. return;
  892. }
  893. if (!audio_play(target_id, filename, offset, offset + length * 2352, false))
  894. {
  895. // Underlying data/media error? Fake a disk scratch, which should
  896. // be a condition most CD-DA players are expecting
  897. scsiDev.status = CHECK_CONDITION;
  898. scsiDev.target->sense.code = MEDIUM_ERROR;
  899. scsiDev.target->sense.asc = 0x1106; // CIRC UNRECOVERED ERROR
  900. scsiDev.phase = STATUS;
  901. return;
  902. }
  903. mechanism_status[target_id].last_lba = lba;
  904. scsiDev.status = 0;
  905. scsiDev.phase = STATUS;
  906. }
  907. else
  908. {
  909. // virtual drive supports audio, just not with this disk image
  910. debuglog("---- Request to play audio on non-audio image");
  911. scsiDev.status = CHECK_CONDITION;
  912. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  913. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  914. scsiDev.phase = STATUS;
  915. }
  916. #else
  917. debuglog("---- Target does not support audio playback");
  918. // per SCSI-2, targets not supporting audio respond to zero-length
  919. // PLAY AUDIO commands with ILLEGAL REQUEST; this seems to be a check
  920. // performed by at least some audio playback software
  921. scsiDev.status = CHECK_CONDITION;
  922. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  923. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  924. scsiDev.phase = STATUS;
  925. #endif
  926. }
  927. static void doPauseResumeAudio(bool resume)
  928. {
  929. #ifdef ENABLE_AUDIO_OUTPUT
  930. log("------ CD-ROM ", resume ? "resume" : "pause", " audio playback");
  931. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  932. uint8_t target_id = img.scsiId & 7;
  933. if (audio_get_owner() == target_id)
  934. {
  935. audio_set_paused(!resume);
  936. scsiDev.status = 0;
  937. scsiDev.phase = STATUS;
  938. }
  939. else
  940. {
  941. scsiDev.status = CHECK_CONDITION;
  942. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  943. scsiDev.target->sense.asc = 0x2C00; // COMMAND SEQUENCE ERROR
  944. scsiDev.phase = STATUS;
  945. }
  946. #else
  947. debuglog("---- Target does not support audio pausing");
  948. scsiDev.status = CHECK_CONDITION;
  949. scsiDev.target->sense.code = ILLEGAL_REQUEST; // assumed from PLAY AUDIO(10)
  950. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  951. scsiDev.phase = STATUS;
  952. #endif
  953. }
  954. static void doMechanismStatus(uint16_t allocation_length)
  955. {
  956. uint8_t *buf = scsiDev.data;
  957. uint8_t status;
  958. uint32_t lba;
  959. cdromGetAudioPlaybackStatus(&status, &lba, true);
  960. *buf++ = 0x00; // No fault state
  961. *buf++ = (status) ? 0x20 : 0x00; // Currently playing?
  962. *buf++ = (lba >> 16) & 0xFF;
  963. *buf++ = (lba >> 8) & 0xFF;
  964. *buf++ = (lba >> 0) & 0xFF;
  965. *buf++ = 0; // No CD changer
  966. *buf++ = 0;
  967. *buf++ = 0;
  968. int len = 8;
  969. if (len > allocation_length) len = allocation_length;
  970. scsiDev.dataLen = len;
  971. scsiDev.phase = DATA_IN;
  972. }
  973. /*******************************************/
  974. /* CD-ROM data reading in low level format */
  975. /*******************************************/
  976. static void doReadCD(uint32_t lba, uint32_t length, uint8_t sector_type,
  977. uint8_t main_channel, uint8_t sub_channel)
  978. {
  979. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  980. CUEParser parser;
  981. if (!loadCueSheet(img, parser)
  982. && (sector_type == 0 || sector_type == 2)
  983. && main_channel == 0x10 && sub_channel == 0)
  984. {
  985. // Simple case, return sector data directly
  986. scsiDiskStartRead(lba, length);
  987. return;
  988. }
  989. // Search the track with the requested LBA
  990. // Supplies dummy data if no cue sheet is active.
  991. CUETrackInfo trackinfo = {};
  992. getTrackFromLBA(parser, lba, &trackinfo);
  993. // Figure out the data offset in the file
  994. uint64_t offset = trackinfo.file_offset + trackinfo.sector_length * (lba - trackinfo.data_start);
  995. debuglog("------ Read CD: ", (int)length, " sectors starting at ", (int)lba,
  996. ", track number ", trackinfo.track_number, ", sector size ", (int)trackinfo.sector_length,
  997. ", main channel ", main_channel, ", sub channel ", sub_channel,
  998. ", data offset in file ", (int)offset);
  999. // Verify sector type
  1000. if (sector_type != 0)
  1001. {
  1002. bool sector_type_ok = false;
  1003. if (sector_type == 1 && trackinfo.track_mode == CUETrack_AUDIO)
  1004. {
  1005. sector_type_ok = true;
  1006. }
  1007. else if (sector_type == 2 && trackinfo.track_mode == CUETrack_MODE1_2048)
  1008. {
  1009. sector_type_ok = true;
  1010. }
  1011. if (!sector_type_ok)
  1012. {
  1013. debuglog("---- Failed sector type check, host requested ", (int)sector_type, " CUE file has ", (int)trackinfo.track_mode);
  1014. scsiDev.status = CHECK_CONDITION;
  1015. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1016. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1017. scsiDev.phase = STATUS;
  1018. return;
  1019. }
  1020. }
  1021. // Select fields to transfer
  1022. // Refer to table 351 in T10/1545-D MMC-4 Revision 5a
  1023. // Only the mandatory cases are supported.
  1024. int sector_length = 0;
  1025. int skip_begin = 0;
  1026. bool add_fake_headers = false;
  1027. if (main_channel == 0)
  1028. {
  1029. // No actual data requested, just sector type check or subchannel
  1030. sector_length = 0;
  1031. }
  1032. else if (trackinfo.track_mode == CUETrack_AUDIO)
  1033. {
  1034. // Transfer whole 2352 byte audio sectors from file to host
  1035. sector_length = 2352;
  1036. }
  1037. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && main_channel == 0x10)
  1038. {
  1039. // Transfer whole 2048 byte data sectors from file to host
  1040. sector_length = 2048;
  1041. }
  1042. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && (main_channel & 0xB8) == 0xB8)
  1043. {
  1044. // Transfer 2048 bytes of data from file and fake the headers
  1045. sector_length = 2048;
  1046. add_fake_headers = true;
  1047. debuglog("------ Host requested ECC data but image file lacks it, replacing with zeros");
  1048. }
  1049. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && main_channel == 0x10)
  1050. {
  1051. // Transfer the 2048 byte payload of data sector to host.
  1052. sector_length = 2048;
  1053. skip_begin = 16;
  1054. }
  1055. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && (main_channel & 0xB8) == 0xB8)
  1056. {
  1057. // Transfer whole 2352 byte data sector with ECC to host
  1058. sector_length = 2352;
  1059. }
  1060. else
  1061. {
  1062. debuglog("---- Unsupported channel request for track type ", (int)trackinfo.track_mode);
  1063. scsiDev.status = CHECK_CONDITION;
  1064. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1065. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1066. scsiDev.phase = STATUS;
  1067. return;
  1068. }
  1069. bool field_q_subchannel = false;
  1070. if (sub_channel == 2)
  1071. {
  1072. // Include position information in Q subchannel
  1073. field_q_subchannel = true;
  1074. }
  1075. else if (sub_channel != 0)
  1076. {
  1077. debuglog("---- Unsupported subchannel request");
  1078. scsiDev.status = CHECK_CONDITION;
  1079. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1080. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1081. scsiDev.phase = STATUS;
  1082. return;
  1083. }
  1084. scsiDev.phase = DATA_IN;
  1085. scsiDev.dataLen = 0;
  1086. scsiDev.dataPtr = 0;
  1087. scsiEnterPhase(DATA_IN);
  1088. // Use two buffers alternately for formatting sector data
  1089. uint32_t result_length = sector_length + (field_q_subchannel ? 16 : 0) + (add_fake_headers ? 304 : 0);
  1090. uint8_t *buf0 = scsiDev.data;
  1091. uint8_t *buf1 = scsiDev.data + result_length;
  1092. // Format the sectors for transfer
  1093. for (uint32_t idx = 0; idx < length; idx++)
  1094. {
  1095. platform_poll();
  1096. img.file.seek(offset + idx * trackinfo.sector_length + skip_begin);
  1097. // Verify that previous write using this buffer has finished
  1098. uint8_t *buf = ((idx & 1) ? buf1 : buf0);
  1099. uint8_t *bufstart = buf;
  1100. uint32_t start = millis();
  1101. while (!scsiIsWriteFinished(buf + result_length - 1) && !scsiDev.resetFlag)
  1102. {
  1103. if ((uint32_t)(millis() - start) > 5000)
  1104. {
  1105. log("doReadCD() timeout waiting for previous to finish");
  1106. scsiDev.resetFlag = 1;
  1107. }
  1108. platform_poll();
  1109. }
  1110. if (scsiDev.resetFlag) break;
  1111. if (add_fake_headers)
  1112. {
  1113. // 12-byte data sector sync pattern
  1114. *buf++ = 0x00;
  1115. for (int i = 0; i < 10; i++)
  1116. {
  1117. *buf++ = 0xFF;
  1118. }
  1119. *buf++ = 0x00;
  1120. // 4-byte data sector header
  1121. LBA2MSFBCD(lba + idx, buf);
  1122. buf += 3;
  1123. *buf++ = 0x01; // Mode 1
  1124. }
  1125. if (sector_length > 0)
  1126. {
  1127. // User data
  1128. img.file.read(buf, sector_length);
  1129. buf += sector_length;
  1130. }
  1131. if (add_fake_headers)
  1132. {
  1133. // 288 bytes of ECC
  1134. memset(buf, 0, 288);
  1135. buf += 288;
  1136. }
  1137. if (field_q_subchannel)
  1138. {
  1139. // Formatted Q subchannel data
  1140. // Refer to table 354 in T10/1545-D MMC-4 Revision 5a
  1141. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14); // Control & ADR
  1142. *buf++ = trackinfo.track_number;
  1143. *buf++ = (lba + idx >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1144. LBA2MSFRaw(lba + idx, buf); buf += 3;
  1145. *buf++ = 0;
  1146. LBA2MSFRaw(lba + idx, buf); buf += 3;
  1147. *buf++ = 0; *buf++ = 0; // CRC (optional)
  1148. *buf++ = 0; *buf++ = 0; *buf++ = 0; // (pad)
  1149. *buf++ = 0; // No P subchannel
  1150. }
  1151. assert(buf == bufstart + result_length);
  1152. scsiStartWrite(bufstart, result_length);
  1153. }
  1154. scsiFinishWrite();
  1155. scsiDev.status = 0;
  1156. scsiDev.phase = STATUS;
  1157. }
  1158. static void doReadSubchannel(bool time, bool subq, uint8_t parameter, uint8_t track_number, uint16_t allocation_length)
  1159. {
  1160. uint8_t *buf = scsiDev.data;
  1161. if (parameter == 0x01)
  1162. {
  1163. uint8_t audiostatus;
  1164. uint32_t lba;
  1165. cdromGetAudioPlaybackStatus(&audiostatus, &lba, false);
  1166. debuglog("------ Get audio playback position: status ", (int)audiostatus, " lba ", (int)lba);
  1167. // Fetch current track info
  1168. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1169. CUEParser parser;
  1170. CUETrackInfo trackinfo = {};
  1171. loadCueSheet(img, parser);
  1172. getTrackFromLBA(parser, lba, &trackinfo);
  1173. // Request sub channel data at current playback position
  1174. *buf++ = 0; // Reserved
  1175. *buf++ = audiostatus;
  1176. int len = 12;
  1177. *buf++ = 0; // Subchannel data length (MSB)
  1178. *buf++ = len; // Subchannel data length (LSB)
  1179. *buf++ = 0x01; // Subchannel data format
  1180. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14);
  1181. *buf++ = trackinfo.track_number;
  1182. *buf++ = (lba >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1183. *buf++ = (lba >> 24) & 0xFF; // Absolute block address
  1184. *buf++ = (lba >> 16) & 0xFF;
  1185. *buf++ = (lba >> 8) & 0xFF;
  1186. *buf++ = (lba >> 0) & 0xFF;
  1187. uint32_t relpos = (uint32_t)((int32_t)lba - (int32_t)trackinfo.data_start);
  1188. *buf++ = (relpos >> 24) & 0xFF; // Track relative position (may be negative)
  1189. *buf++ = (relpos >> 16) & 0xFF;
  1190. *buf++ = (relpos >> 8) & 0xFF;
  1191. *buf++ = (relpos >> 0) & 0xFF;
  1192. if (len > allocation_length) len = allocation_length;
  1193. scsiDev.dataLen = len;
  1194. scsiDev.phase = DATA_IN;
  1195. }
  1196. else
  1197. {
  1198. debuglog("---- Unsupported subchannel request");
  1199. scsiDev.status = CHECK_CONDITION;
  1200. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1201. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1202. scsiDev.phase = STATUS;
  1203. return;
  1204. }
  1205. }
  1206. /**************************************/
  1207. /* CD-ROM command dispatching */
  1208. /**************************************/
  1209. // Handle direct-access scsi device commands
  1210. extern "C" int scsiCDRomCommand()
  1211. {
  1212. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1213. int commandHandled = 1;
  1214. uint8_t command = scsiDev.cdb[0];
  1215. if (command == 0x1B && (scsiDev.cdb[4] & 2))
  1216. {
  1217. // CD-ROM load & eject
  1218. int start = scsiDev.cdb[4] & 1;
  1219. if (start)
  1220. {
  1221. debuglog("------ CDROM close tray");
  1222. img.ejected = false;
  1223. img.cdrom_events = 2; // New media
  1224. }
  1225. else
  1226. {
  1227. debuglog("------ CDROM open tray");
  1228. img.ejected = true;
  1229. img.cdrom_events = 3; // Media removal
  1230. }
  1231. }
  1232. else if (command == 0x43)
  1233. {
  1234. // CD-ROM Read TOC
  1235. bool MSF = (scsiDev.cdb[1] & 0x02);
  1236. uint8_t track = scsiDev.cdb[6];
  1237. uint16_t allocationLength =
  1238. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1239. scsiDev.cdb[8];
  1240. // Reject MMC commands for now, otherwise the TOC data format
  1241. // won't be understood.
  1242. // The "format" field is reserved for SCSI-2
  1243. uint8_t format = scsiDev.cdb[2] & 0x0F;
  1244. switch (format)
  1245. {
  1246. case 0: doReadTOC(MSF, track, allocationLength); break; // SCSI-2
  1247. case 1: doReadSessionInfo(MSF, allocationLength); break; // MMC2
  1248. case 2: doReadFullTOC(0, track, allocationLength); break; // MMC2
  1249. case 3: doReadFullTOC(1, track, allocationLength); break; // MMC2
  1250. default:
  1251. {
  1252. scsiDev.status = CHECK_CONDITION;
  1253. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1254. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1255. scsiDev.phase = STATUS;
  1256. }
  1257. }
  1258. }
  1259. else if (command == 0x44)
  1260. {
  1261. // CD-ROM Read Header
  1262. bool MSF = (scsiDev.cdb[1] & 0x02);
  1263. uint32_t lba = 0; // IGNORED for now
  1264. uint16_t allocationLength =
  1265. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1266. scsiDev.cdb[8];
  1267. doReadHeader(MSF, lba, allocationLength);
  1268. }
  1269. else if (command == 0x51)
  1270. {
  1271. uint16_t allocationLength =
  1272. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1273. scsiDev.cdb[8];
  1274. doReadDiscInformation(allocationLength);
  1275. }
  1276. else if (command == 0x4A)
  1277. {
  1278. // Get event status notifications (media change notifications)
  1279. bool immed = scsiDev.cdb[1] & 1;
  1280. doGetEventStatusNotification(immed);
  1281. }
  1282. else if (command == 0x45)
  1283. {
  1284. // PLAY AUDIO (10)
  1285. uint32_t lba =
  1286. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1287. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1288. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1289. scsiDev.cdb[5];
  1290. uint32_t blocks =
  1291. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1292. scsiDev.cdb[8];
  1293. doPlayAudio(lba, blocks);
  1294. }
  1295. else if (command == 0xA5)
  1296. {
  1297. // PLAY AUDIO (12)
  1298. uint32_t lba =
  1299. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1300. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1301. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1302. scsiDev.cdb[5];
  1303. uint32_t blocks =
  1304. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1305. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1306. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1307. scsiDev.cdb[9];
  1308. doPlayAudio(lba, blocks);
  1309. }
  1310. else if (command == 0x47)
  1311. {
  1312. // PLAY AUDIO (MSF)
  1313. uint32_t start = (scsiDev.cdb[3] * 60 + scsiDev.cdb[4]) * 75 + scsiDev.cdb[5];
  1314. uint32_t end = (scsiDev.cdb[6] * 60 + scsiDev.cdb[7]) * 75 + scsiDev.cdb[8];
  1315. doPlayAudio(start, end - start);
  1316. }
  1317. else if (command == 0x4B)
  1318. {
  1319. // PAUSE/RESUME AUDIO
  1320. doPauseResumeAudio(scsiDev.cdb[8] & 1);
  1321. }
  1322. else if (command == 0xBD)
  1323. {
  1324. // Mechanism status
  1325. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[8]) << 8) + scsiDev.cdb[9];
  1326. doMechanismStatus(allocationLength);
  1327. }
  1328. else if (command == 0xBB)
  1329. {
  1330. // Set CD speed (just ignored)
  1331. scsiDev.status = 0;
  1332. scsiDev.phase = STATUS;
  1333. }
  1334. else if (command == 0xBE)
  1335. {
  1336. // ReadCD (in low level format)
  1337. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1338. uint32_t lba =
  1339. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1340. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1341. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1342. scsiDev.cdb[5];
  1343. uint32_t blocks =
  1344. (((uint32_t) scsiDev.cdb[6]) << 16) +
  1345. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1346. (((uint32_t) scsiDev.cdb[8]));
  1347. uint8_t main_channel = scsiDev.cdb[9];
  1348. uint8_t sub_channel = scsiDev.cdb[10];
  1349. doReadCD(lba, blocks, sector_type, main_channel, sub_channel);
  1350. }
  1351. else if (command == 0xB9)
  1352. {
  1353. // ReadCD MSF
  1354. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1355. uint32_t start = (scsiDev.cdb[3] * 60 + scsiDev.cdb[4]) * 75 + scsiDev.cdb[5];
  1356. uint32_t end = (scsiDev.cdb[6] * 60 + scsiDev.cdb[7]) * 75 + scsiDev.cdb[8];
  1357. uint8_t main_channel = scsiDev.cdb[9];
  1358. uint8_t sub_channel = scsiDev.cdb[10];
  1359. doReadCD(start, end - start, sector_type, main_channel, sub_channel);
  1360. }
  1361. else if (command == 0x42)
  1362. {
  1363. // Read subchannel data
  1364. bool time = (scsiDev.cdb[1] & 0x02);
  1365. bool subq = (scsiDev.cdb[2] & 0x40);
  1366. uint8_t parameter = scsiDev.cdb[3];
  1367. uint8_t track_number = scsiDev.cdb[6];
  1368. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];
  1369. doReadSubchannel(time, subq, parameter, track_number, allocationLength);
  1370. }
  1371. else if (command == 0x28)
  1372. {
  1373. // READ(10) for CDs (may need sector translation for cue file handling)
  1374. uint32_t lba =
  1375. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1376. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1377. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1378. scsiDev.cdb[5];
  1379. uint32_t blocks =
  1380. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1381. scsiDev.cdb[8];
  1382. doReadCD(lba, blocks, 0, 0x10, 0);
  1383. }
  1384. else if (command == 0xA8)
  1385. {
  1386. // READ(12) for CDs (may need sector translation for cue file handling)
  1387. uint32_t lba =
  1388. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1389. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1390. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1391. scsiDev.cdb[5];
  1392. uint32_t blocks =
  1393. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1394. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1395. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1396. scsiDev.cdb[9];
  1397. doReadCD(lba, blocks, 0, 0x10, 0);
  1398. }
  1399. else
  1400. {
  1401. commandHandled = 0;
  1402. }
  1403. return commandHandled;
  1404. }