ncr53cxxx.c revision 1.2 1 1.2 bouyer /* $NetBSD: ncr53cxxx.c,v 1.2 2000/04/25 16:01:16 bouyer Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1995,1999 Michael L. Hitch
5 1.1 bouyer * All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1 bouyer * modification, are permitted provided that the following conditions
9 1.1 bouyer * are met:
10 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
16 1.1 bouyer * must display the following acknowledgement:
17 1.1 bouyer * This product includes software developed by Michael L. Hitch.
18 1.1 bouyer * 4. The name of the author may not be used to endorse or promote products
19 1.1 bouyer * derived from this software without specific prior written permission
20 1.1 bouyer *
21 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 bouyer */
32 1.1 bouyer
33 1.1 bouyer /* ncr53cxxx.c - SCSI SCRIPTS Assembler */
34 1.1 bouyer
35 1.1 bouyer #include <stdio.h>
36 1.1 bouyer #include <stdlib.h>
37 1.1 bouyer #include <string.h>
38 1.1 bouyer #include <time.h>
39 1.1 bouyer
40 1.1 bouyer #ifndef AMIGA
41 1.1 bouyer #define strcmpi strcasecmp
42 1.1 bouyer #endif
43 1.1 bouyer
44 1.1 bouyer #define MAXTOKENS 16
45 1.1 bouyer #define MAXINST 1024
46 1.1 bouyer #define MAXSYMBOLS 128
47 1.1 bouyer
48 1.1 bouyer struct {
49 1.1 bouyer int type;
50 1.1 bouyer char *name;
51 1.1 bouyer } tokens[MAXTOKENS];
52 1.1 bouyer int ntokens;
53 1.1 bouyer int tokenix;
54 1.1 bouyer
55 1.1 bouyer void f_proc (void);
56 1.1 bouyer void f_pass (void);
57 1.1 bouyer void f_list (void); /* ENTRY, EXTERNAL label list */
58 1.1 bouyer void f_define (void); /* ABSOLUTE, RELATIVE label list */
59 1.1 bouyer void f_move (void);
60 1.1 bouyer void f_jump (void);
61 1.1 bouyer void f_call (void);
62 1.1 bouyer void f_return (void);
63 1.1 bouyer void f_int (void);
64 1.1 bouyer void f_select (void);
65 1.1 bouyer void f_reselect (void);
66 1.1 bouyer void f_wait (void);
67 1.1 bouyer void f_disconnect (void);
68 1.1 bouyer void f_set (void);
69 1.1 bouyer void f_clear (void);
70 1.1 bouyer void f_load (void);
71 1.1 bouyer void f_store (void);
72 1.2 bouyer void f_nop (void);
73 1.1 bouyer void f_arch (void);
74 1.1 bouyer
75 1.1 bouyer struct {
76 1.1 bouyer char *name;
77 1.1 bouyer void (*func)(void);
78 1.1 bouyer } directives[] = {
79 1.1 bouyer {"PROC", f_proc},
80 1.1 bouyer {"PASS", f_pass},
81 1.1 bouyer {"ENTRY", f_list},
82 1.1 bouyer {"ABSOLUTE", f_define},
83 1.1 bouyer {"EXTERN", f_list},
84 1.1 bouyer {"EXTERNAL", f_list},
85 1.1 bouyer {"RELATIVE", f_define},
86 1.1 bouyer {"MOVE", f_move},
87 1.1 bouyer {"JUMP", f_jump},
88 1.1 bouyer {"CALL", f_call},
89 1.1 bouyer {"RETURN", f_return},
90 1.1 bouyer {"INT", f_int},
91 1.1 bouyer {"SELECT", f_select},
92 1.1 bouyer {"RESELECT", f_reselect},
93 1.1 bouyer {"WAIT", f_wait},
94 1.1 bouyer {"DISCONNECT", f_disconnect},
95 1.1 bouyer {"SET", f_set},
96 1.1 bouyer {"CLEAR", f_clear},
97 1.1 bouyer {"LOAD", f_load},
98 1.1 bouyer {"STORE", f_store},
99 1.2 bouyer {"NOP", f_nop},
100 1.1 bouyer {"ARCH", f_arch},
101 1.1 bouyer {NULL, NULL}};
102 1.1 bouyer
103 1.1 bouyer u_int32_t script[MAXINST];
104 1.1 bouyer int dsps;
105 1.1 bouyer char *script_name = "SCRIPT";
106 1.1 bouyer u_int32_t inst0, inst1, inst2;
107 1.1 bouyer unsigned int ninsts;
108 1.1 bouyer unsigned int npatches;
109 1.1 bouyer
110 1.1 bouyer struct patchlist {
111 1.1 bouyer struct patchlist *next;
112 1.1 bouyer unsigned offset;
113 1.1 bouyer };
114 1.1 bouyer
115 1.1 bouyer #define S_LABEL 0x0000
116 1.1 bouyer #define S_ABSOLUTE 0x0001
117 1.1 bouyer #define S_RELATIVE 0x0002
118 1.1 bouyer #define S_EXTERNAL 0x0003
119 1.1 bouyer #define F_DEFINED 0x0001
120 1.1 bouyer #define F_ENTRY 0x0002
121 1.1 bouyer struct {
122 1.1 bouyer short type;
123 1.1 bouyer short flags;
124 1.1 bouyer u_int32_t value;
125 1.1 bouyer struct patchlist *patchlist;
126 1.1 bouyer char *name;
127 1.1 bouyer } symbols[MAXSYMBOLS];
128 1.1 bouyer int nsymbols;
129 1.1 bouyer
130 1.1 bouyer char *stypes[] = {"Label", "Absolute", "Relative", "External"};
131 1.1 bouyer
132 1.1 bouyer char *phases[] = {
133 1.1 bouyer "data_out", "data_in", "cmd", "status",
134 1.1 bouyer "res4", "res5", "msg_out", "msg_in"
135 1.1 bouyer };
136 1.1 bouyer
137 1.1 bouyer struct ncrregs {
138 1.1 bouyer char *name;
139 1.1 bouyer int addr[4];
140 1.1 bouyer };
141 1.1 bouyer #define ARCH710 1
142 1.1 bouyer #define ARCH720 2
143 1.1 bouyer #define ARCH810 3
144 1.1 bouyer #define ARCH825 4
145 1.1 bouyer
146 1.1 bouyer struct ncrregs regs[] = {
147 1.1 bouyer {"scntl0", {0x00, 0x00, 0x00, 0x00}},
148 1.1 bouyer {"scntl1", {0x01, 0x01, 0x01, 0x01}},
149 1.1 bouyer {"scntl2", {-1, 0x02, 0x02, 0x02}},
150 1.1 bouyer {"scntl3", {-1, 0x03, 0x03, 0x03}},
151 1.1 bouyer {"scid", {-1, 0x04, 0x04, 0x04}},
152 1.1 bouyer {"sdid", {0x02, -1, -1, -1}},
153 1.1 bouyer {"sien", {0x03, -1, -1, -1}},
154 1.1 bouyer {"scid", {0x04, -1, -1, -1}},
155 1.1 bouyer {"sxfer", {0x05, 0x05, 0x05, 0x05}},
156 1.1 bouyer {"sdid", {-1, 0x06, 0x06, 0x06}},
157 1.1 bouyer {"gpreg", {-1, 0x07, 0x07, 0x07}},
158 1.1 bouyer {"sodl", {0x06, -1, -1, -1}},
159 1.1 bouyer {"socl", {0x07, -1, -1, -1}},
160 1.1 bouyer {"sfbr", {0x08, 0x08, 0x08, 0x08}},
161 1.1 bouyer {"socl", {-1, 0x09, 0x09, 0x09}},
162 1.1 bouyer {"ssid", {-1, 0x0a, 0x0a, 0x0a}},
163 1.1 bouyer {"sidl", {0x09, -1, -1, -1}},
164 1.1 bouyer {"sbdl", {0x0a, -1, -1, -1}},
165 1.1 bouyer {"sbcl", {0x0b, 0x0b, 0x0b, 0x0b}},
166 1.1 bouyer {"dstat", {0x0c, 0x0c, 0x0c, 0x0c}},
167 1.1 bouyer {"sstat0", {0x0d, 0x0d, 0x0d, 0x0d}},
168 1.1 bouyer {"sstat1", {0x0e, 0x0e, 0x0e, 0x0e}},
169 1.1 bouyer {"sstat2", {0x0f, 0x0f, 0x0f, 0x0f}},
170 1.1 bouyer {"dsa0", {0x10, 0x10, 0x10, 0x10}},
171 1.1 bouyer {"dsa1", {0x11, 0x11, 0x11, 0x11}},
172 1.1 bouyer {"dsa2", {0x12, 0x12, 0x12, 0x12}},
173 1.1 bouyer {"dsa3", {0x13, 0x13, 0x13, 0x13}},
174 1.1 bouyer {"istat", {-1, 0x14, 0x14, 0x14}},
175 1.1 bouyer {"ctest0", {0x14, 0x18, 0x18, 0x18}},
176 1.1 bouyer {"ctest1", {0x15, 0x19, 0x19, 0x19}},
177 1.1 bouyer {"ctest2", {0x16, 0x1a, 0x1a, 0x1a}},
178 1.1 bouyer {"ctest3", {0x17, 0x1b, 0x1b, 0x1b}},
179 1.1 bouyer {"temp0", {-1, 0x1c, 0x1c, 0x1c}},
180 1.1 bouyer {"temp1", {-1, 0x1d, 0x1d, 0x1d}},
181 1.1 bouyer {"temp2", {-1, 0x1e, 0x1e, 0x1e}},
182 1.1 bouyer {"temp3", {-1, 0x1f, 0x1f, 0x1f}},
183 1.1 bouyer {"dfifo", {-1, 0x20, 0x20, 0x20}},
184 1.1 bouyer {"ctest4", {0x18, 0x21, 0x21, 0x21}},
185 1.1 bouyer {"ctest5", {0x19, 0x22, 0x22, 0x22}},
186 1.1 bouyer {"ctest6", {0x20, 0x23, 0x23, 0x23}},
187 1.1 bouyer {"ctest7", {0x21, -1, -1, -1}},
188 1.1 bouyer {"temp0", {0x22, -1, -1, -1}},
189 1.1 bouyer {"temp1", {0x23, -1, -1, -1}},
190 1.1 bouyer {"temp2", {0x24, -1, -1, -1}},
191 1.1 bouyer {"temp3", {0x25, -1, -1, -1}},
192 1.1 bouyer {"dfifo", {0x26, -1, -1, -1}},
193 1.1 bouyer {"istat", {0x27, -1, -1, -1}},
194 1.1 bouyer {"ctest8", {0x28, -1, -1, -1}},
195 1.1 bouyer {"lcrc", {0x29, -1, -1, -1}},
196 1.1 bouyer {"dbc0", {0x2a, 0x24, 0x24, 0x24}},
197 1.1 bouyer {"dbc1", {0x2b, 0x25, 0x25, 0x25}},
198 1.1 bouyer {"dbc2", {0x2c, 0x26, 0x26, 0x26}},
199 1.1 bouyer {"dcmd", {0x2d, 0x27, 0x27, 0x27}},
200 1.1 bouyer {"dnad0", {0x2e, 0x28, 0x28, 0x28}},
201 1.1 bouyer {"dnad1", {0x2f, 0x29, 0x29, 0x29}},
202 1.1 bouyer {"dnad2", {0x30, 0x2a, 0x2a, 0x2a}},
203 1.1 bouyer {"dnad3", {0x31, 0x2b, 0x2b, 0x2b}},
204 1.1 bouyer {"dsp0", {0x32, 0x2c, 0x2c, 0x2c}},
205 1.1 bouyer {"dsp1", {0x33, 0x2d, 0x2d, 0x2d}},
206 1.1 bouyer {"dsp2", {0x34, 0x2e, 0x2e, 0x2e}},
207 1.1 bouyer {"dsp3", {0x35, 0x2f, 0x2f, 0x2f}},
208 1.1 bouyer {"dsps0", {0x36, 0x30, 0x30, 0x30}},
209 1.1 bouyer {"dsps1", {0x37, 0x31, 0x31, 0x31}},
210 1.1 bouyer {"dsps2", {0x38, 0x32, 0x32, 0x32}},
211 1.1 bouyer {"dsps3", {0x39, 0x33, 0x33, 0x33}},
212 1.1 bouyer {"scratch0", {0x40, -1, -1, -1}},
213 1.1 bouyer {"scratch1", {0x41, -1, -1, -1}},
214 1.1 bouyer {"scratch2", {0x42, -1, -1, -1}},
215 1.1 bouyer {"scratch3", {0x43, -1, -1, -1}},
216 1.1 bouyer {"scratcha0", { -1, 0x34, 0x34, 0x34}},
217 1.1 bouyer {"scratcha1", { -1, 0x35, 0x35, 0x35}},
218 1.1 bouyer {"scratcha2", { -1, 0x36, 0x36, 0x36}},
219 1.1 bouyer {"scratcha3", { -1, 0x37, 0x37, 0x37}},
220 1.1 bouyer {"dmode", {0x44, 0x38, 0x38, 0x38}},
221 1.1 bouyer {"dien", {0x45, 0x39, 0x39, 0x39}},
222 1.1 bouyer {"dwt", {0x46, 0x3a, -1, -1}},
223 1.1 bouyer {"sbr", { -1, -1, 0x3a, 0x3a}},
224 1.1 bouyer {"dcntl", {0x47, 0x3b, 0x3b, 0x3b}},
225 1.1 bouyer {"addr0", {0x48, 0x3c, 0x3c, 0x3c}},
226 1.1 bouyer {"addr1", {0x49, 0x3d, 0x3d, 0x3d}},
227 1.1 bouyer {"addr2", {0x4A, 0x3e, 0x3e, 0x3e}},
228 1.1 bouyer {"addr3", {0x4B, 0x3f, 0x3f, 0x3f}},
229 1.1 bouyer {"sien0", { -1, 0x40, 0x40, 0x40}},
230 1.1 bouyer {"sien1", { -1, 0x41, 0x41, 0x41}},
231 1.1 bouyer {"sist0", { -1, 0x42, 0x42, 0x42}},
232 1.1 bouyer {"sist1", { -1, 0x43, 0x43, 0x43}},
233 1.1 bouyer {"slpar", { -1, 0x44, 0x44, 0x44}},
234 1.1 bouyer {"swide", { -1, 0x45, -1, 0x45}},
235 1.1 bouyer {"macntl", { -1, 0x46, 0x46, 0x46}},
236 1.1 bouyer {"gpcntl", { -1, 0x47, 0x47, 0x47}},
237 1.1 bouyer {"stime0", { -1, 0x48, 0x48, 0x48}},
238 1.1 bouyer {"stime1", { -1, 0x49, 0x49, 0x49}},
239 1.1 bouyer {"respid0", { -1, 0x4a, 0x4a, 0x4a}},
240 1.1 bouyer {"respid1", { -1, 0x4b, -1, 0x4b}},
241 1.1 bouyer {"stest0", { -1, 0x4c, 0x4c, 0x4c}},
242 1.1 bouyer {"stest1", { -1, 0x4d, 0x4d, 0x4d}},
243 1.1 bouyer {"stest2", { -1, 0x4e, 0x4e, 0x4e}},
244 1.1 bouyer {"stest3", { -1, 0x4f, 0x4f, 0x4f}},
245 1.1 bouyer {"sidl0", { -1, 0x50, 0x50, 0x50}},
246 1.1 bouyer {"sidl1", { -1, 0x51, -1, 0x51}},
247 1.1 bouyer {"sodl0", { -1, 0x54, 0x54, 0x54}},
248 1.1 bouyer {"sodl1", { -1, 0x55, -1, 0x55}},
249 1.1 bouyer {"sbdl0", { -1, 0x58, 0x58, 0x58}},
250 1.1 bouyer {"sbdl1", { -1, 0x59, -1, 0x59}},
251 1.1 bouyer {"scratchb0", { -1, 0x5c, 0x5c, 0x5c}},
252 1.1 bouyer {"scratchb1", { -1, 0x5d, 0x5d, 0x5d}},
253 1.1 bouyer {"scratchb2", { -1, 0x5e, 0x5e, 0x5e}},
254 1.1 bouyer {"scratchb3", { -1, 0x5f, 0x5f, 0x5f}},
255 1.1 bouyer {"scratchc0", { -1, -1, -1, 0x60}},
256 1.1 bouyer {"scratchc1", { -1, -1, -1, 0x61}},
257 1.1 bouyer {"scratchc2", { -1, -1, -1, 0x62}},
258 1.1 bouyer {"scratchc3", { -1, -1, -1, 0x63}},
259 1.1 bouyer {"scratchd0", { -1, -1, -1, 0x64}},
260 1.1 bouyer {"scratchd1", { -1, -1, -1, 0x65}},
261 1.1 bouyer {"scratchd2", { -1, -1, -1, 0x5e}},
262 1.1 bouyer {"scratchd3", { -1, -1, -1, 0x67}},
263 1.1 bouyer {"scratche0", { -1, -1, -1, 0x68}},
264 1.1 bouyer {"scratche1", { -1, -1, -1, 0x69}},
265 1.1 bouyer {"scratche2", { -1, -1, -1, 0x6a}},
266 1.1 bouyer {"scratche3", { -1, -1, -1, 0x6b}},
267 1.1 bouyer {"scratchf0", { -1, -1, -1, 0x6c}},
268 1.1 bouyer {"scratchf1", { -1, -1, -1, 0x6d}},
269 1.1 bouyer {"scratchf2", { -1, -1, -1, 0x6e}},
270 1.1 bouyer {"scratchf3", { -1, -1, -1, 0x6f}},
271 1.1 bouyer {"scratchg0", { -1, -1, -1, 0x70}},
272 1.1 bouyer {"scratchg1", { -1, -1, -1, 0x71}},
273 1.1 bouyer {"scratchg2", { -1, -1, -1, 0x72}},
274 1.1 bouyer {"scratchg3", { -1, -1, -1, 0x73}},
275 1.1 bouyer {"scratchh0", { -1, -1, -1, 0x74}},
276 1.1 bouyer {"scratchh1", { -1, -1, -1, 0x75}},
277 1.1 bouyer {"scratchh2", { -1, -1, -1, 0x7e}},
278 1.1 bouyer {"scratchh3", { -1, -1, -1, 0x77}},
279 1.1 bouyer {"scratchi0", { -1, -1, -1, 0x78}},
280 1.1 bouyer {"scratchi1", { -1, -1, -1, 0x79}},
281 1.1 bouyer {"scratchi2", { -1, -1, -1, 0x7a}},
282 1.1 bouyer {"scratchi3", { -1, -1, -1, 0x7b}},
283 1.1 bouyer {"scratchj0", { -1, -1, -1, 0x7c}},
284 1.1 bouyer {"scratchj1", { -1, -1, -1, 0x7d}},
285 1.1 bouyer {"scratchj2", { -1, -1, -1, 0x7e}},
286 1.1 bouyer {"scratchj3", { -1, -1, -1, 0x7f}},
287 1.1 bouyer };
288 1.1 bouyer
289 1.1 bouyer int lineno;
290 1.1 bouyer int err_listed;
291 1.1 bouyer int arch;
292 1.1 bouyer int partial_flag;
293 1.1 bouyer
294 1.1 bouyer char inbuf[128];
295 1.1 bouyer
296 1.1 bouyer char *sourcefile;
297 1.1 bouyer char *outputfile;
298 1.1 bouyer char *listfile;
299 1.1 bouyer char *errorfile;
300 1.1 bouyer
301 1.1 bouyer FILE *infp;
302 1.1 bouyer FILE *outfp;
303 1.1 bouyer FILE *listfp;
304 1.1 bouyer FILE *errfp;
305 1.1 bouyer
306 1.1 bouyer void setarch(char *);
307 1.1 bouyer void parse (void);
308 1.1 bouyer void process (void);
309 1.1 bouyer void emit_symbols (void);
310 1.1 bouyer void list_symbols (void);
311 1.1 bouyer void errout (char *);
312 1.1 bouyer void define_symbol (char *, u_int32_t, short, short);
313 1.1 bouyer void close_script (void);
314 1.1 bouyer void new_script (char *);
315 1.1 bouyer void store_inst (void);
316 1.1 bouyer int expression (int *);
317 1.1 bouyer int evaluate (int);
318 1.1 bouyer int number (char *);
319 1.1 bouyer int lookup (char *);
320 1.1 bouyer int reserved (char *, int);
321 1.1 bouyer int CheckPhase (int);
322 1.1 bouyer int CheckRegister (int);
323 1.1 bouyer void transfer (int, int);
324 1.1 bouyer void select_reselect (int);
325 1.1 bouyer void set_clear (u_int32_t);
326 1.1 bouyer void block_move (void);
327 1.1 bouyer void register_write (void);
328 1.1 bouyer void memory_to_memory (void);
329 1.1 bouyer void loadstore (int);
330 1.1 bouyer void error_line(void);
331 1.1 bouyer char *makefn(char *, char *);
332 1.1 bouyer void usage(void);
333 1.1 bouyer
334 1.1 bouyer int
335 1.1 bouyer main (int argc, char *argv[])
336 1.1 bouyer {
337 1.1 bouyer int i;
338 1.1 bouyer
339 1.1 bouyer if (argc < 2 || argv[1][0] == '-')
340 1.1 bouyer usage();
341 1.1 bouyer sourcefile = argv[1];
342 1.1 bouyer infp = fopen (sourcefile, "r");
343 1.1 bouyer if (infp == NULL) {
344 1.1 bouyer perror ("open source");
345 1.1 bouyer fprintf (stderr, "scc: error opening source file %s\n", argv[1]);
346 1.1 bouyer exit (1);
347 1.1 bouyer }
348 1.1 bouyer /*
349 1.1 bouyer * process options
350 1.1 bouyer * -l [listfile]
351 1.1 bouyer * -o [outputfile]
352 1.1 bouyer * -p [outputfile]
353 1.1 bouyer * -z [debugfile]
354 1.1 bouyer * -e [errorfile]
355 1.1 bouyer * -a arch
356 1.1 bouyer * -v
357 1.1 bouyer * -u
358 1.1 bouyer */
359 1.1 bouyer for (i = 2; i < argc; ++i) {
360 1.1 bouyer if (argv[i][0] != '-')
361 1.1 bouyer usage();
362 1.1 bouyer switch (argv[i][1]) {
363 1.1 bouyer case 'o':
364 1.1 bouyer case 'p':
365 1.1 bouyer partial_flag = argv[i][1] == 'p';
366 1.1 bouyer if (i + 1 >= argc || argv[i + 1][0] == '-')
367 1.1 bouyer outputfile = makefn (sourcefile, "out");
368 1.1 bouyer else {
369 1.1 bouyer outputfile = argv[i + 1];
370 1.1 bouyer ++i;
371 1.1 bouyer }
372 1.1 bouyer break;
373 1.1 bouyer case 'l':
374 1.1 bouyer if (i + 1 >= argc || argv[i + 1][0] == '-')
375 1.1 bouyer listfile = makefn (sourcefile, "lis");
376 1.1 bouyer else {
377 1.1 bouyer listfile = argv[i + 1];
378 1.1 bouyer ++i;
379 1.1 bouyer }
380 1.1 bouyer break;
381 1.1 bouyer case 'e':
382 1.1 bouyer if (i + 1 >= argc || argv[i + 1][0] == '-')
383 1.1 bouyer errorfile = makefn (sourcefile, "err");
384 1.1 bouyer else {
385 1.1 bouyer errorfile = argv[i + 1];
386 1.1 bouyer ++i;
387 1.1 bouyer }
388 1.1 bouyer break;
389 1.1 bouyer case 'a':
390 1.1 bouyer if (i + 1 == argc)
391 1.1 bouyer usage();
392 1.1 bouyer setarch(argv[i +1]);
393 1.1 bouyer if (arch == 0) {
394 1.1 bouyer fprintf(stderr,"%s: bad arch '%s'\n",
395 1.1 bouyer argv[0], argv[i +1]);
396 1.1 bouyer exit(1);
397 1.1 bouyer }
398 1.1 bouyer ++i;
399 1.1 bouyer break;
400 1.1 bouyer default:
401 1.1 bouyer fprintf (stderr, "scc: unrecognized option '%c'\n",
402 1.1 bouyer argv[i][1]);
403 1.1 bouyer usage();
404 1.1 bouyer }
405 1.1 bouyer }
406 1.1 bouyer if (outputfile)
407 1.1 bouyer outfp = fopen (outputfile, "w");
408 1.1 bouyer if (listfile)
409 1.1 bouyer listfp = fopen (listfile, "w");
410 1.1 bouyer if (errorfile)
411 1.1 bouyer errfp = fopen (errorfile, "w");
412 1.1 bouyer else
413 1.1 bouyer errfp = stderr;
414 1.1 bouyer
415 1.1 bouyer if (outfp) {
416 1.1 bouyer time_t cur_time;
417 1.1 bouyer
418 1.2 bouyer fprintf(outfp, "/*\t$NetBSD: ncr53cxxx.c,v 1.2 2000/04/25 16:01:16 bouyer Exp $\t*/\n");
419 1.1 bouyer fprintf(outfp, "/*\n");
420 1.1 bouyer fprintf(outfp, " *\tDO NOT EDIT - this file is automatically generated.\n");
421 1.1 bouyer time(&cur_time);
422 1.1 bouyer fprintf(outfp, " *\tcreated from %s on %s", sourcefile, ctime(&cur_time));
423 1.1 bouyer fprintf(outfp, " */\n");
424 1.1 bouyer }
425 1.1 bouyer
426 1.1 bouyer while (fgets (inbuf, sizeof (inbuf), infp)) {
427 1.1 bouyer ++lineno;
428 1.1 bouyer if (listfp)
429 1.1 bouyer fprintf (listfp, "%3d: %s", lineno, inbuf);
430 1.1 bouyer err_listed = 0;
431 1.1 bouyer parse ();
432 1.1 bouyer if (ntokens) {
433 1.1 bouyer #ifdef DUMP_TOKENS
434 1.1 bouyer int i;
435 1.1 bouyer
436 1.1 bouyer fprintf (listfp, " %d tokens\n", ntokens);
437 1.1 bouyer for (i = 0; i < ntokens; ++i) {
438 1.1 bouyer fprintf (listfp, " %d: ", i);
439 1.1 bouyer if (tokens[i].type)
440 1.1 bouyer fprintf (listfp,"'%c'\n", tokens[i].type);
441 1.1 bouyer else
442 1.1 bouyer fprintf (listfp, "%s\n", tokens[i].name);
443 1.1 bouyer }
444 1.1 bouyer #endif
445 1.1 bouyer if (ntokens >= 2 && tokens[0].type == 0 &&
446 1.1 bouyer tokens[1].type == ':') {
447 1.1 bouyer define_symbol (tokens[0].name, dsps, S_LABEL, F_DEFINED);
448 1.1 bouyer tokenix += 2;
449 1.1 bouyer }
450 1.1 bouyer if (tokenix < ntokens)
451 1.1 bouyer process ();
452 1.1 bouyer }
453 1.1 bouyer
454 1.1 bouyer }
455 1.1 bouyer close_script ();
456 1.1 bouyer emit_symbols ();
457 1.1 bouyer if (outfp && !partial_flag) {
458 1.1 bouyer fprintf (outfp, "\nu_int32_t INSTRUCTIONS = 0x%08x;\n", ninsts);
459 1.1 bouyer fprintf (outfp, "u_int32_t PATCHES = 0x%08x;\n", npatches);
460 1.1 bouyer }
461 1.1 bouyer list_symbols ();
462 1.1 bouyer exit(0);
463 1.1 bouyer }
464 1.1 bouyer
465 1.1 bouyer void setarch(char *val)
466 1.1 bouyer {
467 1.1 bouyer switch (atoi(val)) {
468 1.1 bouyer case 710:
469 1.1 bouyer arch = ARCH710;
470 1.1 bouyer break;
471 1.1 bouyer case 720:
472 1.1 bouyer arch = ARCH720;
473 1.1 bouyer break;
474 1.1 bouyer case 810:
475 1.1 bouyer arch = ARCH810;
476 1.1 bouyer break;
477 1.1 bouyer case 825:
478 1.1 bouyer arch = ARCH825;
479 1.1 bouyer break;
480 1.1 bouyer default:
481 1.1 bouyer arch = 0;
482 1.1 bouyer }
483 1.1 bouyer }
484 1.1 bouyer
485 1.1 bouyer void emit_symbols ()
486 1.1 bouyer {
487 1.1 bouyer int i;
488 1.1 bouyer struct patchlist *p;
489 1.1 bouyer
490 1.1 bouyer if (nsymbols == 0 || outfp == NULL)
491 1.1 bouyer return;
492 1.1 bouyer
493 1.1 bouyer for (i = 0; i < nsymbols; ++i) {
494 1.1 bouyer char *code;
495 1.1 bouyer if (symbols[i].type == S_ABSOLUTE)
496 1.1 bouyer code = "A_";
497 1.1 bouyer else if (symbols[i].type == S_RELATIVE)
498 1.1 bouyer code = "R_";
499 1.1 bouyer else if (symbols[i].type == S_EXTERNAL)
500 1.1 bouyer code = "E_";
501 1.1 bouyer else if (symbols[i].flags & F_ENTRY)
502 1.1 bouyer code = "Ent_";
503 1.1 bouyer else
504 1.1 bouyer continue;
505 1.1 bouyer fprintf (outfp, "#define\t%s%s\t0x%08x\n", code, symbols[i].name,
506 1.1 bouyer symbols[i].value);
507 1.1 bouyer if (symbols[i].flags & F_ENTRY || symbols[i].patchlist == NULL)
508 1.1 bouyer continue;
509 1.1 bouyer fprintf (outfp, "u_int32_t %s%s_Used[] = {\n", code, symbols[i].name);
510 1.1 bouyer #if 1
511 1.1 bouyer p = symbols[i].patchlist;
512 1.1 bouyer while (p) {
513 1.1 bouyer fprintf (outfp, "\t0x%08x,\n", p->offset / 4);
514 1.1 bouyer p = p->next;
515 1.1 bouyer }
516 1.1 bouyer #endif
517 1.1 bouyer fprintf (outfp, "};\n\n");
518 1.1 bouyer }
519 1.1 bouyer /* patches ? */
520 1.1 bouyer }
521 1.1 bouyer
522 1.1 bouyer void list_symbols ()
523 1.1 bouyer {
524 1.1 bouyer int i;
525 1.1 bouyer
526 1.1 bouyer if (nsymbols == 0 || listfp == NULL)
527 1.1 bouyer return;
528 1.1 bouyer fprintf (listfp, "\n\nValue Type Symbol\n");
529 1.1 bouyer for (i = 0; i < nsymbols; ++i) {
530 1.1 bouyer fprintf (listfp, "%08x: %-8s %s\n", symbols[i].value,
531 1.1 bouyer stypes[symbols[i].type], symbols[i].name);
532 1.1 bouyer }
533 1.1 bouyer }
534 1.1 bouyer
535 1.1 bouyer void errout (char *text)
536 1.1 bouyer {
537 1.1 bouyer error_line();
538 1.1 bouyer fprintf (errfp, "*** %s ***\n", text);
539 1.1 bouyer }
540 1.1 bouyer
541 1.1 bouyer void parse ()
542 1.1 bouyer {
543 1.1 bouyer char *p = inbuf;
544 1.1 bouyer char c;
545 1.1 bouyer char string[64];
546 1.1 bouyer char *s;
547 1.1 bouyer
548 1.1 bouyer ntokens = tokenix = 0;
549 1.1 bouyer while (1) {
550 1.1 bouyer while ((c = *p++) && c != '\n' && (c <= ' ' || c == '\t'))
551 1.1 bouyer ;
552 1.1 bouyer if (c == '\n' || c == 0 || c == ';')
553 1.1 bouyer break;
554 1.1 bouyer if (ntokens >= MAXTOKENS) {
555 1.1 bouyer errout ("Token table full");
556 1.1 bouyer break;
557 1.1 bouyer }
558 1.1 bouyer if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
559 1.1 bouyer (c >= 'A' && c <= 'Z') || c == '$' || c == '_') {
560 1.1 bouyer s = string;
561 1.1 bouyer *s++ = c;
562 1.1 bouyer while (((c = *p) >= '0' && c <= '9') ||
563 1.1 bouyer (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
564 1.1 bouyer c == '_' || c == '$') {
565 1.1 bouyer *s++ = *p++;
566 1.1 bouyer }
567 1.1 bouyer *s = 0;
568 1.1 bouyer tokens[ntokens].name = malloc (strlen (string) + 1);
569 1.1 bouyer strcpy (tokens[ntokens].name, string);
570 1.1 bouyer tokens[ntokens].type = 0;
571 1.1 bouyer }
572 1.1 bouyer else {
573 1.1 bouyer tokens[ntokens].type = c;
574 1.1 bouyer }
575 1.1 bouyer ++ntokens;
576 1.1 bouyer }
577 1.1 bouyer return;
578 1.1 bouyer }
579 1.1 bouyer
580 1.1 bouyer void process ()
581 1.1 bouyer {
582 1.1 bouyer int i;
583 1.1 bouyer
584 1.1 bouyer if (tokens[tokenix].type) {
585 1.1 bouyer error_line();
586 1.1 bouyer fprintf (errfp, "Error: expected directive, found '%c'\n",
587 1.1 bouyer tokens[tokenix].type);
588 1.1 bouyer return;
589 1.1 bouyer }
590 1.1 bouyer for (i = 0; directives[i].name; ++i) {
591 1.1 bouyer if (strcmpi (directives[i].name, tokens[tokenix].name) == 0)
592 1.1 bouyer break;
593 1.1 bouyer }
594 1.1 bouyer if (directives[i].name == NULL) {
595 1.1 bouyer error_line();
596 1.1 bouyer fprintf (errfp, "Error: expected directive, found \"%s\"\n",
597 1.1 bouyer tokens[tokenix].name);
598 1.1 bouyer return;
599 1.1 bouyer }
600 1.1 bouyer if (directives[i].func == NULL) {
601 1.1 bouyer error_line();
602 1.1 bouyer fprintf (errfp, "No function for directive \"%s\"\n", tokens[tokenix].name);
603 1.1 bouyer } else {
604 1.1 bouyer #if 0
605 1.1 bouyer fprintf (listfp, "Processing directive \"%s\"\n", directives[i].name);
606 1.1 bouyer #endif
607 1.1 bouyer ++tokenix;
608 1.1 bouyer (*directives[i].func) ();
609 1.1 bouyer }
610 1.1 bouyer }
611 1.1 bouyer
612 1.1 bouyer void define_symbol (char *name, u_int32_t value, short type, short flags)
613 1.1 bouyer {
614 1.1 bouyer int i;
615 1.1 bouyer struct patchlist *p;
616 1.1 bouyer
617 1.1 bouyer for (i = 0; i < nsymbols; ++i) {
618 1.1 bouyer if (symbols[i].type == type && strcmp (symbols[i].name, name) == 0) {
619 1.1 bouyer if (symbols[i].flags & F_DEFINED) {
620 1.1 bouyer error_line();
621 1.1 bouyer fprintf (errfp, "*** Symbol \"%s\" multiply defined\n",
622 1.1 bouyer name);
623 1.1 bouyer } else {
624 1.1 bouyer symbols[i].flags |= flags;
625 1.1 bouyer symbols[i].value = value;
626 1.1 bouyer p = symbols[i].patchlist;
627 1.1 bouyer while (p) {
628 1.1 bouyer if (p->offset > dsps)
629 1.1 bouyer errout ("Whoops\007");
630 1.1 bouyer else
631 1.1 bouyer script[p->offset / 4] = dsps - p->offset - 4;
632 1.1 bouyer p = p->next;
633 1.1 bouyer }
634 1.1 bouyer }
635 1.1 bouyer return;
636 1.1 bouyer }
637 1.1 bouyer }
638 1.1 bouyer if (nsymbols >= MAXSYMBOLS) {
639 1.1 bouyer errout ("Symbol table full");
640 1.1 bouyer return;
641 1.1 bouyer }
642 1.1 bouyer symbols[nsymbols].type = type;
643 1.1 bouyer symbols[nsymbols].flags = flags;
644 1.1 bouyer symbols[nsymbols].value = value;
645 1.1 bouyer symbols[nsymbols].patchlist = NULL;
646 1.1 bouyer symbols[nsymbols].name = malloc (strlen (name) + 1);
647 1.1 bouyer strcpy (symbols[nsymbols].name, name);
648 1.1 bouyer ++nsymbols;
649 1.1 bouyer }
650 1.1 bouyer
651 1.1 bouyer void close_script ()
652 1.1 bouyer {
653 1.1 bouyer int i;
654 1.1 bouyer
655 1.1 bouyer if (dsps == 0)
656 1.1 bouyer return;
657 1.1 bouyer if (outfp) {
658 1.1 bouyer fprintf (outfp, "const u_int32_t %s[] = {\n", script_name);
659 1.1 bouyer for (i = 0; i < dsps / 4; i += 2) {
660 1.1 bouyer fprintf (outfp, "\t0x%08x, 0x%08x", script[i],
661 1.1 bouyer script[i + 1]);
662 1.1 bouyer /* check for memory move instruction */
663 1.1 bouyer if ((script[i] & 0xe0000000) == 0xc0000000)
664 1.1 bouyer fprintf (outfp, ", 0x%08x,", script[i + 2]);
665 1.1 bouyer else
666 1.1 bouyer if ((i + 2) <= dsps / 4) fprintf (outfp, ",\t\t");
667 1.1 bouyer fprintf (outfp, "\t/* %03x - %3d */\n", i * 4, i * 4);
668 1.1 bouyer if ((script[i] & 0xe0000000) == 0xc0000000)
669 1.1 bouyer ++i;
670 1.1 bouyer }
671 1.1 bouyer fprintf (outfp, "};\n\n");
672 1.1 bouyer }
673 1.1 bouyer dsps = 0;
674 1.1 bouyer }
675 1.1 bouyer
676 1.1 bouyer void new_script (char *name)
677 1.1 bouyer {
678 1.1 bouyer close_script ();
679 1.1 bouyer script_name = malloc (strlen (name) + 1);
680 1.1 bouyer strcpy (script_name, name);
681 1.1 bouyer }
682 1.1 bouyer
683 1.1 bouyer int reserved (char *string, int t)
684 1.1 bouyer {
685 1.1 bouyer if (tokens[t].type == 0 && strcmpi (tokens[t].name, string) == 0)
686 1.1 bouyer return (1);
687 1.1 bouyer return (0);
688 1.1 bouyer }
689 1.1 bouyer
690 1.1 bouyer int CheckPhase (int t)
691 1.1 bouyer {
692 1.1 bouyer int i;
693 1.1 bouyer
694 1.1 bouyer for (i = 0; i < 8; ++i) {
695 1.1 bouyer if (reserved (phases[i], t)) {
696 1.1 bouyer inst0 |= i << 24;
697 1.1 bouyer return (1);
698 1.1 bouyer }
699 1.1 bouyer }
700 1.1 bouyer return (0);
701 1.1 bouyer }
702 1.1 bouyer
703 1.1 bouyer int CheckRegister (int t)
704 1.1 bouyer {
705 1.1 bouyer int i;
706 1.1 bouyer
707 1.1 bouyer if (arch <= 0) {
708 1.1 bouyer errout("'ARCH' statement missing");
709 1.1 bouyer return -1;
710 1.1 bouyer }
711 1.1 bouyer for (i = 0; i < (sizeof(regs) / sizeof(regs[0])); i++) {
712 1.1 bouyer if (regs[i].addr[arch - 1] >= 0 && reserved(regs[i].name, t))
713 1.1 bouyer return regs[i].addr[arch-1];
714 1.1 bouyer }
715 1.1 bouyer return (-1);
716 1.1 bouyer }
717 1.1 bouyer
718 1.1 bouyer int expression (int *t)
719 1.1 bouyer {
720 1.1 bouyer int value;
721 1.1 bouyer int i = *t;
722 1.1 bouyer
723 1.1 bouyer value = evaluate (i++);
724 1.1 bouyer while (i < ntokens) {
725 1.1 bouyer if (tokens[i].type == '+')
726 1.1 bouyer value += evaluate (i + 1);
727 1.1 bouyer else if (tokens[i].type == '-')
728 1.1 bouyer value -= evaluate (i + 1);
729 1.1 bouyer else
730 1.1 bouyer errout ("Unknown identifier");
731 1.1 bouyer i += 2;
732 1.1 bouyer }
733 1.1 bouyer *t = i;
734 1.1 bouyer return (value);
735 1.1 bouyer }
736 1.1 bouyer
737 1.1 bouyer int evaluate (t)
738 1.1 bouyer {
739 1.1 bouyer int value;
740 1.1 bouyer char *name;
741 1.1 bouyer
742 1.1 bouyer if (tokens[t].type) {
743 1.1 bouyer errout ("Expected an identifier");
744 1.1 bouyer return (0);
745 1.1 bouyer }
746 1.1 bouyer name = tokens[t].name;
747 1.1 bouyer if (*name >= '0' && *name <= '9')
748 1.1 bouyer value = number (name);
749 1.1 bouyer else
750 1.1 bouyer value = lookup (name);
751 1.1 bouyer return (value);
752 1.1 bouyer }
753 1.1 bouyer
754 1.1 bouyer int number (char *s)
755 1.1 bouyer {
756 1.1 bouyer int value;
757 1.1 bouyer int n;
758 1.1 bouyer int radix;
759 1.1 bouyer
760 1.1 bouyer radix = 10;
761 1.1 bouyer if (*s == '0') {
762 1.1 bouyer ++s;
763 1.1 bouyer radix = 8;
764 1.1 bouyer switch (*s) {
765 1.1 bouyer case 'x':
766 1.1 bouyer case 'X':
767 1.1 bouyer radix = 16;
768 1.1 bouyer break;
769 1.1 bouyer case 'b':
770 1.1 bouyer case 'B':
771 1.1 bouyer radix = 2;
772 1.1 bouyer }
773 1.1 bouyer if (radix != 8)
774 1.1 bouyer ++s;
775 1.1 bouyer }
776 1.1 bouyer value = 0;
777 1.1 bouyer while (*s) {
778 1.1 bouyer n = *s++;
779 1.1 bouyer if (n >= '0' && n <= '9')
780 1.1 bouyer n -= '0';
781 1.1 bouyer else if (n >= 'a' && n <= 'f')
782 1.1 bouyer n -= 'a' - 10;
783 1.1 bouyer else if (n >= 'A' && n <= 'F')
784 1.1 bouyer n -= 'A' - 10;
785 1.1 bouyer else {
786 1.1 bouyer error_line();
787 1.1 bouyer fprintf (errfp, "*** Expected digit\n");
788 1.1 bouyer n = 0;
789 1.1 bouyer }
790 1.1 bouyer if (n >= radix)
791 1.1 bouyer errout ("Expected digit");
792 1.1 bouyer else
793 1.1 bouyer value = value * radix + n;
794 1.1 bouyer }
795 1.1 bouyer return (value);
796 1.1 bouyer }
797 1.1 bouyer
798 1.1 bouyer int lookup (char *name)
799 1.1 bouyer {
800 1.1 bouyer int i;
801 1.1 bouyer struct patchlist *p;
802 1.1 bouyer
803 1.1 bouyer for (i = 0; i < nsymbols; ++i) {
804 1.1 bouyer if (strcmp (name, symbols[i].name) == 0) {
805 1.1 bouyer if ((symbols[i].flags & F_DEFINED) == 0) {
806 1.1 bouyer p = (struct patchlist *) &symbols[i].patchlist;
807 1.1 bouyer while (p->next)
808 1.1 bouyer p = p->next;
809 1.1 bouyer p->next = (struct patchlist *) malloc (sizeof (struct patchlist));
810 1.1 bouyer p = p->next;
811 1.1 bouyer p->next = NULL;
812 1.1 bouyer p->offset = dsps + 4;
813 1.1 bouyer }
814 1.1 bouyer return ((int) symbols[i].value);
815 1.1 bouyer }
816 1.1 bouyer }
817 1.1 bouyer if (nsymbols >= MAXSYMBOLS) {
818 1.1 bouyer errout ("Symbol table full");
819 1.1 bouyer return (0);
820 1.1 bouyer }
821 1.1 bouyer symbols[nsymbols].type = S_LABEL; /* assume forward reference */
822 1.1 bouyer symbols[nsymbols].flags = 0;
823 1.1 bouyer symbols[nsymbols].value = 0;
824 1.1 bouyer p = (struct patchlist *) malloc (sizeof (struct patchlist));
825 1.1 bouyer symbols[nsymbols].patchlist = p;
826 1.1 bouyer p->next = NULL;
827 1.1 bouyer p->offset = dsps + 4;
828 1.1 bouyer symbols[nsymbols].name = malloc (strlen (name) + 1);
829 1.1 bouyer strcpy (symbols[nsymbols].name, name);
830 1.1 bouyer ++nsymbols;
831 1.1 bouyer return (0);
832 1.1 bouyer }
833 1.1 bouyer
834 1.1 bouyer void f_arch (void)
835 1.1 bouyer {
836 1.1 bouyer int i, archsave;
837 1.1 bouyer
838 1.1 bouyer i = tokenix;
839 1.1 bouyer
840 1.1 bouyer archsave = arch;
841 1.1 bouyer setarch(tokens[i].name);
842 1.1 bouyer if( arch == 0) {
843 1.1 bouyer errout("Unrecognized ARCH");
844 1.1 bouyer arch = archsave;
845 1.1 bouyer }
846 1.1 bouyer }
847 1.1 bouyer
848 1.1 bouyer void f_proc (void)
849 1.1 bouyer {
850 1.1 bouyer if (tokens[tokenix].type != 0 || tokens[tokenix + 1].type != ':')
851 1.1 bouyer errout ("Invalid PROC statement");
852 1.1 bouyer else
853 1.1 bouyer new_script (tokens[tokenix].name);
854 1.1 bouyer }
855 1.1 bouyer
856 1.1 bouyer void f_pass (void)
857 1.1 bouyer {
858 1.1 bouyer errout ("PASS option not implemented");
859 1.1 bouyer }
860 1.1 bouyer
861 1.1 bouyer /*
862 1.1 bouyer * f_list: process list of symbols for the ENTRY and EXTERNAL directive
863 1.1 bouyer */
864 1.1 bouyer
865 1.1 bouyer void f_list (void)
866 1.1 bouyer {
867 1.1 bouyer int i;
868 1.1 bouyer short type;
869 1.1 bouyer short flags;
870 1.1 bouyer
871 1.1 bouyer type = strcmpi (tokens[tokenix-1].name, "ENTRY") ? S_EXTERNAL : S_LABEL;
872 1.1 bouyer flags = type == S_LABEL ? F_ENTRY : 0;
873 1.1 bouyer for (i = tokenix; i < ntokens; ++i) {
874 1.1 bouyer if (tokens[i].type != 0) {
875 1.1 bouyer errout ("Expected an identifier");
876 1.1 bouyer return;
877 1.1 bouyer }
878 1.1 bouyer define_symbol (tokens[i].name, 0, type, flags);
879 1.1 bouyer if (i + 1 < ntokens) {
880 1.1 bouyer if (tokens[++i].type == ',')
881 1.1 bouyer continue;
882 1.1 bouyer errout ("Expected a separator");
883 1.1 bouyer return;
884 1.1 bouyer }
885 1.1 bouyer }
886 1.1 bouyer }
887 1.1 bouyer
888 1.1 bouyer /*
889 1.1 bouyer * f_define: process list of definitions for ABSOLUTE and RELATIVE directive
890 1.1 bouyer */
891 1.1 bouyer
892 1.1 bouyer void f_define (void)
893 1.1 bouyer {
894 1.1 bouyer int i;
895 1.1 bouyer char *name;
896 1.1 bouyer u_int32_t value;
897 1.1 bouyer int type;
898 1.1 bouyer
899 1.1 bouyer type = strcmpi (tokens[tokenix-1].name, "ABSOLUTE") ? S_RELATIVE : S_ABSOLUTE;
900 1.1 bouyer i = tokenix;
901 1.1 bouyer while (i < ntokens) {
902 1.1 bouyer if (tokens[i].type) {
903 1.1 bouyer errout ("Expected an identifier");
904 1.1 bouyer return;
905 1.1 bouyer }
906 1.1 bouyer if (tokens[i + 1].type != '=') {
907 1.1 bouyer errout ("Expected a separator");
908 1.1 bouyer return;
909 1.1 bouyer }
910 1.1 bouyer name = tokens[i].name;
911 1.1 bouyer i += 2;
912 1.1 bouyer value = expression (&i);
913 1.1 bouyer define_symbol (name, value, type, F_DEFINED);
914 1.1 bouyer }
915 1.1 bouyer }
916 1.1 bouyer
917 1.1 bouyer void store_inst ()
918 1.1 bouyer {
919 1.1 bouyer int i = dsps / 4;
920 1.1 bouyer int l = 8;
921 1.1 bouyer
922 1.1 bouyer if ((inst0 & 0xe0000000) == 0xc0000000)
923 1.1 bouyer l = 12; /* Memory to memory move is 12 bytes */
924 1.1 bouyer if ((dsps + l) / 4 > MAXINST) {
925 1.1 bouyer errout ("Instruction table overflow");
926 1.1 bouyer return;
927 1.1 bouyer }
928 1.1 bouyer script[i++] = inst0;
929 1.1 bouyer script[i++] = inst1;
930 1.1 bouyer if (l == 12)
931 1.1 bouyer script[i++] = inst2;
932 1.1 bouyer if (listfp) {
933 1.1 bouyer fprintf (listfp, "\t%04x: %08x %08x", dsps, inst0, inst1);
934 1.1 bouyer if (l == 12)
935 1.1 bouyer fprintf (listfp, " %08x", inst2);
936 1.1 bouyer fprintf (listfp, "\n");
937 1.1 bouyer }
938 1.1 bouyer dsps += l;
939 1.1 bouyer inst0 = inst1 = inst2 = 0;
940 1.1 bouyer ++ninsts;
941 1.1 bouyer }
942 1.1 bouyer
943 1.1 bouyer void f_move (void)
944 1.1 bouyer {
945 1.1 bouyer if (reserved ("memory", tokenix))
946 1.1 bouyer memory_to_memory ();
947 1.1 bouyer else if (reserved ("from", tokenix) || tokens[tokenix+1].type == ',')
948 1.1 bouyer block_move ();
949 1.1 bouyer else
950 1.1 bouyer register_write ();
951 1.1 bouyer store_inst ();
952 1.1 bouyer }
953 1.1 bouyer
954 1.1 bouyer void f_jump (void)
955 1.1 bouyer {
956 1.1 bouyer transfer (0x80000000, 0);
957 1.1 bouyer }
958 1.1 bouyer
959 1.1 bouyer void f_call (void)
960 1.1 bouyer {
961 1.1 bouyer transfer (0x88000000, 0);
962 1.1 bouyer }
963 1.1 bouyer
964 1.1 bouyer void f_return (void)
965 1.1 bouyer {
966 1.1 bouyer transfer (0x90000000, 1);
967 1.1 bouyer }
968 1.1 bouyer
969 1.1 bouyer void f_int (void)
970 1.1 bouyer {
971 1.1 bouyer transfer (0x98000000, 2);
972 1.1 bouyer }
973 1.1 bouyer
974 1.1 bouyer void f_select (void)
975 1.1 bouyer {
976 1.1 bouyer int t = tokenix;
977 1.1 bouyer
978 1.1 bouyer if (reserved ("atn", t)) {
979 1.1 bouyer inst0 = 0x01000000;
980 1.1 bouyer ++t;
981 1.1 bouyer }
982 1.1 bouyer select_reselect (t);
983 1.1 bouyer }
984 1.1 bouyer
985 1.1 bouyer void f_reselect (void)
986 1.1 bouyer {
987 1.1 bouyer select_reselect (tokenix);
988 1.1 bouyer }
989 1.1 bouyer
990 1.1 bouyer void f_wait (void)
991 1.1 bouyer {
992 1.1 bouyer int i = tokenix;
993 1.1 bouyer
994 1.1 bouyer inst1 = 0;
995 1.1 bouyer if (reserved ("disconnect", i)) {
996 1.1 bouyer inst0 = 0x48000000;
997 1.1 bouyer }
998 1.1 bouyer else {
999 1.1 bouyer if (reserved ("reselect", i))
1000 1.1 bouyer inst0 = 0x50000000;
1001 1.1 bouyer else if (reserved ("select", i))
1002 1.1 bouyer inst0 = 0x50000000;
1003 1.1 bouyer else
1004 1.1 bouyer errout ("Expected SELECT or RESELECT");
1005 1.1 bouyer ++i;
1006 1.1 bouyer if (reserved ("rel", i)) {
1007 1.1 bouyer i += 2;
1008 1.1 bouyer inst1 = evaluate (i) - dsps - 8;
1009 1.1 bouyer inst0 |= 0x04000000;
1010 1.1 bouyer }
1011 1.1 bouyer else
1012 1.1 bouyer inst1 = evaluate (i);
1013 1.1 bouyer }
1014 1.1 bouyer store_inst ();
1015 1.1 bouyer }
1016 1.1 bouyer
1017 1.1 bouyer void f_disconnect (void)
1018 1.1 bouyer {
1019 1.1 bouyer inst0 = 0x48000000;
1020 1.1 bouyer store_inst ();
1021 1.1 bouyer }
1022 1.1 bouyer
1023 1.1 bouyer void f_set (void)
1024 1.1 bouyer {
1025 1.1 bouyer set_clear (0x58000000);
1026 1.1 bouyer }
1027 1.1 bouyer
1028 1.1 bouyer void f_clear (void)
1029 1.1 bouyer {
1030 1.1 bouyer set_clear (0x60000000);
1031 1.1 bouyer }
1032 1.1 bouyer
1033 1.1 bouyer void f_load (void)
1034 1.1 bouyer {
1035 1.1 bouyer inst0 = 0xe1000000;
1036 1.1 bouyer if (arch < ARCH810) {
1037 1.1 bouyer errout ("Wrong arch for load/store");
1038 1.1 bouyer return;
1039 1.1 bouyer }
1040 1.1 bouyer loadstore(tokenix);
1041 1.1 bouyer }
1042 1.1 bouyer
1043 1.1 bouyer void f_store (void)
1044 1.1 bouyer {
1045 1.1 bouyer int i;
1046 1.1 bouyer inst0 = 0xe0000000;
1047 1.1 bouyer if (arch < ARCH810) {
1048 1.1 bouyer errout ("Wrong arch for load/store");
1049 1.1 bouyer return;
1050 1.1 bouyer }
1051 1.1 bouyer i = tokenix;
1052 1.1 bouyer if (reserved("noflush", i)) {
1053 1.1 bouyer inst0 |= 0x2000000;
1054 1.1 bouyer i++;
1055 1.1 bouyer }
1056 1.1 bouyer loadstore(i);
1057 1.1 bouyer }
1058 1.1 bouyer
1059 1.2 bouyer void f_nop (void)
1060 1.2 bouyer {
1061 1.2 bouyer inst0 = 0x10000000;
1062 1.2 bouyer inst1 = 0x00000000;
1063 1.2 bouyer store_inst ();
1064 1.2 bouyer }
1065 1.2 bouyer
1066 1.1 bouyer void loadstore(int i)
1067 1.1 bouyer {
1068 1.1 bouyer int reg, size;
1069 1.1 bouyer
1070 1.1 bouyer reg = CheckRegister(i);
1071 1.1 bouyer if (reg < 0)
1072 1.1 bouyer errout ("Expected register");
1073 1.1 bouyer else
1074 1.1 bouyer inst0 |= reg << 16;
1075 1.1 bouyer if (reg == 8)
1076 1.1 bouyer errout ("Register can't be SFBR");
1077 1.1 bouyer i++;
1078 1.1 bouyer if (tokens[i].type == ',')
1079 1.1 bouyer i++;
1080 1.1 bouyer else
1081 1.1 bouyer errout ("expected ','");
1082 1.1 bouyer size = evaluate(i);
1083 1.1 bouyer if (i < 1 || i > 4)
1084 1.1 bouyer errout("wrong size");
1085 1.1 bouyer if ((reg & 0x3) + size > 4)
1086 1.1 bouyer errout("size too big for register");
1087 1.1 bouyer inst0 |= size;
1088 1.1 bouyer i++;
1089 1.1 bouyer if (tokens[i].type == ',')
1090 1.1 bouyer i++;
1091 1.1 bouyer else
1092 1.1 bouyer errout ("expected ','");
1093 1.1 bouyer if (reserved("from", i) || reserved("dsarel", i)) {
1094 1.1 bouyer i++;
1095 1.1 bouyer inst0 |= 0x10000000;
1096 1.1 bouyer }
1097 1.1 bouyer inst1 = evaluate(i);
1098 1.1 bouyer store_inst ();
1099 1.1 bouyer }
1100 1.1 bouyer
1101 1.1 bouyer void transfer (int word0, int type)
1102 1.1 bouyer {
1103 1.1 bouyer int i;
1104 1.1 bouyer
1105 1.1 bouyer i = tokenix;
1106 1.1 bouyer inst0 = word0;
1107 1.1 bouyer if (type == 0 && reserved ("rel", i)) {
1108 1.1 bouyer inst1 = evaluate (i + 2) - dsps - 8;
1109 1.1 bouyer i += 3;
1110 1.1 bouyer inst0 |= 0x00800000;
1111 1.1 bouyer }
1112 1.1 bouyer else if (type != 1) {
1113 1.1 bouyer inst1 = evaluate (i);
1114 1.1 bouyer }
1115 1.1 bouyer ++i;
1116 1.1 bouyer if (i >= ntokens) {
1117 1.1 bouyer inst0 |= 0x00080000;
1118 1.1 bouyer store_inst ();
1119 1.1 bouyer return;
1120 1.1 bouyer }
1121 1.1 bouyer if (tokens[i].type != ',')
1122 1.1 bouyer errout ("Expected a separator, ',' assumed");
1123 1.1 bouyer else
1124 1.1 bouyer ++i;
1125 1.1 bouyer if (reserved("when", i))
1126 1.1 bouyer inst0 |= 0x00010000;
1127 1.1 bouyer else if (reserved ("if", i) == 0) {
1128 1.1 bouyer errout ("Expected a reserved word");
1129 1.1 bouyer store_inst ();
1130 1.1 bouyer return;
1131 1.1 bouyer }
1132 1.1 bouyer if (reserved ("not", ++i))
1133 1.1 bouyer ++i;
1134 1.1 bouyer else
1135 1.1 bouyer inst0 |= 0x00080000;
1136 1.1 bouyer if (reserved ("atn", i)) {
1137 1.1 bouyer inst0 |= 0x00020000;
1138 1.1 bouyer ++i;
1139 1.1 bouyer } else if (CheckPhase (i)) {
1140 1.1 bouyer inst0 |= 0x00020000;
1141 1.1 bouyer ++i;
1142 1.1 bouyer }
1143 1.1 bouyer if (i < ntokens && tokens[i].type != ',') {
1144 1.1 bouyer if (inst0 & 0x00020000) {
1145 1.1 bouyer if (inst0 & 0x00080000 && reserved ("and", i)) {
1146 1.1 bouyer ++i;
1147 1.1 bouyer }
1148 1.1 bouyer else if ((inst0 & 0x00080000) == 0 && reserved ("or", i)) {
1149 1.1 bouyer ++i;
1150 1.1 bouyer }
1151 1.1 bouyer else
1152 1.1 bouyer errout ("Expected a reserved word");
1153 1.1 bouyer }
1154 1.1 bouyer inst0 |= 0x00040000 + (evaluate (i++) & 0xff);
1155 1.1 bouyer }
1156 1.1 bouyer if (i < ntokens) {
1157 1.1 bouyer if (tokens[i].type == ',')
1158 1.1 bouyer ++i;
1159 1.1 bouyer else
1160 1.1 bouyer errout ("Expected a separator, ',' assumed");
1161 1.1 bouyer if (reserved ("and", i) && reserved ("mask", i + 1))
1162 1.1 bouyer inst0 |= ((evaluate (i + 2) & 0xff) << 8);
1163 1.1 bouyer else
1164 1.1 bouyer errout ("Expected , AND MASK");
1165 1.1 bouyer }
1166 1.1 bouyer store_inst ();
1167 1.1 bouyer }
1168 1.1 bouyer
1169 1.1 bouyer void select_reselect (int t)
1170 1.1 bouyer {
1171 1.1 bouyer inst0 |= 0x40000000; /* ATN may be set from SELECT */
1172 1.1 bouyer if (reserved ("from", t)) {
1173 1.1 bouyer ++t;
1174 1.1 bouyer inst0 |= 0x02000000 | evaluate (t++);
1175 1.1 bouyer }
1176 1.1 bouyer else
1177 1.1 bouyer inst0 |= (evaluate (t++) & 0xff) << 16;
1178 1.1 bouyer if (tokens[t++].type == ',') {
1179 1.1 bouyer if (reserved ("rel", t)) {
1180 1.1 bouyer inst0 |= 0x04000000;
1181 1.1 bouyer inst1 = evaluate (t + 2) - dsps - 8;
1182 1.1 bouyer }
1183 1.1 bouyer else
1184 1.1 bouyer inst1 = evaluate (t);
1185 1.1 bouyer }
1186 1.1 bouyer else
1187 1.1 bouyer errout ("Expected separator");
1188 1.1 bouyer store_inst ();
1189 1.1 bouyer }
1190 1.1 bouyer
1191 1.1 bouyer void set_clear (u_int32_t code)
1192 1.1 bouyer {
1193 1.1 bouyer int i = tokenix;
1194 1.1 bouyer short need_and = 0;
1195 1.1 bouyer
1196 1.1 bouyer inst0 = code;
1197 1.1 bouyer while (i < ntokens) {
1198 1.1 bouyer if (need_and) {
1199 1.1 bouyer if (reserved ("and", i))
1200 1.1 bouyer ++i;
1201 1.1 bouyer else
1202 1.1 bouyer errout ("Expected AND");
1203 1.1 bouyer }
1204 1.1 bouyer if (reserved ("atn", i)) {
1205 1.1 bouyer inst0 |= 0x0008;
1206 1.1 bouyer ++i;
1207 1.1 bouyer }
1208 1.1 bouyer else if (reserved ("ack", i)) {
1209 1.1 bouyer inst0 |= 0x0040;
1210 1.1 bouyer ++i;
1211 1.1 bouyer }
1212 1.1 bouyer else if (reserved ("target", i)) {
1213 1.1 bouyer inst0 |= 0x0200;
1214 1.1 bouyer ++i;
1215 1.1 bouyer }
1216 1.1 bouyer else
1217 1.1 bouyer errout ("Expected ATN, ACK, or TARGET");
1218 1.1 bouyer need_and = 1;
1219 1.1 bouyer }
1220 1.1 bouyer store_inst ();
1221 1.1 bouyer }
1222 1.1 bouyer
1223 1.1 bouyer void block_move ()
1224 1.1 bouyer {
1225 1.1 bouyer if (reserved ("from", tokenix)) {
1226 1.1 bouyer inst1 = evaluate (tokenix+1);
1227 1.1 bouyer inst0 |= 0x10000000 | inst1; /*** ??? to match Zeus script */
1228 1.1 bouyer tokenix += 2;
1229 1.1 bouyer }
1230 1.1 bouyer else {
1231 1.1 bouyer inst0 |= evaluate (tokenix++); /* count */
1232 1.1 bouyer tokenix++; /* skip ',' */
1233 1.1 bouyer if (reserved ("ptr", tokenix)) {
1234 1.1 bouyer ++ tokenix;
1235 1.1 bouyer inst0 |= 0x20000000;
1236 1.1 bouyer }
1237 1.1 bouyer inst1 = evaluate (tokenix++); /* address */
1238 1.1 bouyer }
1239 1.1 bouyer if (tokens[tokenix].type != ',')
1240 1.1 bouyer errout ("Expected separator");
1241 1.1 bouyer if (reserved ("when", tokenix + 1)) {
1242 1.1 bouyer inst0 |= 0x08000000;
1243 1.1 bouyer CheckPhase (tokenix + 2);
1244 1.1 bouyer }
1245 1.1 bouyer else if (reserved ("with", tokenix + 1)) {
1246 1.1 bouyer CheckPhase (tokenix + 2);
1247 1.1 bouyer }
1248 1.1 bouyer else
1249 1.1 bouyer errout ("Expected WITH or WHEN");
1250 1.1 bouyer }
1251 1.1 bouyer
1252 1.1 bouyer void register_write ()
1253 1.1 bouyer {
1254 1.1 bouyer /*
1255 1.1 bouyer * MOVE reg/data8 TO reg register write
1256 1.1 bouyer * MOVE reg <op> data8 TO reg register write
1257 1.1 bouyer * MOVE reg + data8 TO reg WITH CARRY register write
1258 1.1 bouyer */
1259 1.1 bouyer int op;
1260 1.1 bouyer int reg;
1261 1.1 bouyer int data;
1262 1.1 bouyer
1263 1.1 bouyer if (reserved ("to", tokenix+1))
1264 1.1 bouyer op = 0;
1265 1.1 bouyer else if (tokens[tokenix+1].type == '|')
1266 1.1 bouyer op = 2;
1267 1.1 bouyer else if (tokens[tokenix+1].type == '&')
1268 1.1 bouyer op = 4;
1269 1.1 bouyer else if (tokens[tokenix+1].type == '+')
1270 1.1 bouyer op = 6;
1271 1.1 bouyer else if (tokens[tokenix+1].type == '-')
1272 1.1 bouyer op = 8;
1273 1.1 bouyer else
1274 1.1 bouyer errout ("Unknown register operator");
1275 1.1 bouyer if (op && reserved ("to", tokenix+3) == 0)
1276 1.1 bouyer errout ("Register command expected TO");
1277 1.1 bouyer reg = CheckRegister (tokenix);
1278 1.1 bouyer if (reg < 0) { /* Not register, must be data */
1279 1.1 bouyer data = evaluate (tokenix);
1280 1.1 bouyer if (op)
1281 1.1 bouyer errout ("Register operator not move");
1282 1.1 bouyer reg = CheckRegister (tokenix+2);
1283 1.1 bouyer if (reg < 0)
1284 1.1 bouyer errout ("Expected register");
1285 1.1 bouyer inst0 = 0x78000000 | (data << 8) | reg << 16;
1286 1.1 bouyer #if 0
1287 1.1 bouyer fprintf (listfp, "Move data to register: %02x %d\n", data, reg);
1288 1.1 bouyer #endif
1289 1.1 bouyer }
1290 1.1 bouyer else if (op) { /* A register read/write operator */
1291 1.1 bouyer data = evaluate (tokenix+2);
1292 1.1 bouyer if (tokenix+5 < ntokens) {
1293 1.1 bouyer if (!reserved("with", tokenix+5) ||
1294 1.1 bouyer !reserved("carry", tokenix+6)) {
1295 1.1 bouyer errout("Expected 'WITH CARRY'");
1296 1.1 bouyer } else if (op != 6) {
1297 1.1 bouyer errout("'WITH CARRY' only valide with '+'");
1298 1.1 bouyer }
1299 1.1 bouyer op = 7;
1300 1.1 bouyer }
1301 1.1 bouyer if (op == 8) {
1302 1.1 bouyer data = -data;
1303 1.1 bouyer op = 6;
1304 1.1 bouyer }
1305 1.1 bouyer inst0 = (data & 0xff) << 8;
1306 1.1 bouyer data = CheckRegister (tokenix+4);
1307 1.1 bouyer if (data < 0)
1308 1.1 bouyer errout ("Expected register");
1309 1.1 bouyer if (reg != data && reg != 8 && data != 8)
1310 1.1 bouyer errout ("One register MUST be SBFR");
1311 1.1 bouyer if (reg == data) { /* A register read/modify/write */
1312 1.1 bouyer #if 0
1313 1.1 bouyer fprintf (listfp, "Read/modify register: %02x %d %d\n", inst0 >> 8, op, reg);
1314 1.1 bouyer #endif
1315 1.1 bouyer inst0 |= 0x78000000 | (op << 24) | (reg << 16);
1316 1.1 bouyer }
1317 1.1 bouyer else { /* A move to/from SFBR */
1318 1.1 bouyer if (reg == 8) { /* MOVE SFBR <> TO reg */
1319 1.1 bouyer #if 0
1320 1.1 bouyer fprintf (listfp, "Move SFBR to register: %02x %d %d\n", inst0 >> 8, op, data);
1321 1.1 bouyer #endif
1322 1.1 bouyer inst0 |= 0x68000000 | (op << 24) | (data << 16);
1323 1.1 bouyer }
1324 1.1 bouyer else {
1325 1.1 bouyer #if 0
1326 1.1 bouyer fprintf (listfp, "Move register to SFBR: %02x %d %d\n", inst0 >> 8, op, reg);
1327 1.1 bouyer #endif
1328 1.1 bouyer inst0 |= 0x70000000 | (op << 24) | (reg << 16);
1329 1.1 bouyer }
1330 1.1 bouyer }
1331 1.1 bouyer }
1332 1.1 bouyer else { /* register to register */
1333 1.1 bouyer data = CheckRegister (tokenix+2);
1334 1.1 bouyer if (reg == 8) /* move SFBR to reg */
1335 1.1 bouyer inst0 = 0x6a000000 | (data << 16);
1336 1.1 bouyer else if (data == 8) /* move reg to SFBR */
1337 1.1 bouyer inst0 = 0x72000000 | (reg << 16);
1338 1.1 bouyer else
1339 1.1 bouyer errout ("One register must be SFBR");
1340 1.1 bouyer }
1341 1.1 bouyer }
1342 1.1 bouyer
1343 1.1 bouyer void memory_to_memory ()
1344 1.1 bouyer {
1345 1.1 bouyer inst0 = 0xc0000000 + evaluate (tokenix+1);
1346 1.1 bouyer inst1 = evaluate (tokenix+3);
1347 1.2 bouyer /*
1348 1.2 bouyer * need to hack dsps, otherwise patch offset will be wrong for
1349 1.2 bouyer * second pointer
1350 1.2 bouyer */
1351 1.2 bouyer dsps += 4;
1352 1.1 bouyer inst2 = evaluate (tokenix+5);
1353 1.2 bouyer dsps -= 4;
1354 1.1 bouyer }
1355 1.1 bouyer
1356 1.1 bouyer void error_line()
1357 1.1 bouyer {
1358 1.1 bouyer if (errfp != listfp && errfp && err_listed == 0) {
1359 1.1 bouyer fprintf (errfp, "%3d: %s", lineno, inbuf);
1360 1.1 bouyer err_listed = 1;
1361 1.1 bouyer }
1362 1.1 bouyer }
1363 1.1 bouyer
1364 1.1 bouyer char * makefn (base, sub)
1365 1.1 bouyer char *base;
1366 1.1 bouyer char *sub;
1367 1.1 bouyer {
1368 1.1 bouyer char *fn;
1369 1.1 bouyer
1370 1.1 bouyer fn = malloc (strlen (base) + strlen (sub) + 2);
1371 1.1 bouyer strcpy (fn, base);
1372 1.1 bouyer base = strrchr(fn, '.');
1373 1.1 bouyer if (base)
1374 1.1 bouyer *base = 0;
1375 1.1 bouyer strcat (fn, ".");
1376 1.1 bouyer strcat (fn, sub);
1377 1.1 bouyer return (fn);
1378 1.1 bouyer }
1379 1.1 bouyer
1380 1.1 bouyer void usage()
1381 1.1 bouyer {
1382 1.1 bouyer fprintf (stderr, "usage: scc sourcfile [options]\n");
1383 1.1 bouyer exit(1);
1384 1.1 bouyer }
1385