Pristine Ack-5.5
[Ack-5.5.git] / mach / m68k2 / dl / dl.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  */
5 static char rcsid[] = "$Id: dl.c,v 2.5 1994/06/24 13:03:47 ceriel Exp $";
6 #define MAXBYTE 24
7 #include <stdio.h>
8 #include <out.h>
9 char hex[] =  "0123456789ABCDEF";
10 FILE *fp, *fopen();
11 char **s;
12 int bytes, bytcnt, checksum;
13 long pc;
14 struct outhead ohead;
15 struct outsect sect[MAXSECT];
16
17
18 main (argc,argv)
19 int argc;
20 char *argv[];
21         {
22         if (argc != 2)   fatal ("usage: %s filename\n",argv[0]);
23         if (! rd_open (*++argv))
24                 fatal ("can't open %s\n",*argv);
25         else    {
26                 s = argv;
27                 convert ();
28                 }
29         }
30
31 convert ()
32         {
33         int i;
34
35         rd_ohead(&ohead);
36         if (ohead.oh_flags & HF_LINK) {
37                 fatal("%s contains unresolved references\n",s);
38         }
39         rd_sect(sect, ohead.oh_nsect);
40         for (i = 0; i < ohead.oh_nsect; i++) {
41                 rd_outsect(i);
42                 pc = sect[i].os_base;
43                 while (sect[i].os_size) {
44                         unsigned int sz = 8096, fl;
45                         extern char *calloc();
46                         register char *buf;
47                         char *pbuf;
48
49                         if (sz > sect[i].os_size) sz = sect[i].os_size;
50                         sect[i].os_size -= sz;
51                         pbuf = buf = calloc(sz, 1);
52                         if (fl = sect[i].os_flen) {
53                                 if (fl > sz) fl = sz;
54                                 sect[i].os_flen -= fl;
55
56                                 rd_emit(buf, (long) fl);
57                         }
58                         while (sz > 0) {
59                                 int p = bytcnt = sz < MAXBYTE ? sz : MAXBYTE;
60                                 checksum = 0;
61                                 sz -= p;
62                                 if (pc > 0xffffL) 
63                                         S2record (buf);
64                                 else S1record (buf);
65                                 buf += p;
66                         }
67                         free(pbuf);
68                 }
69         }
70         printf ("S9030000FC\n");
71         }
72
73
74 S2record (buf)
75         char *buf;
76         {
77         printf ("S2");
78         bytcnt += 4;
79         outbyte (bytcnt);
80         outbyte ((int) (pc >> 16));
81         outbyte ((int) (pc >> 8));
82         outbyte ((int) pc);
83         record (buf);
84         }
85
86 S1record (buf)
87         char *buf;
88         {
89         printf ("S1");
90         bytcnt += 3;
91         outbyte (bytcnt);
92         outbyte ((int) (pc >> 8));
93         outbyte((int) pc);
94         record (buf);
95         }
96
97 record (buf)
98         register char *buf;
99         {
100         while (bytcnt != 0) 
101                 {
102                 outbyte (*buf++);
103                 pc ++;
104                 }
105         outbyte (~checksum);
106         putchar ('\n');
107         putchar (0);
108         putchar (0);
109         }
110
111 outbyte (b)
112 int b;
113         {
114         checksum = (checksum + b) & 0377;
115         putchar (hex[(b>>4) & 017]);
116         putchar  (hex[b & 017]);
117         -- bytcnt;
118         }
119
120 rd_fatal() { fatal("Read error\n"); }
121
122 fatal (s,a)
123         {
124         printf (s,a);
125         exit (-1);
126         }