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