]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/z180-serv.c
switch fifos conin,conout
[z180-stamp.git] / avr / z180-serv.c
index 9b4228a1b68be76336e8bf8caab7ec4f90905588..7193495d513e4b9f42a743d108e4291b34a21e46 100644 (file)
@@ -111,6 +111,15 @@ void do_msg_echo(uint8_t subf, int len, uint8_t * msg)
        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 MAX_DRIVE      4
@@ -122,11 +131,64 @@ struct cpm_drive_s {
        uint8_t drv;
        uint8_t device;
        char *img_name;
+       bool dirty;
        FIL fd;
 };
 
 static uint8_t disk_buffer[BLOCK_SIZE];
 static struct cpm_drive_s drv_table[MAX_DRIVE];
+static int handle_cpm_drv_to;
+
+#define f_dirty(fp) ((fp)->fs->wflag != 0)
+
+
+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 < MAX_DRIVE; i++) {
+//                             if (&drv_table[i].fd && f_dirty(&drv_table[i].fd)) {
+                               if (drv_table[i].dirty) {
+                                       f_sync(&drv_table[i].fd);
+                                       drv_table[i].dirty = false;
+                                       debug_cpmsd("## %7lu f_sync: %c:\n", get_timer(0), i+'A');
+                               }
+                       }
+                       state = 0;
+               }
+       }
+       return state;
+}
+
+
+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;
+
+       if (rc) {
+               debug_cpmsd("###%7lu  error rc: %.02x, res: %d\n", get_timer(0), rc, res);
+       }
+
+       msg_xmit(2, subf, sizeof(result_msg), result_msg);
+}
 
 /*
        db      2       ; disk command
@@ -140,16 +202,13 @@ void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
 {
 
        FRESULT res = 0;
-       uint8_t rc = 0;
        uint8_t drv;
        char *np;
-       uint8_t result_msg[3];
 
        (void)subf;
 
        if (len != 5) {     /* TODO: check adrv, rdrv */
-               rc = 0x01;
-               goto out;
+               return msg_cpm_result(subf, 0x01, res);
        }
 
        debug_cpmsd("\n## %7lu  login: %c:\n", get_timer(0), msg[0]+'A');
@@ -157,8 +216,7 @@ void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
 
        drv = msg[0];
        if ( drv>= MAX_DRIVE) {
-               rc = 0x02;
-               goto out;
+               return msg_cpm_result(subf, 0x02, res);
        }
 
 /*
@@ -168,6 +226,7 @@ void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
        if (drv_table[drv].img_name != NULL) {
                debug_cpmsd("## %7lu  close: '%s'\n", get_timer(0), drv_table[drv].img_name);
                f_close(&drv_table[drv].fd);
+               drv_table[drv].dirty = false;
                free(drv_table[drv].img_name);
                drv_table[drv].img_name = NULL;
        }
@@ -176,8 +235,7 @@ void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
        disk_buffer[3] = msg[0] + '0';
        if (((np = getenv((char*)disk_buffer)) == NULL) ||
                        ((drv_table[drv].img_name = strdup(np)) == NULL)) {
-               rc = 0x03;
-               goto out;
+               return msg_cpm_result(subf, 0x03, res);
        }
 
 
@@ -187,21 +245,8 @@ void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
     debug_cpmsd("## %7lu   open: '%s', (env: '%s'), res: %d\n", get_timer(0),
                        drv_table[drv].img_name, disk_buffer, res);
 
-out:
-
-       if (res)
-               rc |= 0x80;
-
-       result_msg[0] = rc;
-       result_msg[1] = res;
-       result_msg[2] = res >> 8;
-
-       if (rc) {
-               debug_cpmsd("## %7lu   error    rc: %.02x, res: %d\n", get_timer(0), rc, res);
-       };
-
        /* send  result*/
-       msg_xmit(2, subf, sizeof(result_msg), result_msg);
+       msg_cpm_result(subf, 0x00, res);
 }
 
 
@@ -233,17 +278,14 @@ void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg)
        FRESULT res = 0;
        uint8_t rc = 0;
        bool buserr = 0;
-       uint8_t result_msg[3];
 
        if (len != 10) {     /* TODO: check adrv, rdrv */
-               rc = 0x01;
-               goto out;
+               return msg_cpm_result(subf, 0x01, res);
        }
 
        drv = msg[ADRV];
        if ( drv>= MAX_DRIVE) {
-               rc = 0x02;
-               goto out;
+               return msg_cpm_result(subf, 0x02, res);
        }
 
        secs = msg[CNT];
@@ -314,27 +356,20 @@ void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg)
                addr += BLOCK_SIZE;
        }
 
-       if (dowrite && !res)
-               res = f_sync(&drv_table[drv].fd);
+       if (dowrite && !res) {
+//             res = f_sync(&drv_table[drv].fd);
+               drv_table[drv].dirty = true;
+               bg_setstat(handle_cpm_drv_to, 1);
+       }
+
 
-out:
        if (buserr) {
                debug_cpmsd("Bus timeout\n");
                rc = 0x03;
        }
-       if (res)
-               rc |= 0x80;
-
-       result_msg[0] = rc;
-       result_msg[1] = res;
-       result_msg[2] = res >> 8;
-
-       if (rc) {
-               debug_cpmsd("###%7lu  error rc: %.02x, res: %d\n", get_timer(0), rc, res);
-       }
 
        /* send  result*/
-       msg_xmit(2, subf, sizeof(result_msg), result_msg);
+       msg_cpm_result(subf, rc, res);
 }
 
 
@@ -355,6 +390,9 @@ const FLASH struct msg_item z80_messages[] =
        { 2,
          1, 2,
          do_msg_cpm_rw},
+       { 3,
+         1, 1,
+         do_msg_get_timer},
        { 0xff,                         /* end mark */
          0, 0,
          0},
@@ -453,6 +491,9 @@ int msg_handling(int state)
                pending = (Stat & S_MSG_PENDING) != 0;
                Stat &= ~S_MSG_PENDING;
        }
+/*
+ * TODO: if pending but no message chr --> special condition. ie init,...
+ */
 
        if (pending) {
                switch (state) {
@@ -492,6 +533,7 @@ void setup_z180_serv(void)
 {
 
        handle_msg_handling = bg_register(msg_handling, 0);
+       handle_cpm_drv_to = bg_register(cpm_drv_to, 0);
 }
 
 void restart_z180_serv(void)