Pristine Ack-5.5
[Ack-5.5.git] / mach / i80 / dl / nascom.c
1 /* $Id: nascom.c,v 2.6 1994/06/24 12:58:29 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  * Download Z80 load module into the NASCOM
8  *
9  * Johan Stevenson, Vrije Universiteit, Amsterdam
10  * Adapted (untested) to new ack.out format by
11  * Ceriel Jacobs, Vrije Universiteit, Amsterdam
12  */
13 #include        <stdio.h>
14 #include        <assert.h>
15 #include        <sgtty.h>
16 #include        <signal.h>
17 #include        <out.h>
18
19 int     check;
20 int     nascom = 1;
21 int     nl = '\037';
22 int     zero = 0;
23 int     disp = 0;
24
25 char    hex[] = "0123456789ABCDEF";
26
27 char *progname;
28
29 struct sgttyb   ttynormal;
30 struct sgttyb   ttyraw;
31 int             rawmode = 0;
32
33 struct outhead ohead;
34 struct outsect sect[MAXSECT];
35
36 stop(code) {
37         if (rawmode)
38                 stty(1, &ttynormal);
39         exit(code);
40 }
41
42 main(argc,argv) char **argv; {
43         register unsigned nd;
44         long pc;
45         register char *s;
46         int i;
47
48         progname = argv[0];
49         while (argc > 1 && argv[1][0] == '-') {
50                 switch (argv[1][1]) {
51                 case 'u':
52                         /* unix output */
53                         nascom = 0;
54                         nl = '\n';
55                         break;
56                 case 'e':
57                         /* fill EPROM. make minimal change */
58                         zero = 0xFF;
59                         break;
60                 case 'd':
61                         /* displacement at load time */
62                         disp = atoi(&argv[1][2]);
63                         break;
64                 }
65                 argc--;
66                 argv++;
67         }
68         s = "a.out";
69         if (argc == 2)
70                 s = argv[1];
71         else if (argc != 1) {
72                 fprintf(stderr,"usage: %s [flags] [object file]\n",progname);
73                 stop(-1);
74         }
75         if (! rd_open(s)) {
76                 fprintf(stderr,"%s: can't open %s\n",progname,s);
77                 stop(-1);
78         }
79         if (nascom) {
80                 signal(SIGHUP, SIG_IGN);
81                 signal(SIGINT, SIG_IGN);
82                 signal(SIGQUIT, stop);
83                 signal(SIGTERM, stop);
84                 if (gtty(1, &ttynormal) < 0) {
85                         fprintf(stderr, "no tty\n");
86                         stop(-1);
87                 }
88                 rawmode++;
89                 ttyraw = ttynormal;
90                 ttyraw.sg_flags |= RAW;
91                 ttyraw.sg_ispeed = B1200;
92                 ttyraw.sg_ospeed = B1200;
93                 stty(1, &ttyraw);
94                 sleep(5);
95         }
96         rd_ohead(&ohead);
97         if (ohead.oh_flags & HF_LINK) {
98                 fprintf(stderr,"%s: %s contains unresolved references\n",progname,s);
99                 stop(-1);
100         }
101         rd_sect(sect, ohead.oh_nsect);
102         for (i = 0; i < ohead.oh_nsect; i++) {
103                 rd_outsect(i);
104                 pc = sect[i].os_base;
105                 while (sect[i].os_size) {
106                         unsigned int sz = 8096, fl;
107                         extern char *calloc();
108                         register char *buf;
109                         char *pbuf;
110
111                         if (sz > sect[i].os_size) sz = sect[i].os_size;
112                         sect[i].os_size -= sz;
113                         pbuf = buf = calloc(sz, 1);
114                         if (fl = sect[i].os_flen) {
115                                 if (fl > sz) fl = sz;
116                                 sect[i].os_flen -= fl;
117
118                                 rd_emit(buf, (long) fl);
119                         }
120                         while (sz >= 8) {
121                                 data(8, (int) pc, buf);
122                                 sz -= 8;
123                                 buf += 8;
124                                 pc += 8;
125                         }
126                         if (sz > 0) {
127                                 data(sz, (int) pc, buf);
128                         }
129                         free(pbuf);
130                 }
131         }
132         putchar('.');
133         putchar(nl);
134         if (nascom)
135                 sleep(5);
136         stop(0);
137 }
138
139 data(nd,pc,buf)
140         register char *buf;
141         int pc;
142 {
143         register i;
144
145         check = 0;
146         pc += disp;
147         byte(pc>>8);
148         byte(pc);
149         for (i = 0; i < nd; i++) {
150                 putchar(' ');
151                 byte(*buf++);
152         }
153         while (i < 8) {
154                 putchar(' ');
155                 byte(zero);
156                 i++;
157         }
158         putchar(' ');
159         byte(check);
160         putchar(nl);
161 }
162
163 byte(b) {
164
165         check += b;
166         putchar(hex[(b>>4) & 017]);
167         putchar(hex[b & 017]);
168 }
169
170 rd_fatal()
171 {
172         fprintf(stderr, "%s: Read error\n", progname);
173         stop(-1);
174 }