A large commit.
[pdp8.git] / sw / dumprest / original / dumprk05.c
1 /* This program receives an rk05 image from the serial port from the PDP8 dump
2 program. It will prompt for the file to receive or use first command
3 line argument. It needs a config file dumprest.cfg or $HOME/.dumprest.cfg
4 with the format defined in config.c
5
6 This program should be running before the PDP8 end is started.
7
8 On the PC ctrl-break will terminate the program
9 */
10 #ifdef PC
11 #include "encom.h"
12 #else
13 #include <termios.h>
14 #include <unistd.h>
15 #include <memory.h>
16 #endif
17
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <time.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
26
27 int terminate = 0;
28
29 #include "config.c"
30 #include "comm.c"
31
32 main(argc,argv)
33 int argc;
34 char *argv[];
35 {
36 int fd,c,i;
37 FILE *out;
38 char filename[256];
39 char serial_dev[256];
40 long baud;
41 int two_stop;
42 unsigned char buf[200];
43 unsigned short temp;
44 int count,sect,byte;
45 int chksum = 0;
46
47 setup_config(&baud,&two_stop,serial_dev);
48
49 if (argc > 1) {
50 strcpy(filename,argv[1]);
51 } else {
52 printf("Enter file name to receive\n");
53 fflush(stdout);
54 scanf("%s",filename);
55 }
56
57 #ifdef PC
58 out = fopen(filename,"wb");
59 #else
60 out = fopen(filename,"w");
61 #endif
62 if (out < 0) {
63 fprintf(stderr,"On file %s ",filename);
64 perror("open failed");
65 exit(1);
66 }
67
68 #if 0
69 /* For testing read from file, only works in unix version */
70 fd = open("dat",O_RDONLY,0666);
71 if (fd < 0) {
72 perror("Open failed on dat");
73 exit(1);
74 }
75 #else
76 fd = init_comm(serial_dev,baud,two_stop);
77 #endif
78 count = -1;
79 sect = 0;
80 byte = 0;
81 while(!terminate) {
82 c = ser_read(fd,(char *)buf,sizeof(buf));
83 if (c < 0) {
84 perror("Serial read failed");
85 exit(1);
86 }
87 for (i = 0; i < c; i++) {
88 if (count < 0 ) {
89 if (count == -2) {
90 if (byte == 0) {
91 temp = buf[i];
92 byte = 1;
93 } else {
94 fclose(out);
95 temp = temp | (buf[i] << 8);
96 if (((temp + chksum) & 0xfff) != 0) {
97 printf("\nChecksum mismatch %x %x\n",temp,chksum);
98 exit(1);
99 }
100 printf("\nDone\n");
101 exit(0);
102 }
103 } else {
104 if (buf[i] != 0xff && buf[i] != 0xfd) {
105 if (buf[i] == 0xfe) {
106 count = -2;
107 byte = 0;
108 } else {
109 printf("\nMissing start of block flag\n");
110 exit(1);
111 }
112 } else {
113 count = 0;
114 if (buf[i] == 0xfd)
115 printf("\nsector %d, cyl %d side %d sect %d bad\n",
116 sect,sect / 32, (sect & 16) >> 4, sect % 16);
117 }
118 }
119 } else {
120 if (byte == 0) {
121 temp = buf[i];
122 byte++;
123 } else
124 if (byte == 1) {
125 temp = (temp | (buf[i] << 8)) & 0xfff;
126 fwrite(&temp,2,1,out);
127 chksum = chksum + temp;
128 temp = buf[i] >> 4;
129 byte++;
130 } else
131 if (byte == 2) {
132 temp = (temp | (buf[i] << 4)) & 0xfff;
133 fwrite(&temp,2,1,out);
134 chksum = chksum + temp;
135 byte = 0;
136 count = count + 2;
137 if (count == 256) {
138 count = -1;
139 sect++;
140 if (sect % 32 == 0) {
141 printf("Cyl %d\r",sect / 32);
142 fflush(stdout);
143 }
144 }
145 }
146 }
147 }
148 }
149 return 1;
150 }