-
Notifications
You must be signed in to change notification settings - Fork 5
/
as1-z8.c
789 lines (747 loc) · 15.5 KB
/
as1-z8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
* Z8 assembler.
* Assemble one line of input.
* Knows all the dirt.
*
* Differences from the 'standard' Z8 stuff
*
* - 0x is used for hex not %XX
* - %(base)foo is not supported
* - %L etc for line feed and the like are not supported
* - None of the PLZ stuff
* - ; is used for comments not '!' or '//'
* - manufactured instructions (RES etc) are not yet supported (use the
* underlying and/or immediate)
* - no built in register alias names - .EQU in ABS should do fine ?
*/
#include "as.h"
/* FIXME: we should malloc/realloc this on non 8bit machines */
static uint8_t reltab[1024];
static unsigned int nextrel;
int passbegin(int pas)
{
segment = 1;
if (pass == 3)
nextrel = 0;
return 1;
}
void doflush(void)
{
}
static void setnextrel(int flag)
{
if (nextrel == 8 * sizeof(reltab))
aerr(TOOMANYJCC);
if (flag)
reltab[nextrel >> 3] |= (1 << (nextrel & 7));
nextrel++;
}
static unsigned int getnextrel(void)
{
unsigned int n = reltab[nextrel >> 3] & (1 << (nextrel & 7));
nextrel++;
return n;
}
static void constify(ADDR *ap)
{
if ((ap->a_type & TMMODE) == (TUSER|TMINDIR))
ap->a_type = TUSER;
}
static int segment_incompatible(ADDR *ap)
{
if (ap->a_segment == segment)
return 0;
return 1;
}
/*
* Read in an address
* descriptor, and fill in
* the supplied "ADDR" structure
* with the mode and value.
* Exits directly to "qerr" if
* there is no address field or
* if the syntax is bad.
*
* Z8 syntax wants %xx for hex and HI xx or LO xx not < >. Look at that
* later.
*
* We recognize the following
* Rn - register short form
* RRn - register pair short form
* @Rn - register, indirect short form
* @RRn - register pair, indirect short form
* n - register
* @n - register indirect
* #n - 8bit constant
*/
void getaddr_r(ADDR *ap)
{
int indirect = 0;
int pair = 0;
int c;
ap->a_type = 0;
ap->a_flags = 0;
ap->a_sym = NULL;
c = getnb();
/* #foo */
if (c == '#') {
c = getnb();
if (c == '<')
ap->a_flags |= A_LOW;
else if (c == '>')
ap->a_flags |= A_HIGH;
else
unget(c);
expr1(ap, LOPRI, 0);
istuser(ap);
constify(ap);
ap->a_type |= TIMMED;
return;
}
if (c == '@') {
indirect = 1;
c = getnb();
}
/* Register short forms */
if (c == 'R' || c == 'r') {
c = getnb();
if (c == 'R' || c == 'r')
pair = 1;
else
unget(c);
expr1(ap, LOPRI, 0);
istuser(ap);
constify(ap);
if (ap->a_sym)
qerr(SYNTAX_ERROR);
if (ap->a_value < 0 || ap->a_value > 15)
aerr(RSHORT_RANGE);
ap->a_type &= ~TMREG;
ap->a_type |= ap->a_value;
ap->a_value = 0;
if (pair == 0) {
if (indirect)
ap->a_type |= TSIND;
else
ap->a_type |= TRS;
} else {
if (indirect)
ap->a_type |= TRRIND;
else {
if (ap->a_value & 1)
aerr(ODD_REGISTER);
ap->a_type |= TRRS;
}
}
return;
}
unget(c);
/* If it wasn't a short register or a constant it's a register. We
have no 'address' formats except for constant load and indexed */
expr1(ap, LOPRI, 1);
c = getnb();
if (c == '(') {
ADDR tmp;
ap->a_type = TINDEX;
if (indirect)
aerr(INVALID_FORM);
getaddr_r(&tmp);
if ((tmp.a_type & TMADDR) != TRS)
qerr(RSHORT_RANGE);
c = getnb();
if (c != ')')
qerr(SYNTAX_ERROR);
ap->a_type &= ~TMREG;
ap->a_type |= tmp.a_type & TMREG;
} else {
unget(c);
if (indirect)
ap->a_type = TIND;
else
ap->a_type = TREG;
}
}
#define T_REG(x) ((x)->a_type & TMREG)
/* Unshorten a register form */
unsigned unshort_reg(ADDR *ap)
{
unsigned t = ap->a_type & TMADDR;
if (t == TRS) {
ap->a_value = (ap->a_type & TMREG) | 0xE0;
ap->a_type &= ~TMADDR;
ap->a_type |= TREG;
return TREG;
} else if (t == TRRS) {
ap->a_value = (ap->a_type & TMREG) | 0xE0;
ap->a_type &= ~TMADDR;
ap->a_type |= TREG;
return TREG;
} else if (t == TSIND) {
ap->a_value = (ap->a_type & TMREG) | 0xE0;
ap->a_type &= ~TMADDR;
ap->a_type |= TIND;
return TIND;
} else if (t == TRRIND) {
ap->a_value = (ap->a_type & TMREG) | 0xE0;
ap->a_type &= ~TMADDR;
ap->a_type |= TIND;
return TIND;
} else
return ap->a_type & TMADDR;
}
void check_8bit(ADDR *ap)
{
if (!(ap->a_flags & (A_HIGH|A_LOW)) && (ap->a_value < -128 || ap->a_value > 255))
aerr(CONSTANT_RANGE);
}
void getaddr8(ADDR *ap)
{
getaddr_r(ap);
check_8bit(ap);
}
int getcond(ADDR *ap)
{
if ((ap->a_type & TMMODE) == TCC)
return (ap->a_type & TMREG);
return -1;
}
void getaddr(ADDR *ap)
{
int c = getnb();
ap->a_type = 0;
ap->a_flags = 0;
ap->a_sym = NULL;
if (c == '<')
ap->a_flags |= A_LOW;
else if (c == '>')
ap->a_flags |= A_HIGH;
else
unget(c);
expr1(ap, LOPRI, 0);
/* Condition code */
if ((ap->a_type & TMMODE) == TCC)
return;
istuser(ap);
constify(ap);
ap->a_type |= TIMMED;
}
void check_pair(ADDR *ap)
{
if (ap->a_value & 1)
aerr(ODD_REGISTER);
}
/* Must be a constant - will be seen as a reg form */
void check_ra(ADDR *ap)
{
unsigned t = ap->a_type & TMADDR;
if (t != TREG && t != TIMMED)
aerr(INVALID_FORM);
}
/* Must be a constant - will be seen as a reg form */
void check_da(ADDR *ap)
{
unsigned t = ap->a_type & TMADDR;
if (t != TREG && t != TIMMED)
aerr(INVALID_FORM);
}
/*
* Assemble one line.
* The line in in "ib", the "ip"
* scans along it. The code is written
* right out.
*/
void asmline(void)
{
SYM *sp;
int c;
int opcode;
int cc;
VALUE value;
int delim;
SYM *sp1;
char id[NCPS];
char id1[NCPS];
ADDR a1;
ADDR a2;
int ta1;
int ta2;
int disp;
loop:
if ((c=getnb())=='\n' || c==';')
return;
if (isalpha(c) == 0 && c != '_' && c != '.')
qerr(UNEXPECTED_CHR);
getid(id, c);
if ((c=getnb()) == ':') {
sp = lookup(id, uhash, 1);
if (pass == 0) {
if ((sp->s_type&TMMODE) != TNEW
&& (sp->s_type&TMASG) == 0)
sp->s_type |= TMMDF;
sp->s_type &= ~TMMODE;
sp->s_type |= TUSER;
sp->s_value = dot[segment];
sp->s_segment = segment;
} else if (pass != 3) {
/* Don't check for duplicates, we did it already
and we will confuse ourselves with the pass
before. Instead blindly update the values */
sp->s_type &= ~TMMODE;
sp->s_type |= TUSER;
sp->s_value = dot[segment];
sp->s_segment = segment;
} else {
if ((sp->s_type&TMMDF) != 0)
err('m', MULTIPLE_DEFS);
if (sp->s_value != dot[segment])
err('p', PHASE_ERROR);
}
goto loop;
}
/*
* If the first token is an
* id and not an operation code,
* assume that it is the name in front
* of an "equ" assembler directive.
*/
if ((sp=lookup(id, phash, 0)) == NULL) {
getid(id1, c);
if ((sp1=lookup(id1, phash, 0)) == NULL
|| (sp1->s_type&TMMODE) != TEQU) {
err('o', SYNTAX_ERROR);
return;
}
getaddr(&a1);
constify(&a1);
istuser(&a1);
sp = lookup(id, uhash, 1);
if ((sp->s_type&TMMODE) != TNEW
&& (sp->s_type&TMASG) == 0)
err('m', MULTIPLE_DEFS);
sp->s_type &= ~(TMMODE|TPUBLIC);
sp->s_type |= TUSER|TMASG;
sp->s_value = a1.a_value;
sp->s_segment = a1.a_segment;
/* FIXME: review .equ to an external symbol/offset and
what should happen */
goto loop;
}
unget(c);
opcode = sp->s_value;
switch (sp->s_type&TMMODE) {
case TORG:
getaddr(&a1);
constify(&a1);
istuser(&a1);
if (a1.a_segment != ABSOLUTE)
qerr(MUST_BE_ABSOLUTE);
outsegment(ABSOLUTE);
dot[segment] = a1.a_value;
/* Tell the binary generator we've got a new absolute
segment. */
outabsolute(a1.a_value);
break;
case TEXPORT:
getid(id, getnb());
sp = lookup(id, uhash, 1);
sp->s_type |= TPUBLIC;
break;
/* .code etc */
case TSEGMENT:
segment = sp->s_value;
/* Tell the binary generator about a segment switch to a non
absolute segnent */
outsegment(segment);
break;
case TDEFB:
do {
getaddr(&a1);
constify(&a1);
istuser(&a1);
outrab(&a1);
} while ((c=getnb()) == ',');
unget(c);
break;
case TDEFW:
do {
getaddr(&a1);
constify(&a1);
istuser(&a1);
outraw(&a1);
} while ((c=getnb()) == ',');
unget(c);
break;
case TDEFM:
if ((delim=getnb()) == '\n')
qerr(MISSING_DELIMITER);
while ((c=get()) != delim) {
if (c == '\n')
qerr(MISSING_DELIMITER);
outab(c);
}
break;
case TDEFS:
getaddr(&a1);
constify(&a1);
istuser(&a1);
/* Write out the bytes. The BSS will deal with the rest */
for (value = 0 ; value < a1.a_value; value++)
outab(0);
break;
case TIMPL:
outab(opcode);
break;
/* Logic and maths operations with a 4 bit operation and 4 bits of
addressing information */
case TOP4BIT:
getaddr8(&a1);
c = getnb();
if (c != ',')
qerr(MISSING_DELIMITER);
getaddr8(&a2);
/* Not work out how to encode it */
/* OP r,r */
ta1 = a1.a_type & TMADDR;
ta2 = a2.a_type & TMADDR;
/* Short forms are dest << 4 | soure */
if (ta1 == TRS && ta2 == TRS) {
outab(opcode | 0x02);
outab((T_REG(&a1) << 4) | T_REG(&a2));
break;
}
/* OP r,Ir */
if (ta1 == TRS && ta2 == TSIND) {
outab(opcode | 0x03);
outab((T_REG(&a1) << 4) | T_REG(&a2));
break;
}
/* Look for long forms using 0xE0 for current reg set */
if (ta1 == TRS || ta1 == TSIND)
ta1 = unshort_reg(&a1);
if (ta2 == TRS || ta2 == TSIND)
ta2 = unshort_reg(&a2);
/* Long forms are src, dst except when the are not (sigh)*/
/* OP R,R */
if (ta1 == TREG && ta2 == TREG)
outab(opcode | 0x04);
/* OP R,@R */
else if (ta1 == TREG && ta2 == TIND)
outab(opcode | 0x05);
/* OP R,IMM */
else if (ta1 == TREG && ta2 == TIMMED)
outab(opcode | 0x06);
/* OP @R,IMM */
else if (ta1 == TIND && ta2 == TIMMED)
outab(opcode | 0x07);
else
qerr(INVALID_FORM);
/* Immediate is backwards to the others */
if (ta2 == TIMMED) {
/* dst src */
outab(a1.a_value);
outrab(&a2);
} else {
/* src dst */
outrab(&a2);
outab(a1.a_value);
}
break;
case TRRIR:
/* RR or IR format */
/* We accept both rr0 and r0 */
getaddr8(&a1);
switch(a1.a_type & TMADDR) {
case TRS:
case TRRS:
unshort_reg(&a1);
case TREG:
check_pair(&a1);
outab(opcode);
break;
case TSIND:
case TRRIND:
unshort_reg(&a1);
case TIND:
outab(opcode + 0x01);
break;
default:
qerr(INVALID_FORM);
}
outrab(&a1);
break;
case TRIR:
/* R or IR format */
getaddr8(&a1);
switch(a1.a_type & TMADDR) {
case TRS:
if (opcode == 0x20) { /* INC has an r form */
opcode = 0x0E | (T_REG(&a1) << 4);
outab(opcode);
break;
}
unshort_reg(&a1);
case TREG:
outab(opcode);
outrab(&a1);
break;
case TSIND:
unshort_reg(&a1);
case TIND:
outab(opcode + 0x01);
outrab(&a1);
break;
default:
qerr(INVALID_FORM);
}
break;
case TIMM8:
/* 8bit immediate */
getaddr8(&a1);
if ((a1.a_type & TMADDR) != TIMMED)
qerr(INVALID_FORM);
outab(opcode);
outrab(&a1);
break;
case TCRA:
getaddr(&a1);
cc = getcond(&a1);
if (cc != -1) {
c = getnb();
if (c != ',')
qerr(MISSING_DELIMITER);
getaddr(&a1);
} else
cc = 0x08; /* True */
disp = a1.a_value - dot[segment] - 2;
if (pass == 3)
c = getnextrel();
else {
c = 0;
if (pass == 0 || segment_incompatible(&a1) ||
disp < -128 || disp > 127)
c = 1;
if (pass == 2)
setnextrel(c);
}
if (c) {
/* Write a JP cc instead */
outab(0x0D + (cc << 4));
outraw(&a1);
} else {
outab(opcode + (cc << 4));
if (disp < -128 || disp > 127)
aerr(BRA_RANGE);
/* Relative branches are always in segment and within our
generated space so don't relocate */
outab(disp);
}
break;
case TJMP:
c = getnb();
unget(c);
if (c == '@') {
getaddr_r(&a1);
ta1 = a1.a_type & TMADDR;
if (ta1 == TRRIND)
ta1 = unshort_reg(&a1);
if (ta1 == TIND) {
check_pair(&a1);
outab(0x30);
outrab(&a1);
break;
}
qerr(INVALID_FORM);
break;
}
getaddr(&a1);
cc = getcond(&a1);
if (cc != -1) {
c = getnb();
if (c != ',')
qerr(MISSING_DELIMITER);
getaddr_r(&a1);
} else
cc = 0x08; /* True */
check_da(&a1);
outab(opcode + (cc << 4));
/* Relocatable label */
outraw(&a1);
break;
case TIRRDA: /* Call */
getaddr_r(&a1);
ta1 = a1.a_type & TMADDR;
if (ta1 == TRRIND)
ta1 = unshort_reg(&a1);
if (ta1 == TIND) {
/* CALL @RR */
outab(0xD4);
outrab(&a1);
} else if (ta1 == TREG) {
outab(0xD6);
/* Relocatable label */
outraw(&a1);
} else
qerr(INVALID_FORM);
break;
case TRA: /* DJNZ */
getaddr8(&a1);
if ((a1.a_type & TMADDR) != TRS)
qerr(INVALID_FORM);
c = getnb();
if (c != ',')
qerr(MISSING_DELIMITER);
outab(0x0A | (T_REG(&a1) << 4));
/* And then a relative address */
getaddr(&a2);
check_ra(&a2);
a2.a_value -= dot[segment] + 1;
outrabrel(&a2);
break;
case TLDC:
getaddr8(&a1);
c = getnb();
if (c != ',')
qerr(MISSING_DELIMITER);
getaddr8(&a2);
ta1 = a1.a_type & TMADDR;
ta2 = a2.a_type & TMADDR;
if (ta1 == TRS && ta2 == TRRIND) {
outab(opcode);
outab((T_REG(&a1) << 4) | T_REG(&a2));
/* dst, src */
} else if (ta1 == TRRIND && ta2 == TRS) {
/* dst, src */
outab(opcode + 0x10);
outab((T_REG(&a2) << 4) | T_REG(&a1));
} else
qerr(INVALID_FORM);
break;
case TLDCI:
getaddr8(&a1);
c = getnb();
if (c != ',')
qerr(MISSING_DELIMITER);
getaddr8(&a2);
ta1 = a1.a_type & TMADDR;
ta2 = a2.a_type & TMADDR;
if (ta1 == TSIND && ta2 == TRRIND) {
/* dst, src */
outab(opcode);
outab((T_REG(&a1) << 4) | T_REG(&a2));
} else if (ta1 == TRRIND && ta2 == TSIND) {
outab(opcode + 0x10);
/* src, dst */
outab((T_REG(&a1) << 4) | T_REG(&a2));
} else
qerr(INVALID_FORM);
break;
case TLOAD:
/* Load is its own special complicated case */
getaddr8(&a1);
c = getnb();
if (c != ',')
qerr(MISSING_DELIMITER);
getaddr8(&a2);
ta1 = a1.a_type & TMADDR;
ta2 = a2.a_type & TMADDR;
/* Now encode the load by type */
if (ta1 == TRS && ta2 == TIMMED) {
outab(0x0C | (T_REG(&a1) << 4));
outrab(&a2);
break;
}
/* As with the logic/maths ops the encoding order isn't
entirely sane */
if (ta1 == TRS && ta2 == TREG) {
/* dst|mode , src */
outab(0x08 | (T_REG(&a1) << 4));
outrab(&a2);
break;
}
if (ta1 == TREG && ta2 == TRS) {
/* src|mode , dst */
outab(0x09 | (T_REG(&a2) << 4));
outrab(&a1);
break;
}
if (ta1 == TRS && ta2 == TSIND) {
/* mode | 3 , dst | src */
outab(0xE3);
outab((T_REG(&a1) << 4) | T_REG(&a2));
break;
}
if (ta1 == TSIND && ta2 == TRS) {
/* mode | 3 , dst | src */
outab(0xF3);
outab((T_REG(&a1) << 4) | T_REG(&a2));
break;
}
if (ta1 == TRS && ta2 == TINDEX) {
/* op , dst | x, offset */
outab(0xC7);
outab((T_REG(&a1) << 4) | T_REG(&a2));
outrab(&a2);
break;
}
if (ta1 == TINDEX && ta2 == TRS) {
/* op , src | x, offset */
outab(0xD7);
outab(T_REG(&a1) | (T_REG(&a2) << 4));
outrab(&a1);
break;
}
/* Partial short form. Turn r,r into r,R not R,R */
if (ta1 == TRS && ta2 == TRS) {
outab(0x08 | (T_REG(&a1) << 4));
unshort_reg(&a2);
outab(a2.a_value);
break;
}
/* If we get here in short form we need to try the long
form alias */
if (ta1 == TRS || ta1 == TSIND)
ta1 = unshort_reg(&a1);
if (ta2 == TRS || ta2 == TSIND)
ta2 = unshort_reg(&a2);
if (ta1 == TREG && ta2 == TREG) {
/* op, src, dst */
outab(0xE4);
outrab(&a2);
outrab(&a1);
break;
}
if (ta1 == TREG && ta2 == TIND) {
/* op, src, dst */
outab(0xE5);
outrab(&a2);
outrab(&a1);
break;
}
if (ta1 == TREG && ta2 == TIMMED) {
/* op, dst, src */
outab(0xE6);
outrab(&a1);
outrab(&a2);
break;
}
if (ta1 == TIND && ta2 == TIMMED) {
/* op, dst, src */
outab(0xE7);
outrab(&a1);
outrab(&a2);
break;
}
if (ta1 == TIND && ta2 == TREG) {
/* op, src, dst */
outab(0xF5);
outrab(&a2);
outrab(&a1);
break;
}
qerr(INVALID_FORM);
break;
default:
aerr(SYNTAX_ERROR);
}
goto loop;
}