1 /* MAIN ASSEMBLING FUNCTIONS */
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include <stdio.h>
6
7 #include "regs.h"
8 #include "compile.h"
9 #include "hash.h"
10 #include "expression.h"
11 #include "execute_token"
12 #include "z80-cpu.h"
13 #include "instr.h"
14 #include "asm_interface.h"
15 #include "asm_token"
16
17 #define MAX_TEXT 1024
18
19 #define TAB '\t'
20 #define SPACE ' '
21 #define COLON ':'
22 #define QUOTE '"'
23 #define SINGLE '\''
24
25 bool LISTING=0;
26 bool WARNINGS=0;
27
28 bool disable_pseudo=1;
29 static unsigned short end; /* undefined as long as count=0 */
30
31 static unsigned lineno, count, cond_level, last_true_cond_level;
32 static bool label_access, expect_defl;
33
34 struct argument_type
35 {
36 unsigned char type;
37 int value;
38 char *text; /* this item contains defm string or equ expression */
39 bool is_label; /* indicates if number was from label for JR & DJNZ */
40 bool is_resolved; /* indicates whether value is well defined */
41 struct argument_type *next,*previous;
42 };
43
44 struct lex_type
45 {
46 int instruction;
47 struct argument_type *arg;
48 }lex;
49
50 /* pruchod=1: parse label definitions, pruchod=2: evaluate labels in operands */
51 static int pruchod=0; /* one line compilation, single pass, no labels permitted */
52 static unsigned short address, last;
53 static unsigned char fill_byte;
54
55
56 #ifdef UNIX
57
58 char *
strupr(char * txt)59 strupr (char *txt)
60 {
61 char *p;
62 for (p=txt;*p;p++)
63 *p=toupper(*p);
64 return txt;
65 }
66
67 #endif /* UNIX */
68
69
70 void
asm_close(void)71 asm_close(void)
72 {
73 free_hash_table();
74 }
75
76
77 void
asm_init(unsigned char fill)78 asm_init(unsigned char fill)
79 {
80 hash_table_init();
81 count=0;
82 address=0;
83 fill_byte= fill;
84 /* funckin' DJGPP doesn't initialize global variables in modules */
85 /* with zero so variable end contains garbage in programs using */
86 /* asm.a library */
87 }
88
89
90 /* binary finds txt in seznam */
91
92 static int
convert(const struct seznam_type * seznam,int seznam_size,char * txt)93 convert(const struct seznam_type *seznam,int seznam_size,char *txt)
94 {
95 int a,b,begin,end;
96 begin=0;end=seznam_size-1;
97
98 if (!(*txt))return 0;
99 while (1)
100 {
101 a=((end-begin)>>1)+begin;
102 if (!(b=strcmp(seznam[a].name,txt)))return seznam[a].code;
103 if (b>0)end=a-1;
104 if (b<0)begin=a+1;
105 if (end<begin)return -1;
106 }
107 }
108
109
110 static int
is_indirect(char * txt)111 is_indirect(char *txt)
112 {
113 int a, b= txt[0]=='(';
114 if (!b) return 0;
115 for (a=1;txt[a];a++)
116 if (!b) break;
117 else if (txt[a]=='(') b++;
118 else if (txt[a]==')') b--;
119 return !txt[a];
120 }
121
122
123 static int
convert_arg(struct argument_type * arg,char * txt)124 convert_arg(struct argument_type *arg,char *txt)
125 {
126 int a,l=strlen(txt);
127
128 arg->is_label=0;
129 arg->is_resolved=1;
130 arg->text=0;
131
132 /* nothing */
133 if (!*txt){arg->type=A_EMPTY;arg->value=0;return NIL;}
134
135 /* check for string (number of quotes must already be even) */
136 if ((*txt)==QUOTE)
137 {
138 arg->type=A_STRING;
139 arg->text=malloc(l-1);
140 if(!arg->text) return IEM;
141 for (l=0,a=1;txt[a];a++)
142 {
143 if (txt[a]==QUOTE)
144 { a++;
145 if (txt[a]!=QUOTE)
146 break;
147 }
148 arg->text[l++]=txt[a];
149 }
150 arg->text[l]=0;
151 if (txt[a]) return UNQ;
152 return NIL;
153 }
154
155 if (*txt != SINGLE) strupr(txt); /* convert all letters to upper case */
156 else if (!txt[1] || txt[2]!=SINGLE)
157 return UNS;
158
159 /* check for register */
160 a=convert(reg,N_REGISTERS,txt);
161 if (a!=-1){arg->type=A_REG;arg->value=a;return NIL;}
162
163 /* check for flag */
164 a=convert(flag,N_FLAGS,txt);
165 if (a!=-1){arg->type=A_FLAG;arg->value=a;return NIL;}
166
167 if (txt[0]=='(' && txt[l-1]==')' && is_indirect(txt))
168 {
169 txt=memmove(txt,txt+1,strlen(txt));
170 txt[strlen(txt)-1]=0;
171
172 /* (check for register) */
173 arg->value=convert(reg,N_REGISTERS,txt);
174 if (arg->value!=-1){arg->type=A_PODLE_REG;return NIL;}
175
176 if (txt[0]=='I' && (txt[1]=='Y'||txt[1]=='X') && (txt[2]=='+'||txt[2]=='-'))
177 { /* (IX+num) or (IY+num) or (IX-num) or (IY-num) */
178 arg->type= (txt[1]=='Y' ? A_PODLE_IY_PLUS : A_PODLE_IX_PLUS);
179 if (test_number(txt+2,&a))
180 arg->value=a;
181 else
182 { a=parse_expr(txt+2,&arg->value,lineno);
183 arg->is_resolved= !a;
184 if (a < 0) return -a;
185 if (a > 0 && pruchod!=1) return LBNO;
186 arg->is_label=1;
187 }
188 return NIL;
189 }
190 if (!txt[test_number(txt,&a)])
191 arg->value=a;
192 else
193 { a=parse_expr(txt,&arg->value,lineno);
194 arg->is_resolved= !a;
195 if (a < 0) return -a;
196 if (a > 0 && pruchod!=1) return LBNO;
197 arg->is_label=1;
198 }
199 arg->type=A_PODLE_NUM;
200 return NIL;
201 }
202 if (!txt[test_number(txt,&a)])
203 arg->value=a;
204 else
205 { a=parse_expr(txt,&arg->value,lineno);
206 arg->is_resolved= !a;
207 if (a < 0) return -a;
208 if (a > 0 && pruchod!=1) return LBNO;
209 if (a > 0) arg->value=address; /* value needed for JR/DJNZ/EQU/DEFL/DEFS/ORG */
210 arg->is_label=1;
211 }
212 arg->type=A_NUM;
213 return NIL;
214 }
215
216
217 /* converts line into structure lex */
218 /* return value:
219 0=O.K.
220 1=memory allocation error
221 2=unknown label
222 3=label previously defined
223 4=invalid label name
224 5=illegal label declaration
225 6=illegal operator
226 7=unbalanced quotes
227 8=missing opening parenthesis
228 9=missing closing parenthesis
229 10=unbalanced single quotes
230 11=token following string
231 12=token following pharanthesis
232 13=invalid character argument
233 14=syntax error in expression
234 15=defl expected
235 16=expression not pass 1 evaluable
236 17=missing label
237 18=missing operand
238 */
239
240 static int
lexical_analysis(const char * line)241 lexical_analysis(const char *line)
242 {
243 char txt1[MAX_TEXT];
244 const char *p;
245 char *b=NULL;
246 struct argument_type *a,*c;
247 int ret;
248
249 lex.instruction=I_EMPTY;
250 lex.arg=NULL;
251 a=NULL;c=NULL;
252
253 if ((*line)==';')return 0; /* ignore comment */
254
255 /* LABEL */
256
257 label_access= 0;
258 expect_defl=0;
259 if (!pruchod)
260 {
261 for (p=line;(*p)!=SPACE&&(*p)!=TAB&&(*p);p++) /* skip label */
262 if (!pruchod && *p > SPACE) return ILD;
263 if (!(*p))return 0;
264 }
265 else
266 {
267 for (p=line,b=txt1;(*p)!=SPACE&&(*p)!=TAB&&(*p);b++,p++)
268 *b=toupper(*p);
269 if (p!=line && cond_level == last_true_cond_level)
270 {
271 if (*(b-1)==COLON && b-1 != txt1) b--; /* delete one trailing colon */
272 *b=0;
273 if (b==txt1 || test_label(txt1) != b-txt1) return ILB;
274 if (is_in_table(txt1,0,0,0))
275 { if (pruchod==1)
276 { if (last_label_reusable())
277 {
278 if (!(label_access=add_to_table(txt1,address,lineno,1))) return IEM;
279 expect_defl=1;
280 }
281 else
282 return LBAR;
283 }
284 else
285 label_access=reaccess_label(txt1,lineno);
286 }
287 else
288 { if (pruchod==2) return MISL;
289 if (!(label_access=add_to_table(txt1,address,lineno,1))) return IEM;
290 }
291 }
292 }
293
294 /* SPACE */
295
296 while((*p)==TAB||(*p)==SPACE) /* skip spaces */
297 p++;
298 if ((*p)==';')return 0;
299 if (!(*p))return 0;
300
301 /* INSTRUCTION */
302
303 for (b=txt1;((*p)!=SPACE&&(*p)!=TAB&&(*p));p++,b++)
304 *b=toupper(*p);
305 *b=0;
306 lex.instruction=convert(instruction,N_INSTRUCTIONS,txt1);
307 if (expect_defl && lex.instruction != I_DEFL)
308 return DEX;
309
310 /* SPACE */
311
312 while((*p)==TAB||(*p)==SPACE)
313 p++;
314 if (!(*p) || (*p)==';')
315 return 0;
316
317 a=(struct argument_type *)malloc(sizeof(struct argument_type));
318 if (!a) return IEM;
319 a->next=NULL;
320 c=a;
321 if (cond_level > last_true_cond_level){lex.arg=a->next;free(c);return 0;}
322
323 /* ARGUMENTS */
324
325 while(1)
326 {
327 int single=0, quotes=0, parent=0;
328 /* arguments are separated by ' ' or ','; separators in quotes are ignored */
329 /* we stop reading on terminating null character */
330 for (b=txt1; (((*p)!=SPACE&&(*p)!=TAB&&(*p!=','))||(quotes&1)||single) && (*p);
331 p++,b++)
332 {
333 if (*p==QUOTE && !single) quotes++;
334 else if (quotes&1)
335 ;
336 else if (b==txt1 && *p==SINGLE)
337 single=1;
338 else if (single && *p==SINGLE)
339 single=0;
340 else if (*p=='(') parent++;
341 else if (*p==')') parent--;
342 if (parent < 0) return MOP;
343 *b=*p;
344 }
345 *b=0;
346 if (quotes&1) return UNQ;
347 if (single) return UNS;
348 if (parent > 0) return MCP;
349 a->next=(struct argument_type *)malloc(sizeof(struct argument_type));
350 if (!a->next){free(a);return IEM;}
351 a->next->is_label=0;
352 a->next->value=0;
353 a->next->type=A_EMPTY;
354 a->next->next=NULL;
355 a->next->previous=(a==c)?NULL:a;
356 a=a->next;
357 if ((ret=convert_arg(a,txt1))) {free (c); return ret;}
358 if (a->is_label && pruchod==1)
359 if (lex.instruction == I_ORG || lex.instruction == I_DEFS ||
360 lex.instruction == I_COND || lex.instruction == I_ALIGN)
361 { if (!a->is_resolved)
362 return EP1;
363 }
364 else if (lex.instruction == I_EQU || lex.instruction == I_DEFL)
365 { if (!a->is_resolved)
366 if (!(a->text=resolve_current_pc_and_store(txt1)))
367 return IEM;
368 }
369 else
370 /** if (a->type == A_PODLE_IY_PLUS || a->type == A_PODLE_IX_PLUS ||
371 c_adc_sbc || c_add || c_logical || c_in || c_out || c_ld) **/
372 a->value= 0; /* avoids 8 bit overflows during pass 1 */
373 while((*p)==TAB||(*p)==SPACE||(*p)==',')
374 p++;
375 if (!(*p)||(*p)==';'){lex.arg=c->next;free(c);return 0;}
376 }
377 }
378
379
380 void
out(unsigned char a)381 out(unsigned char a)
382 {
383 if (pruchod==1){address++;return;}
384
385 if (LISTING && pruchod==2) printf(" %02x",(unsigned)a);
386 if (write_to_memory(address,a) != fill_byte && WARNINGS)
387 fprintf(stderr,"Warning: overwriting code at address 0x%04x (%05u)\n",
388 (unsigned)address,(unsigned)address);
389
390 if (!count || address>end) end=address;
391 address++;
392 count++;
393 }
394
395
compile_pass(void)396 unsigned compile_pass(void)
397 {
398 return pruchod;
399 }
400
set_compile_pass(unsigned pass)401 void set_compile_pass(unsigned pass)
402 {
403 pruchod=pass;
404 lineno=(pass?1:0);
405 last_true_cond_level= cond_level=0;
406 }
407
generated_bytes(void)408 int generated_bytes(void)
409 {
410 return count;
411 }
412
highest_address(void)413 unsigned short highest_address(void)
414 {
415 return end;
416 }
417
get_current_address(void)418 unsigned short get_current_address(void)
419 {
420 return address;
421 }
422
set_start_address(unsigned short addr)423 void set_start_address(unsigned short addr)
424 {
425 address=addr;
426 }
427
428 /*** see instr_token for the correct order ***/
429 static unsigned char no_para[] =
430 { 0 ,
431 1<<2, 1<<0, 1<<0, 1<<0, 1<<0,
432 1<<2, 1<<0, 1<<1, 1<<1,
433 1<<1, 1<<1,
434 1<<2, 1<<2, 1<<2,
435 1<<1, 1<<1, 1<<1, 1<<1, 1<<1,
436 1<<0, 1<<0, 1<<0, 1<<0,
437 1<<1|1<<2, 1<<1, 1<<1|1<<2,
438 1<<1|1<<2, 1<<1, 1<<0|1<<1, 1<<0, 1<<0,
439 1<<1, 1<<0, 1<<1, 1<<0, 1<<1, 1<<0, 1<<1, 1<<0,
440 1<<1, 1<<1, 1<<1, 1<<1,
441 1<<0, 1<<0, 1<<0,
442 1<<2, 1<<2, 1<<2,
443 1<<0, 1<<0, 1<<0, 1<<0,
444 1<<2, 1<<0, 1<<0, 1<<0, 1<<0,
445 1<<2, 1<<0, 1<<0, 1<<0, 1<<0,
446 1<<0, 1<<0, 1<<0, 1<<0, 1<<1,
447
448 1<<0, 1<<1, 1<<1, 1<<1, ~0^1, ~0^1, 1<<1,
449 1<<1, 1<<1,
450 1<<1, 1<<0,
451 } ;
452
453 /*** see asm_token for the correct order ***/
454 static char *msg[] = { "",
455 /* IEM */ "Can't allocate sufficient memory.",
456 /* LBNO */ "Label not defined.",
457 /* LBAR */ "Label already declared.",
458 /* ILB */ "Invalid Labelname.",
459 /* ILD */ "Illegal Labeldeclaration.",
460 /* ILO */ "Illegal Operator.",
461 /* UNQ */ "Unbalanced Quotes.",
462 /* MOP */ "Missing Opening Parenthesis.",
463 /* MCP */ "Missing Closing Parenthesis.",
464 /* UNS */ "Unbalanced Single Quote.",
465 /* STRT */ "String appended by text (missing quotes?).",
466 /* PART */ "Parenthesis appended by text (missing parentheses?).",
467 /* IAC */ "Invalid character argument.",
468 /* SYNT */ "Syntax error in expression.",
469 /* DEX */ "DEFL statement expected.",
470 /* EP1 */ "Expression can't be evaluated in Pass 1.",
471 /* MISL */ "Missing Label.",
472 /* MISO */ "Missing Operand.",
473 /* FOR */ "Forbidden instruction.",
474 /* ILL */ "Illegal instruction.",
475 /* UNK */ "Unknown instruction.",
476 /* MIS1 */ "Missing argument.",
477 /* MIS */ "Missing arguments.",
478 /* MIS2 */ "Missing second argument.",
479 /* EXT */ "Extra argument.",
480 /* TOO */ "Too many arguments.",
481 /* VOR */ "Unsigned Value out of range.",
482 /* OOR */ "Signed Offset out of range.",
483 /* IA */ "Invalid argument.",
484 /* IA1 */ "Invalid first argument.",
485 /* IA2 */ "Invalid second argument.",
486 /* IAT */ "Invalid argument type.",
487 /* IAS */ "Invalid argument. String expected.",
488 /* IAN */ "Invalid argument. Number expected.",
489 /* IAH */ "Invalid argument. One Hexadigit expected.",
490 /* IAV */ "Invalid argument value.",
491 /* TCC */ "ENDC statement without COND statement.",
492 /* MEC */ "missing ENDC statement(s) at end of source."
493 } ;
494
495
496 unsigned
check_cond_nesting(void)497 check_cond_nesting(void)
498 {
499 if (cond_level)
500 error(lineno,"",msg[MEC]);
501 return cond_level ? MEC : 0;
502 }
503
504
505 unsigned
compile(const char * txt)506 compile(const char *txt)
507 /* return value: 0=OK, 1-18=errors */
508 {
509 int a;
510 struct argument_type *t;
511 int ret=0;
512 a=lexical_analysis(txt);
513 if (a >= 1 && a <= 18)
514 {
515 error(lineno,txt,msg[a]);
516 ret=1;
517 return ret;
518 }
519 else if (a)
520 {
521 fprintf(stderr,"%d ",a);
522 error(lineno,txt,"internal error");
523 ret=1;
524 return ret;
525 }
526
527 if (!ret)
528 { if (LISTING && pruchod==2)
529 printf("%04x",last=address);
530 if (cond_level > last_true_cond_level)
531 { if (lex.instruction != I_ENDC && lex.instruction != I_COND)
532 lex.instruction=I_EMPTY; /* we ignore also I_ILLEGAL opcodes */
533 }
534 if (!lex.arg) a=0;
535 else if (!lex.arg->next) a=1;
536 else if (!lex.arg->next->next) a=2;
537 else a=3;
538 if (lex.instruction >= I_END && lex.instruction < N_INSTRUCTIONS &&
539 disable_pseudo)
540 { error(lineno,txt,msg[FOR]);ret=1; }
541 else if (lex.instruction >= 0 && lex.instruction < N_INSTRUCTIONS &&
542 no_para[lex.instruction] && !(no_para[lex.instruction]&1<<a))
543 { switch (a)
544 { case 0: if(no_para[lex.instruction]&2)
545 a=MIS1;
546 else
547 a=MIS;
548 break;
549 case 1: if(no_para[lex.instruction]&1)
550 a=EXT;
551 else if(no_para[lex.instruction]&4)
552 a=MIS2;
553 else
554 a=MIS;
555 break;
556 default: if (no_para[lex.instruction]&1)
557 a=EXT;
558 else if(!(no_para[lex.instruction]>>a))
559 a=TOO;
560 else
561 a=MIS;
562 break;
563 }
564 if (a) error(lineno,txt,msg[a]);ret=1;
565 }
566 }
567 if (!ret)
568 { a=0;
569 switch (lex.instruction)
570 {
571 case I_END:
572 if (disable_pseudo){error(lineno,txt,msg[FOR]);ret=1;break;}
573 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
574 ret=8;
575 break;
576
577 case I_EQU:
578 if (disable_pseudo){error(lineno,txt,msg[FOR]);ret=1;break;}
579 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
580 if (lex.arg->type!=A_NUM){error(lineno,txt,msg[IAT]);ret=1;break;}
581 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
582 if (!label_access){error(lineno,txt,msg[MISL]);ret=1;break;}
583 update_last_added_entry(lex.arg->value,lex.arg->text,1);
584 break;
585
586 case I_DEFL:
587 if (disable_pseudo){error(lineno,txt,msg[FOR]);ret=1;break;}
588 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
589 if (lex.arg->type!=A_NUM){error(lineno,txt,msg[IAT]);ret=1;break;}
590 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
591 if (!label_access){error(lineno,txt,msg[MISL]);ret=1;break;}
592 update_last_added_entry(lex.arg->value,lex.arg->text,0);
593 break;
594
595 case I_ORG:
596 if (disable_pseudo){error(lineno,txt,msg[FOR]);ret=1;break;}
597 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
598 if (lex.arg->type!=A_NUM){error(lineno,txt,msg[IAN]);ret=1;break;}
599 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
600 address=lex.arg->value;
601 last=address; /* we write no fill bytes into listing */
602 break;
603
604 case I_ILLEGAL:
605 error(lineno,txt,msg[ILL]);
606 ret=1;
607 break;
608
609 case I_EMPTY:
610 break;
611
612 case I_HALT:
613 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
614 else out(0x76);
615 break;
616
617 case I_CCF:
618 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
619 else out(0x3f);
620 break;
621
622 case I_CPD:
623 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
624 else {out(0xed);out(0xa9);}
625 break;
626
627 case I_CPDR:
628 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
629 else {out(0xed);out(0xb9);}
630 break;
631
632 case I_CPI:
633 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
634 else {out(0xed);out(0xa1);}
635 break;
636
637 case I_CPIR:
638 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
639 else {out(0xed);out(0xb1);}
640 break;
641
642 case I_CPL:
643 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
644 else out(0x2f);
645 break;
646
647 case I_DAA:
648 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
649 else out(0x27);
650 break;
651
652 case I_DI:
653 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
654 else out(0xf3);
655 break;
656
657 case I_EI:
658 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
659 else out(0xfb);
660 break;
661
662 case I_EXX:
663 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
664 else out(0xd9);
665 break;
666
667 case I_IND:
668 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
669 else {out(0xed);out(0xaa);}
670 break;
671
672 case I_INI:
673 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
674 else {out(0xed);out(0xa2);}
675 break;
676
677 case I_INDR:
678 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
679 else {out(0xed);out(0xba);}
680 break;
681
682 case I_INIR:
683 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
684 else {out(0xed);out(0xb2);}
685 break;
686
687 case I_LDD:
688 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
689 else {out(0xed);out(0xa8);}
690 break;
691
692 case I_LDI:
693 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
694 else {out(0xed);out(0xa0);}
695 break;
696
697 case I_LDDR:
698 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
699 else {out(0xed);out(0xb8);}
700 break;
701
702 case I_LDIR:
703 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
704 else {out(0xed);out(0xb0);}
705 break;
706
707 case I_NEG:
708 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
709 else {out(0xed);out(0x44);}
710 break;
711
712 case I_NOP:
713 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
714 else out(0x00);
715 break;
716
717 case I_OTDR:
718 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
719 else {out(0xed);out(0xbb);}
720 break;
721
722 case I_OTIR:
723 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
724 else {out(0xed);out(0xb3);}
725 break;
726
727 case I_OUTD:
728 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
729 else {out(0xed);out(0xab);}
730 break;
731
732 case I_OUTI:
733 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
734 else {out(0xed);out(0xa3);}
735 break;
736
737 case I_RETI:
738 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
739 else {out(0xed);out(0x4d);}
740 break;
741
742 case I_RETN:
743 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
744 else {out(0xed);out(0x45);}
745 break;
746
747 case I_RLA:
748 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
749 else out(0x17);
750 break;
751
752 case I_RLCA:
753 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
754 else out(0x07);
755 break;
756
757 case I_RLD:
758 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
759 else {out(0xed);out(0x6f);}
760 break;
761
762 case I_RRA:
763 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
764 else out(0x1f);
765 break;
766
767 case I_RRCA:
768 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
769 else out(0x0f);
770 break;
771
772 case I_RRD:
773 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
774 else {out(0xed);out(0x67);}
775 break;
776
777 case I_SCF:
778 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
779 else out(0x37);
780 break;
781
782 case I_ADC:
783 if (!lex.arg){error(lineno,txt,msg[MIS]);ret=1;break;}
784 if ((lex.arg->type!=A_REG)||(lex.arg->value!=R_A&&lex.arg->value!=R_HL))
785 {error(lineno,txt,msg[IA1]);ret=1;break;}
786 if (!lex.arg->next)
787 {error(lineno,txt,msg[MIS2]);ret=1;break;}
788 if (lex.arg->next->next)
789 {error(lineno,txt,msg[TOO]);ret=1;break;}
790 a=c_adc_sbc(lex.arg->value,lex.arg->next->type,lex.arg->next->value,0x88,0x0a);
791 if (a==1) {error(lineno,txt,msg[IA]);ret=1;break;}
792 break;
793
794 case I_ADD:
795 if (!lex.arg){error(lineno,txt,msg[MIS]);ret=1;break;}
796 if ((lex.arg->type!=A_REG)||(lex.arg->value!=R_A&&lex.arg->value!=R_HL&&lex.arg->value!=R_IX&&lex.arg->value!=R_IY))
797 {error(lineno,txt,msg[IA1]);ret=1;break;}
798 if (!lex.arg->next)
799 {error(lineno,txt,msg[MIS2]);ret=1;break;}
800 if (lex.arg->next->next)
801 {error(lineno,txt,msg[TOO]);ret=1;break;}
802 a=c_add(lex.arg->value,lex.arg->next->type,lex.arg->next->value);
803 if (a==1) {error(lineno,txt,msg[IA]);ret=1;break;}
804 break;
805
806 case I_AND:
807 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
808 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
809 a=c_logical(lex.arg->type,lex.arg->value,0xa0);
810 if (a==1) {error(lineno,txt,msg[IA]);ret=1;break;}
811 break;
812
813 case I_BIT:
814 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
815 if (lex.arg->type!=A_NUM||(lex.arg->type==A_NUM&&lex.arg->value>7))
816 {error(lineno,txt,msg[IA1]);ret=1;break;}
817 if (!lex.arg->next){error(lineno,txt,msg[MIS2]);ret=1;break;}
818 if (lex.arg->next->next)
819 {error(lineno,txt,msg[TOO]);ret=1;break;}
820 a=c_bit(lex.arg->value,lex.arg->next->type,lex.arg->next->value,0x40);
821 if (a==1){error(lineno,txt,msg[IA2]);ret=1;break;}
822 break;
823
824 case I_CP:
825 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
826 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
827 a=c_logical(lex.arg->type,lex.arg->value,0xb8);
828 if (a==1) {error(lineno,txt,msg[IA]);ret=1;break;}
829 break;
830
831 case I_DEC:
832 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
833 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
834 a=c_inc_dec(lex.arg->type,lex.arg->value,0x05,0x0b);
835 if (a==1){error(lineno,txt,msg[IA]);ret=1;break;}
836 break;
837
838 case I_EX:
839 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
840 if (!lex.arg->next){error(lineno,txt,msg[MIS2]);ret=1;break;}
841 if (lex.arg->next->next){error(lineno,txt,msg[TOO]);ret=1;break;}
842 if (lex.arg->next->type!=A_REG){error(lineno,txt,msg[IA2]);ret=1;break;}
843 if (c_ex(lex.arg->type,lex.arg->value,lex.arg->next->value))
844 {error(lineno,txt,msg[IA]);ret=1;}
845 break;
846
847 case I_IM:
848 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
849 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
850 if (lex.arg->type!=A_NUM||(lex.arg->type==A_NUM&&lex.arg->value>2))
851 {error(lineno,txt,msg[IA]);ret=1;break;}
852 c_im(lex.arg->value);
853 break;
854
855 case I_IN:
856 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
857 if (!lex.arg->next){error(lineno,txt,msg[MIS2]);ret=1;break;}
858 if (lex.arg->next->next){error(lineno,txt,msg[TOO]);ret=1;break;}
859 if (lex.arg->type!=A_REG){error(lineno,txt,msg[IA1]);ret=1;break;}
860 a=c_in(lex.arg->value,lex.arg->next->type,lex.arg->next->value);
861 if (a==1) {error(lineno,txt,msg[IA]);ret=1;break;}
862 break;
863
864 case I_INC:
865 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
866 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
867 a=c_inc_dec(lex.arg->type,lex.arg->value,0x04,0x03);
868 if (a==1){error(lineno,txt,msg[IA]);ret=1;break;}
869 break;
870
871 case I_OR:
872 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
873 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
874 a=c_logical(lex.arg->type,lex.arg->value,0xb0);
875 if (a==1) {error(lineno,txt,msg[IA]);ret=1;break;}
876 break;
877
878 case I_OUT:
879 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
880 if (!lex.arg->next){error(lineno,txt,msg[MIS2]);ret=1;break;}
881 if (lex.arg->next->next){error(lineno,txt,msg[TOO]);ret=1;break;}
882 if (lex.arg->next->type!=A_REG){error(lineno,txt,msg[IA2]);ret=1;break;}
883 a=c_out(lex.arg->type,lex.arg->value,lex.arg->next->value);
884 if (a==1) {error(lineno,txt,msg[IA]);ret=1;break;}
885 break;
886
887 case I_POP:
888 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
889 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
890 if (lex.arg->type!=A_REG){error(lineno,txt,msg[IA]);ret=1;break;}
891 if (c_push_pop(lex.arg->value,0x01))
892 {error(lineno,txt,msg[IA]);ret=1;}
893 break;
894
895 case I_PUSH:
896 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
897 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
898 if (lex.arg->type!=A_REG){error(lineno,txt,msg[IA]);ret=1;break;}
899 if (c_push_pop(lex.arg->value,0x05))
900 {error(lineno,txt,msg[IA]);ret=1;}
901 break;
902
903 case I_RES:
904 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
905 if (lex.arg->type!=A_NUM||(lex.arg->type==A_NUM&&lex.arg->value>7))
906 {error(lineno,txt,msg[IA1]);ret=1;break;}
907 if (!lex.arg->next){error(lineno,txt,msg[MIS2]);ret=1;break;}
908 if (lex.arg->next->next)
909 {error(lineno,txt,msg[TOO]);ret=1;break;}
910 a=c_bit(lex.arg->value,lex.arg->next->type,lex.arg->next->value,0x80);
911 if (a==1) {error(lineno,txt,msg[IA2]);ret=1;break;}
912 break;
913
914 case I_RET:
915 if (!lex.arg){out(0xc9);break;}
916 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
917 if (lex.arg->type==A_REG)
918 {
919 if (lex.arg->value!=R_C){error(lineno,txt,msg[IA]);ret=1;break;}
920 }
921 else {if (lex.arg->type!=A_FLAG){error(lineno,txt,msg[IA]);ret=1;break;}}
922 if (c_ret(lex.arg->value))
923 {error(lineno,txt,msg[IA]);ret=1;}
924 break;
925
926 case I_RL:
927 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
928 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
929 a=c_shift_rot(lex.arg->type,lex.arg->value,0x10);
930 if (a==1){error(lineno,txt,msg[IA]);ret=1;break;}
931 break;
932
933 case I_RLC:
934 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
935 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
936 a=c_shift_rot(lex.arg->type,lex.arg->value,0x00);
937 if (a==1){error(lineno,txt,msg[IA]);ret=1;break;}
938 break;
939
940 case I_RR:
941 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
942 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
943 a=c_shift_rot(lex.arg->type,lex.arg->value,0x18);
944 if (a==1){error(lineno,txt,msg[IA]);ret=1;break;}
945 break;
946
947 case I_RRC:
948 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
949 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
950 a=c_shift_rot(lex.arg->type,lex.arg->value,0x08);
951 if (a==1){error(lineno,txt,msg[IA]);ret=1;break;}
952 break;
953
954 case I_RST:
955 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
956 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
957 if (lex.arg->type!=A_NUM){error(lineno,txt,msg[IAT]);ret=1;break;}
958 if (c_rst(lex.arg->value))
959 {error(lineno,txt,msg[IAV]);ret=1;}
960 break;
961
962 case I_SBC:
963 if (!lex.arg){error(lineno,txt,msg[MIS]);ret=1;break;}
964 if ((lex.arg->type!=A_REG)||(lex.arg->value!=R_A&&lex.arg->value!=R_HL))
965 {error(lineno,txt,msg[IA1]);ret=1;break;}
966 if (!lex.arg->next)
967 {error(lineno,txt,msg[MIS2]);ret=1;break;}
968 if (lex.arg->next->next)
969 {error(lineno,txt,msg[TOO]);ret=1;break;}
970 a=c_adc_sbc(lex.arg->value,lex.arg->next->type,lex.arg->next->value,0x98,0x02);
971 if (a==1) {error(lineno,txt,msg[IA]);ret=1;break;}
972 break;
973
974 case I_SET:
975 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
976 if (lex.arg->type!=A_NUM||(lex.arg->type==A_NUM&&lex.arg->value>7))
977 {error(lineno,txt,msg[IA1]);ret=1;break;}
978 if (!lex.arg->next){error(lineno,txt,msg[MIS2]);ret=1;break;}
979 if (lex.arg->next->next)
980 {error(lineno,txt,msg[TOO]);ret=1;break;}
981 a=c_bit(lex.arg->value,lex.arg->next->type,lex.arg->next->value,0xc0);
982 if (a==1) {error(lineno,txt,msg[IA2]);ret=1;}
983 break;
984
985 case I_SLA:
986 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
987 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
988 a=c_shift_rot(lex.arg->type,lex.arg->value,0x20);
989 if (a==1){error(lineno,txt,msg[IA]);ret=1;break;}
990 break;
991
992 case I_SLL:
993 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
994 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
995 a=c_shift_rot(lex.arg->type,lex.arg->value,0x30);
996 if (a==1){error(lineno,txt,msg[IA]);ret=1;break;}
997 break;
998
999 case I_SRA:
1000 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1001 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
1002 a=c_shift_rot(lex.arg->type,lex.arg->value,0x28);
1003 if (a==1){error(lineno,txt,msg[IA]);ret=1;break;}
1004 break;
1005
1006 case I_SRL:
1007 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1008 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
1009 a=c_shift_rot(lex.arg->type,lex.arg->value,0x38);
1010 if (a==1){error(lineno,txt,msg[IA]);ret=1;break;}
1011 break;
1012
1013 case I_SUB:
1014 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1015 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
1016 a=c_logical(lex.arg->type,lex.arg->value,0x90);
1017 if (a==1) {error(lineno,txt,msg[IA]);ret=1;break;}
1018 break;
1019
1020 case I_XOR:
1021 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1022 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
1023 a=c_logical(lex.arg->type,lex.arg->value,0xa8);
1024 if (a==1) {error(lineno,txt,msg[IA]);ret=1;break;}
1025 break;
1026
1027 case I_JP:
1028 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1029 if (!lex.arg->next)
1030 {
1031 if (c_jp(lex.arg->type,lex.arg->value,A_EMPTY,0))
1032 {error(lineno,txt,msg[IA]);ret=1;break;}
1033 }
1034 else
1035 {
1036 if (lex.arg->next->next){error(lineno,txt,msg[TOO]);ret=1;break;}
1037 if (c_jp(lex.arg->type,lex.arg->value,lex.arg->next->type,lex.arg->next->value))
1038 {error(lineno,txt,msg[IA]);ret=1;break;}
1039 }
1040 break;
1041
1042 case I_CALL:
1043 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1044 if (!lex.arg->next)
1045 {
1046 if (c_call(lex.arg->type,lex.arg->value,A_EMPTY,0))
1047 {error(lineno,txt,msg[IA]);ret=1;break;}
1048 }
1049 else
1050 {
1051 if (lex.arg->next->next){error(lineno,txt,msg[TOO]);ret=1;break;}
1052 if (c_call(lex.arg->type,lex.arg->value,lex.arg->next->type,lex.arg->next->value))
1053 {error(lineno,txt,msg[IA]);ret=1;break;}
1054 }
1055 break;
1056
1057 case I_JR:
1058 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1059 if (!lex.arg->next)
1060 a=c_jr(lex.arg->type,lex.arg->value-(lex.arg->is_label?address+2:0),A_EMPTY,0);
1061 else
1062 {
1063 if (lex.arg->next->next){error(lineno,txt,msg[TOO]);ret=1;break;}
1064 a=c_jr(lex.arg->type,lex.arg->value,lex.arg->next->type,
1065 lex.arg->next->value-(lex.arg->next->is_label?address+2:0));
1066 }
1067 if(a==1){error(lineno,txt,msg[IA]);ret=1;break;}
1068 break;
1069
1070 case I_DJNZ:
1071 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1072 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
1073 a=c_djnz(lex.arg->type,lex.arg->value-(lex.arg->is_label?address+2:0));
1074 if(a==1){error(lineno,txt,msg[IA]);ret=1;break;}
1075 break;
1076
1077 case I_LD:
1078 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1079 if (!lex.arg->next){error(lineno,txt,msg[MIS1]);ret=1;break;}
1080 if (lex.arg->next->next){error(lineno,txt,msg[TOO]);ret=1;break;}
1081 a=c_ld(lex.arg->type,lex.arg->value,lex.arg->next->type,lex.arg->next->value);
1082 if (a==1){error(lineno,txt,msg[IA]);ret=1;break;}
1083 break;
1084
1085 case I_ALIGN:
1086 if (disable_pseudo){error(lineno,txt,msg[FOR]);ret=1;break;}
1087 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1088 t=lex.arg;
1089 if (t->type!=A_NUM){error(lineno,txt,msg[IAH]);ret=1;break;}
1090 if (t->value >15 || t->value<0){error(lineno,txt,msg[VOR]);ret=1;break;}
1091 if (address&(1<<t->value)-1) address= ((address>>t->value)+1)<<t->value;
1092 last=address; /* we write no fill bytes into listing */
1093 break;
1094
1095 case I_DEFS:
1096 if (disable_pseudo){error(lineno,txt,msg[FOR]);ret=1;break;}
1097 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1098 t=lex.arg;
1099 if (t->type!=A_NUM){error(lineno,txt,msg[IAN]);ret=1;break;}
1100 if (t->value>65535||t->value<0){error(lineno,txt,msg[VOR]);ret=1;break;}
1101 if (address+t->value>=65536) address=0; else address += t->value;
1102 last=address; /* we write no fill bytes into listing */
1103 break;
1104
1105 case I_DEFM:
1106 if (disable_pseudo){error(lineno,txt,msg[FOR]);ret=1;break;}
1107 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1108 t=lex.arg;
1109 do
1110 {
1111 unsigned a;
1112 bool saved_listing_mode=LISTING;
1113 if (t->type!=A_STRING){error(lineno,txt,msg[IAS]);ret=1;break;}
1114 for (a=0;a<strlen(t->text);a++)
1115 { if (a>=4) LISTING=0;
1116 out(t->text[a]);
1117 }
1118 LISTING=saved_listing_mode;
1119 free(t->text);
1120 t=t->next;
1121 }
1122 while (t);
1123 break;
1124
1125 case I_DEFB:
1126 if (disable_pseudo){error(lineno,txt,msg[FOR]);ret=1;break;}
1127 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1128 t=lex.arg;
1129 do
1130 {
1131 if (t->type!=A_NUM){error(lineno,txt,msg[IAN]);ret=1;break;}
1132 if (t->value>255||t->value<-128){error(lineno,txt,msg[VOR]);ret=1;break;}
1133 out(t->value);
1134 t=t->next;
1135 }
1136 while (t);
1137 break;
1138
1139 case I_DEFW:
1140 if (disable_pseudo){error(lineno,txt,msg[FOR]);ret=1;break;}
1141 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1142 t=lex.arg;
1143 do
1144 {
1145 if (t->type!=A_NUM){error(lineno,txt,msg[IAN]);ret=1;break;}
1146 if (t->value>65535||t->value<-32768){error(lineno,txt,msg[VOR]);ret=1;break;}
1147 out(t->value&255);out((t->value>>8)&255);
1148 t=t->next;
1149 }
1150 while (t);
1151 break;
1152
1153 case I_COND:
1154 if (disable_pseudo){error(lineno,txt,msg[FOR]);ret=1;break;}
1155 if (!lex.arg){error(lineno,txt,msg[MIS1]);ret=1;break;}
1156 if (lex.arg->type!=A_NUM){error(lineno,txt,msg[IAT]);ret=1;break;}
1157 if (lex.arg->next){error(lineno,txt,msg[TOO]);ret=1;break;}
1158 if (cond_level == last_true_cond_level && lex.arg->value)
1159 last_true_cond_level++;
1160 cond_level++;
1161 break;
1162
1163 case I_ENDC:
1164 if (disable_pseudo){error(lineno,txt,msg[FOR]);ret=1;break;}
1165 if (lex.arg){error(lineno,txt,msg[EXT]);ret=1;}
1166 if (!cond_level){error(lineno,txt,msg[TCC]);ret=1;break;}
1167 if (last_true_cond_level == cond_level)
1168 last_true_cond_level--;
1169 cond_level--;
1170 break;
1171
1172 default:
1173 error(lineno,txt,msg[UNK]);
1174 ret=1;
1175 break;
1176 }
1177 if (pruchod!=1 && a==8) {error(lineno,txt,msg[VOR]);ret=1;}
1178 if (pruchod!=1 && a==9) {error(lineno,txt,msg[OOR]);ret=1;}
1179
1180 if (!(ret&7) && LISTING && pruchod==2)
1181 { static char blank[13];
1182 int i;
1183 for (i=0;i<12;i++) blank[i]=SPACE;
1184 i= address-last;
1185 printf("%s%5u %s\n",blank+3*(i>=1&&i<=4?i:(i>4?4:0)),lineno,txt);
1186 }
1187 }
1188 lineno++;
1189 if (lex.arg)
1190 {
1191 t=lex.arg;
1192 while((t=t->next))
1193 free(t->previous);
1194 free(t);
1195 }
1196
1197 return ret;
1198 }
1199