2
0

abcdisk.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * abcdisk.c
  3. *
  4. * Emulate an ABC80/800 disk controller
  5. */
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include "common.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. unsigned int ndrive;
  243. /* All drive names are three letters long */
  244. if (strlen(drive) != 3)
  245. return NULL;
  246. ndrive = drive[2] - '0';
  247. if (ndrive > 7)
  248. return NULL;
  249. if (!memcmp("dr", drive, 2)) {
  250. /* DRx alias for MOx (matches "old DOS") */
  251. return &controllers[MOx].drv[ndrive];
  252. }
  253. for (int i = 0; i < CONTROLLER_TYPES; i++) {
  254. struct ctl_state * const state = &controllers[i];
  255. if (!memcmp(state->params->name, drive, 2))
  256. return &state->drv[ndrive];
  257. }
  258. return NULL; /* No such disk */
  259. }
  260. bool valid_drive_name(const char *drive)
  261. {
  262. return name_to_drive(drive) != NULL;
  263. }
  264. static int mount_drive(struct drive_state *drv, struct ctl_state *state,
  265. const char *filename)
  266. {
  267. FRESULT rv;
  268. if (mounted(drv)) {
  269. if (!(sdc.fsstatus & STA_NOINIT))
  270. f_close(&drv->file);
  271. drv->flags &= ~DF_MOUNTED;
  272. state->drives--;
  273. }
  274. if (drv->force & DF_DISABLED)
  275. return -1;
  276. drv->flags = drv->force & ~DF_MOUNTED;
  277. if (!filename) {
  278. return 0; /* Explicit unmount */
  279. } else {
  280. while (1) {
  281. BYTE mode = FA_OPEN_EXISTING | FA_READ;
  282. if (!(drv->flags & DF_READONLY))
  283. mode |= FA_WRITE;
  284. rv = f_open(&drv->file, filename, mode);
  285. if (rv == FR_WRITE_PROTECTED && (mode & FA_WRITE)) {
  286. drv->flags |= DF_READONLY;
  287. continue;
  288. }
  289. drv->flags |= (rv == FR_OK) ? DF_MOUNTED : DF_DISABLED;
  290. break;
  291. }
  292. if (!(drv->flags & DF_MOUNTED))
  293. return -1;
  294. }
  295. con_printf("abcdisk: %-3s = %s\n", drv->name, filename);
  296. /* Try to memoize extents for fast seek */
  297. drv->cltbl[0] = CLTBL_SIZE;
  298. drv->file.cltbl = drv->cltbl;
  299. rv = f_lseek(&drv->file, CREATE_LINKMAP);
  300. if (rv != FR_OK) {
  301. con_printf("abcdisk: %-3s ! file too fragmented, will be slow\n",
  302. drv->name);
  303. }
  304. /*
  305. * Smaller than the standard disk size? Treat the sectors
  306. * beyond the end as bad.
  307. */
  308. unsigned int filesec = f_size(&drv->file) >> 8;
  309. drv->sectors = min(filesec, state->params->maxsectors);
  310. /* Interleaving parameters */
  311. #if INTERLEAVE
  312. drv->ilfac = state->params->ilfac;
  313. drv->ilmsk = state->params->ilmsk;
  314. #endif
  315. state->drives++;
  316. return 0;
  317. }
  318. static void unmount_drives(struct ctl_state *state)
  319. {
  320. int i;
  321. for (int i = 0; i < 8; i++) {
  322. struct drive_state *drv = &state->drv[i];
  323. if (drv->flags & DF_MOUNTED)
  324. mount_drive(drv, state, NULL);
  325. }
  326. }
  327. /* RST# = global reset, C3# = local reset, C1# = goto command start */
  328. #define IDLE_CALLBACK_MASK ((1 << 7)|(1 << 4)|(1 << 2))
  329. static ABC_CALLBACK(abcdisk_callback_out_inp)
  330. {
  331. struct ctl_state * const state = container_of(dev, struct ctl_state, iodev);
  332. dev->callback_mask = IDLE_CALLBACK_MASK;
  333. __abc_set_inp_status(dev, 0);
  334. state->error = 0;
  335. state->pending |= PEND_IO;
  336. }
  337. static ABC_CALLBACK(abcdisk_callback_restart)
  338. {
  339. struct ctl_state * const state = container_of(dev, struct ctl_state, iodev);
  340. state->pending |= PEND_STARTCMD;
  341. }
  342. static ABC_CALLBACK(abcdisk_callback_rst)
  343. {
  344. struct ctl_state * const state = container_of(dev, struct ctl_state, iodev);
  345. state->pending |= PEND_RESET;
  346. }
  347. static char abcdisk_80_800[] = "/abcdisk.800/";
  348. static const char * const disk_pfx[] = {
  349. abcdisk_80_800, "/abcdisk/", "/abcdisk.", "/", NULL
  350. };
  351. static void init_drives(struct ctl_state *state)
  352. {
  353. uint32_t abc_status = ABC_STATUS;
  354. char *p80;
  355. int i;
  356. /* .80/ or .800/ for the first entry */
  357. p80 = abcdisk_80_800 + 10 + !!(abc_status & ABC_STATUS_800);
  358. p80[0] = '0';
  359. p80[1] = '/';
  360. p80[2] = '\0';
  361. for (i = 0; i < 8; i++) {
  362. struct drive_state *drv = &state->drv[i];
  363. unsigned int filesec;
  364. const char * const *pfx;
  365. snprintf(drv->name, sizeof drv->name, "%-.2s%c",
  366. state->params->name, i + '0');
  367. for (pfx = disk_pfx; *pfx; pfx++) {
  368. char filename_buf[64];
  369. snprintf(filename_buf, sizeof filename_buf,
  370. "%s%s", *pfx, drv->name);
  371. if (!mount_drive(drv, state, filename_buf))
  372. break;
  373. }
  374. }
  375. state->initialized = true;
  376. }
  377. static void do_next_command(struct ctl_state *state)
  378. {
  379. struct drive_state * const drv = cur_drv_mutable(state);
  380. uint8_t *buf = cur_buf(state);
  381. #if 0
  382. con_printf("%-3s: cmd %02x %02x %02x %02x\n",
  383. drv->name, state->k[0], state->k[1],
  384. state->k[2], state->k[3]);
  385. #endif
  386. if (state->k[0] & 0x01) {
  387. /* READ SECTOR */
  388. if (!(drv->flags & DF_MOUNTED)) {
  389. disk_set_error(state, DISK_NOT_READY);
  390. } else if (!cur_sector_valid(state)) {
  391. disk_set_error(state, OUT_OF_RANGE);
  392. } else {
  393. UINT rlen = 0;
  394. FRESULT rv;
  395. set_led(LED_DISKIO, true);
  396. abc_set_inp_status(&state->iodev, AS_READING|AS_READY);
  397. rv = f_lseek(&drv->file, file_pos(state));
  398. if (rv == FR_OK)
  399. rv = f_read(&drv->file, buf, 256, &rlen);
  400. if (rv != FR_OK || rlen != 256) {
  401. disk_set_error(state, CRC_ERROR);
  402. }
  403. }
  404. state->k[0] &= ~0x01; /* Command done */
  405. }
  406. if (state->k[0] & 0x02) {
  407. /* SECTOR TO HOST */
  408. state->k[0] &= ~0x02; /* Command done */
  409. state->iodev.callback_mask |= 1 << 8;
  410. abc_setup_inp_queue(&state->iodev, buf, 256, AS_READY);
  411. return;
  412. }
  413. if (state->k[0] & 0x04) {
  414. /* SECTOR FROM HOST */
  415. state->k[0] &= ~0x04; /* Command done */
  416. state->iodev.callback_mask |= 1 << 0;
  417. abc_setup_out_queue(&state->iodev, buf, 256, AS_WRITE|AS_READY);
  418. return;
  419. }
  420. if (state->k[0] & 0x08) {
  421. /* WRITE SECTOR */
  422. if (!(drv->flags & DF_MOUNTED)) {
  423. disk_set_error(state, DISK_NOT_READY);
  424. } else if (drv->flags & DF_READONLY) {
  425. disk_set_error(state, WRITE_PROTECT);
  426. } else if (!cur_sector_valid(state)) {
  427. disk_set_error(state, OUT_OF_RANGE);
  428. } else {
  429. UINT wlen = 0;
  430. FRESULT rv;
  431. set_led(LED_DISKIO, true);
  432. rv = f_lseek(&drv->file, file_pos(state));
  433. if (rv == FR_OK) {
  434. drv->flags |= DF_DIRTY;
  435. rv = f_write(&drv->file, buf, 256, &wlen);
  436. }
  437. if (rv != FR_OK || wlen != 256)
  438. disk_set_error(state, WRITE_FAULT);
  439. }
  440. state->k[0] &= ~0x08; /* Command done */
  441. }
  442. #if FORMAT_SUPPORT
  443. /* This code needs additional work */
  444. if (state->k[0] & 0x10 && state->k[1] & 0x08) {
  445. state->out_ptr = 0;
  446. /* FORMAT */
  447. if (!drv->hf) {
  448. state->error = 0x80; /* Not ready */
  449. } else if (!file_wrok(hf)) {
  450. state->error = 0x40; /* Write protect */
  451. } else {
  452. unsigned int s, c0, c1, s0, s1;
  453. unsigned int cylsec = state->s * state->h;
  454. uint8_t data[256];
  455. unsigned int fmtsec, filesec;
  456. /* Sector count produced by format */
  457. fmtsec = state->params->maxsectors;
  458. /* For non-MO-drives, this seems to be internally generated */
  459. memset(data, 0x40, 256);
  460. if (state->fmtdata_in_buf) {
  461. /*
  462. * MO drives put the sector image in the buffers, for
  463. * backwards compatibility and to support single density.
  464. *
  465. * Right before the F7 header CRC opcode is a density byte;
  466. * 00 for single, and 01 for double. The data begins after
  467. * a byte of FB.
  468. */
  469. bool single = false;
  470. const uint8_t *p, *ep;
  471. ep = state->buf[1];
  472. for (p = state->buf[0]+1; p < ep; p++) {
  473. if (*p == 0xf7)
  474. single = (p[-1] == 0);
  475. if (*p == 0xfb)
  476. break;
  477. }
  478. fmtsec >>= single;
  479. if (*p++ == 0xfb) { /* Data block found */
  480. if (single) {
  481. /* Really two 128-byte sectors! */
  482. memcpy(data, p, 128);
  483. memcpy(data+128, p, 128);
  484. } else {
  485. memcpy(data, p, 256);
  486. }
  487. }
  488. }
  489. /*
  490. * Adjust the size of the accessible device to the smallest
  491. * of the physical file and the formatted size
  492. */
  493. filesec = drv->hf->filesize >> 8;
  494. drv->sectors = (filesec && filesec < fmtsec) ? filesec : fmtsec;
  495. /*
  496. * k2 and k3 contain the first and last cylinder numbers to
  497. * format, inclusively. The last cylinder may be partial due
  498. * to virtual remapping, e.g. for sf floppies.
  499. */
  500. c0 = state->k[2];
  501. s0 = c0 * cylsec;
  502. c1 = state->k[3] + 1;
  503. s1 = c1 * cylsec;
  504. if (tracing(TRACE_DISK)) {
  505. fprintf(tracef, "%s: formatting cyl %u..%u, sectors %u..%u\n",
  506. drv->name, c0, c1-1, s0, s1-1);
  507. }
  508. clearerr(hf->f);
  509. state->error = 0;
  510. for (s = s0; s < s1; s++) {
  511. unsigned int ps = virt2phys(drv, s);
  512. if (ps >= drv->sectors) {
  513. state->error |= 0x02; /* Track 0/Lost data? */
  514. break;
  515. } else if (hf->map) {
  516. memcpy(hf->map + (ps << 8), data, 256);
  517. } else {
  518. fseek(hf->f, ps << 8, SEEK_SET);
  519. fwrite(data, 1, 256, hf->f);
  520. }
  521. }
  522. if (ferror(hf->f))
  523. state->error |= 0x20; /* Write fault */
  524. }
  525. state->k[1] &= ~0x08;
  526. }
  527. if (!(state->k[1] & 0x38))
  528. state->k[0] &= ~0x10;
  529. #else
  530. /* No FORMAT support */
  531. state->k[0] &= ~0x10;
  532. #endif
  533. state->k[0] &= ~0xe0; /* Unimplemented commands */
  534. disk_start_command(state);
  535. }
  536. static FATFS sd_fs;
  537. static int mount_disk(void)
  538. {
  539. FRESULT rv;
  540. char label[128];
  541. uint32_t volid, freeclust;
  542. FATFS *fs;
  543. set_led(LED_DISKIO, true);
  544. rv = f_mount(&sd_fs, "", 1);
  545. if (rv != FR_OK) {
  546. con_printf("sdcard: no volume found\n");
  547. set_led(LED_DISKIO, false);
  548. return -1;
  549. }
  550. label[0] = '\0';
  551. volid = 0;
  552. f_getlabel("", label, &volid);
  553. con_printf("sdcard: volume found, label \"%s\", volid %08x\n", label, volid);
  554. freeclust = 0;
  555. f_getfree("", &freeclust, &fs);
  556. con_printf("sdcard: %u/%u clusters free, clusters = %u bytes\n",
  557. freeclust, fs->n_fatent - 2, fs->csize << 9);
  558. /* Check for firmware update image and install if it exists */
  559. rom_flash_from_sdcard();
  560. return 0;
  561. }
  562. #define SYNC_TIME (1*TIMER_HZ) /* 1 s */
  563. /* Called from the main loop */
  564. void __hot abcdisk_io_poll(void)
  565. {
  566. static uint32_t last_sync;
  567. static uint32_t last_timer = -1U;
  568. static uint32_t prev_abc_status = -1U;
  569. uint32_t abc_status_change;
  570. uint32_t now = timer_count();
  571. bool need_sync = false;
  572. uint32_t abc_status = ABC_STATUS & (ABC_STATUS_LIVE | ABC_STATUS_800);
  573. bool unmount_all = false;
  574. bool reset_all = false;
  575. if (now != last_timer) {
  576. /*
  577. * Periodically try to initialize the SD card if we are waiting
  578. * for it. The disk cache, however, will be initialized when fatfs
  579. * tries to mount the device.
  580. */
  581. if (sdc.status & STA_NOINIT) {
  582. if (!(sdcard_init() & STA_NOINIT))
  583. mount_disk();
  584. unmount_all = true;
  585. } else if (sdcard_present_poll() & STA_NOINIT) {
  586. unmount_all = true;
  587. } else {
  588. if ((now - last_sync) >= SYNC_TIME) {
  589. need_sync = true;
  590. last_sync = now;
  591. }
  592. }
  593. last_timer = now;
  594. }
  595. abc_status_change = abc_status ^ prev_abc_status;
  596. if (unlikely(abc_status_change)) {
  597. const char *host;
  598. prev_abc_status = abc_status;
  599. set_led(LED_ABCBUS, !!(abc_status & ABC_STATUS_LIVE));
  600. con_puts("ABC-bus host: ");
  601. con_puts(abc_status & ABC_STATUS_800 ? "ABC800" : "ABC80");
  602. con_puts(abc_status & ABC_STATUS_LIVE ? " (online)\n" : " (offline)\n");
  603. unmount_all = !!(abc_status_change & ABC_STATUS_800);
  604. reset_all = true;
  605. need_sync = true;
  606. }
  607. for (int i = 0; i < CONTROLLER_TYPES; i++) {
  608. struct ctl_state * const state = &controllers[i];
  609. enum pending pending = 0;
  610. if (unmount_all && state->initialized) {
  611. unmount_drives(state);
  612. state->initialized = false;
  613. } else if (!state->initialized && !(sdc.status & STA_NOINIT)) {
  614. init_drives(state);
  615. }
  616. if (reset_all)
  617. disk_reset_state(state);
  618. if (abc_status & ABC_STATUS_LIVE) {
  619. mask_irq(ABC_IRQ);
  620. pending = state->pending;
  621. state->pending = 0;
  622. unmask_irq(ABC_IRQ);
  623. if (pending & (PEND_RESET|PEND_STARTCMD))
  624. disk_reset_state(state);
  625. else if (pending & PEND_IO)
  626. do_next_command(state);
  627. }
  628. if (state->initialized && (need_sync || (pending & PEND_RESET)))
  629. sync_drives(state);
  630. }
  631. if (need_sync) {
  632. /* Did we sync drives? Clear LED. */
  633. set_led(LED_DISKIO, false);
  634. }
  635. }
  636. /*
  637. * Called during initialization. Don't initialize the SD card here;
  638. * it will take too long and ABC will time out claiming no drives present.
  639. */
  640. void abcdisk_init(void)
  641. {
  642. static const struct abc_dev iodev_template = {
  643. .callback_mask = IDLE_CALLBACK_MASK,
  644. .inp_en = 3,
  645. .status_first_out_mask = (uint8_t)~0x80,
  646. .status_first_inp_mask = (uint8_t)~0x80,
  647. .callback_out[0] = abcdisk_callback_out_inp,
  648. .callback_inp[0] = abcdisk_callback_out_inp,
  649. .callback_out[2] = abcdisk_callback_restart,
  650. .callback_out[4] = abcdisk_callback_rst, /* C3# = local reset */
  651. .callback_rst = abcdisk_callback_rst
  652. };
  653. for (int i = 0; i < CONTROLLER_TYPES; i++) {
  654. struct ctl_state * const state = &controllers[i];
  655. state->params = &parameters[i];
  656. state->iodev = iodev_template;
  657. disk_reset_state(state);
  658. abc_register(&state->iodev, state->params->devsel);
  659. }
  660. }