+/* echo message */
+void do_msg_echo(uint8_t subf, int len, uint8_t * msg)
+{
+ (void)subf;
+
+ /* send re-echo */
+ msg_xmit(1, 3, len, msg);
+}
+
+/* get timer */
+void do_msg_get_timer(uint8_t subf, int len, uint8_t * msg)
+{
+ uint32_t time_ms = (len >= 4) ? *(uint32_t *) msg : 0;
+
+ time_ms = get_timer(time_ms);
+ msg_xmit(3, subf, sizeof(time_ms), (uint8_t *) &time_ms);
+}
+
+/* ---------------------------------------------------------------------------*/
+
+#define CPM_DAY_OFFSET ((1978-1900) * 365 + 19) /* 19 leap years */
+
+/*
+ * Convert CP/M time stamp to a broken-down time structure
+ *
+ */
+int mk_date_time (int len, uint8_t *msg, struct tm *tmp)
+{
+ time_t stamp;
+
+ if (len != 5)
+ return -1;
+
+ /* days since 2000-01-01 */
+ long days = msg[3] + (msg[4] << 8) - 8036;
+
+ if (days < 0)
+ return -1;
+
+ stamp = days * ONE_DAY;
+ stamp += bcd2bin(msg[0]);
+ stamp += bcd2bin(msg[1]) * 60 ;
+ stamp += bcd2bin(msg[2]) * 3600L;
+ gmtime_r(&stamp, tmp);
+ return 0;
+}
+
+void mk_cpm_time(struct tm *tmp, uint8_t cpm_time[5])
+{
+ uint16_t days = 1;
+ uint_fast8_t leap=2;
+
+ for (int year=78; year < tmp->tm_year; year++) {
+ days = days + 365 + (leap == 0);
+ leap = (leap+1)%4;
+ }
+ days += tmp->tm_yday;
+
+ cpm_time[0] = bin2bcd(tmp->tm_sec);
+ cpm_time[1] = bin2bcd(tmp->tm_min);
+ cpm_time[2] = bin2bcd(tmp->tm_hour);
+ cpm_time[3] = days;
+ cpm_time[4] = days >> 8;
+}
+
+/* get/set cp/m time */
+void do_msg_get_set_time(uint8_t subf, int len, uint8_t * msg)
+{
+ struct tm t;
+ uint8_t cpm_time[5];
+ int rc;
+
+ memset(cpm_time, 0, ARRAY_SIZE(cpm_time));
+
+ switch (subf) {
+ case 3: /* set date & time */
+ /* initialize t with current time */
+ rc = rtc_get (&t);
+
+ if (rc >= 0) {
+ /* insert new date & time */
+ if (mk_date_time (len, msg, &t) != 0) {
+ my_puts_P(PSTR("## set_time: Bad date format\n"));
+ break;
+ }
+
+ time_t time;
+ time = mk_gmtime(&t);
+ gmtime_r(&time, &t);
+
+ /* and write to RTC */
+ rc = rtc_set (&t);
+ if(rc)
+ my_puts_P(PSTR("## set_time: Set date failed\n"));
+ } else {
+ my_puts_P(PSTR("## set_time: Get date failed\n"));
+ }
+ /* FALL TROUGH */
+ case 2: /* get date & time */
+ rc = rtc_get (&t);
+ if (rc >= 0) {
+ time_t time;
+ time = mk_gmtime(&t);
+ //mktime(&t);
+ gmtime_r(&time, &t);
+
+ mk_cpm_time(&t, cpm_time);
+ } else {
+ my_puts_P(PSTR("## get_time: Get date failed\n"));
+ }
+ break;
+ }
+
+ msg_xmit(3, subf, sizeof(cpm_time), cpm_time);
+}
+
+/* ---------------------------------------------------------------------------*/
+
+static uint8_t drv;
+static uint8_t disk_buffer[CONFIG_CPM_BLOCK_SIZE];
+static struct cpm_drive_s drv_table[CONFIG_CPM_MAX_DRIVE];
+static int handle_cpm_drv_to;
+
+typedef enum {SINGLE, START, MIDDLE, END} dbgmsg_t;
+
+void drv_debug(dbgmsg_t phase, const FLASH char *const fmt, ...) \
+{
+ struct cpm_drive_s *dp = &drv_table[drv];
+
+ if (dp->opt & DRV_OPT_DEBUG) {
+
+ va_list ap;
+ va_start (ap, fmt);
+
+ if (phase == SINGLE || phase == START)
+ printf_P(PSTR("# %7lu dsk%d: "), get_timer(0), drv);
+
+ vfprintf_P (stdout, fmt, ap);
+
+ if (phase == SINGLE || phase == END)
+ putc('\n', stdout);
+
+ va_end (ap);
+ }
+}
+
+int drv_list(void)
+{
+ for (uint8_t i = 0; i < CONFIG_CPM_MAX_DRIVE; i++) {
+ struct cpm_drive_s * p = &drv_table[i];
+ if (p->img_name) {
+ printf_P(PSTR(" dsk%d: %2s %3s attached to %s\n"), i,
+ p->opt&DRV_OPT_RO ? "RO":"RW", p->opt&DRV_OPT_DEBUG ? "DBG":"",
+ p->img_name);
+ }
+ }
+ return 0;
+}
+
+int drv_detach(uint8_t unit)
+{
+ 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 AT_RANGE;
+
+ struct cpm_drive_s *p = &drv_table[drv];
+
+ if (options & DRV_OPT_REATTATCH) {
+ if (filename) {
+ return AT_ERROR;
+ }
+
+ if (!p->img_name) {
+ return AT_NOT;
+ }
+
+ /* 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 AT_ALREADY;
+ if (drv_find_file_attached(filename) >= 0)
+ return AT_OTHER;
+
+ p->opt = options;
+
+ /* new attachment */
+
+ if ((p->img_name = strdup(filename)) == NULL)
+ return AT_NOMEM;
+
+ 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 AT_OPEN;
+ }
+ }
+
+ return AT_OK;
+}
+
+
+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;
+
+ (void)subf;
+
+ /* Get relative drive number */
+ drv = msg[1];
+ drv_debug(START, PSTR("login"));
+
+ if (len != 5) {
+ return msg_cpm_result(subf, 0x01, res);
+ }
+
+ if ( drv >= CONFIG_CPM_MAX_DRIVE) {
+ /* invalid relative drive number */
+ return msg_cpm_result(subf, 0x02, res);
+ }
+
+ dp = &drv_table[drv];
+ dp->flags &= ~DRV_FLG_OPEN;
+ dp->dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2];
+
+ if (dp->img_name == NULL) {
+ /* no file attached */
+ return msg_cpm_result(subf, 0x06, res);
+ }
+
+ f_close(&dp->fd);
+ res = f_open(&dp->fd, dp->img_name,
+ FA_READ | (dp->opt&DRV_OPT_RO ? 0 : FA_WRITE));
+
+ dp->flags |= DRV_FLG_OPEN;
+
+ /* send result*/
+ msg_cpm_result(subf, 0x00, res);
+}
+
+
+/*
+ db 2 ; disk command
+ ds 1 ; subcommand (login/read/write)
+ ds 1 ; @adrv (8 bits) +0
+ ds 1 ; @rdrv (8 bits) +1
+ ds 2 ; @trk (16 bits) +2
+ ds 2 ; @sect(16 bits) +4
+ ds 1 ; @cnt (8 bits) +6
+ ds 3 ; phys. transfer addr +7
+*/
+
+#define ADRV 0
+#define RDRV 1
+#define TRK 2
+#define SEC 4
+#define CNT 6
+#define ADDR 7
+
+void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg)
+{
+ struct cpm_drive_s *dp;
+ uint32_t addr;
+ uint32_t pos;
+ uint16_t track;
+ uint16_t sec;
+ uint8_t secs;
+ bool dowrite;
+ FRESULT res = 0;
+ uint8_t rc = 0;
+ bool buserr = 0;
+
+ drv = msg[RDRV];
+ dowrite = (subf == 2);
+
+ drv_debug(START, PSTR("%2S"), dowrite ? PSTR("W ") : PSTR(" R"));
+
+ if (len != 10) {
+ return msg_cpm_result(subf, 0x01, res);
+ }
+ if ( drv>= CONFIG_CPM_MAX_DRIVE) {
+ return msg_cpm_result(subf, 0x02, res);
+ }
+
+ dp = &drv_table[drv];
+ track = (uint16_t)(msg[TRK+1] << 8) + msg[TRK];
+ sec = (uint16_t)(msg[SEC+1] << 8) + msg[SEC];
+ secs = msg[CNT];
+ addr = ((uint32_t)msg[ADDR+2] << 16) + ((uint16_t)msg[ADDR+1] << 8) + msg[ADDR];
+
+ if (dp->img_name == NULL) {
+ /* no media */
+ return msg_cpm_result(subf, 0x06, res);
+ }
+
+ /* TODO: tracks per sector from dpb */
+ pos = (track * 8UL + sec) * CONFIG_CPM_BLOCK_SIZE;
+
+ drv_debug(MIDDLE, PSTR(" T:%4d, S:%2d, cnt:%2d, lba: %.8lx, addr: %.5lx"),
+ track, sec, secs, pos, addr);
+
+ if (addr == 0) {
+ return msg_cpm_result(subf, 0x07, res);
+ }
+
+ if (dowrite && dp->opt & DRV_OPT_RO) {
+ return msg_cpm_result(subf, 0x05, res);
+ }
+
+
+ if (pos + secs * CONFIG_CPM_BLOCK_SIZE > CONFIG_CPM_DISKSIZE) {
+ drv_debug(MIDDLE, PSTR(" access > DISKSIZE:%.8lx!"),
+ CONFIG_CPM_DISKSIZE);
+ return msg_cpm_result(subf, 0x04, res);
+ }
+
+ res = f_lseek(&dp->fd, pos);
+
+ while (!res && secs--) {
+ unsigned int brw;
+ if (dowrite) {
+ if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
+ buserr = 1;
+ break;
+ } else {
+ z80_read_block(disk_buffer, addr, CONFIG_CPM_BLOCK_SIZE);
+ z80_bus_cmd(Release);
+ }
+ res = f_write(&dp->fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &brw);
+ } else {
+ res = f_read(&dp->fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &brw);
+ if (res == FR_OK) {
+ if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
+ buserr = 1;
+ break;
+ } else {
+ z80_write_block(disk_buffer, addr, CONFIG_CPM_BLOCK_SIZE);
+ z80_bus_cmd(Release);
+ }
+ }
+ }
+ if (brw != CONFIG_CPM_BLOCK_SIZE) {
+ drv_debug(MIDDLE, PSTR(" short rd/wr: res: %d, brw: %u"),
+ res, brw);
+ res = 64;
+ }
+ addr += CONFIG_CPM_BLOCK_SIZE;
+ }
+
+ if (dowrite && !res) {
+ dp->flags |= DRV_FLG_DIRTY;
+ bg_setstat(handle_cpm_drv_to, 1);
+ }
+
+ if (buserr) {
+ /* Bus timeout. how can this happen? */
+ rc = 0x03;
+ }
+
+ /* send result*/
+ msg_cpm_result(subf, rc, res);
+}
+