/
a685util.c
528 lines (442 loc) · 14.5 KB
/
a685util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
HEADER: CUG149;
TITLE: 6805 Cross-Assembler (Portable);
FILENAME: A685UTIL.C;
VERSION: 0.3;
DATE: 08/27/1988;
DESCRIPTION: "This program lets you use your computer to assemble
code for the Motorola 6805 family microprocessors.
The program is written in portable C rather than BDS
C. All assembler features are supported except
relocation linkage, and macros.";
KEYWORDS: Software Development, Assemblers, Cross-Assemblers,
Motorola, MC6805;
SYSTEM: CP/M-80, CP/M-86, HP-UX, MSDOS, PCDOS, QNIX;
COMPILERS: Aztec C86, Aztec CII, CI-C86, Eco-C, Eco-C88, HP-UX,
Lattice C, Microsoft C, QNIX C;
WARNINGS: "This program has compiled successfully on 2 UNIX
compilers, 5 MSDOS compilers, and 2 CP/M compilers.
A port to BDS C would be extremely difficult, but see
volume CUG113. A port to Toolworks C is untried."
AUTHORS: William C. Colley III;
*/
/*
6805 Cross-Assembler in Portable C
Copyright (c) 1985 William C. Colley, III
Revision History:
Ver Date Description
0.0 SEP 1985 Adapted from version 3.2 of the portable 6801 cross-
assembler. WCC3.
0.1 JUL 1986 Added compilation instructions and tweaks for CI-C86,
Eco-C88, and Lattice C. WCC3.
0.2 JAN 1987 Fixed bug that made "FCB 0," legal syntax. WCC3.
0.3 AUG 1988 Fixed a bug in the command line parser that puts it
into a VERY long loop if the user types a command line
like "A685 FILE.ASM -L". WCC3 per Alex Cameron.
This module contains the following utility packages:
1) symbol table building and searching
2) opcode and operator table searching
3) listing file output
4) hex file output
5) error flagging
*/
/* Get global goodies: */
#include "a685.h"
/* Make sure that MSDOS compilers using the large memory model know */
/* that calloc() returns pointer to char as an MSDOS far pointer is */
/* NOT compatible with the int type as is usually the case. */
char *calloc();
/* Get access to global mailboxes defined in A68.C: */
extern char errcode, line[], title[];
extern int eject, listhex;
extern unsigned address, bytes, errors, listleft, obj[], pagelen;
/* The symbol table is a binary tree of variable-length blocks drawn */
/* from the heap with the calloc() function. The root pointer lives */
/* here: */
static SYMBOL *sroot = NULL;
/* Add new symbol to symbol table. Returns pointer to symbol even if */
/* the symbol already exists. If there's not enough memory to store */
/* the new symbol, a fatal error occurs. */
SYMBOL *new_symbol(nam)
char *nam;
{
SCRATCH int i;
SCRATCH SYMBOL **p, *q;
void fatal_error();
for (p = &sroot; (q = *p) && (i = strcmp(nam,q -> sname)); )
p = i < 0 ? &(q -> left) : &(q -> right);
if (!q) {
if (!(*p = q = (SYMBOL *)calloc(1,sizeof(SYMBOL) + strlen(nam))))
fatal_error(SYMBOLS);
strcpy(q -> sname,nam);
}
return q;
}
/* Look up symbol in symbol table. Returns pointer to symbol or NULL */
/* if symbol not found. */
SYMBOL *find_symbol(nam)
char *nam;
{
SCRATCH int i;
SCRATCH SYMBOL *p;
for (p = sroot; p && (i = strcmp(nam,p -> sname));
p = i < 0 ? p -> left : p -> right);
return p;
}
/* Opcode table search routine. This routine pats down the opcode */
/* table for a given opcode and returns either a pointer to it or */
/* NULL if the opcode doesn't exist. */
OPCODE *find_code(nam)
char *nam;
{
OPCODE *bsearch();
static OPCODE opctbl[] = {
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb9, "ADC" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xbb, "ADD" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb4, "AND" },
{ DIR + IDX + 2, 0x38, "ASL" },
{ NULL + 1, 0x48, "ASLA" },
{ NULL + 1, 0x58, "ASLX" },
{ DIR + IDX + 2, 0x37, "ASR" },
{ NULL + 1, 0x47, "ASRA" },
{ NULL + 1, 0x57, "ASRX" },
{ REL + 2, 0x24, "BCC" },
{ BIT + DIR + 2, 0x11, "BCLR" },
{ REL + 2, 0x25, "BCS" },
{ REL + 2, 0x27, "BEQ" },
{ REL + 2, 0x28, "BHCC" },
{ REL + 2, 0x29, "BHCS" },
{ REL + 2, 0x22, "BHI" },
{ REL + 2, 0x24, "BHS" },
{ REL + 2, 0x2f, "BIH" },
{ REL + 2, 0x2e, "BIL" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb5, "BIT" },
{ REL + 2, 0x25, "BLO" },
{ REL + 2, 0x23, "BLS" },
{ REL + 2, 0x2c, "BMC" },
{ REL + 2, 0x2b, "BMI" },
{ REL + 2, 0x2d, "BMS" },
{ REL + 2, 0x26, "BNE" },
{ REL + 2, 0x2a, "BPL" },
{ REL + 2, 0x20, "BRA" },
{ BIT + REL + DIR + 3, 0x01, "BRCLR" },
{ REL + 2, 0x21, "BRN" },
{ BIT + REL + DIR + 3, 0x00, "BRSET" },
{ BIT + DIR + 2, 0x10, "BSET" },
{ REL + 2, 0xad, "BSR" },
{ NULL + 1, 0x98, "CLC" },
{ NULL + 1, 0x9a, "CLI" },
{ DIR + IDX + 2, 0x3f, "CLR" },
{ NULL + 1, 0x4f, "CLRA" },
{ NULL + 1, 0x5f, "CLRX" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb1, "CMP" },
{ DIR + IDX + 2, 0x33, "COM" },
{ NULL + 1, 0x43, "COMA" },
{ NULL + 1, 0x53, "COMX" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb3, "CPX" },
{ DIR + IDX + 2, 0x3a, "DEC" },
{ NULL + 1, 0x4a, "DECA" },
{ NULL + 1, 0x5a, "DECX" },
{ NULL + 1, 0x5a, "DEX" },
{ PSEUDO + ISIF, ELSE, "ELSE" },
{ PSEUDO, END, "END" },
{ PSEUDO + ISIF, ENDIF, "ENDIF" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb8, "EOR" },
{ PSEUDO, EQU, "EQU" },
{ PSEUDO, FCB, "FCB" },
{ PSEUDO, FCC, "FCC" },
{ PSEUDO, FDB, "FDB" },
{ PSEUDO + ISIF, IF, "IF" },
{ DIR + IDX + 2, 0x3c, "INC" },
{ NULL + 1, 0x4c, "INCA" },
{ PSEUDO, INCL, "INCL" },
{ NULL + 1, 0x5c, "INCX" },
{ NULL + 1, 0x5c, "INX" },
{ DIR + EXT + IDX + EXT_IDX + 3, 0xbc, "JMP" },
{ DIR + EXT + IDX + EXT_IDX + 3, 0xbd, "JSR" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb6, "LDA" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xbe, "LDX" },
{ DIR + IDX + 2, 0x38, "LSL" },
{ NULL + 1, 0x48, "LSLA" },
{ NULL + 1, 0x58, "LSLX" },
{ DIR + IDX + 2, 0x34, "LSR" },
{ NULL + 1, 0x44, "LSRA" },
{ NULL + 1, 0x54, "LSRX" },
{ NULL + 1, 0x42, "MUL" },
{ DIR + IDX + 2, 0x30, "NEG" },
{ NULL + 1, 0x40, "NEGA" },
{ NULL + 1, 0x50, "NEGX" },
{ NULL + 1, 0x9d, "NOP" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xba, "ORA" },
{ PSEUDO, ORG, "ORG" },
{ PSEUDO, PAGE, "PAGE" },
{ PSEUDO, RMB, "RMB" },
{ DIR + IDX + 2, 0x39, "ROL" },
{ NULL + 1, 0x49, "ROLA" },
{ NULL + 1, 0x59, "ROLX" },
{ DIR + IDX + 2, 0x36, "ROR" },
{ NULL + 1, 0x46, "RORA" },
{ NULL + 1, 0x56, "RORX" },
{ NULL + 1, 0x9c, "RSP" },
{ NULL + 1, 0x80, "RTI" },
{ NULL + 1, 0x81, "RTS" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb2, "SBC" },
{ NULL + 1, 0x99, "SEC" },
{ NULL + 1, 0x9b, "SEI" },
{ PSEUDO, SET, "SET" },
{ DIR + EXT + IDX + EXT_IDX + 3, 0xb7, "STA" },
{ NULL + 1, 0x8e, "STOP" },
{ DIR + EXT + IDX + EXT_IDX + 3, 0xbf, "STX" },
{ IMMED + DIR + EXT + IDX + EXT_IDX + 3, 0xb0, "SUB" },
{ NULL + 1, 0x83, "SWI" },
{ NULL + 1, 0x97, "TAX" },
{ PSEUDO, TITLE, "TITLE" },
{ DIR + IDX + 2, 0x3d, "TST" },
{ NULL + 1, 0x4d, "TSTA" },
{ NULL + 1, 0x5d, "TSTX" },
{ NULL + 1, 0x9f, "TXA" },
{ NULL + 1, 0x8f, "WAIT" }
};
return bsearch(opctbl,opctbl + (sizeof(opctbl) / sizeof(OPCODE)),nam);
}
/* Operator table search routine. This routine pats down the */
/* operator table for a given operator and returns either a pointer */
/* to it or NULL if the opcode doesn't exist. */
OPCODE *find_operator(nam)
char *nam;
{
OPCODE *bsearch();
static OPCODE oprtbl[] = {
{ BINARY + LOG1 + OPR, AND, "AND" },
{ BINARY + RELAT + OPR, '=', "EQ" },
{ BINARY + RELAT + OPR, GE, "GE" },
{ BINARY + RELAT + OPR, '>', "GT" },
{ UNARY + UOP3 + OPR, HIGH, "HIGH" },
{ BINARY + RELAT + OPR, LE, "LE" },
{ UNARY + UOP3 + OPR, LOW, "LOW" },
{ BINARY + RELAT + OPR, '<', "LT" },
{ BINARY + MULT + OPR, MOD, "MOD" },
{ BINARY + RELAT + OPR, NE, "NE" },
{ UNARY + UOP2 + OPR, NOT, "NOT" },
{ BINARY + LOG2 + OPR, OR, "OR" },
{ BINARY + MULT + OPR, SHL, "SHL" },
{ BINARY + MULT + OPR, SHR, "SHR" },
{ REG, 'X', "X" },
{ BINARY + LOG2 + OPR, XOR, "XOR" }
};
return bsearch(oprtbl,oprtbl + (sizeof(oprtbl) / sizeof(OPCODE)),nam);
}
static OPCODE *bsearch(lo,hi,nam)
OPCODE *lo, *hi;
char *nam;
{
SCRATCH int i;
SCRATCH OPCODE *chk;
for (;;) {
chk = lo + (hi - lo) / 2;
if (!(i = ustrcmp(chk -> oname,nam))) return chk;
if (chk == lo) return NULL;
if (i < 0) lo = chk;
else hi = chk;
}
}
static int ustrcmp(s,t)
char *s, *t;
{
SCRATCH int i;
while (!(i = toupper(*s++) - toupper(*t)) && *t++);
return i;
}
/* Buffer storage for line listing routine. This allows the listing */
/* output routines to do all operations without the main routine */
/* having to fool with it. */
static FILE *list = NULL;
/* Listing file open routine. If a listing file is already open, a */
/* warning occurs. If the listing file doesn't open correctly, a */
/* fatal error occurs. If no listing file is open, all calls to */
/* lputs() and lclose() have no effect. */
void lopen(nam)
char *nam;
{
FILE *fopen();
void fatal_error(), warning();
if (list) warning(TWOLST);
else if (!(list = fopen(nam,"w"))) fatal_error(LSTOPEN);
return;
}
/* Listing file line output routine. This routine processes the */
/* source line saved by popc() and the output of the line assembler in */
/* buffer obj into a line of the listing. If the disk fills up, a */
/* fatal error occurs. */
void lputs()
{
SCRATCH int i, j;
SCRATCH unsigned *o;
void check_page(), fatal_error();
if (list) {
i = bytes; o = obj;
do {
fprintf(list,"%c ",errcode);
if (listhex) {
fprintf(list,"%04x ",address);
for (j = 4; j; --j) {
if (i) { --i; ++address; fprintf(list," %02x",*o++); }
else fprintf(list," ");
}
}
else fprintf(list,"%18s","");
fprintf(list," %s",line); strcpy(line,"\n");
check_page();
if (ferror(list)) fatal_error(DSKFULL);
} while (listhex && i);
}
return;
}
/* Listing file close routine. The symbol table is appended to the */
/* listing in alphabetic order by symbol name, and the listing file is */
/* closed. If the disk fills up, a fatal error occurs. */
static int col = 0;
void lclose()
{
void fatal_error(), list_sym();
if (list) {
if (sroot) {
list_sym(sroot);
if (col) fprintf(list,"\n");
}
fprintf(list,"\f");
if (ferror(list) || fclose(list) == EOF) fatal_error(DSKFULL);
}
return;
}
static void list_sym(sp)
SYMBOL *sp;
{
void check_page();
if (sp) {
list_sym(sp -> left);
fprintf(list,"%04x %-10s",sp -> valu,sp -> sname);
if (col = ++col % SYMCOLS) fprintf(list," ");
else {
fprintf(list,"\n");
if (sp -> right) check_page();
}
list_sym(sp -> right);
}
return;
}
static void check_page()
{
if (pagelen && !--listleft) eject = TRUE;
if (eject) {
eject = FALSE; listleft = pagelen; fprintf(list,"\f");
if (title[0]) { listleft -= 2; fprintf(list,"%s\n\n",title); }
}
return;
}
/* Buffer storage for hex output file. This allows the hex file */
/* output routines to do all of the required buffering and record */
/* forming without the main routine having to fool with it. */
static FILE *hex = NULL;
static unsigned cnt = 0;
static unsigned addr = 0;
static unsigned sum = 0;
static unsigned buf[HEXSIZE];
/* Hex file open routine. If a hex file is already open, a warning */
/* occurs. If the hex file doesn't open correctly, a fatal error */
/* occurs. If no hex file is open, all calls to hputc(), hseek(), and */
/* hclose() have no effect. */
void hopen(nam)
char *nam;
{
FILE *fopen();
void fatal_error(), warning();
if (hex) warning(TWOHEX);
else if (!(hex = fopen(nam,"w"))) fatal_error(HEXOPEN);
return;
}
/* Hex file write routine. The data byte is appended to the current */
/* record. If the record fills up, it gets written to disk. If the */
/* disk fills up, a fatal error occurs. */
void hputc(c)
unsigned c;
{
void record();
if (hex) {
buf[cnt++] = c;
if (cnt == HEXSIZE) record(0);
}
return;
}
/* Hex file address set routine. The specified address becomes the */
/* load address of the next record. If a record is currently open, */
/* it gets written to disk. If the disk fills up, a fatal error */
/* occurs. */
void hseek(a)
unsigned a;
{
void record();
if (hex) {
if (cnt) record(0);
addr = a;
}
return;
}
/* Hex file close routine. Any open record is written to disk, the */
/* EOF record is added, and file is closed. If the disk fills up, a */
/* fatal error occurs. */
void hclose()
{
void fatal_error(), record();
if (hex) {
if (cnt) record(0);
record(1);
if (fclose(hex) == EOF) fatal_error(DSKFULL);
}
return;
}
static void record(typ)
unsigned typ;
{
SCRATCH unsigned i;
void fatal_error(), putb();
putc(':',hex); putb(cnt); putb(high(addr));
putb(low(addr)); putb(typ);
for (i = 0; i < cnt; ++i) putb(buf[i]);
putb(low(-sum)); putc('\n',hex);
addr += cnt; cnt = 0;
if (ferror(hex)) fatal_error(DSKFULL);
return;
}
static void putb(b)
unsigned b;
{
static char digit[] = "0123456789ABCDEF";
putc(digit[b >> 4],hex); putc(digit[b & 0x0f],hex);
sum += b; return;
}
/* Error handler routine. If the current error code is non-blank, */
/* the error code is filled in and the number of lines with errors */
/* is adjusted. */
void error(code)
char code;
{
if (errcode == ' ') { errcode = code; ++errors; }
return;
}
/* Fatal error handler routine. A message gets printed on the stderr */
/* device, and the program bombs. */
void fatal_error(msg)
char *msg;
{
printf("Fatal Error -- %s\n",msg);
exit(-1);
}
/* Non-fatal error handler routine. A message gets printed on the */
/* stderr device, and the routine returns. */
void warning(msg)
char *msg;
{
printf("Warning -- %s\n",msg);
return;
}