A large commit.
[pdp8.git] / sw / dumprest / original / dumptd8e.c
diff --git a/sw/dumprest/original/dumptd8e.c b/sw/dumprest/original/dumptd8e.c
new file mode 100644 (file)
index 0000000..632d4e1
--- /dev/null
@@ -0,0 +1,157 @@
+/* This program receives an td8e Dectape image from the serial port from the
+   PDP8 dump program.  It will prompt for the file to receive or use first
+   command line argument. It needs a config file dumprest.cfg or
+   $HOME/.dumprest.cfg with the format defined in config.c
+
+   This program should be running before the PDP8 end is started.
+
+   On the PC ctrl-break will terminate the program
+*/
+#ifdef PC
+#include "encom.h"
+#else
+#include <termios.h>
+#include <unistd.h>
+#include <memory.h>
+#endif
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <time.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
+
+int terminate = 0;
+
+#include "config.c"
+#include "comm.c"
+
+main(argc,argv)
+   int argc;
+   char *argv[];
+{
+   int fd,c,i;
+   FILE *out;
+   char filename[256];
+   char serial_dev[256];
+   long baud;
+   int two_stop;
+   unsigned char buf[200];
+   unsigned short temp;
+   int count,block,byte;
+   int chksum = 0;
+
+   setup_config(&baud,&two_stop,serial_dev);
+
+   if (argc > 1) {
+      strcpy(filename,argv[1]);
+   } else {
+      printf("Enter file name to receive\n");
+      fflush(stdout);
+      scanf("%s",filename);
+   }
+
+#ifdef PC
+   out = fopen(filename,"wb");
+#else
+   out = fopen(filename,"w");
+#endif
+   if (out < 0) {
+      fprintf(stderr,"On file %s ",filename);
+      perror("open failed");
+      exit(1);
+   }
+
+#if 0
+      /* For testing read from file, only works in unix version */
+   fd = open("dat",O_RDONLY,0666);
+   if (fd < 0) {
+      perror("Open failed on dat");
+      exit(1);
+   }
+#else
+   fd = init_comm(serial_dev,baud,two_stop);
+#endif
+   count = -1;
+   block = 0;
+   byte = 0;
+   while(!terminate) {
+      c = ser_read(fd,(char *)buf,sizeof(buf));
+      if (c < 0) {
+         perror("Serial read failed");
+         exit(1);
+      }
+      for (i = 0; i < c; i++) {
+         if (count < 0 ) {
+               /* -2 = waiting for checksum */
+            if (count == -2) {
+               if (byte == 0) {
+                  temp = buf[i];
+                  byte = 1;
+               } else {
+                  fclose(out);
+                  temp = temp | (buf[i] << 8);
+                  if (((temp + chksum) & 0xfff) != 0) {
+                     printf("\nChecksum mismatch %x %x\n",temp,chksum);
+                     exit(1);
+                  }
+                  printf("\nDone, wait for program to exit\n");
+                  exit(0);
+               }
+            } else {
+                  /* -1 = waiting for block flag */
+               if (buf[i] != 0xff && buf[i] != 0xfd) {
+                    /* End of data flag */
+                  if (buf[i] == 0xfe) {
+                     count = -2;
+                     byte = 0;
+                  } else {
+                     printf("\nMissing start of block flag\n");
+                     exit(1);
+                  }
+               } else {
+                  count = 0;
+                  if (buf[i] == 0xfd)
+                     printf("\nblock %d bad\n", block);
+               }
+            }
+         } else {
+              /* Count >= 0 is word count in block */
+              /* Byte is byte in 3 byte sequence for 2 words */
+           if (byte == 0) {
+              temp = buf[i];
+              byte++;
+           } else
+           if (byte == 1) {
+              temp = (temp | (buf[i] << 8)) & 0xfff;
+              fwrite(&temp,2,1,out);
+              chksum = chksum + temp;
+              temp = buf[i] >> 4;
+              byte++;
+              count++;
+           } else
+           if (byte == 2) {
+              temp = (temp | (buf[i] << 4)) & 0xfff;
+              fwrite(&temp,2,1,out);
+              chksum = chksum + temp;
+              byte = 0;
+              count++;
+           }
+              /* If at end of block setup for next */
+           if (count == 129) {
+              count = -1;
+              block++;
+              byte = 0;
+              if (block % 5 == 0) {
+                 printf("Block %d\r",block);
+                 fflush(stdout);
+              }
+           }
+        }
+      }
+   }
+   return 1;
+}