Pristine Ack-5.5
[Ack-5.5.git] / mach / z80 / int / dl.c
1 /* $Id: dl.c,v 1.5 1994/06/24 13:55:44 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  *
6  */
7
8 #include        <sgtty.h>
9 #include        <stdio.h>
10 #include        <assert.h>
11
12 struct sgttyb   tty;
13
14 #define DATTYPE         0
15 #define EOFTYPE         1
16 #define SEGTYPE         2
17 #define PCTYPE          3
18
19 #define MAXBYTE         32
20
21 int     check;
22 int     echo;
23 int     istty;
24 int     bytecount;
25 int     ttyfd;
26
27 char    *progname;
28
29 char    hex[] = "0123456789ABCDEF";
30
31 main(argc,argv) char **argv; {
32         register nd,pc,sg,osg,first;
33         register char *s;
34         int uid;
35
36         progname = argv[0];
37         if (argc > 3)
38                 fatal("usage: %s [object [tty]]\n",argv[0]);
39         s = "a.out";
40         if (argc >= 2)
41                 s = argv[1];
42         if (freopen(s,"r",stdin) == NULL)
43                 fatal("can't open %s",s);
44         s = "/dev/tty05";
45         if (argc >= 3)
46                 s = argv[2];
47         if ((ttyfd = open(s,2)) < 0)
48                 if ((ttyfd = creat(s,0666)) < 0)
49                         fatal("can't open %s",s);
50         if (gtty(ttyfd,&tty) == 0) {
51                 echo++;
52                 istty++;
53                 tty.sg_ispeed = tty.sg_ospeed = B2400;
54                 tty.sg_flags = RAW;
55                 stty(ttyfd,&tty);
56         } else {
57                 freopen(s,"w",stdout);
58         }
59         first = 1; osg = 0;
60         /* uid = getuid(); */
61         /* lock(1); */
62         for (;;) {
63                 pc = get2c(stdin);
64                 if (feof(stdin))
65                         break;
66                 sg = get2c(stdin);
67                 nd = get2c(stdin);
68                 if (first) {
69                         put('L'); reply();
70                         put('S'); reply();
71                         first = 0;
72                 }
73                 if (sg != osg) {
74                         segment(sg);
75                         osg = sg;
76                 }
77                 while (nd > MAXBYTE) {
78                         data(MAXBYTE,pc);
79                         nd -= MAXBYTE;
80                         pc += MAXBYTE;
81                 }
82                 if (nd > 0)
83                         data(nd,pc);
84                 assert(feof(stdin) == 0);
85         }
86         if (first == 0)
87                 eof();
88 /*      lock(0); */
89 /*      setuid(uid); */
90 /*      if (echo) */
91 /*              for (;;) */
92 /*                      reply(); */
93 }
94
95 segment(sg) {
96
97         newline(2,0,SEGTYPE);
98         word(sg);
99         endline();
100 }
101
102 startad(pc) {
103
104         newline(4,0,PCTYPE);
105         word(0);
106         word(pc);
107         endline();
108 }
109
110 data(nd,pc) {
111
112         newline(nd,pc,DATTYPE);
113         do
114                 byte(getc(stdin));
115         while (--nd);
116         endline();
117 }
118
119 eof() {
120
121         newline(0,0,EOFTYPE);
122         byte(0xFF);
123         put('\n');
124 }
125
126 newline(n,pc,typ) {
127
128         check = 0;
129         bytecount = n+5;
130         put('\n');      /* added instruction */
131         put(':');
132         byte(n);
133         word(pc);
134         byte(typ);
135 }
136
137 endline() {
138
139         byte(-check);
140         assert(bytecount == 0);
141         assert(check == 0);
142 }
143
144 word(w) {
145
146         byte(w>>8);
147         byte(w);
148 }
149
150 byte(b) {
151
152         check += b;
153         --bytecount;
154         put(hex[(b>>4) & 017]);
155         put(hex[b & 017]);
156 }
157
158 put(c) {
159
160         if (istty)
161                 write(ttyfd,&c,1);
162         else
163                 putchar(c);
164 }
165
166 reply() {
167         register i;
168         int c;
169
170         if (echo == 0)
171                 return;
172         i = read(ttyfd,&c,1);
173         assert(i > 0);
174         write(1,&c,1);
175 }
176
177 get2c(f) FILE *f; {
178         register c;
179
180         c = getc(f);
181         return((getc(f) << 8) | c);
182 }
183
184 fatal(s,a) {
185
186         fprintf(stderr,"%s: ",progname);
187         fprintf(stderr,s,a);
188         fprintf(stderr,"\n");
189         exit(-1);
190 }