A large commit.
[pdp8.git] / sw / dumprest / original / restrk05.c
CommitLineData
81e70d48
PH
1/* This program sends an rk05 image out the serial port to the PDP8 restore
2 program. It will prompt for the file to send 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 The PDP8 end should be running before this program 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
27int terminate = 0;
28
29#include "config.c"
30#include "comm.c"
31
32main(argc,argv)
33 int argc;
34 char *argv[];
35{
36 int fd;
37 FILE *in;
38 char filename[256];
39 char serial_dev[256];
40 long baud;
41 int two_stop;
42 unsigned char buf[3];
43 unsigned short temp[2];
44 int count,sect;
45 unsigned int chksum = 0;
46 int rc;
47
48 setup_config(&baud,&two_stop,serial_dev);
49
50 if (argc > 1) {
51 strcpy(filename,argv[1]);
52 } else {
53 printf("Enter file name to send\n");
54 fflush(stdout);
55 scanf("%s",filename);
56 }
57
58#ifdef PC
59 in = fopen(filename,"rb");
60#else
61 in = fopen(filename,"r");
62#endif
63 if (in == NULL) {
64 fprintf(stderr,"On file %s ",filename);
65 perror("open failed");
66 exit(1);
67 }
68
69#if 0
70 /* For testing write to file, only works in unix version */
71 fd = open("dat",O_RDWR | O_CREAT | O_TRUNC,0666);
72 if (fd < 0) {
73 perror("Open failed on dat");
74 exit(1);
75 }
76#else
77 fd = init_comm(serial_dev,baud,two_stop);
78#endif
79
80 count = 256;
81 sect = 0;
82 while(!terminate) {
83 if ((rc = fread(temp,2,2,in)) < 2) {
84 /* No more data */
85 if (rc < 0) {
86 perror("\nfile read failed\n");
87 exit(1);
88 }
89 /* Must be at start of block when data done */
90 if (count != 256 || rc != 0) {
91 printf("\nEarly end of file %d, %d\n",count,rc);
92 exit(1);
93 }
94 /* Send end of data flag and checksum */
95 buf[0] = 0xfe;
96 chksum = -chksum;
97 buf[1] = chksum;
98 buf[2] = (chksum & 0xfff) >> 8;
99 ser_write(fd,(char *)buf,3);
100 exit(0);
101 }
102 /* If start of new block send block flag */
103 if (count == 256) {
104 count = 0;
105 buf[0] = 0xff;
106 ser_write(fd,(char *)buf,1);
107 sect++;
108 if (sect % 32 == 0) {
109 printf("Cyl %d\r",sect / 32);
110 fflush(stdout);
111 }
112 }
113 chksum += temp[0] + temp[1];
114 buf[0] = temp[0];
115 buf[1] = (temp[0] >> 8) | (temp[1] << 4);
116 buf[2] = (temp[1] >> 4);
117 ser_write(fd,(char *)buf,3);
118 count += 2;
119 }
120 return 1;
121}