abcdisk.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * abcdisk.c
  3. *
  4. * Emulate an ABC80/800 disk controller
  5. */
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include "fw.h"
  9. #include "io.h"
  10. #include "abcio.h"
  11. #include "console.h"
  12. #include "sdcard.h"
  13. #include "ff.h"
  14. /* Option flags, mostly for debugging */
  15. #define NOTTHERE 0
  16. #define READONLY 0
  17. #define INTERLEAVE 0
  18. #define FORMAT_SUPPORT 0
  19. enum drive_flags {
  20. DF_MOUNTED = 1, /* Host file open, drive exported */
  21. DF_READONLY = 2, /* Drive is readonly */
  22. DF_DISABLED = 4, /* Drive is disabled */
  23. DF_DIRTY = 8 /* Disk has been written */
  24. };
  25. enum pending {
  26. PEND_IO = 1, /* I/O transfer complete */
  27. PEND_STARTCMD = 2, /* C1# = go to command state */
  28. PEND_RESET = 4 /* C3# or RST# = local/global reset */
  29. };
  30. /*
  31. * Status bits in INP(1); bits 5 & 2 are unknown, bit 4 is apparently
  32. * always 0 on at least card 66 81046-02 (55 21046-x1)...
  33. * "fast DMA controller".
  34. */
  35. enum abc_status {
  36. AS_CMD = 0x80, /* Start of command */
  37. AS_WRITE = 0x40, /* Data direction OUT(0) (to controller) */
  38. AS_ERROR = 0x08, /* INP(0) contains error info */
  39. AS_READING = 0x04, /* Waiting for sector load */
  40. AS_READY = 0x01 /* Ready for input */
  41. };
  42. /* Number of fragments (extents) we can memoize */
  43. #define MAX_FAST_FRAGMENTS 16
  44. #define CLTBL_SIZE ((MAX_FAST_FRAGMENTS+1)*2)
  45. /* Per-drive state */
  46. struct drive_state {
  47. FIL file; /* Host file */
  48. char name[4]; /* Drive name */
  49. uint16_t sectors; /* Total size in 256-byte sectors */
  50. #if INTERLEAVE
  51. uint8_t ilmsk, ilfac; /* Software interleaving parameters */
  52. #endif
  53. enum drive_flags flags;
  54. enum drive_flags force; /* Option to force flags */
  55. DWORD cltbl[CLTBL_SIZE]; /* Extent map */
  56. };
  57. /* Fixed parameters for each controller */
  58. struct ctl_params {
  59. uint8_t clustshift; /* log2(clustersize/256) */
  60. uint8_t devsel; /* I/O device select */
  61. uint16_t maxsectors; /* Maximum sectors for this controller */
  62. uint8_t c, h, s; /* Disk geometry */
  63. bool newaddr; /* "New addressing" */
  64. const char name[4]; /* Name of controller (disk type) */
  65. #if INTERLEAVE
  66. uint8_t ilmsk, ilfac; /* Software interleaving parea */
  67. #endif
  68. #if FORMAT_SUPPORT
  69. bool fmtdata_i_buf; /* Use user-provided formatting data */
  70. #endif
  71. } __attribute__((aligned(4)));
  72. /* Per-controller state */
  73. struct ctl_state {
  74. struct abc_dev iodev;
  75. const struct ctl_params *params;
  76. uint8_t k[4]; /* Command bytes */
  77. uint8_t drives; /* Total drives present */
  78. uint8_t error; /* Error status */
  79. bool initialized; /* Controller initialized */
  80. volatile enum pending pending; /* Need to do I/O */
  81. struct drive_state drv[8]; /* Per-drive state */
  82. uint8_t buf[4][256]; /* 4 host buffers @ 256 bytes */
  83. };
  84. /*
  85. * Error codes for INP(0), derived from FD1791 status codes
  86. *
  87. * DOSGEN depends on OUT_OF_RANGE having this value... and different
  88. * DOSGEN expect different values. If this value is wrong, DOSGEN will
  89. * spin forever on "testing sector..."
  90. */
  91. #define OUT_OF_RANGE 0x21 /* Status code for an invalid sector */
  92. #define DISK_NOT_READY 0x80
  93. #define WRITE_PROTECT 0x40
  94. #define WRITE_FAULT 0x20
  95. #define CRC_ERROR 0x08
  96. enum controller_types {
  97. MOx,
  98. MFx,
  99. SFx,
  100. HDx,
  101. CONTROLLER_TYPES
  102. };
  103. #if INTERLEAVE
  104. # define IL(mask, fac) .ilmsk = (mask), .ilfac = (fac),
  105. #else
  106. # define IL(mask, fac)
  107. #endif
  108. static const struct ctl_params parameters[CONTROLLER_TYPES] = {
  109. /*
  110. * MOx: covers all of these formats:
  111. * SSSD = 40×8×1 (80K, FD2/DD80),
  112. * SSDD = 40×16×1 (160K, FD2D/DD82/ABC830) and
  113. * DSDD = 40×16×2 (320K, FD4D/DD84/DD52)
  114. */
  115. [MOx] = {
  116. .devsel = 45,
  117. .clustshift = 0,
  118. .maxsectors = 40 * 2 * 16,
  119. .c = 40, .h = 2, .s = 16,
  120. .name = "mo",
  121. #if FORMAT_SUPPORT
  122. .fmtdata_in_buf = true,
  123. #endif
  124. IL(15, 7)
  125. },
  126. /* MFx: DSQD = 80×16x2 (640K, ABC832/834) */
  127. [MFx] = {
  128. .devsel = 44,
  129. .clustshift = 2,
  130. .maxsectors = 80 * 2 * 16,
  131. .c = 80, .h = 2, .s = 16,
  132. .name = "mf"
  133. },
  134. /* SFx: 8" floppy (DD88, ABC838) */
  135. [SFx] = {
  136. .devsel = 46,
  137. .clustshift = 2,
  138. .maxsectors = (77 * 2 - 1) * 26, /* Track 0, side 0 not used */
  139. .c = 77, .h = 2, .s = 26,
  140. .name = "sf"
  141. },
  142. [HDx] = {
  143. .devsel = 36,
  144. .clustshift = 5,
  145. .newaddr = true, /* Actually irrelevant for clustshift = 5 */
  146. .maxsectors = (239 * 8 - 1) * 32, /* Maximum supported by UFD-DOS */
  147. .c = 238, .h = 16, .s = 64,
  148. .name = "hd"
  149. }
  150. };
  151. static struct ctl_state __dram_bss controllers[CONTROLLER_TYPES];
  152. static inline bool mounted(const struct drive_state *drv)
  153. {
  154. return !!(drv->flags & DF_MOUNTED);
  155. }
  156. static inline struct drive_state *cur_drv_mutable(struct ctl_state *state)
  157. {
  158. return &state->drv[state->k[1] & 7];
  159. }
  160. static inline const struct drive_state *cur_drv(const struct ctl_state *state)
  161. {
  162. return &state->drv[state->k[1] & 7];
  163. }
  164. static inline unsigned int cur_sector(const struct ctl_state *state)
  165. {
  166. uint8_t k2 = state->k[2], k3 = state->k[3];
  167. if (state->params->newaddr)
  168. return (k2 << 8) + k3;
  169. else
  170. return (((k2 << 3) + (k3 >> 5)) << state->params->clustshift)
  171. + (k3 & 31);
  172. }
  173. /* Get physical sector number, after interleaving */
  174. static inline unsigned int
  175. virt2phys(const struct drive_state *drv, unsigned int sector)
  176. {
  177. #if INTERLEAVE
  178. unsigned int ilmsk = drv->ilmsk;
  179. unsigned int ilfac = drv->ilfac;
  180. sector = (sector & ~ilmsk) | ((sector * ilfac) & ilmsk);
  181. #endif
  182. return sector;
  183. }
  184. static inline unsigned int phys_sector(const struct ctl_state *state)
  185. {
  186. return virt2phys(cur_drv(state), cur_sector(state));
  187. }
  188. static inline unsigned int file_pos(const struct ctl_state *state)
  189. {
  190. return phys_sector(state) << 8;
  191. }
  192. static inline bool file_pos_valid(const struct ctl_state *state)
  193. {
  194. return phys_sector(state) < cur_drv(state)->sectors;
  195. }
  196. static inline bool cur_sector_valid(const struct ctl_state *state)
  197. {
  198. uint8_t k3 = state->k[3];
  199. if (!state->params->newaddr && ((k3 & 31) >> state->params->clustshift))
  200. return false;
  201. return phys_sector(state) < state->params->maxsectors;
  202. }
  203. static inline uint8_t *cur_buf(struct ctl_state *state)
  204. {
  205. return state->buf[state->k[1] >> 6];
  206. }
  207. static void disk_start_command(struct ctl_state *state)
  208. {
  209. #if 0
  210. con_printf("%-2s: start command\n", state->params->name);
  211. #endif
  212. memset(&state->k, 0xee, sizeof state->k);
  213. state->iodev.callback_mask |= 1 << 0;
  214. abc_setup_out_queue(&state->iodev, state->k, 4,
  215. AS_CMD|AS_READY|AS_WRITE|
  216. (state->error ? AS_ERROR : 0));
  217. }
  218. static void sync_drives(struct ctl_state *state)
  219. {
  220. for (int i = 0; i < 8; i++) {
  221. struct drive_state *drv = &state->drv[i];
  222. if (drv->flags & DF_DIRTY) {
  223. f_sync(&state->drv[i].file);
  224. drv->flags &= ~DF_DIRTY;
  225. }
  226. }
  227. }
  228. static void disk_set_error(struct ctl_state *state, unsigned int error)
  229. {
  230. abc_set_inp_default(&state->iodev, state->error = error);
  231. state->k[0] = 0; /* Abort remaining command sequence */
  232. }
  233. static void disk_reset_state(struct ctl_state *state)
  234. {
  235. abc_set_inp_status(&state->iodev, 0);
  236. disk_set_error(state, 0);
  237. disk_start_command(state);
  238. }
  239. static struct drive_state *
  240. name_to_drive(const char *drive)
  241. {
  242. struct ctl_state *state;
  243. unsigned int ndrive;
  244. /* All drive names are three letters long */
  245. if (strlen(drive) != 3)
  246. return NULL;
  247. ndrive = drive[2] - '0';
  248. if (ndrive > 7)
  249. return NULL;
  250. if (!memcmp("dr", drive, 2)) {
  251. /* DRx alias for MOx (matches "old DOS") */
  252. return &controllers[MOx].drv[ndrive];
  253. }
  254. for (int i = 0; i < CONTROLLER_TYPES; i++) {
  255. struct ctl_state *state = &controllers[i];
  256. if (!memcmp(state->params->name, drive, 2))
  257. return &state->drv[ndrive];
  258. }
  259. return NULL; /* No such disk */
  260. }
  261. bool valid_drive_name(const char *drive)
  262. {
  263. return name_to_drive(drive) != NULL;
  264. }
  265. static int mount_drive(struct drive_state *drv, struct ctl_state *state,
  266. const char *filename)
  267. {
  268. FRESULT rv;
  269. if (mounted(drv)) {
  270. if (!(sdc.fsstatus & STA_NOINIT))
  271. f_close(&drv->file);
  272. drv->flags &= ~DF_MOUNTED;
  273. state->drives--;
  274. }
  275. if (drv->force & DF_DISABLED)
  276. return -1;
  277. drv->flags = drv->force & ~DF_MOUNTED;
  278. if (!filename) {
  279. return 0; /* Explicit unmount */
  280. } else {
  281. while (1) {
  282. BYTE mode = FA_OPEN_EXISTING | FA_READ;
  283. if (!(drv->flags & DF_READONLY))
  284. mode |= FA_WRITE;
  285. rv = f_open(&drv->file, filename, mode);
  286. if (rv == FR_WRITE_PROTECTED && (mode & FA_WRITE)) {
  287. drv->flags |= DF_READONLY;
  288. continue;
  289. }
  290. drv->flags |= (rv == FR_OK) ? DF_MOUNTED : DF_DISABLED;
  291. break;
  292. }
  293. if (!(drv->flags & DF_MOUNTED))
  294. return -1;
  295. }
  296. con_printf("abcdisk: %-3s = %s\n", drv->name, filename);
  297. /* Try to memoize extents for fast seek */
  298. drv->cltbl[0] = CLTBL_SIZE;
  299. drv->file.cltbl = drv->cltbl;
  300. rv = f_lseek(&drv->file, CREATE_LINKMAP);
  301. if (rv != FR_OK) {
  302. con_printf("abcdisk: %-3s ! file too fragmented, will be slow\n",
  303. drv->name);
  304. }
  305. /*
  306. * Smaller than the standard disk size? Treat the sectors
  307. * beyond the end as bad.
  308. */
  309. unsigned int filesec = f_size(&drv->file) >> 8;
  310. drv->sectors = min(filesec, state->params->maxsectors);
  311. /* Interleaving parameters */
  312. #if INTERLEAVE
  313. drv->ilfac = state->params->ilfac;
  314. drv->ilmsk = state->params->ilmsk;
  315. #endif
  316. state->drives++;
  317. return 0;
  318. }
  319. static void unmount_drives(struct ctl_state *state)
  320. {
  321. int i;
  322. for (int i = 0; i < 8; i++) {
  323. struct drive_state *drv = &state->drv[i];
  324. if (drv->flags & DF_MOUNTED)
  325. mount_drive(drv, state, NULL);
  326. }
  327. }
  328. /* RST# = global reset, C3# = local reset, C1# = goto command start */
  329. #define IDLE_CALLBACK_MASK ((1 << 7)|(1 << 4)|(1 << 2))
  330. static ABC_CALLBACK(abcdisk_callback_out)
  331. {
  332. struct ctl_state *state = container_of(dev, struct ctl_state, iodev);
  333. dev->callback_mask = IDLE_CALLBACK_MASK;
  334. ABC_INP1_DATA = dev->inp_data[1] = 0;
  335. state->error = 0;
  336. state->pending |= PEND_IO;
  337. }
  338. static ABC_CALLBACK(abcdisk_callback_inp)
  339. {
  340. struct ctl_state *state = container_of(dev, struct ctl_state, iodev);
  341. dev->callback_mask = IDLE_CALLBACK_MASK;
  342. ABC_INP1_DATA = dev->inp_data[1] = 0;
  343. state->error = 0;
  344. state->pending |= PEND_IO;
  345. }
  346. static ABC_CALLBACK(abcdisk_callback_restart)
  347. {
  348. struct ctl_state *state = container_of(dev, struct ctl_state, iodev);
  349. state->pending |= PEND_STARTCMD;
  350. }
  351. static ABC_CALLBACK(abcdisk_callback_rst)
  352. {
  353. struct ctl_state *state = container_of(dev, struct ctl_state, iodev);
  354. state->pending |= PEND_RESET;
  355. }
  356. static char abcdisk_80_800[] = "/abcdisk.800/";
  357. static const char * const disk_pfx[] = {
  358. abcdisk_80_800, "/abcdisk/", "/abcdisk.", "/", NULL
  359. };
  360. static void init_drives(struct ctl_state *state)
  361. {
  362. uint32_t abc_status = ABC_STATUS;
  363. char *p80;
  364. int i;
  365. /* .80/ or .800/ for the first entry */
  366. p80 = abcdisk_80_800 + 10 + !!(abc_status & ABC_STATUS_800);
  367. p80[0] = '0';
  368. p80[1] = '/';
  369. p80[2] = '\0';
  370. for (i = 0; i < 8; i++) {
  371. struct drive_state *drv = &state->drv[i];
  372. unsigned int filesec;
  373. const char * const *pfx;
  374. snprintf(drv->name, sizeof drv->name, "%-.2s%c",
  375. state->params->name, i + '0');
  376. for (pfx = disk_pfx; *pfx; pfx++) {
  377. char filename_buf[64];
  378. snprintf(filename_buf, sizeof filename_buf,
  379. "%s%s", *pfx, drv->name);
  380. if (!mount_drive(drv, state, filename_buf))
  381. break;
  382. }
  383. }
  384. state->initialized = true;
  385. }
  386. static void do_next_command(struct ctl_state *state)
  387. {
  388. struct drive_state *drv = cur_drv_mutable(state);
  389. uint8_t *buf = cur_buf(state);
  390. #if 0
  391. con_printf("%-3s: cmd %02x %02x %02x %02x\n",
  392. drv->name, state->k[0], state->k[1],
  393. state->k[2], state->k[3]);
  394. #endif
  395. if (state->k[0] & 0x01) {
  396. /* READ SECTOR */
  397. if (!(drv->flags & DF_MOUNTED)) {
  398. disk_set_error(state, DISK_NOT_READY);
  399. } else if (!cur_sector_valid(state)) {
  400. disk_set_error(state, OUT_OF_RANGE);
  401. } else {
  402. UINT rlen = 0;
  403. FRESULT rv;
  404. set_led(LED_DISKIO, true);
  405. abc_set_inp_status(&state->iodev, AS_READING|AS_READY);
  406. rv = f_lseek(&drv->file, file_pos(state));
  407. if (rv == FR_OK)
  408. rv = f_read(&drv->file, buf, 256, &rlen);
  409. if (rv != FR_OK || rlen != 256) {
  410. disk_set_error(state, CRC_ERROR);
  411. }
  412. }
  413. state->k[0] &= ~0x01; /* Command done */
  414. }
  415. if (state->k[0] & 0x02) {
  416. /* SECTOR TO HOST */
  417. state->k[0] &= ~0x02; /* Command done */
  418. state->iodev.callback_mask |= 1 << 8;
  419. abc_setup_inp_queue(&state->iodev, buf, 256, AS_READY);
  420. return;
  421. }
  422. if (state->k[0] & 0x04) {
  423. /* SECTOR FROM HOST */
  424. state->k[0] &= ~0x04; /* Command done */
  425. state->iodev.callback_mask |= 1 << 0;
  426. abc_setup_out_queue(&state->iodev, buf, 256, AS_WRITE|AS_READY);
  427. return;
  428. }
  429. if (state->k[0] & 0x08) {
  430. /* WRITE SECTOR */
  431. if (!(drv->flags & DF_MOUNTED)) {
  432. disk_set_error(state, DISK_NOT_READY);
  433. } else if (drv->flags & DF_READONLY) {
  434. disk_set_error(state, WRITE_PROTECT);
  435. } else if (!cur_sector_valid(state)) {
  436. disk_set_error(state, OUT_OF_RANGE);
  437. } else {
  438. UINT wlen = 0;
  439. FRESULT rv;
  440. set_led(LED_DISKIO, true);
  441. rv = f_lseek(&drv->file, file_pos(state));
  442. if (rv == FR_OK) {
  443. drv->flags |= DF_DIRTY;
  444. rv = f_write(&drv->file, buf, 256, &wlen);
  445. }
  446. if (rv != FR_OK || wlen != 256)
  447. disk_set_error(state, WRITE_FAULT);
  448. }
  449. state->k[0] &= ~0x08; /* Command done */
  450. }
  451. #if FORMAT_SUPPORT
  452. /* This code needs additional work */
  453. if (state->k[0] & 0x10 && state->k[1] & 0x08) {
  454. state->out_ptr = 0;
  455. /* FORMAT */
  456. if (!drv->hf) {
  457. state->error = 0x80; /* Not ready */
  458. } else if (!file_wrok(hf)) {
  459. state->error = 0x40; /* Write protect */
  460. } else {
  461. unsigned int s, c0, c1, s0, s1;
  462. unsigned int cylsec = state->s * state->h;
  463. uint8_t data[256];
  464. unsigned int fmtsec, filesec;
  465. /* Sector count produced by format */
  466. fmtsec = state->params->maxsectors;
  467. /* For non-MO-drives, this seems to be internally generated */
  468. memset(data, 0x40, 256);
  469. if (state->fmtdata_in_buf) {
  470. /*
  471. * MO drives put the sector image in the buffers, for
  472. * backwards compatibility and to support single density.
  473. *
  474. * Right before the F7 header CRC opcode is a density byte;
  475. * 00 for single, and 01 for double. The data begins after
  476. * a byte of FB.
  477. */
  478. bool single = false;
  479. const uint8_t *p, *ep;
  480. ep = state->buf[1];
  481. for (p = state->buf[0]+1; p < ep; p++) {
  482. if (*p == 0xf7)
  483. single = (p[-1] == 0);
  484. if (*p == 0xfb)
  485. break;
  486. }
  487. fmtsec >>= single;
  488. if (*p++ == 0xfb) { /* Data block found */
  489. if (single) {
  490. /* Really two 128-byte sectors! */
  491. memcpy(data, p, 128);
  492. memcpy(data+128, p, 128);
  493. } else {
  494. memcpy(data, p, 256);
  495. }
  496. }
  497. }
  498. /*
  499. * Adjust the size of the accessible device to the smallest
  500. * of the physical file and the formatted size
  501. */
  502. filesec = drv->hf->filesize >> 8;
  503. drv->sectors = (filesec && filesec < fmtsec) ? filesec : fmtsec;
  504. /*
  505. * k2 and k3 contain the first and last cylinder numbers to
  506. * format, inclusively. The last cylinder may be partial due
  507. * to virtual remapping, e.g. for sf floppies.
  508. */
  509. c0 = state->k[2];
  510. s0 = c0 * cylsec;
  511. c1 = state->k[3] + 1;
  512. s1 = c1 * cylsec;
  513. if (tracing(TRACE_DISK)) {
  514. fprintf(tracef, "%s: formatting cyl %u..%u, sectors %u..%u\n",
  515. drv->name, c0, c1-1, s0, s1-1);
  516. }
  517. clearerr(hf->f);
  518. state->error = 0;
  519. for (s = s0; s < s1; s++) {
  520. unsigned int ps = virt2phys(drv, s);
  521. if (ps >= drv->sectors) {
  522. state->error |= 0x02; /* Track 0/Lost data? */
  523. break;
  524. } else if (hf->map) {
  525. memcpy(hf->map + (ps << 8), data, 256);
  526. } else {
  527. fseek(hf->f, ps << 8, SEEK_SET);
  528. fwrite(data, 1, 256, hf->f);
  529. }
  530. }
  531. if (ferror(hf->f))
  532. state->error |= 0x20; /* Write fault */
  533. }
  534. state->k[1] &= ~0x08;
  535. }
  536. if (!(state->k[1] & 0x38))
  537. state->k[0] &= ~0x10;
  538. #else
  539. /* No FORMAT support */
  540. state->k[0] &= ~0x10;
  541. #endif
  542. state->k[0] &= ~0xe0; /* Unimplemented commands */
  543. disk_start_command(state);
  544. }
  545. static FATFS sd_fs;
  546. static int mount_disk(void)
  547. {
  548. FRESULT rv;
  549. char label[128];
  550. uint32_t volid, freeclust;
  551. FATFS *fs;
  552. set_led(LED_DISKIO, true);
  553. rv = f_mount(&sd_fs, "", 1);
  554. if (rv != FR_OK) {
  555. con_printf("sdcard: no volume found\n");
  556. set_led(LED_DISKIO, false);
  557. return -1;
  558. }
  559. label[0] = '\0';
  560. volid = 0;
  561. f_getlabel("", label, &volid);
  562. con_printf("sdcard: volume found, label \"%s\", volid %08x\n", label, volid);
  563. freeclust = 0;
  564. f_getfree("", &freeclust, &fs);
  565. con_printf("sdcard: %u/%u clusters free, clusters = %u bytes\n",
  566. freeclust, fs->n_fatent - 2, fs->csize << 9);
  567. return 0;
  568. }
  569. #define SYNC_TIME (1*TIMER_HZ) /* 1 s */
  570. /* Called from the main loop */
  571. void abcdisk_io_poll(void)
  572. {
  573. static uint32_t last_sync;
  574. static uint32_t last_timer = -1U;
  575. static uint32_t prev_abc_status = -1U;
  576. uint32_t abc_status_change;
  577. uint32_t now = timer_count();
  578. bool need_sync = false;
  579. uint32_t abc_status = ABC_STATUS & (ABC_STATUS_LIVE | ABC_STATUS_800);
  580. bool unmount_all = false;
  581. bool reset_all = false;
  582. if (now != last_timer) {
  583. /*
  584. * Periodically try to initialize the SD card if we are waiting
  585. * for it. The disk cache, however, will be initialized when fatfs
  586. * tries to mount the device.
  587. */
  588. if (sdc.status & STA_NOINIT) {
  589. if (!(sdcard_init() & STA_NOINIT))
  590. mount_disk();
  591. unmount_all = true;
  592. } else if (sdcard_present_poll() & STA_NOINIT) {
  593. unmount_all = true;
  594. } else {
  595. if ((now - last_sync) >= SYNC_TIME) {
  596. need_sync = true;
  597. last_sync = now;
  598. }
  599. }
  600. last_timer = now;
  601. }
  602. abc_status_change = abc_status ^ prev_abc_status;
  603. if (unlikely(abc_status_change)) {
  604. const char *host;
  605. prev_abc_status = abc_status;
  606. set_led(LED_ABCBUS, !!(abc_status & ABC_STATUS_LIVE));
  607. con_puts("ABC-bus host: ");
  608. con_puts(abc_status & ABC_STATUS_800 ? "ABC800" : "ABC80");
  609. con_puts(abc_status & ABC_STATUS_LIVE ? " (online)\n" : " (offline)\n");
  610. unmount_all = !!(abc_status_change & ABC_STATUS_800);
  611. reset_all = true;
  612. need_sync = true;
  613. }
  614. for (int i = 0; i < CONTROLLER_TYPES; i++) {
  615. struct ctl_state *state = &controllers[i];
  616. enum pending pending = 0;
  617. if (unmount_all && state->initialized) {
  618. unmount_drives(state);
  619. state->initialized = false;
  620. } else if (!state->initialized && !(sdc.status & STA_NOINIT)) {
  621. init_drives(state);
  622. }
  623. if (reset_all)
  624. disk_reset_state(state);
  625. if (abc_status & ABC_STATUS_LIVE) {
  626. mask_irq(ABC_IRQ);
  627. pending = state->pending;
  628. state->pending = 0;
  629. unmask_irq(ABC_IRQ);
  630. if (pending & (PEND_RESET|PEND_STARTCMD))
  631. disk_reset_state(state);
  632. else if (pending & PEND_IO)
  633. do_next_command(state);
  634. }
  635. if (state->initialized && (need_sync || (pending & PEND_RESET)))
  636. sync_drives(state);
  637. }
  638. if (need_sync) {
  639. /* Did we sync drives? Clear LED. */
  640. set_led(LED_DISKIO, false);
  641. }
  642. }
  643. /*
  644. * Called during initialization. Don't initialize the SD card here;
  645. * it will take too long and ABC will time out claiming no drives present.
  646. */
  647. void abcdisk_init(void)
  648. {
  649. static const struct abc_dev iodev_template = {
  650. .callback_mask = IDLE_CALLBACK_MASK,
  651. .inp_en = 3,
  652. .status_first_out_mask = (uint8_t)~0x80,
  653. .status_first_inp_mask = (uint8_t)~0x80,
  654. .callback_out[0] = abcdisk_callback_out,
  655. .callback_inp[0] = abcdisk_callback_inp,
  656. .callback_out[2] = abcdisk_callback_restart,
  657. .callback_out[4] = abcdisk_callback_rst, /* C3# = local reset */
  658. .callback_rst = abcdisk_callback_rst
  659. };
  660. for (int i = 0; i < CONTROLLER_TYPES; i++) {
  661. struct ctl_state *state = &controllers[i];
  662. state->params = &parameters[i];
  663. state->iodev = iodev_template;
  664. disk_reset_state(state);
  665. abc_register(&state->iodev, state->params->devsel);
  666. }
  667. }