Mercurial > hg > Members > kono > os9 > sbc09
annotate a09.c @ 33:7c5379eb406e
nitors9 version
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 14 Jul 2018 15:22:54 +0900 |
parents | 3c14d647bb51 |
children | 2032755628dc |
rev | line source |
---|---|
33 | 1 /* A09, 6809 Assembler2 |
0 | 2 |
3 created 1993,1994 by L.C. Benschop. | |
4 copyleft (c) 1994-2014 by the sbc09 team, see AUTHORS for more details. | |
5 license: GNU General Public License version 2, see LICENSE for more details. | |
6 THERE IS NO WARRANTY ON THIS PROGRAM. | |
7 | |
8 Generates binary image file from the lowest to | |
9 the highest address with actually assembled data. | |
10 | |
5 | 11 Machin edependencies: |
0 | 12 char is 8 bits. |
13 short is 16 bits. | |
14 integer arithmetic is twos complement. | |
15 | |
16 syntax a09 [-o filename] [-l filename] sourcefile. | |
17 | |
18 Options | |
19 -o filename name of the output file (default name minus a09 suffix) | |
20 -s filename name of the s-record output file (default its a binary file) | |
21 -l filename list file name (default no listing) | |
22 -d enable debugging | |
23 | |
24 recognized pseudoops: | |
25 extern public | |
26 macro endm if else endif | |
27 org equ set setdp | |
28 fcb fcw fdb fcc rmb | |
29 end include title | |
30 | |
31 Not all of these are actually IMPLEMENTED!!!!!! | |
32 | |
33 Revisions: | |
34 1993-11-03 v0.1 | |
35 Initial version. | |
36 1994/03/21 v0.2 | |
37 Fixed PC relative addressing bug | |
38 Added SET, SETDP, INCLUDE. IF/ELSE/ENDIF | |
39 No macros yet, and no separate linkable modules. | |
40 2012-06-04 j at klasek at | |
41 New: debugging parameter/option. | |
42 Fixed additional possible issue PC relative addressing. | |
43 Compatibility: Octal number prefix "&". | |
44 2014-07-15 j at klasek at | |
45 Fixed usage message. | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
46 2018-07-11 |
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
47 leax $ED00/256,x kernel offset in map |
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
48 should be positive offset expr should be int(32bit) |
0 | 49 */ |
50 | |
51 #include <stdlib.h> | |
52 #include <stdio.h> | |
53 #include <string.h> | |
54 #include <ctype.h> | |
55 | |
56 #define NLABELS 2048 | |
57 #define MAXIDLEN 16 | |
58 #define MAXLISTBYTES 8 | |
59 #define FNLEN 30 | |
60 #define LINELEN 128 | |
61 | |
62 static int debug=0; | |
33 | 63 static struct incl { |
64 char *name; | |
65 struct incl *next; | |
66 } *incls = 0; | |
67 | |
68 static struct longer { | |
69 int gline; | |
70 int change; | |
71 struct longer *next; | |
72 } *lglist = 0; | |
73 | |
74 void makelonger(int gl) { | |
75 for(struct longer *p=lglist;p;p=p->next) { | |
76 if (p->gline==gl) { // already fixed | |
77 p->change = 1; | |
78 return; | |
79 } | |
80 } | |
81 struct longer *p = (struct longer *)calloc(sizeof(struct longer *),1); | |
82 p->gline=gl; | |
83 p->next = lglist; | |
84 lglist = p; | |
85 } | |
86 | |
87 int longer() { | |
88 for(struct longer *p=lglist;p;p=p->next) { | |
89 if (p->change == 0) return 1; | |
90 } | |
91 return 0; | |
92 } | |
0 | 93 |
94 struct oprecord{char * name; | |
95 unsigned char cat; | |
96 unsigned short code;}; | |
97 | |
98 /* Instruction categories: | |
99 0 one byte oprcodes NOP | |
100 1 two byte opcodes SWI2 | |
101 2 opcodes w. imm byte ANDCC | |
102 3 LEAX etc. | |
103 4 short branches. BGE | |
104 5 long branches 2byte opc LBGE | |
105 6 long branches 1byte opc LBRA | |
106 7 accumulator instr. ADDA | |
107 8 double reg instr 1byte opc LDX | |
108 9 double reg instr 2 byte opc LDY | |
109 10 single address instrs NEG | |
110 11 TFR, EXG | |
111 12 push,pull | |
112 13 pseudoops | |
113 */ | |
114 | |
115 struct oprecord optable[]={ | |
116 {"ABX",0,0x3a},{"ADCA",7,0x89},{"ADCB",7,0xc9}, | |
117 {"ADDA",7,0x8b},{"ADDB",7,0xcb},{"ADDD",8,0xc3}, | |
118 {"ANDA",7,0X84},{"ANDB",7,0xc4},{"ANDCC",2,0x1c}, | |
119 {"ASL",10,0x08},{"ASLA",0,0x48},{"ASLB",0,0x58}, | |
120 {"ASR",10,0x07},{"ASRA",0,0x47},{"ASRB",0,0x57}, | |
121 {"BCC",4,0x24},{"BCS",4,0x25},{"BEQ",4,0x27}, | |
122 {"BGE",4,0x2c},{"BGT",4,0x2e},{"BHI",4,0x22}, | |
123 {"BHS",4,0x24},{"BITA",7,0x85},{"BITB",7,0xc5}, | |
124 {"BLE",4,0x2f},{"BLO",4,0x25},{"BLS",4,0x23}, | |
125 {"BLT",4,0x2d},{"BMI",4,0x2b},{"BNE",4,0x26}, | |
126 {"BPL",4,0x2a},{"BRA",4,0x20},{"BRN",4,0x21}, | |
127 {"BSR",4,0x8d}, | |
128 {"BVC",4,0x28},{"BVS",4,0x29}, | |
129 {"CLC",1,0x1cfe},{"CLF",1,0x1cbf},{"CLI",1,0x1cef}, | |
130 {"CLIF",1,0x1caf}, | |
131 {"CLR",10,0x0f},{"CLRA",0,0x4f},{"CLRB",0,0x5f}, | |
132 {"CLV",1,0x1cfd}, | |
133 {"CMPA",7,0x81},{"CMPB",7,0xc1},{"CMPD",9,0x1083}, | |
134 {"CMPS",9,0x118c},{"CMPU",9,0x1183},{"CMPX",8,0x8c}, | |
135 {"CMPY",9,0x108c}, | |
136 {"COM",10,0x03},{"COMA",0,0x43},{"COMB",0,0x53}, | |
137 {"CWAI",2,0x3c},{"DAA",0,0x19}, | |
138 {"DEC",10,0x0a},{"DECA",0,0x4a},{"DECB",0,0x5a}, | |
139 {"DES",1,0x327f},{"DEU",1,0x335f},{"DEX",1,0x301f}, | |
140 {"DEY",1,0x313f}, | |
141 {"ELSE",13,1}, | |
142 {"EMOD",13,25}, | |
143 {"END",13,2}, | |
144 {"ENDC",13,3}, | |
145 {"ENDIF",13,3}, | |
146 {"ENDM",13,4}, | |
147 {"EORA",7,0x88},{"EORB",7,0xc8}, | |
148 {"EQU",13,5},{"EXG",11,0x1e},{"EXTERN",13,6}, | |
149 {"FCB",13,7},{"FCC",13,8}, | |
150 {"FCS",13,23}, | |
151 {"FCW",13,9}, | |
152 {"FDB",13,9}, | |
153 {"IF",13,10}, | |
33 | 154 {"IFEQ",13,30}, |
155 {"IFGT",13,29}, | |
156 {"IFNDEF",13,33}, | |
0 | 157 {"IFNE",13,28}, |
158 {"IFP1",13,21}, | |
159 {"INC",10,0x0c},{"INCA",0,0x4c},{"INCB",0,0x5c}, | |
160 {"INCLUDE",13,16}, | |
161 {"INS",1,0x3261},{"INU",1,0x3341},{"INX",1,0x3001}, | |
162 {"INY",1,0x3121},{"JMP",10,0x0e},{"JSR",8,0x8d}, | |
163 {"LBCC",5,0x1024},{"LBCS",5,0x1025},{"LBEQ",5,0x1027}, | |
164 {"LBGE",5,0x102c},{"LBGT",5,0x102e},{"LBHI",5,0x1022}, | |
165 {"LBHS",5,0x1024}, | |
166 {"LBLE",5,0x102f},{"LBLO",5,0x1025},{"LBLS",5,0x1023}, | |
167 {"LBLT",5,0x102d},{"LBMI",5,0x102b},{"LBNE",5,0x1026}, | |
168 {"LBPL",5,0x102a},{"LBRA",6,0x16},{"LBRN",5,0x1021}, | |
169 {"LBSR",6,0x17}, | |
170 {"LBVC",5,0x1028},{"LBVS",5,0x1029}, | |
171 {"LDA",7,0x86},{"LDB",7,0xc6},{"LDD",8,0xcc}, | |
172 {"LDS",9,0x10ce},{"LDU",8,0xce},{"LDX",8,0x8e}, | |
173 {"LDY",9,0x108e},{"LEAS",3,0x32}, | |
174 {"LEAU",3,0x33},{"LEAX",3,0x30},{"LEAY",3,0x31}, | |
175 {"LSL",10,0x08},{"LSLA",0,0x48},{"LSLB",0,0x58}, | |
176 {"LSR",10,0x04},{"LSRA",0,0x44},{"LSRB",0,0x54}, | |
177 {"MACRO",13,11}, | |
178 {"MOD",13,24}, | |
179 {"MUL",0,0x3d}, | |
180 {"NAM",13,26}, | |
181 {"NEG",10,0x00},{"NEGA",0,0x40},{"NEGB",0,0x50}, | |
182 {"NOP",0,0x12}, | |
183 {"OPT",13,19}, | |
184 {"ORA",7,0x8a},{"ORB",7,0xca},{"ORCC",2,0x1a}, | |
185 {"ORG",13,12}, | |
186 {"OS9",13,32}, | |
187 {"PAG",13,20}, {"PAGE",13,20}, | |
188 {"PSHS",12,0x34},{"PSHU",12,0x36},{"PUBLIC",13,13}, | |
189 {"PULS",12,0x35},{"PULU",12,0x37},{"RMB",13,0}, | |
190 {"ROL",10,0x09},{"ROLA",0,0x49},{"ROLB",0,0x59}, | |
191 {"ROR",10,0x06},{"RORA",0,0x46},{"RORB",0,0x56}, | |
192 {"RTI",0,0x3b},{"RTS",0,0x39}, | |
193 {"SBCA",7,0x82},{"SBCB",7,0xc2}, | |
194 {"SEC",1,0x1a01},{"SEF",1,0x1a40},{"SEI",1,0x1a10}, | |
195 {"SEIF",1,0x1a50},{"SET",13,15}, | |
196 {"SETDP",13,14},{"SEV",1,0x1a02},{"SEX",0,0x1d}, | |
197 {"STA",7,0x87},{"STB",7,0xc7},{"STD",8,0xcd}, | |
198 {"STS",9,0x10cf},{"STU",8,0xcf},{"STX",8,0x8f}, | |
199 {"STY",9,0x108f}, | |
200 {"SUBA",7,0x80},{"SUBB",7,0xc0},{"SUBD",8,0x83}, | |
201 {"SWI",0,0x3f},{"SWI2",1,0x103f},{"SWI3",1,0x113f}, | |
202 {"SYNC",0,0x13},{"TFR",11,0x1f}, | |
203 {"TITLE",13,18}, | |
204 {"TST",10,0x0d},{"TSTA",0,0x4d},{"TSTB",0,0x5d}, | |
205 {"TTL",13,18}, | |
206 {"USE",13,27}, | |
207 }; | |
208 | |
209 struct symrecord{char name[MAXIDLEN+1]; | |
210 char cat; | |
211 unsigned short value; | |
33 | 212 struct symrecord *next; |
0 | 213 }; |
214 | |
215 int symcounter=0; | |
3 | 216 int os9 = 0; // os9 flag |
6 | 217 int prevloc = 0; |
33 | 218 int prevrmb = 0; |
26 | 219 struct symrecord * prevlp = 0; |
0 | 220 |
8 | 221 /* expression categories... |
222 ECORD all zeros is ordinary constant. | |
223 ECADR bit 1 indicates address within module. | |
224 ECEXT bit 2 indicates external address. | |
225 ECLBL bit 3 public label | |
226 ECABS bit 4 indicates this can't be relocated if it's an address. | |
227 ECNEG bit 5 indicates address (if any) is negative. | |
228 */ | |
229 | |
230 | |
231 /* Symbol categories. exprcat ( symcat & 0xe ) | |
232 0 SCC Constant value (from equ). ECORD | |
233 1 SCV Variable value (from set) ECORD | |
234 2 SCC__ADR Address within program module (label). ECADR | |
235 3 SCV__ADR Variable containing address. ECADR | |
236 4 SC_E_ADR Adress in other program module (extern) ECEXT | |
237 5 SCVE_ADR Variable containing external address. ECEXT | |
238 6 SCU _ADR Unresolved address. ECEXT+ECADR | |
239 7 SCV_UADR Variable containing unresolved address. ECEXT+ECADR | |
240 8 SC___LBL Public label. ECLBL | |
241 9 SCMACRO Macro definition. xxx | |
242 10 SCU__LBL Public label (yet undefined). ECADR+ECLBL | |
243 11 SCPARAM parameter name. ECADR+ECLBL | |
244 12 SCLOCAL local label. ECEXT+ECLBL | |
245 13 SCEMPTY empty. xxx | |
0 | 246 */ |
247 | |
248 struct symrecord symtable[NLABELS]; | |
249 | |
250 void processfile(char *name); | |
251 | |
252 struct oprecord * findop(char * nm) | |
253 /* Find operation (mnemonic) in table using binary search */ | |
254 { | |
255 int lo,hi,i,s; | |
256 lo=0;hi=sizeof(optable)/sizeof(optable[0])-1; | |
257 do { | |
258 i=(lo+hi)/2; | |
259 s=strcmp(optable[i].name,nm); | |
260 if(s<0) lo=i+1; | |
261 else if(s>0) hi=i-1; | |
262 else break; | |
263 } while(hi>=lo); | |
264 if (s) return NULL; | |
265 return optable+i; | |
266 } | |
267 | |
268 struct symrecord * findsym(char * nm) | |
269 /* finds symbol table record; inserts if not found | |
270 uses binary search, maintains sorted table */ | |
271 { | |
272 int lo,hi,i,j,s; | |
273 lo=0;hi=symcounter-1; | |
274 s=1;i=0; | |
275 while (hi>=lo) { | |
276 i=(lo+hi)/2; | |
277 s=strcmp(symtable[i].name,nm); | |
278 if(s<0) lo=i+1; | |
279 else if(s>0) hi=i-1; | |
280 else break; | |
281 } | |
282 if(s) { | |
283 i=(s<0?i+1:i); | |
284 if(symcounter==NLABELS) { | |
285 fprintf(stderr,"Sorry, no storage for symbols!!!"); | |
286 exit(4); | |
287 } | |
288 for(j=symcounter;j>i;j--) symtable[j]=symtable[j-1]; | |
289 symcounter++; | |
290 strcpy(symtable[i].name,nm); | |
291 symtable[i].cat=13; | |
292 } | |
293 return symtable+i; | |
294 } | |
295 | |
296 FILE *listfile,*objfile; | |
297 char *listname,*objname,*srcname,*curname; | |
33 | 298 int lineno,glineno; |
0 | 299 |
300 void | |
301 outsymtable() | |
302 { | |
303 int i,j=0; | |
304 fprintf(listfile,"\nSYMBOL TABLE"); | |
305 for(i=0;i<symcounter;i++) | |
306 if(symtable[i].cat!=13) { | |
307 if(j%4==0)fprintf(listfile,"\n"); | |
308 fprintf(listfile,"%10s %02d %04x",symtable[i].name,symtable[i].cat, | |
309 symtable[i].value); | |
310 j++; | |
311 } | |
312 fprintf(listfile,"\n"); | |
313 } | |
314 | |
315 struct regrecord{char *name;unsigned char tfr,psh;}; | |
316 struct regrecord regtable[]= | |
317 {{"D",0x00,0x06},{"X",0x01,0x10},{"Y",0x02,0x20}, | |
318 {"U",0x03,0x40},{"S",0x04,0x40},{"PC",0x05,0x80}, | |
319 {"A",0x08,0x02},{"B",0x09,0x04},{"CC",0x0a,0x01}, | |
320 {"CCR",0x0a,0x01},{"DP",0x0b,0x08},{"DPR",0x0b,0x08}}; | |
321 | |
322 struct regrecord * findreg(char *nm) | |
323 { | |
324 int i; | |
325 for(i=0;i<12;i++) { | |
326 if(strcmp(regtable[i].name,nm)==0) return regtable+i; | |
327 } | |
328 return 0; | |
329 } | |
330 | |
331 | |
332 char pass; /* Assembler pass=1 or 2 */ | |
333 char listing; /* flag to indicate listing */ | |
334 char relocatable; /* flag to indicate relocatable object. */ | |
335 char terminate; /* flag to indicate termination. */ | |
336 char generating; /* flag to indicate that we generate code */ | |
337 unsigned short loccounter,oldlc; /* Location counter */ | |
338 | |
339 char inpline[128]; /* Current input line (not expanded)*/ | |
340 char srcline[128]; /* Current source line */ | |
341 char * srcptr; /* Pointer to line being parsed */ | |
342 | |
343 char unknown; /* flag to indicate value unknown */ | |
344 char certain; /* flag to indicate value is certain at pass 1*/ | |
345 int error; /* flags indicating errors in current line. */ | |
346 int errors; /* number of errors */ | |
347 char exprcat; /* category of expression being parsed, eg. | |
348 label or constant, this is important when | |
349 generating relocatable object code. */ | |
350 | |
6 | 351 void generate() |
352 { | |
353 generating = 1; | |
354 if (prevloc) { | |
33 | 355 prevrmb = loccounter+1; // we were in rmb mode |
7
a6db579d8c11
level 2 rom preparing...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
356 oldlc = loccounter = prevloc-1 ; |
33 | 357 prevloc = 0; |
6 | 358 } |
359 } | |
360 | |
0 | 361 |
362 char namebuf[MAXIDLEN+1]; | |
363 | |
364 void | |
365 err(int er) { | |
366 error |= er ; | |
367 } | |
368 | |
369 void | |
370 scanname() | |
371 { | |
372 int i=0; | |
373 char c; | |
374 while(1) { | |
375 c=*srcptr++; | |
376 if(c>='a'&&c<='z')c-=32; | |
33 | 377 if(c!='_'&&c!='@'&&c!='.'&&c!='$'&&(c<'0'||c>'9')&&(c<'A'||c>'Z'))break; |
0 | 378 if(i<MAXIDLEN)namebuf[i++]=c; |
379 } | |
380 namebuf[i]=0; | |
381 srcptr--; | |
382 } | |
383 | |
384 void | |
385 skipspace() | |
386 { | |
387 char c; | |
388 do { | |
389 c=*srcptr++; | |
390 } while(c==' '||c=='\t'); | |
391 srcptr--; | |
392 } | |
393 | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
394 int scanexpr(int); |
0 | 395 |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
396 int scandecimal() |
0 | 397 { |
398 char c; | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
399 int t=0; |
0 | 400 c=*srcptr++; |
401 while(isdigit(c)) { | |
402 t=t*10+c-'0'; | |
403 c=*srcptr++; | |
404 } | |
405 srcptr--; | |
406 return t; | |
407 } | |
408 | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
409 int scanhex() |
0 | 410 { |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
411 int t=0,i=0; |
0 | 412 srcptr++; |
413 scanname(); | |
414 while(namebuf[i]>='0'&&namebuf[i]<='F') { | |
415 t=t*16+namebuf[i]-'0'; | |
416 if(namebuf[i]>'9')t-=7; | |
417 i++; | |
418 } | |
419 if(i==0)error|=1; | |
420 return t; | |
421 } | |
422 | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
423 int scanchar() |
0 | 424 { |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
425 int t; |
0 | 426 srcptr++; |
427 t=*srcptr; | |
428 if(t)srcptr++; | |
429 if (*srcptr=='\'')srcptr++; | |
430 return t; | |
431 } | |
432 | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
433 int scanbin() |
0 | 434 { |
435 char c; | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
436 int t=0; |
0 | 437 srcptr++; |
438 c=*srcptr++; | |
439 while(c=='0'||c=='1') { | |
440 t=t*2+c-'0'; | |
441 c=*srcptr++; | |
442 } | |
443 srcptr--; | |
444 return t; | |
445 } | |
446 | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
447 int scanoct() |
0 | 448 { |
449 char c; | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
450 int t=0; |
0 | 451 srcptr++; |
452 c=*srcptr++; | |
453 while(c>='0'&&c<='7') { | |
454 t=t*8+c-'0'; | |
455 c=*srcptr++; | |
456 } | |
457 srcptr--; | |
458 return t; | |
459 } | |
460 | |
461 | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
462 int scanlabel() |
0 | 463 { |
464 struct symrecord * p; | |
465 scanname(); | |
466 p=findsym(namebuf); | |
467 if(p->cat==13) { | |
468 p->cat=6; | |
469 p->value=0; | |
470 } | |
471 if(p->cat==9||p->cat==11)error|=1; | |
472 exprcat=p->cat&14; | |
473 if(exprcat==6||exprcat==10)unknown=1; | |
474 if(((exprcat==2||exprcat==8) | |
475 && (unsigned short)(p->value)>(unsigned short)loccounter)|| | |
476 exprcat==4) | |
477 certain=0; | |
478 if(exprcat==8||exprcat==6||exprcat==10)exprcat=2; | |
479 return p->value; | |
480 } | |
481 | |
482 | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
483 int scanfactor() |
0 | 484 { |
485 char c; | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
486 int t; |
0 | 487 skipspace(); |
488 c=*srcptr; | |
489 if(isalpha(c))return scanlabel(); | |
490 else if(isdigit(c))return scandecimal(); | |
491 else switch(c) { | |
33 | 492 case '*' : srcptr++;exprcat|=2; if(prevloc) return prevloc-1; else return loccounter; |
493 case '.' : srcptr++;exprcat|=2; if(prevrmb) return prevrmb-1; else return loccounter; | |
0 | 494 case '$' : return scanhex(); |
495 case '%' : return scanbin(); | |
496 case '&' : /* compatibility */ | |
497 case '@' : return scanoct(); | |
498 case '\'' : return scanchar(); | |
499 case '(' : srcptr++;t=scanexpr(0);skipspace(); | |
500 if(*srcptr==')')srcptr++;else error|=1; | |
501 return t; | |
502 case '-' : srcptr++;exprcat^=32;return -scanfactor(); | |
503 case '+' : srcptr++;return scanfactor(); | |
504 case '!' : srcptr++;exprcat|=16;return !scanfactor(); | |
505 case '^' : | |
506 case '~' : srcptr++;exprcat|=16;return ~scanfactor(); | |
507 } | |
508 error|=1; | |
509 return 0; | |
510 } | |
511 | |
512 #define EXITEVAL {srcptr--;return t;} | |
513 | |
514 #define RESOLVECAT if((oldcat&15)==0)oldcat=0;\ | |
515 if((exprcat&15)==0)exprcat=0;\ | |
516 if((exprcat==2&&oldcat==34)||(exprcat==34&&oldcat==2)) {\ | |
517 exprcat=0;\ | |
518 oldcat=0;}\ | |
519 exprcat|=oldcat;\ | |
520 /* resolve such cases as constant added to address or difference between | |
521 two addresses in same module */ | |
522 | |
523 | |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
524 int scanexpr(int level) /* This is what you call _recursive_ descent!!!*/ |
0 | 525 { |
29
3c14d647bb51
assembler and emulator fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
526 int t,u; |
0 | 527 char oldcat,c; |
528 exprcat=0; | |
529 if(level==10)return scanfactor(); | |
530 t=scanexpr(level+1); | |
531 while(1) { | |
5 | 532 // skipspace(); |
0 | 533 c=*srcptr++; |
534 switch(c) { | |
535 case '*':oldcat=exprcat; | |
536 t*=scanexpr(10); | |
537 exprcat|=oldcat|16; | |
538 break; | |
539 case '/':oldcat=exprcat; | |
540 u=scanexpr(10); | |
541 if(u)t/=u;else error|=1; | |
542 exprcat|=oldcat|16; | |
543 break; | |
544 case '%':oldcat=exprcat; | |
545 u=scanexpr(10); | |
546 if(u)t%=u;else error|=1; | |
547 exprcat|=oldcat|16; | |
548 break; | |
549 case '+':if(level==9)EXITEVAL | |
550 oldcat=exprcat; | |
551 t+=scanexpr(9); | |
552 RESOLVECAT | |
553 break; | |
554 case '-':if(level==9)EXITEVAL | |
555 oldcat=exprcat; | |
556 t-=scanexpr(9); | |
557 exprcat^=32; | |
558 RESOLVECAT | |
559 break; | |
560 case '<':if(*(srcptr)=='<') { | |
561 if(level>=8)EXITEVAL | |
562 srcptr++; | |
563 oldcat=exprcat; | |
564 t<<=scanexpr(8); | |
565 exprcat|=oldcat|16; | |
566 break; | |
567 } else if(*(srcptr)=='=') { | |
568 if(level>=7)EXITEVAL | |
569 srcptr++; | |
570 oldcat=exprcat; | |
571 t=t<=scanexpr(7); | |
572 exprcat|=oldcat|16; | |
573 break; | |
574 } else { | |
575 if(level>=7)EXITEVAL | |
576 oldcat=exprcat; | |
577 t=t<scanexpr(7); | |
578 exprcat|=oldcat|16; | |
579 break; | |
580 } | |
581 case '>':if(*(srcptr)=='>') { | |
582 if(level>=8)EXITEVAL | |
583 srcptr++; | |
584 oldcat=exprcat; | |
585 t>>=scanexpr(8); | |
586 exprcat|=oldcat|16; | |
587 break; | |
588 } else if(*(srcptr)=='=') { | |
589 if(level>=7)EXITEVAL | |
590 srcptr++; | |
591 oldcat=exprcat; | |
592 t=t>=scanexpr(7); | |
593 exprcat|=oldcat|16; | |
594 break; | |
595 } else { | |
596 if(level>=7)EXITEVAL | |
597 oldcat=exprcat; | |
598 t=t>scanexpr(7); | |
599 exprcat|=oldcat|16; | |
600 break; | |
601 } | |
5 | 602 case '!':if(level>=6) { |
603 if (*srcptr=='=') { | |
604 srcptr++; | |
605 oldcat=exprcat; | |
606 t=t!=scanexpr(6); | |
607 exprcat|=oldcat|16; | |
608 } else { | |
609 oldcat=exprcat; | |
610 t|=scanexpr(6); | |
611 exprcat|=oldcat|16; | |
612 } | |
613 } | |
0 | 614 break; |
615 case '=':if(level>=6)EXITEVAL | |
616 if(*srcptr=='=')srcptr++; | |
617 oldcat=exprcat; | |
618 t=t==scanexpr(6); | |
619 exprcat|=oldcat|16; | |
620 break; | |
621 case '&':if(level>=5)EXITEVAL | |
622 oldcat=exprcat; | |
623 t&=scanexpr(5); | |
624 exprcat|=oldcat|16; | |
625 break; | |
626 case '^':if(level>=4)EXITEVAL | |
627 oldcat=exprcat; | |
628 t^=scanexpr(4); | |
629 exprcat|=oldcat|16; | |
630 break; | |
631 case '|':if(level>=3)EXITEVAL | |
632 oldcat=exprcat; | |
633 t|=scanexpr(3); | |
634 exprcat|=oldcat|16; | |
635 default: EXITEVAL | |
636 } | |
637 } | |
638 } | |
639 | |
640 char mode; /* addressing mode 0=immediate,1=direct,2=extended,3=postbyte | |
641 4=pcrelative(with postbyte) 5=indirect 6=pcrel&indirect*/ | |
642 char opsize; /*desired operand size 0=dunno,1=5,2=8,3=16*/ | |
643 short operand; | |
644 unsigned char postbyte; | |
645 | |
646 int dpsetting; | |
647 | |
648 | |
649 int scanindexreg() | |
650 { | |
651 char c; | |
652 c=*srcptr; | |
653 if(islower(c))c-=32; | |
654 if (debug) fprintf(stderr,"DEBUG: scanindexreg: indexreg=%d, mode=%d, opsize=%d, error=%d, postbyte=%02X\n",c,mode,opsize,error,postbyte); | |
655 switch(c) { | |
656 case 'X':return 1; | |
657 case 'Y':postbyte|=0x20;return 1; | |
658 case 'U':postbyte|=0x40;return 1; | |
659 case 'S':postbyte|=0x60;return 1; | |
660 default: return 0; | |
661 } | |
662 } | |
663 | |
664 void | |
665 set3() | |
666 { | |
667 if(mode<3)mode=3; | |
668 } | |
669 | |
670 void | |
671 scanspecial() | |
672 { | |
673 set3(); | |
674 skipspace(); | |
675 if(*srcptr=='-') { | |
676 srcptr++; | |
677 if(*srcptr=='-') { | |
678 srcptr++; | |
679 postbyte=0x83; | |
680 } else postbyte=0x82; | |
681 if(!scanindexreg())error|=2;else srcptr++; | |
682 } else { | |
683 postbyte=0x80; | |
684 if(!scanindexreg())error|=2;else srcptr++; | |
685 if(*srcptr=='+') { | |
686 srcptr++; | |
687 if(*srcptr=='+') { | |
688 srcptr++; | |
689 postbyte+=1; | |
690 } | |
691 } else postbyte+=4; | |
692 } | |
693 } | |
694 | |
695 void | |
696 scanindexed() | |
697 { | |
698 set3(); | |
699 postbyte=0; | |
700 if(scanindexreg()) { | |
701 srcptr++; | |
702 if(opsize==0) { | |
703 if(unknown||!certain)opsize=3; | |
704 else if(operand>=-16&&operand<16&&mode==3)opsize=1; | |
705 else if(operand>=-128&&operand<128)opsize=2; | |
706 else opsize=3; | |
707 } | |
708 switch(opsize) { | |
709 case 1:postbyte+=(operand&31);opsize=0;break; | |
710 case 2:postbyte+=0x88;break; | |
711 case 3:postbyte+=0x89;break; | |
712 } | |
713 } else { /*pc relative*/ | |
714 if(toupper(*srcptr)!='P')error|=2; | |
715 else { | |
716 srcptr++; | |
717 if(toupper(*srcptr)!='C')error|=2; | |
718 else { | |
719 srcptr++; | |
720 if(toupper(*srcptr)=='R')srcptr++; | |
721 } | |
722 } | |
723 mode++;postbyte+=0x8c; | |
724 if(opsize==1)opsize=2; | |
725 } | |
726 } | |
727 | |
728 #define RESTORE {srcptr=oldsrcptr;c=*srcptr;goto dodefault;} | |
729 | |
730 void | |
731 scanoperands() | |
732 { | |
733 char c,d,*oldsrcptr; | |
734 unknown=0; | |
735 opsize=0; | |
736 certain=1; | |
737 skipspace(); | |
738 c=*srcptr; | |
739 mode=0; | |
740 if(c=='[') { | |
741 srcptr++; | |
742 c=*srcptr; | |
743 mode=5; | |
744 } | |
745 if (debug) fprintf(stderr,"DEBUG: scanoperands: c=%c (%02X)\n",c,c); | |
746 switch(c) { | |
747 case 'D': case 'd': | |
748 oldsrcptr=srcptr; | |
749 srcptr++; | |
750 skipspace(); | |
751 if(*srcptr!=',')RESTORE else { | |
752 postbyte=0x8b; | |
753 srcptr++; | |
754 if(!scanindexreg())RESTORE else {srcptr++;set3();} | |
755 } | |
756 break; | |
757 case 'A': case 'a': | |
758 oldsrcptr=srcptr; | |
759 srcptr++; | |
760 skipspace(); | |
761 if(*srcptr!=',')RESTORE else { | |
762 postbyte=0x86; | |
763 srcptr++; | |
764 if(!scanindexreg())RESTORE else {srcptr++;set3();} | |
765 } | |
766 break; | |
767 case 'B': case 'b': | |
768 oldsrcptr=srcptr; | |
769 srcptr++; | |
770 skipspace(); | |
771 if(*srcptr!=',')RESTORE else { | |
772 postbyte=0x85; | |
773 srcptr++; | |
774 if (debug) fprintf(stderr,"DEBUG: scanoperands: breg preindex: c=%c (%02X)\n",*srcptr,*srcptr); | |
775 if(!scanindexreg())RESTORE else {srcptr++;set3();} | |
776 if (debug) fprintf(stderr,"DEBUG: scanoperands: breg: postindex c=%c (%02X)\n",*srcptr,*srcptr); | |
777 } | |
778 break; | |
779 case ',': | |
780 srcptr++; | |
781 scanspecial(); | |
782 break; | |
783 case '#': | |
784 if(mode==5)error|=2;else mode=0; | |
785 srcptr++; | |
5 | 786 if (*srcptr=='"') { |
787 operand = (srcptr[1]<<8) + srcptr[2] ; | |
788 srcptr += 3; | |
789 break; | |
790 } | |
0 | 791 operand=scanexpr(0); |
792 break; | |
793 case '<': | |
794 srcptr++; | |
795 if(*srcptr=='<') { | |
796 srcptr++; | |
797 opsize=1; | |
798 } else opsize=2; | |
799 goto dodefault; | |
800 case '>': | |
801 srcptr++; | |
802 opsize=3; | |
803 default: dodefault: | |
804 operand=scanexpr(0); | |
805 skipspace(); | |
806 if(*srcptr==',') { | |
807 srcptr++; | |
808 scanindexed(); | |
809 } else { | |
810 if(opsize==0) { | |
811 if(unknown||!certain||dpsetting==-1|| | |
812 (unsigned short)(operand-dpsetting*256)>=256) | |
813 opsize=3; else opsize=2; | |
814 } | |
815 if(opsize==1)opsize=2; | |
816 if(mode==5){ | |
817 postbyte=0x8f; | |
818 opsize=3; | |
819 } else mode=opsize-1; | |
820 } | |
821 } | |
822 if (debug) fprintf(stderr,"DEBUG: scanoperands: mode=%d, error=%d, postbyte=%02X\n",mode,error,postbyte); | |
823 if(mode>=5) { | |
824 skipspace(); | |
825 postbyte|=0x10; | |
826 if(*srcptr!=']')error|=2;else srcptr++; | |
827 } | |
828 if(pass==2&&unknown)error|=4; | |
829 } | |
830 | |
831 unsigned char codebuf[128]; | |
832 int codeptr; /* byte offset within instruction */ | |
833 int suppress; /* 0=no suppress 1=until ENDIF 2=until ELSE 3=until ENDM */ | |
834 int ifcount; /* count of nested IFs within suppressed text */ | |
835 | |
836 unsigned char outmode; /* 0 is binary, 1 is s-records */ | |
837 | |
838 unsigned short hexaddr; | |
839 int hexcount; | |
840 unsigned char hexbuffer[16]; | |
841 unsigned int chksum; | |
842 | |
843 extern int os9crc(unsigned char c, int crcp); | |
844 int crc; | |
845 | |
846 void | |
847 reset_crc() | |
848 { | |
849 crc = -1; | |
850 } | |
851 | |
852 | |
853 void | |
854 flushhex() | |
855 { | |
856 int i; | |
857 if(hexcount){ | |
858 fprintf(objfile,"S1%02X%04X",(hexcount+3)&0xff,hexaddr&0xffff); | |
859 for(i=0;i<hexcount;i++)fprintf(objfile,"%02X",hexbuffer[i]); | |
860 chksum+=(hexaddr&0xff)+((hexaddr>>8)&0xff)+hexcount+3; | |
861 fprintf(objfile,"%02X\n",0xff-(chksum&0xff)); | |
862 hexaddr+=hexcount; | |
863 hexcount=0; | |
864 chksum=0; | |
865 } | |
866 } | |
867 | |
868 void | |
869 outhex(unsigned char x) | |
870 { | |
871 if(hexcount==16)flushhex(); | |
872 hexbuffer[hexcount++]=x; | |
873 chksum+=x; | |
874 } | |
875 | |
876 void | |
877 outbuffer() | |
878 { | |
879 int i; | |
880 for(i=0;i<codeptr;i++) { | |
881 crc = os9crc(codebuf[i],crc); | |
882 if(!outmode)fputc(codebuf[i],objfile);else outhex(codebuf[i]); | |
883 } | |
884 } | |
885 | |
886 char *errormsg[]={"Error in expression", | |
887 "Illegal addressing mode", | |
888 "Undefined label", | |
889 "Multiple definitions of label", | |
890 "Relative branch out of range", | |
891 "Missing label", | |
892 "","","","","","","","","", | |
893 "Illegal mnemonic" | |
894 }; | |
895 | |
896 void | |
897 report() | |
898 { | |
899 int i; | |
900 fprintf(stderr,"File %s, line %d:%s\n",curname,lineno,srcline); | |
901 for(i=0;i<16;i++) { | |
902 if(error&1) { | |
903 fprintf(stderr,"%s\n",errormsg[i]); | |
904 if(pass==2&&listing)fprintf(listfile,"**** %s\n",errormsg[i]); | |
905 } | |
906 error>>=1; | |
907 } | |
5 | 908 error = 0; |
0 | 909 errors++; |
910 } | |
911 | |
912 void | |
913 outlist() | |
914 { | |
915 int i; | |
916 fprintf(listfile,"%04X: ",oldlc); | |
917 for(i=0;i<codeptr&&i<MAXLISTBYTES;i++) | |
918 fprintf(listfile,"%02X",codebuf[i]); | |
919 for(;i<=MAXLISTBYTES;i++) | |
920 fprintf(listfile," "); | |
921 fprintf(listfile,"%s\n",srcline); | |
922 while(i<codeptr) { | |
923 fprintf(listfile,"%04X: ",oldlc + i); | |
924 for(int j=0;i<codeptr&&j<MAXLISTBYTES;j++) { | |
925 fprintf(listfile,"%02X",codebuf[i]); i++; | |
926 } | |
927 fprintf(listfile,"\n"); | |
928 } | |
929 } | |
930 | |
931 void | |
932 setlabel(struct symrecord * lp) | |
933 { | |
33 | 934 while (prevlp) { |
26 | 935 struct symrecord *l = prevlp; |
33 | 936 prevlp = prevlp->next; |
26 | 937 setlabel(l); |
938 } | |
0 | 939 if(lp) { |
940 if(lp->cat!=13&&lp->cat!=6) { | |
941 if(lp->cat!=2||lp->value!=loccounter) | |
33 | 942 ; // error|=8; |
0 | 943 } else { |
944 lp->cat=2; | |
945 lp->value=loccounter; | |
946 } | |
947 } | |
948 } | |
949 | |
950 void | |
951 putbyte(unsigned char b) | |
952 { | |
953 codebuf[codeptr++]=b; | |
954 } | |
955 | |
956 void | |
957 putword(unsigned short w) | |
958 { | |
959 codebuf[codeptr++]=w>>8; | |
960 codebuf[codeptr++]=w&0x0ff; | |
961 } | |
962 | |
963 void | |
964 doaddress() /* assemble the right addressing bytes for an instruction */ | |
965 { | |
966 int offs; | |
967 switch(mode) { | |
968 case 0: if(opsize==2)putbyte(operand);else putword(operand);break; | |
969 case 1: putbyte(operand);break; | |
970 case 2: putword(operand);break; | |
971 case 3: case 5: putbyte(postbyte); | |
972 switch(opsize) { | |
973 case 2: putbyte(operand);break; | |
974 case 3: putword(operand); | |
975 } | |
976 break; | |
977 case 4: case 6: offs=(unsigned short)operand-loccounter-codeptr-2; | |
978 if(offs<-128||offs>=128||opsize==3||unknown||!certain) { | |
33 | 979 if((!unknown)&&opsize==2&&(offs<-128||offs>=128) ) { |
980 error|=16; makelonger(glineno); | |
981 } | |
12
111e5defb8ab
boot is called, rti failed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
982 offs--; |
111e5defb8ab
boot is called, rti failed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
983 opsize=3; |
111e5defb8ab
boot is called, rti failed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
984 postbyte++; |
0 | 985 } |
986 putbyte(postbyte); | |
987 if (debug) fprintf(stderr,"DEBUG: doaddress: mode=%d, opsize=%d, error=%d, postbyte=%02X, operand=%04X offs=%d\n",mode,opsize,error,postbyte,operand,offs); | |
988 if(opsize==3)putword(offs); | |
989 else putbyte(offs); | |
990 } | |
991 } | |
992 | |
993 void | |
994 onebyte(int co) | |
995 { | |
996 putbyte(co); | |
997 } | |
998 | |
999 void | |
1000 twobyte(int co) | |
1001 { | |
1002 putword(co); | |
1003 } | |
1004 | |
1005 void | |
1006 oneimm(int co) | |
1007 { | |
1008 scanoperands(); | |
5 | 1009 if(mode>=3) |
1010 error|=2; | |
0 | 1011 putbyte(co); |
1012 putbyte(operand); | |
1013 } | |
1014 | |
1015 void | |
1016 lea(int co) | |
1017 { | |
1018 putbyte(co); | |
1019 scanoperands(); | |
1020 if(mode==0) error|=2; | |
1021 if(mode<3) { | |
1022 opsize=3; | |
1023 postbyte=0x8f; | |
1024 mode=3; | |
1025 } | |
1026 if (debug) fprintf(stderr,"DEBUG: lea: mode=%d, opsize=%d, error=%d, postbyte=%02X, *src=%c\n",mode,opsize,error,postbyte,*srcptr); | |
1027 doaddress(); | |
1028 } | |
1029 | |
1030 void | |
1031 sbranch(int co) | |
1032 { | |
1033 int offs; | |
1034 scanoperands(); | |
1035 if(mode!=1&&mode!=2)error|=2; | |
1036 offs=(unsigned short)operand-loccounter-2; | |
33 | 1037 if(!unknown&&(offs<-128||offs>=128)) { |
1038 error|=16;makelonger(glineno); | |
1039 if (co==0x20) { | |
1040 if(mode!=1&&mode!=2)error|=2; | |
1041 putbyte(0x16); | |
1042 putword(operand-loccounter-3); | |
1043 } else { | |
1044 if(mode!=1&&mode!=2)error|=2; | |
1045 putbyte(0x10); | |
1046 putbyte(co); | |
1047 putword(operand-loccounter-4); | |
1048 } | |
1049 return; | |
1050 } | |
0 | 1051 if(pass==2&&unknown)error|=4; |
1052 putbyte(co); | |
1053 putbyte(offs); | |
1054 } | |
1055 | |
1056 void | |
1057 lbra(int co) | |
1058 { | |
1059 scanoperands(); | |
1060 if(mode!=1&&mode!=2)error|=2; | |
1061 putbyte(co); | |
1062 putword(operand-loccounter-3); | |
1063 } | |
1064 | |
1065 void | |
1066 lbranch(int co) | |
1067 { | |
1068 scanoperands(); | |
1069 if(mode!=1&&mode!=2)error|=2; | |
1070 putword(co); | |
1071 putword(operand-loccounter-4); | |
1072 } | |
1073 | |
1074 void | |
1075 arith(int co) | |
1076 { | |
1077 scanoperands(); | |
1078 switch(mode) { | |
1079 case 0:opsize=2;putbyte(co);break; | |
1080 case 1:putbyte(co+0x010);break; | |
1081 case 2:putbyte(co+0x030);break; | |
1082 default:putbyte(co+0x020); | |
1083 } | |
1084 doaddress(); | |
1085 } | |
1086 | |
1087 void | |
1088 darith(int co) | |
1089 { | |
1090 scanoperands(); | |
1091 switch(mode) { | |
1092 case 0:opsize=3;putbyte(co);break; | |
1093 case 1:putbyte(co+0x010);break; | |
1094 case 2:putbyte(co+0x030);break; | |
1095 default:putbyte(co+0x020); | |
1096 } | |
1097 doaddress(); | |
1098 } | |
1099 | |
1100 void | |
1101 d2arith(int co) | |
1102 { | |
1103 scanoperands(); | |
1104 switch(mode) { | |
1105 case 0:opsize=3;putword(co);break; | |
1106 case 1:putword(co+0x010);break; | |
1107 case 2:putword(co+0x030);break; | |
1108 default:putword(co+0x020); | |
1109 } | |
1110 doaddress(); | |
1111 } | |
1112 | |
1113 void | |
1114 oneaddr(int co) | |
1115 { | |
1116 scanoperands(); | |
1117 switch(mode) { | |
1118 case 0: error|=2;break; | |
1119 case 1: putbyte(co);break; | |
1120 case 2: putbyte(co+0x70);break; | |
1121 default: putbyte(co+0x60);break; | |
1122 } | |
1123 doaddress(); | |
1124 } | |
1125 | |
1126 void | |
1127 tfrexg(int co) | |
1128 { | |
1129 struct regrecord * p; | |
1130 putbyte(co); | |
1131 skipspace(); | |
1132 scanname(); | |
1133 if((p=findreg(namebuf))==0)error|=2; | |
1134 else postbyte=(p->tfr)<<4; | |
1135 skipspace(); | |
1136 if(*srcptr==',')srcptr++;else error|=2; | |
1137 skipspace(); | |
1138 scanname(); | |
1139 if((p=findreg(namebuf))==0)error|=2; | |
1140 else postbyte|=p->tfr; | |
1141 putbyte(postbyte); | |
1142 } | |
1143 | |
1144 void | |
1145 pshpul(int co) | |
1146 { | |
1147 struct regrecord *p; | |
1148 putbyte(co); | |
1149 postbyte=0; | |
1150 do { | |
1151 if(*srcptr==',')srcptr++; | |
1152 skipspace(); | |
1153 scanname(); | |
1154 if((p=findreg(namebuf))==0)error|=2; | |
1155 else postbyte|=p->psh; | |
1156 skipspace(); | |
1157 }while (*srcptr==','); | |
1158 putbyte(postbyte); | |
1159 } | |
1160 | |
1161 void | |
1162 skipComma() | |
1163 { | |
1164 while(*srcptr && *srcptr!='\n' && *srcptr!=',')srcptr++; | |
1165 if (*srcptr==',') { | |
1166 srcptr++; | |
1167 } else { | |
1168 error|=1; | |
1169 } | |
1170 } | |
1171 | |
1172 int modStart; | |
1173 | |
1174 void os9begin() | |
1175 { | |
6 | 1176 generate(); |
3 | 1177 os9=1; // contiguous code generation ( i.e. ignore org nor rmb ) |
0 | 1178 modStart = loccounter; |
1179 reset_crc(); | |
1180 putword(0x87cd); | |
1181 putword(scanexpr(0)-loccounter); // module size | |
1182 skipComma(); | |
1183 putword(scanexpr(0)-loccounter); // offset to module name | |
1184 skipComma(); | |
1185 putbyte(scanexpr(0)); // type / language | |
1186 skipComma(); | |
1187 putbyte(scanexpr(0)); // attribute | |
1188 int parity=0; | |
1189 for(int i=0; i< 8; i++) parity^=codebuf[i]; | |
1190 putbyte(parity^0xff); // header parity | |
1191 skipspace(); | |
1192 while (*srcptr==',') { // there are some more | |
1193 srcptr++; | |
1194 putword(scanexpr(0)); | |
1195 skipspace(); | |
1196 } | |
1197 } | |
1198 | |
1199 void os9end() | |
1200 { | |
1201 crc = crc ^ 0xffffff; | |
1202 | |
1203 putbyte((crc>>16)&0xff); | |
1204 putbyte((crc>>8)&0xff); | |
1205 putbyte(crc&0xff); | |
1206 } | |
1207 | |
1208 | |
1209 void | |
1210 pseudoop(int co,struct symrecord * lp) | |
1211 { | |
1212 int i; | |
1213 char c; | |
1214 char *fname; | |
1215 int locsave; | |
1216 | |
1217 switch(co) { | |
1218 case 0:/* RMB */ | |
3 | 1219 // in OS9 mode, this generates no data |
1220 // loccounter will be reset after any code to the current code generation | |
33 | 1221 if (prevrmb) { |
1222 oldlc = loccounter = prevrmb-1 ; | |
1223 prevrmb = 0; | |
1224 } | |
0 | 1225 setlabel(lp); |
1226 operand=scanexpr(0); | |
1227 if(unknown)error|=4; | |
1228 loccounter+=operand; | |
1229 if(generating&&pass==2) { | |
6 | 1230 if(!outmode && !os9 ) { |
1231 for(i=0;i<operand;i++) { fputc(0,objfile); } | |
1232 } else | |
1233 flushhex(); | |
0 | 1234 } |
1235 hexaddr=loccounter; | |
1236 break; | |
1237 case 5:/* EQU */ | |
1238 operand=scanexpr(0); | |
1239 if(!lp)error|=32; | |
1240 else { | |
1241 if(lp->cat==13||lp->cat==6|| | |
1242 (lp->value==(unsigned short)operand&&pass==2)) { | |
1243 if(exprcat==2)lp->cat=2; | |
1244 else lp->cat=0; | |
1245 lp->value=operand; | |
33 | 1246 } // else error|=8; |
0 | 1247 } |
1248 break; | |
1249 case 7:/* FCB */ | |
6 | 1250 generate(); |
0 | 1251 setlabel(lp); |
1252 do { | |
1253 if(*srcptr==',')srcptr++; | |
1254 skipspace(); | |
1255 if(*srcptr=='\"') { | |
1256 srcptr++; | |
1257 while(*srcptr!='\"'&&*srcptr) | |
1258 putbyte(*srcptr++); | |
1259 if(*srcptr=='\"')srcptr++; | |
1260 } else { | |
1261 putbyte(scanexpr(0)); | |
1262 if(unknown&&pass==2)error|=4; | |
1263 } | |
1264 skipspace(); | |
1265 } while(*srcptr==','); | |
1266 break; | |
1267 case 8:/* FCC */ | |
6 | 1268 generate(); |
0 | 1269 setlabel(lp); |
1270 skipspace(); | |
1271 c=*srcptr++; | |
1272 while(*srcptr!=c&&*srcptr) | |
1273 putbyte(*srcptr++); | |
1274 if(*srcptr==c)srcptr++; | |
1275 break; | |
1276 case 9:/* FDB */ | |
6 | 1277 generate(); |
0 | 1278 setlabel(lp); |
1279 do { | |
1280 if(*srcptr==',')srcptr++; | |
1281 skipspace(); | |
1282 putword(scanexpr(0)); | |
1283 if(unknown&&pass==2)error|=4; | |
1284 skipspace(); | |
1285 } while(*srcptr==','); | |
1286 break; | |
1287 case 23 :/* FCS */ | |
6 | 1288 generate(); |
0 | 1289 setlabel(lp); |
1290 skipspace(); | |
1291 int sep = *srcptr; | |
1292 if(sep=='\"' || sep=='/') { | |
1293 srcptr++; | |
1294 while(*srcptr!=sep&&*srcptr) | |
1295 putbyte(*srcptr++); | |
1296 if(*srcptr==sep)srcptr++; | |
1297 codebuf[codeptr-1] |= 0x80; // os9 string termination | |
1298 } | |
1299 break; | |
1300 case 1: /* ELSE */ | |
1301 suppress=1; | |
1302 break; | |
1303 case 21: /* IFP1 */ | |
1304 if(pass==2)suppress=2; | |
1305 break; | |
1306 case 29: /* IFGT */ | |
1307 operand=scanexpr(0); | |
33 | 1308 if(operand<=0)suppress=2; |
0 | 1309 break; |
1310 case 31: /* IFLT */ | |
1311 operand=scanexpr(0); | |
33 | 1312 if(operand>=0)suppress=2; |
0 | 1313 break; |
3 | 1314 case 30: /* IFEQ */ |
0 | 1315 operand=scanexpr(0); |
33 | 1316 if(operand!=0)suppress=2; |
0 | 1317 break; |
3 | 1318 case 28: /* IFNE */ |
0 | 1319 case 10: /* IF */ |
1320 operand=scanexpr(0); | |
33 | 1321 if(operand==0)suppress=2; |
1322 break; | |
1323 case 33: /* IFNDEF */ | |
1324 operand=scanexpr(0); | |
1325 if(!unknown)suppress=2; | |
0 | 1326 break; |
1327 case 12: /* ORG */ | |
1328 operand=scanexpr(0); | |
1329 if(unknown)error|=4; | |
3 | 1330 if(generating&&pass==2&&!outmode&&!os9) { |
0 | 1331 for(i=0;i<(unsigned short)operand-loccounter;i++) |
3 | 1332 fputc(0,objfile); |
1333 } else flushhex(); | |
0 | 1334 loccounter=operand; |
1335 hexaddr=loccounter; | |
1336 break; | |
1337 case 14: /* SETDP */ | |
1338 operand=scanexpr(0); | |
1339 if(unknown)error|=4; | |
1340 if(!(operand&255))operand=(unsigned short)operand>>8; | |
1341 if((unsigned)operand>255)operand=-1; | |
1342 dpsetting=operand; | |
1343 break; | |
1344 case 15: /* SET */ | |
1345 operand=scanexpr(0); | |
1346 if(!lp)error|=32; | |
1347 else { | |
1348 if(lp->cat&1||lp->cat==6) { | |
1349 if(exprcat==2)lp->cat=3; | |
1350 else lp->cat=1; | |
1351 lp->value=operand; | |
33 | 1352 } // else error|=8; |
0 | 1353 } |
1354 break; | |
1355 case 2: /* END */ | |
1356 terminate=1; | |
1357 break; | |
1358 case 27: /* USE */ | |
6 | 1359 // locsave = loccounter ; |
0 | 1360 case 16: /* INCLUDE */ |
1361 skipspace(); | |
1362 if(*srcptr=='"')srcptr++; | |
1363 i = 0; | |
1364 for(i=0; !(srcptr[i]==0||srcptr[i]=='"'); i++); | |
1365 int len = i; | |
1366 fname = calloc(1,len); | |
1367 for(i=0;i<len;i++) { | |
1368 if(*srcptr==0||*srcptr=='"')break; | |
1369 fname[i]=*srcptr++; | |
1370 } | |
1371 fname[i]=0; | |
1372 processfile(fname); | |
1373 codeptr=0; | |
1374 srcline[0]=0; | |
3 | 1375 // if (co==27) loccounter = locsave; |
0 | 1376 break; |
1377 case 24: /* MOD */ | |
6 | 1378 oldlc = loccounter = 0; |
0 | 1379 setlabel(lp); |
1380 os9begin(); | |
1381 break; | |
1382 case 25: /* EMOD */ | |
1383 os9end(); | |
1384 break; | |
1385 case 32: /* OS9 */ | |
6 | 1386 generate(); |
0 | 1387 setlabel(lp); |
3 | 1388 putword(0x103f); // SWI2 |
0 | 1389 putbyte(scanexpr(0)); |
15 | 1390 if(unknown&&pass==2)error|=4; |
0 | 1391 break; |
1392 case 18: /* TTL */ | |
1393 break; | |
1394 case 19: /* OPT */ | |
1395 case 26: /* NAM */ | |
1396 case 20: /* PAG */ | |
1397 case 3: /* ENDIF/ENDC */ | |
1398 break; | |
1399 } | |
1400 } | |
1401 | |
1402 | |
1403 void | |
1404 processline() | |
1405 { | |
1406 struct symrecord * lp; | |
1407 struct oprecord * op; | |
1408 int co; | |
1409 char c; | |
1410 srcptr=srcline; | |
1411 oldlc=loccounter; | |
5 | 1412 // error=0; |
0 | 1413 unknown=0;certain=1; |
1414 lp=0; | |
1415 codeptr=0; | |
1416 if(isalnum(*srcptr)) { | |
1417 scanname();lp=findsym(namebuf); | |
1418 if(*srcptr==':') srcptr++; | |
26 | 1419 if(lp && pass==2) { |
1420 oldlc = lp->value; | |
1421 } | |
0 | 1422 } |
1423 skipspace(); | |
1424 if(isalnum(*srcptr)) { | |
1425 scanname(); | |
1426 op=findop(namebuf); | |
1427 if(op) { | |
1428 if(op->cat!=13){ | |
6 | 1429 generate(); |
0 | 1430 setlabel(lp); |
1431 } | |
1432 co=op->code; | |
1433 switch(op->cat) { | |
1434 case 0:onebyte(co);break; | |
1435 case 1:twobyte(co);break; | |
1436 case 2:oneimm(co);break; | |
1437 case 3:lea(co);break; | |
1438 case 4:sbranch(co);break; | |
1439 case 5:lbranch(co);break; | |
1440 case 6:lbra(co);break; | |
1441 case 7:arith(co);break; | |
1442 case 8:darith(co);break; | |
1443 case 9:d2arith(co);break; | |
1444 case 10:oneaddr(co);break; | |
1445 case 11:tfrexg(co);break; | |
1446 case 12:pshpul(co);break; | |
1447 case 13:pseudoop(co,lp); | |
1448 } | |
1449 c=*srcptr; | |
1450 if (debug) fprintf(stderr,"DEBUG: processline: mode=%d, opsize=%d, error=%d, postbyte=%02X c=%c\n",mode,opsize,error,postbyte,c); | |
1451 if(c!=' '&&*(srcptr-1)!=' '&&c!=0&&c!=';')error|=2; | |
1452 } | |
1453 else error|=0x8000; | |
26 | 1454 } else { |
33 | 1455 if (lp) { |
1456 lp->next = prevlp; | |
1457 prevlp = lp; // os9 mode label can be data or code | |
1458 } | |
26 | 1459 } |
0 | 1460 if(pass==2) { |
1461 outbuffer(); | |
1462 if(listing)outlist(); | |
1463 } | |
1464 if(error)report(); | |
1465 loccounter+=codeptr; | |
25 | 1466 if (os9 && prevloc==0 ) { |
1467 prevloc = loccounter+1; | |
1468 oldlc = loccounter = 0; | |
1469 } | |
0 | 1470 } |
1471 | |
1472 void | |
1473 suppressline() | |
1474 { | |
1475 struct oprecord * op; | |
1476 srcptr=srcline; | |
1477 oldlc=loccounter; | |
1478 codeptr=0; | |
1479 if(isalnum(*srcptr)) { | |
1480 scanname(); | |
1481 if(*srcptr==':')srcptr++; | |
1482 } | |
1483 skipspace(); | |
1484 scanname();op=findop(namebuf); | |
1485 if(op && op->cat==13) { | |
33 | 1486 if(op->code==10||op->code==13||op->code==29||op->code==28||op->code==21||op->code==30||op->code==31||op->code==33) ifcount++; |
0 | 1487 else if(op->code==3) { |
1488 if(ifcount>0)ifcount--;else if(suppress==1|suppress==2)suppress=0; | |
1489 } else if(op->code==1) { | |
1490 if(ifcount==0 && suppress==2)suppress=0; | |
1491 } | |
1492 } | |
6 | 1493 if(pass==2&&listing)outlist(); } |
0 | 1494 |
1495 void | |
1496 usage(char*nm) | |
1497 { | |
1498 fprintf(stderr,"Usage: %s [-o objname] [-l listname] [-s srecord-file] srcname\n",nm); | |
1499 exit(2); | |
1500 } | |
1501 | |
1502 char * | |
1503 strconcat(char *s,int spos,char *d) | |
1504 { | |
1505 int slen = strlen(s); | |
1506 int dlen = strlen(d); | |
1507 if ( spos == 0) spos = slen; | |
1508 char *out = calloc(1,spos+dlen+1); | |
1509 int i = 0; | |
1510 for(; i< spos; i++ ) out[i] = s[i]; | |
1511 for(; i< spos+dlen+1; i++ ) out[i] = *d++; | |
1512 return out; | |
1513 } | |
1514 | |
1515 | |
1516 void | |
1517 getoptions(int c,char*v[]) | |
1518 { | |
6 | 1519 int i=1; |
0 | 1520 if(c==1)usage(v[0]); |
6 | 1521 while(v[i]) { |
1522 if(strcmp(v[i],"-d")==0) { | |
1523 debug=1; | |
1524 i++; | |
1525 } else if(strcmp(v[i],"-o")==0) { | |
1526 objname = v[i+1]; | |
1527 i+=2; | |
1528 } else if(strcmp(v[i],"-s")==0) { | |
1529 objname=v[i+1]; | |
1530 outmode=1; | |
1531 i+=2; | |
1532 } else if(strcmp(v[i],"-l")==0) { | |
1533 listname=v[i+1]; | |
1534 i+=2; | |
33 | 1535 } else if(strcmp(v[i],"-I")==0) { |
1536 struct incl *j = (struct incl *)malloc(sizeof(struct incl)); | |
1537 j->name = v[i+1]; | |
1538 j->next = 0; | |
1539 if (!incls) incls = j; | |
1540 else { | |
1541 struct incl *k=incls ; | |
1542 for(; k->next ; k = k->next ) ; | |
1543 k->next = j; | |
1544 } | |
1545 i+=2; | |
6 | 1546 } else if(*v[i]=='-') { |
1547 usage(v[0]); | |
1548 } else { | |
1549 if (srcname) usage(v[0]); | |
1550 srcname=v[i]; | |
1551 i++; | |
1552 } | |
0 | 1553 } |
1554 if(objname==0) { | |
1555 for(i=0;srcname[i]!='.' && srcname[i]!=0 ;i++) ; | |
1556 objname = strconcat(srcname,i,".b"); | |
1557 } | |
1558 listing=(listname!=0); | |
1559 } | |
1560 | |
1561 void | |
1562 expandline() | |
1563 { | |
1564 int i=0,j=0,k,j1; | |
1565 for(i=0;i<128&&j<128;i++) | |
1566 { | |
1567 if(inpline[i]=='\n') { | |
1568 srcline[j]=0;break; | |
1569 } | |
1570 if(inpline[i]=='\t') { | |
1571 j1=j; | |
1572 for(k=0;k<8-j1%8 && j<128;k++)srcline[j++]=' '; | |
1573 }else srcline[j++]=inpline[i]; | |
1574 } | |
1575 srcline[127]=0; | |
1576 } | |
1577 | |
1578 | |
1579 void | |
1580 processfile(char *name) | |
1581 { | |
1582 char *oldname; | |
1583 int oldno; | |
1584 FILE *srcfile; | |
1585 oldname=curname; | |
1586 curname=name; | |
1587 oldno=lineno; | |
1588 lineno=0; | |
1589 if((srcfile=fopen(name,"r"))==0) { | |
6 | 1590 int i = 0; |
1591 if (oldname) { | |
1592 i = strlen(oldname); | |
1593 while(i>0 && oldname[i]!='/') i--; | |
1594 } | |
0 | 1595 if (i>0) { |
1596 char *next = strconcat(oldname,i+1,name); | |
33 | 1597 if((srcfile=fopen(next,"r"))!=0) { |
1598 curname = next; | |
0 | 1599 } |
33 | 1600 } |
1601 if (!srcfile) { | |
1602 for( struct incl *d = incls; d ; d = d->next) { | |
1603 char *next = strconcat(d->name,0,name); | |
1604 if((srcfile=fopen(next,"r"))!=0) { | |
1605 curname = next; | |
1606 break; | |
1607 } | |
1608 } | |
1609 } | |
1610 } | |
1611 if (!srcfile) { | |
0 | 1612 fprintf(stderr,"Cannot open source file %s\n",name); |
1613 exit(4); | |
1614 } | |
1615 while(!terminate&&fgets(inpline,128,srcfile)) { | |
1616 expandline(); | |
33 | 1617 lineno++; glineno++; |
0 | 1618 srcptr=srcline; |
1619 if(suppress)suppressline(); else processline(); | |
1620 } | |
26 | 1621 setlabel(0); // process prevlp |
0 | 1622 fclose(srcfile); |
1623 if(suppress) { | |
1624 fprintf(stderr,"improperly nested IF statements in %s",curname); | |
1625 errors++; | |
1626 suppress=0; | |
1627 } | |
1628 lineno=oldno; | |
1629 curname=oldname; | |
1630 } | |
1631 | |
1632 int | |
1633 main(int argc,char *argv[]) | |
1634 { | |
1635 char c; | |
1636 getoptions(argc,argv); | |
1637 pass=1; | |
1638 errors=0; | |
1639 generating=0; | |
1640 terminate=0; | |
1641 processfile(srcname); | |
1642 if(errors) { | |
1643 fprintf(stderr,"%d Pass 1 Errors, Continue?",errors); | |
1644 c=getchar(); | |
1645 if(c=='n'||c=='N') exit(3); | |
1646 } | |
33 | 1647 do { |
0 | 1648 pass=2; |
6 | 1649 prevloc = 0; |
0 | 1650 loccounter=0; |
1651 errors=0; | |
1652 generating=0; | |
1653 terminate=0; | |
33 | 1654 glineno=0; |
0 | 1655 if(listing&&((listfile=fopen(listname,"w"))==0)) { |
1656 fprintf(stderr,"Cannot open list file"); | |
1657 exit(4); | |
1658 } | |
1659 if((objfile=fopen(objname,outmode?"w":"wb"))==0) { | |
1660 fprintf(stderr,"Cannot write object file\n"); | |
1661 exit(4); | |
1662 } | |
1663 processfile(srcname); | |
33 | 1664 } while (longer()); |
0 | 1665 fprintf(stderr,"%d Pass 2 errors.\n",errors); |
1666 if(listing) { | |
1667 fprintf(listfile,"%d Pass 2 errors.\n",errors); | |
1668 outsymtable(); | |
1669 fclose(listfile); | |
1670 } | |
1671 if(outmode){ | |
1672 flushhex(); | |
1673 fprintf(objfile,"S9030000FC\n"); | |
1674 } | |
1675 fclose(objfile); | |
1676 return 0; | |
1677 } | |
1678 |