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