+ drv = unit;
+ if (drv < CONFIG_CPM_MAX_DRIVE) {
+ struct cpm_drive_s *p = &drv_table[drv];
+
+ drv_debug(SINGLE, PSTR("detach from '%s'"), p->img_name ? p->img_name : "-");
+
+ if (p->img_name) {
+ f_close(&p->fd);
+ free(p->img_name);
+ p->opt = 0;
+ p->flags &= ~DRV_FLG_DIRTY;
+ p->img_name = NULL;
+
+ uint32_t scb = getenv_ulong(PSTR(ENV_CPM3_SCB), 16, 0);
+ if (scb && (z80_bus_cmd(Request) & ZST_ACQUIRED)) {
+ z80_write(scb + 0xf0, 0xff);
+ z80_write(p->dph + 11, 0xff);
+ z80_bus_cmd(Release);
+ }
+ }
+ }
+ return 0;
+}
+
+static int drv_find_file_attached(const char *fn)
+{
+ for (uint8_t i = 0; i < CONFIG_CPM_MAX_DRIVE; i++) {
+ struct cpm_drive_s *p = &drv_table[i];
+ if (p->img_name && !strcmp(fn, p->img_name)) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+int drv_attach(uint8_t unit, const char *filename, drv_opt_t options)
+{
+ int res;
+
+ drv = unit;
+ if (drv >= CONFIG_CPM_MAX_DRIVE)
+ return EATRANGE;
+
+ struct cpm_drive_s *p = &drv_table[drv];
+
+ if (options & DRV_OPT_REATTATCH) {
+ if (filename) {
+ return EUNEXPARG;
+ }
+
+ if (!p->img_name) {
+ return EATNOT;
+ }
+
+ /* change options */
+ if ((p->opt ^ options) & DRV_OPT_RO) {
+ f_close(&p->fd);
+ res = f_open(&p->fd, p->img_name,
+ FA_READ | (options&DRV_OPT_RO ? 0 : FA_WRITE));
+ }
+
+ p->opt = options & ~DRV_OPT_REATTATCH;
+
+ } else {
+
+ if (p->img_name)
+ return EATALRDY;
+ if (drv_find_file_attached(filename) >= 0)
+ return EATOTHER;
+
+ p->opt = options;
+
+ /* new attachment */
+
+ if ((p->img_name = strdup(filename)) == NULL)
+ return ENOMEM;
+
+ res = f_open(&p->fd, p->img_name,
+ FA_READ | (options&DRV_OPT_RO ? 0 : FA_WRITE));
+
+ if (!res && f_size(&p->fd) < CONFIG_CPM_DISKSIZE) {
+#if 0
+ unsigned int bw;
+ debug_cpmsd(" expanding image file from %ld to %ld\n",
+ f_size(&p->fd), CONFIG_CPM_DISKSIZE);
+
+ res = f_lseek(&p->fd, CONFIG_CPM_DISKSIZE-CONFIG_CPM_BLOCK_SIZE);
+ if (!res) {
+ memset(disk_buffer, 0xe5, CONFIG_CPM_BLOCK_SIZE);
+ res = f_write(&p->fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &bw);
+ if (res || bw < CONFIG_CPM_BLOCK_SIZE) {
+ debug_cpmsd(" failed! res: %d, bytes written: %u\n", res, bw);
+ }
+ p->flags |= DRV_FLG_DIRTY;
+ bg_setstat(handle_cpm_drv_to, 1);
+ }
+#else
+ drv_debug(SINGLE, PSTR("wrong image file size: %ld, should be %ld"),
+ f_size(&p->fd), CONFIG_CPM_DISKSIZE);
+ res = 64;
+#endif
+ }
+ if (res) {
+ drv_detach(drv);
+ return EATOPEN;
+ }
+ }
+
+ return ESUCCESS;
+}
+
+
+int cpm_drv_to(int state)
+{
+ static uint32_t ts;
+
+ switch(state) {
+ case 0:
+ break;
+
+ case 1:
+ ts = get_timer(0);
+ state = 2;
+ break;
+
+ case 2:
+ if (get_timer(ts) > 1000) {
+ for (uint_fast8_t i=0; i < CONFIG_CPM_MAX_DRIVE; i++) {
+ if (drv_table[i].flags & DRV_FLG_DIRTY) {
+ drv_table[i].flags &= ~DRV_FLG_DIRTY;
+ f_sync(&drv_table[i].fd);
+ drv = i;
+ drv_debug(SINGLE, PSTR("f_sync"));
+ }
+ }
+ state = 0;
+ }
+ }
+ return state;
+}
+
+static const FLASH char * const FLASH rc_messages[] = {
+ FSTR("OK"),
+ FSTR("Internal error: wrong message len"), /* 01 */
+ FSTR("Invalid relative drive #"), /* 02 */
+ FSTR("Bus timeout"), /* 03 */
+ FSTR("Access byond disk size"), /* 04 */
+ FSTR("Write protect"), /* 05 */
+ FSTR("No media"), /* 06 */
+ FSTR("R/W address == 0 !!!!"), /* 07 */
+ };
+
+void msg_cpm_result(uint8_t subf, uint8_t rc, int res)
+{
+ uint8_t result_msg[3];
+
+ if (res)
+ rc |= 0x80;
+
+ result_msg[0] = rc;
+ result_msg[1] = res;
+ result_msg[2] = res >> 8;
+
+ msg_xmit(2, subf, sizeof(result_msg), result_msg);
+
+ if (rc) {
+#if GCC_BUG_61443
+ char msg[40];
+ strncpy_P(msg, rc_messages[rc & 0x7f], sizeof msg -1);
+ drv_debug(END, PSTR(" rc: %.02x/%d, '%s'"),
+ rc, res, msg);
+#else
+ drv_debug(END, PSTR(" rc: %.02x/%d, '%S'"),
+ rc, res, rc_messages[rc & 0x7f]);
+#endif
+ } else
+ drv_debug(END, PSTR(""));
+
+}
+
+/*
+ db 2 ; disk command
+ ds 1 ; subcommand (login/read/write)
+ ds 1 ; @adrv (8 bits) +0
+ ds 1 ; @rdrv (8 bits) +1
+ ds 3 ; @xdph (24 bits) +2
+*/
+
+void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
+{
+ struct cpm_drive_s *dp;
+ FRESULT res = 0;
+