A large commit.
[pdp8.git] / sw / dumprest / original / dumprx01.c
1 /* This program receives an rx01 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 long baud;
40 char serial_dev[256];
41 int two_stop;
42 unsigned char buf[200];
43 int count,scnt,byte;
44 int chksum = 0;
45 unsigned int track;
46 int badflag = 0;
47 int sect_size;
48
49 setup_config(&baud,&two_stop,serial_dev);
50
51 if (argc > 1) {
52 strcpy(filename,argv[1]);
53 } else {
54 printf("Enter file name to receive\n");
55 fflush(stdout);
56 scanf("%s",filename);
57 }
58
59 #ifdef PC
60 out = fopen(filename,"wb");
61 #else
62 out = fopen(filename,"w");
63 #endif
64 if (out < 0) {
65 fprintf(stderr,"On file %s ",filename);
66 perror("open failed");
67 exit(1);
68 }
69
70 #if 0
71 /* For testing read from file, only works in unix version */
72 fd = open("dat",O_RDONLY,0666);
73 if (fd < 0) {
74 perror("Open failed on dat");
75 exit(1);
76 }
77 #else
78 fd = init_comm(serial_dev,baud,two_stop);
79 #endif
80
81 count = -3;
82 scnt = 0;
83 byte = 0;
84 while(!terminate) {
85 c = ser_read(fd,(char *)buf,sizeof(buf));
86 if (c < 0) {
87 perror("Serial read failed");
88 exit(1);
89 }
90 for (i = 0; i < c; i++) {
91 chksum = chksum + buf[i];
92 if (count < 0 ) {
93 /* - Checksum at end */
94 if (count == -3) {
95 switch(buf[i]) {
96 case 022:
97 printf("Single density disk (RX01)\n");
98 sect_size = 128;
99 break;
100 case 044:
101 printf("Double density disk (RX02)\n");
102 sect_size = 256;
103 break;
104 default:
105 printf("Unknow or quad density, not able to dump %o\n",buf[i]);
106 exit(1);
107 break;
108 }
109 count = -1;
110 } else
111 if (count == -2) {
112 fclose(out);
113 if ((chksum & 0xff) != 0) {
114 printf("\nChecksum bad %x\n",chksum & 0xff);
115 exit(1);
116 }
117 printf("\nDone\n");
118 exit(0);
119 } else {
120 /* Start of sector flag */
121 if (buf[i] == 0xff || buf[i] == 0xfd) {
122 count = 0;
123 byte = 0;
124 if (buf[i] == 0xfd)
125 badflag = 1;
126 } else {
127 /* How about checksum flag */
128 if (buf[i] == 0xfe) {
129 count = -2;
130 byte = 0;
131 } else {
132 printf("\nMissing start of block flag\n");
133 exit(1);
134 }
135 }
136 }
137 } else {
138 /* get track */
139 if (byte == 0) {
140 track = buf[i];
141 byte++;
142 } else
143 /* and sector */
144 if (byte == 1) {
145 if (badflag) {
146 printf("\nTrack %d sector %d bad\n", track,buf[i]);
147 badflag = 0;
148 }
149 fseek(out, ((long)track*26 + buf[i] - 1) * sect_size, SEEK_SET);
150 byte++;
151 } else
152 /* Sector data */
153 if (byte == 2) {
154 fwrite(&buf[i],1,1,out);
155 count++;
156 if (count == sect_size) {
157 count = -1;
158 scnt++;
159 if (scnt % 26 == 0) {
160 printf("Track %d\r",scnt / 26);
161 fflush(stdout);
162 }
163 }
164 }
165 }
166 }
167 }
168 return 1;
169 }