AbstractIDEDevice.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #include "AbstractIDEDevice.hh"
  2. #include "MSXMotherBoard.hh"
  3. #include "LedStatus.hh"
  4. #include "Version.hh"
  5. #include "serialize.hh"
  6. #include "unreachable.hh"
  7. #include <cassert>
  8. #include <cstring>
  9. #include <cstdio>
  10. namespace openmsx {
  11. AbstractIDEDevice::AbstractIDEDevice(MSXMotherBoard& motherBoard_)
  12. : motherBoard(motherBoard_)
  13. {
  14. transferRead = false;
  15. transferWrite = false;
  16. transferIdx = 0;
  17. // avoid UMR on serialize
  18. memset(buffer, 0, sizeof(buffer));
  19. bufferLeft = 0;
  20. transferCount = 0;
  21. }
  22. byte AbstractIDEDevice::diagnostic()
  23. {
  24. // The Execute Device Diagnostic command is executed by both devices in
  25. // parallel. Fortunately, returning 0x01 is valid in all cases:
  26. // - for device 0 it means: device 0 passed, device 1 passed or not present
  27. // - for device 1 it means: device 1 passed
  28. return 0x01;
  29. }
  30. void AbstractIDEDevice::createSignature(bool preserveDevice)
  31. {
  32. sectorCountReg = 0x01;
  33. sectorNumReg = 0x01;
  34. if (isPacketDevice()) {
  35. cylinderLowReg = 0x14;
  36. cylinderHighReg = 0xEB;
  37. if (preserveDevice) {
  38. devHeadReg &= 0x10;
  39. } else {
  40. // The current implementation of SunriseIDE will substitute the
  41. // current device for DEV, so it will always act like DEV is
  42. // preserved.
  43. devHeadReg = 0x00;
  44. }
  45. } else {
  46. cylinderLowReg = 0x00;
  47. cylinderHighReg = 0x00;
  48. devHeadReg = 0x00;
  49. }
  50. }
  51. void AbstractIDEDevice::reset(EmuTime::param /*time*/)
  52. {
  53. errorReg = diagnostic();
  54. statusReg = DRDY | DSC;
  55. featureReg = 0x00;
  56. createSignature();
  57. setTransferRead(false);
  58. setTransferWrite(false);
  59. }
  60. byte AbstractIDEDevice::readReg(nibble reg, EmuTime::param /*time*/)
  61. {
  62. switch (reg) {
  63. case 1: // error register
  64. return errorReg;
  65. case 2: // sector count register
  66. return sectorCountReg;
  67. case 3: // sector number register / LBA low
  68. return sectorNumReg;
  69. case 4: // cyclinder low register / LBA mid
  70. return cylinderLowReg;
  71. case 5: // cyclinder high register / LBA high
  72. return cylinderHighReg;
  73. case 6: // device/head register
  74. // DEV bit is handled by IDE interface
  75. return devHeadReg;
  76. case 7: // status register
  77. return statusReg;
  78. case 8:
  79. case 9:
  80. case 10:
  81. case 11:
  82. case 12:
  83. case 13:
  84. case 15:// not used
  85. return 0x7F;
  86. case 0: // data register, converted to readData by IDE interface
  87. case 14:// alternate status reg, converted to read from normal
  88. // status register by IDE interface
  89. default:
  90. UNREACHABLE; return 0x7F; // avoid warning
  91. }
  92. }
  93. void AbstractIDEDevice::writeReg(
  94. nibble reg, byte value, EmuTime::param /*time*/
  95. )
  96. {
  97. switch (reg) {
  98. case 1: // feature register
  99. featureReg = value;
  100. break;
  101. case 2: // sector count register
  102. sectorCountReg = value;
  103. break;
  104. case 3: // sector number register / LBA low
  105. sectorNumReg = value;
  106. break;
  107. case 4: // cyclinder low register / LBA mid
  108. cylinderLowReg = value;
  109. break;
  110. case 5: // cyclinder high register / LBA high
  111. cylinderHighReg = value;
  112. break;
  113. case 6: // device/head register
  114. // DEV bit is handled by IDE interface
  115. devHeadReg = value;
  116. break;
  117. case 7: // command register
  118. statusReg &= ~(DRQ | ERR);
  119. setTransferRead(false);
  120. setTransferWrite(false);
  121. executeCommand(value);
  122. break;
  123. case 8:
  124. case 9:
  125. case 10:
  126. case 11:
  127. case 12:
  128. case 13:
  129. case 15: // not used
  130. case 14: // device control register, handled by IDE interface
  131. // do nothing
  132. break;
  133. case 0: // data register, converted to readData by IDE interface
  134. default:
  135. UNREACHABLE; break;
  136. }
  137. }
  138. word AbstractIDEDevice::readData(EmuTime::param /*time*/)
  139. {
  140. if (!transferRead) {
  141. // no read in progress
  142. return 0x7F7F;
  143. }
  144. assert((transferIdx + 1) < sizeof(buffer));
  145. word result = (buffer[transferIdx + 0] << 0) +
  146. (buffer[transferIdx + 1] << 8);
  147. transferIdx += 2;
  148. bufferLeft -= 2;
  149. if (bufferLeft == 0) {
  150. if (transferCount == 0) {
  151. // End of transfer.
  152. setTransferRead(false);
  153. statusReg &= ~DRQ;
  154. readEnd();
  155. } else {
  156. // Buffer empty, but transfer not done yet.
  157. readNextBlock();
  158. }
  159. }
  160. return result;
  161. }
  162. void AbstractIDEDevice::readNextBlock()
  163. {
  164. bufferLeft = readBlockStart(
  165. buffer, std::min<unsigned>(sizeof(buffer), transferCount));
  166. assert((bufferLeft & 1) == 0);
  167. transferIdx = 0;
  168. transferCount -= bufferLeft;
  169. }
  170. void AbstractIDEDevice::writeData(word value, EmuTime::param /*time*/)
  171. {
  172. if (!transferWrite) {
  173. // no write in progress
  174. return;
  175. }
  176. assert((transferIdx + 1) < sizeof(buffer));
  177. buffer[transferIdx + 0] = value & 0xFF;
  178. buffer[transferIdx + 1] = value >> 8;
  179. transferIdx += 2;
  180. bufferLeft -= 2;
  181. if (bufferLeft == 0) {
  182. unsigned bytesInBuffer = transferIdx;
  183. if (transferCount == 0) {
  184. // End of transfer.
  185. setTransferWrite(false);
  186. statusReg &= ~DRQ;
  187. } else {
  188. // Buffer full, but transfer not done yet.
  189. writeNextBlock();
  190. }
  191. // Packet commands can start a second transfer, so the command
  192. // execution must happen after we close this transfer.
  193. writeBlockComplete(buffer, bytesInBuffer);
  194. }
  195. }
  196. void AbstractIDEDevice::writeNextBlock()
  197. {
  198. transferIdx = 0;
  199. bufferLeft = std::min<unsigned>(sizeof(buffer), transferCount);
  200. transferCount -= bufferLeft;
  201. }
  202. void AbstractIDEDevice::setError(byte error)
  203. {
  204. errorReg = error;
  205. if (error) {
  206. statusReg |= ERR;
  207. } else {
  208. statusReg &= ~ERR;
  209. }
  210. statusReg &= ~DRQ;
  211. setTransferWrite(false);
  212. setTransferRead(false);
  213. }
  214. unsigned AbstractIDEDevice::getSectorNumber() const
  215. {
  216. return sectorNumReg | (cylinderLowReg << 8) |
  217. (cylinderHighReg << 16) | ((devHeadReg & 0x0F) << 24);
  218. }
  219. unsigned AbstractIDEDevice::getNumSectors() const
  220. {
  221. return (sectorCountReg == 0) ? 256 : sectorCountReg;
  222. }
  223. void AbstractIDEDevice::setInterruptReason(byte value)
  224. {
  225. sectorCountReg = value;
  226. }
  227. unsigned AbstractIDEDevice::getByteCount() const
  228. {
  229. return cylinderLowReg | (cylinderHighReg << 8);
  230. }
  231. void AbstractIDEDevice::setByteCount(unsigned count)
  232. {
  233. cylinderLowReg = count & 0xFF;
  234. cylinderHighReg = count >> 8;
  235. }
  236. void AbstractIDEDevice::setSectorNumber(unsigned lba)
  237. {
  238. sectorNumReg = (lba & 0x000000FF) >> 0;
  239. cylinderLowReg = (lba & 0x0000FF00) >> 8;
  240. cylinderHighReg = (lba & 0x00FF0000) >> 16;
  241. devHeadReg = (lba & 0x0F000000) >> 24; // note: only 4 bits
  242. }
  243. void AbstractIDEDevice::readEnd()
  244. {
  245. }
  246. void AbstractIDEDevice::executeCommand(byte cmd)
  247. {
  248. switch (cmd) {
  249. case 0x08: // Device Reset
  250. if (isPacketDevice()) {
  251. errorReg = diagnostic();
  252. createSignature(true);
  253. // TODO: Which is correct?
  254. //statusReg = 0x00;
  255. statusReg = DRDY | DSC;
  256. } else {
  257. // Command is only implemented for packet devices.
  258. setError(ABORT);
  259. }
  260. break;
  261. case 0x90: // Execute Device Diagnostic
  262. errorReg = diagnostic();
  263. createSignature();
  264. break;
  265. case 0x91: // Initialize Device Parameters
  266. // ignore command
  267. break;
  268. case 0xA1: // Identify Packet Device
  269. if (isPacketDevice()) {
  270. createIdentifyBlock(startShortReadTransfer(512));
  271. } else {
  272. setError(ABORT);
  273. }
  274. break;
  275. case 0xEC: // Identify Device
  276. if (isPacketDevice()) {
  277. setError(ABORT);
  278. } else {
  279. createIdentifyBlock(startShortReadTransfer(512));
  280. }
  281. break;
  282. case 0xEF: // Set Features
  283. switch (getFeatureReg()) {
  284. case 0x03: // Set Transfer Mode
  285. break;
  286. default:
  287. fprintf(stderr, "Unhandled set feature subcommand: %02X\n",
  288. getFeatureReg());
  289. setError(ABORT);
  290. }
  291. break;
  292. default: // unsupported command
  293. fprintf(stderr, "unsupported IDE command %02X\n", cmd);
  294. setError(ABORT);
  295. }
  296. }
  297. AlignedBuffer& AbstractIDEDevice::startShortReadTransfer(unsigned count)
  298. {
  299. assert(count <= sizeof(buffer));
  300. assert((count & 1) == 0);
  301. startReadTransfer();
  302. transferCount = 0;
  303. bufferLeft = count;
  304. transferIdx = 0;
  305. memset(buffer, 0x00, count);
  306. return buffer;
  307. }
  308. void AbstractIDEDevice::startLongReadTransfer(unsigned count)
  309. {
  310. assert((count & 1) == 0);
  311. startReadTransfer();
  312. transferCount = count;
  313. readNextBlock();
  314. }
  315. void AbstractIDEDevice::startReadTransfer()
  316. {
  317. statusReg |= DRQ;
  318. setTransferRead(true);
  319. }
  320. void AbstractIDEDevice::abortReadTransfer(byte error)
  321. {
  322. setError(error | ABORT);
  323. setTransferRead(false);
  324. }
  325. void AbstractIDEDevice::startWriteTransfer(unsigned count)
  326. {
  327. statusReg |= DRQ;
  328. setTransferWrite(true);
  329. transferCount = count;
  330. writeNextBlock();
  331. }
  332. void AbstractIDEDevice::abortWriteTransfer(byte error)
  333. {
  334. setError(error | ABORT);
  335. setTransferWrite(false);
  336. }
  337. void AbstractIDEDevice::setTransferRead(bool status)
  338. {
  339. if (status != transferRead) {
  340. transferRead = status;
  341. if (!transferWrite) {
  342. // (this is a bit of a hack!)
  343. motherBoard.getLedStatus().setLed(LedStatus::FDD, transferRead);
  344. }
  345. }
  346. }
  347. void AbstractIDEDevice::setTransferWrite(bool status)
  348. {
  349. if (status != transferWrite) {
  350. transferWrite = status;
  351. if (!transferRead) {
  352. // (this is a bit of a hack!)
  353. motherBoard.getLedStatus().setLed(LedStatus::FDD, transferWrite);
  354. }
  355. }
  356. }
  357. /** Writes a string to a location in the identify block.
  358. * Helper method for createIdentifyBlock.
  359. * @param p Pointer to write the characters to.
  360. * @param len Number of words to write.
  361. * @param s ASCII string to write.
  362. * If the string is longer than len*2 characters, it is truncated.
  363. * If the string is shorter than len*2 characters, it is padded with spaces.
  364. */
  365. static void writeIdentifyString(byte* p, unsigned len, std::string s)
  366. {
  367. s.resize(2 * len, ' ');
  368. for (unsigned i = 0; i < len; ++i) {
  369. // copy and swap
  370. p[2 * i + 0] = s[2 * i + 1];
  371. p[2 * i + 1] = s[2 * i + 0];
  372. }
  373. }
  374. void AbstractIDEDevice::createIdentifyBlock(AlignedBuffer& buf)
  375. {
  376. // According to the spec, the combination of model and serial should be
  377. // unique. But I don't know any MSX software that cares about this.
  378. writeIdentifyString(&buf[10 * 2], 10, "s00000001"); // serial
  379. writeIdentifyString(&buf[23 * 2], 4,
  380. // Use openMSX version as firmware revision, because most of our
  381. // IDE emulation code is in fact emulating the firmware.
  382. Version::RELEASE ? strCat('v', Version::VERSION)
  383. : strCat('d', Version::REVISION));
  384. writeIdentifyString(&buf[27 * 2], 20, getDeviceName()); // model
  385. fillIdentifyBlock(buf);
  386. }
  387. template<typename Archive>
  388. void AbstractIDEDevice::serialize(Archive& ar, unsigned /*version*/)
  389. {
  390. // no need to serialize IDEDevice base class
  391. ar.serialize_blob("buffer", buffer, sizeof(buffer));
  392. ar.serialize("transferIdx", transferIdx,
  393. "bufferLeft", bufferLeft,
  394. "transferCount", transferCount,
  395. "errorReg", errorReg,
  396. "sectorCountReg", sectorCountReg,
  397. "sectorNumReg", sectorNumReg,
  398. "cylinderLowReg", cylinderLowReg,
  399. "cylinderHighReg", cylinderHighReg,
  400. "devHeadReg", devHeadReg,
  401. "statusReg", statusReg,
  402. "featureReg", featureReg);
  403. bool transferIdentifyBlock = false; // remove on next version increment
  404. // no need to break bw-compat now
  405. ar.serialize("transferIdentifyBlock", transferIdentifyBlock,
  406. "transferRead", transferRead,
  407. "transferWrite", transferWrite);
  408. }
  409. INSTANTIATE_SERIALIZE_METHODS(AbstractIDEDevice);
  410. } // namespace openmsx