-
Notifications
You must be signed in to change notification settings - Fork 1
/
opc65.c
1675 lines (1626 loc) · 42.9 KB
/
opc65.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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
opc65.c - Part of macxx, a cross assembler family for various micro-processors
Copyright (C) 2008 David Shepperd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
Change Log
08/09/2023 - Added support for BR S,+n and BR S,-n syntax - Tim Giddens
All Branch instructions now support this syntax used in
very old source code.
12/10/2022 - Changed default LIST Flags - Tim Giddens
******************************************************************************/
#if !defined(MAC_65)
#define MAC_65
#endif
#include "token.h"
#include "pst_tokens.h"
#include "exproper.h"
#include "listctrl.h"
#include "psttkn65.h"
#include "add_defs.h"
#include "memmgt.h"
#define DEFNAM(name,numb) {"name",name,numb},
/* The following are variables specific to the particular assembler */
char macxx_name[] = "mac65";
char *macxx_target = "6502";
char *macxx_descrip = "Cross assembler for the 6502, 65C02 and 65816.";
#if 0
unsigned short macxx_rel_salign = 0; /* default alignment for .REL. segment */
unsigned short macxx_rel_dalign = 0; /* default alignment for data in .REL. segment */
unsigned short macxx_abs_salign = 0; /* default alignments for .ABS. segment */
unsigned short macxx_abs_dalign = 0;
#else
unsigned short macxx_salign = 0; /* default alignment segments by LLF */
unsigned short macxx_dalign = 0; /* default alignment data within segment */
#endif
unsigned short macxx_min_dalign = 0;
char macxx_mau = 8; /* number of bits/minimum addressable unit */
char macxx_bytes_mau = 1; /* number of bytes/mau */
char macxx_mau_byte = 1; /* number of mau's in a byte */
char macxx_mau_word = 2; /* number of mau's in a word */
char macxx_mau_long = 4; /* number of mau's in a long */
char macxx_nibbles_byte = 2; /* For the listing output routines */
char macxx_nibbles_word = 4;
char macxx_nibbles_long = 8;
unsigned long macxx_edm_default = ED_AMA|ED_TRUNC; /* default edmask */
unsigned long macxx_lm_default = ~(LIST_ME | LIST_MEB | LIST_MES | LIST_LD | LIST_COD); /* default list mask */
int current_radix = 16; /* default the radix to hex */
char expr_open = '<'; /* char that opens an expression */
char expr_close = '>'; /* char that closes an expression */
char expr_escape = '^'; /* char that escapes an expression term */
char macro_arg_open = '<'; /* char that opens a macro argument */
char macro_arg_close = '>'; /* char that closes a macro argument */
char macro_arg_escape = '^'; /* char that escapes a macro argument */
char macro_arg_gensym = '?'; /* char indicating generated symbol for macro */
char macro_arg_genval = '\\'; /* char indicating generated value for macro */
char open_operand = '('; /* char that opens an operand indirection */
char close_operand = ')'; /* char that closes an operand indirection */
int max_opcode_length = 6; /* significant length of opcodes and */
int max_symbol_length = 6; /* symbols */
static char *am_ptr;
static long cpu_index,cpu_acc;
static int cpu_hist_nest;
enum
{
CPU_DEFABS, /* default to absolute addressing */
CPU_DEFLNG, /* default to long addressing */
CPU_DEFDIR /* default to direct addressing */
} cpu_defam;
/* End of processor specific stuff */
extern unsigned char opc_to_hex[][OP_TO_HEX_SIZE];
enum amflag
{
MNBI = 1,
FIN = MNBI << 1,
LFIN = FIN << 1,
OPENAT = LFIN << 1,
OPENBKT = OPENAT << 1,
GOTSTK = OPENBKT << 1
};
static long ndxtbl[] = { ZX_NUM, AX_NUM, ZY_NUM, AY_NUM };
static struct
{
char *name;
AModes am_num;
} forced_am[] = {
{ "I", I_NUM },
{ "A", A_NUM },
{ "AL", AL_NUM },
{ "D", D_NUM },
{ "NY", NY_NUM },
{ "LNY", NNY_NUM },
{ "XN", NX_NUM },
{ "DX", ZX_NUM },
{ "DY", ZY_NUM },
{ "AX", AX_NUM },
{ "ALX", ALX_NUM },
{ "AY", AY_NUM },
{ "R", R_NUM },
{ "RL", RL_NUM },
{ "DN", ND_NUM },
{ "AN", N_NUM },
{ "DLN", NND_NUM },
{ "AXN", NAX_NUM },
{ "DS", DS_NUM },
{ "DSNY", NDSY_NUM },
{ "XYC", XYC_NUM },
{ "L", DES_NUM },
{ 0, 0 } };
typedef struct
{
int flag; /* .ne. if value is expression */
unsigned long value; /* value if not expression */
EXP_stk *exptr; /* ptr to expression stack */
} DPage;
typedef struct
{
int flag; /* .ne. if value is expression */
unsigned long value; /* value if not expression */
EXP_stk *exptr; /* ptr to expression stack */
} Bank;
DPage **dpage;
static int dpage_size,dpage_stack;
Bank **bank;
static int bank_size,bank_stack;
static EXP_stk *tmp_expr;
static int tmp_expr_size;
int ust_init(void)
{
if ( image_name != 0 )
{
char *s;
s = strchr(image_name->name_only, '8');
if ( s != 0 && strncmp(s, "816", 3) == 0 )
options[QUAL_P816] = 1;
}
if ( options[QUAL_P816] )
{
bank_size = dpage_size = 16;
dpage = (DPage **)MEM_alloc(dpage_size * sizeof(DPage *));
bank = (Bank **)MEM_alloc(bank_size * sizeof(Bank *));
misc_pool_used += (sizeof(Bank *) + sizeof(DPage *)) * dpage_size;
}
return 0; /* would fill a user symbol table */
}
static void do_branch(Opcode *opc)
{
int s_test;
long offset;
EXPR_struct *exp_ptr;
const char *badExpr=NULL;
offset = (opc->op_amode & RL) ? 3 : 2;
s_test = 0; /* set FALSE */
get_token();
if ( *inp_ptr == ',' && token_type == TOKEN_strng && token_value == 1 && toupper(token_pool[0]) == 'S' )
{
++inp_ptr; /* eat the comma */
get_token();
s_test = 1; /* set TRUE */
}
if ( exprs(1, &EXP1) < 1 )
{
badExpr = "Invalid or no branch target"; /* expression nfg or not present */
}
else
{
if (s_test) /* If s_test TRUE make it a br .+n or br .-n syntax */
{
EXP1.ptr = compress_expr(&EXP1);
exp_ptr = EXP1.stack;
if ( EXP1.ptr != 1 || exp_ptr->expr_code != EXPR_VALUE )
{
badExpr = "Branch target must resolve to an absolute value";
}
else
{
exp_ptr->expr_code = EXPR_SEG;
exp_ptr->expr_value += current_offset;
exp_ptr->expr_seg = current_section;
}
}
if ( !badExpr )
{
exp_ptr = EXP1.stack + EXP1.ptr;
exp_ptr->expr_code = EXPR_SEG;
exp_ptr->expr_value = current_offset + offset;
(exp_ptr++)->expr_seg = current_section;
exp_ptr->expr_code = EXPR_OPER;
exp_ptr->expr_value = '-';
EXP1.ptr += 2;
}
}
if ( !badExpr )
{
EXP1.ptr = compress_expr(&EXP1);
EXP1.tag = (opc->op_amode & RL) ? 'y' : 'z'; /* set branch type operand */
exp_ptr = EXP1.stack;
if ( EXP1.ptr == 1 && exp_ptr->expr_code == EXPR_VALUE )
{
long max_dist;
max_dist = (opc->op_amode & RL) ? 32767 : 127;
if ( exp_ptr->expr_value < -(max_dist + 1) || exp_ptr->expr_value > max_dist )
{
long toofar;
toofar = exp_ptr->expr_value;
if ( toofar > 0 )
{
toofar -= max_dist;
}
else
{
toofar = -toofar - (max_dist + 1);
}
sprintf(emsg, "Branch offset 0x%lX byte(s) out of range", toofar);
badExpr = emsg;
}
}
else
{
EXP1.psuedo_value = 0;
}
if ( !badExpr && options[QUAL_P816] )
{
if ( EXP1.ptr + 6 > EXPR_MAXDEPTH )
{
badExpr = "Branch address expression too complex";
}
else
{
exp_ptr = EXP2.stack;
if ( EXP1.ptr == 1 && EXP1.stack->expr_code == EXPR_VALUE )
{
exp_ptr->expr_code = EXPR_SEG;
exp_ptr->expr_value = EXP1.stack->expr_value + current_offset + offset;
(exp_ptr++)->expr_seg = current_section;
}
else
{
memcpy(exp_ptr, EXP1.stack, EXP1.ptr * sizeof(EXPR_struct));
exp_ptr += EXP1.ptr;
exp_ptr->expr_code = EXPR_SEG;
exp_ptr->expr_value = current_offset + offset;
(exp_ptr++)->expr_seg = current_section;
exp_ptr->expr_code = EXPR_OPER;
(exp_ptr++)->expr_value = '+';
}
exp_ptr->expr_code = EXPR_VALUE;
(exp_ptr++)->expr_value = 0x00FF0000;
exp_ptr->expr_code = EXPR_OPER;
(exp_ptr++)->expr_value = '&';
exp_ptr->expr_code = EXPR_SEG;
exp_ptr->expr_value = 0;
(exp_ptr++)->expr_seg = current_section;
exp_ptr->expr_code = EXPR_VALUE;
(exp_ptr++)->expr_value = 0x00FF0000;
exp_ptr->expr_code = EXPR_OPER;
(exp_ptr++)->expr_value = '&';
exp_ptr->expr_code = EXPR_OPER;
(exp_ptr++)->expr_value = EXPROPER_TST | (EXPROPER_TST_NE << 8);
EXP2.ptr = exp_ptr - EXP2.stack;
snprintf(emsg, ERRMSG_SIZE, "%s:%d: Branch or jmp out of program bank",
current_fnd->fn_buff,current_fnd->fn_line);
write_to_tmp(TMP_TEST, 0, &EXP2, 0);
write_to_tmp(TMP_ASTNG, strlen(emsg) + 1, emsg, 1);
EXP2.ptr = 0; /* don't use this expression any more */
}
}
}
if ( badExpr )
{
bad_token(NULL,badExpr);
EXP1.ptr = 1;
exp_ptr = EXP1.stack;
exp_ptr->expr_code = EXPR_VALUE; /* make it a 0 */
exp_ptr->expr_value = -offset; /* make it a br . */
}
return;
}
static int do_operand(Opcode *opc)
{ /* 0, 1 or 2 operands */
AModes amdcdnum = ILL_NUM;
int ct;
long amflag = 0;
int str=0;
ct = get_token(); /* pickup the next token */
switch (ct)
{
case EOL:
{
return AC_NUM; /* no operand supplied */
}
case TOKEN_pcx:
{
if ( token_value == '#' )
{
get_token(); /* get the next token */
if ( exprs(1, &EXP1) < 1 )
break; /* quit if expr nfg */
return I_NUM; /* return with immediate mode address */
}
if ( token_value == open_operand )
{
amflag = FIN;
}
else
{
if ( options[QUAL_P816] )
{
if ( token_value == '[' )
{
amflag = FIN | OPENBKT;
}
}
else if ( token_value == '@' )
{
amflag = OPENAT;
}
}
if ( amflag == 0 )
break; /* give 'em an illegal am */
get_token(); /* pickup the next token */
}
str = 1; /* force fall through to default */
/* fall through to default */
case TOKEN_strng:
if ( !str && (edmask & ED_MOS) && ((1L << AC_NUM) & opc->op_amode) && token_type == TOKEN_strng && token_value == 1 && _toupper(*token_pool) == 'A' )
{
return AC_NUM;
}
/* else fall through to default */
default:
{
if ( options[QUAL_P816] || (edmask & ED_MOS) )
{ /* if mostech format */
if ( exprs(1, &EXP1) < 1 )
break;
if ( amflag & FIN )
{ /* item started with a '(' */
int tst;
tst = (amflag & OPENBKT) ? ']' : close_operand;
if ( *inp_ptr == tst )
{
while ( ++inp_ptr,isspace(*inp_ptr) ); /* eat ) and ws */
if ( *inp_ptr == ',' )
{ /* followed with a comma */
++inp_ptr; /* eat the comma */
get_token(); /* pickup the address mode */
if ( token_type == TOKEN_strng &&
token_value == 1 &&
_toupper(*token_pool) == 'Y' )
{
return (amflag & OPENBKT) ? NNY_NUM : NY_NUM; /* syntax [nn],y */
}
bad_token(am_ptr, (amflag & OPENBKT) ?
"Invalid syntax. Expected [nn],Y form" :
"Invalid syntax. Expected (nn),Y form");
break;
}
else
{
if ( amflag & OPENBKT )
return NND_NUM; /* plain direct long indirect [d] or [a] */
return N_NUM; /* plain indirect (d) or (a) */
}
}
else
{ /* -- ended with ']' or ')' */
if ( *inp_ptr == ',' )
{ /* perhaps a (nn,X) format */
int reg;
++inp_ptr; /* eat the comma */
get_token(); /* pickup the next item */
reg = _toupper(*token_pool);
if ( token_type == TOKEN_strng &&
token_value == 1 &&
*inp_ptr == close_operand &&
(amflag & OPENBKT) == 0 &&
(reg == 'X' || reg == 'S') )
{
++inp_ptr; /* eat the closing ')' */
if ( reg == 'X' )
return (opc->op_amode & NAX) ? NAX_NUM : NX_NUM;
amflag |= GOTSTK;
}
else
{
bad_token(am_ptr, "Invalid syntax. Expected form (nn,X)");
break;
}
}
else
{
bad_token(am_ptr, "Invalid syntax. Expected form (nn) or (nn,X)");
break;
}
} /* -- if closed with a ')' */
if ( amflag & GOTSTK )
{
if ( *inp_ptr != ',' )
{
bad_token(inp_ptr, "Expected (nn,S),Y syntax");
break;
}
while ( ++inp_ptr,isspace(*inp_ptr) ); /* eat comma and ws */
get_token(); /* pickup the next item */
if ( token_type != TOKEN_strng || token_value != 1 ||
_toupper(*token_pool) != 'Y' )
{
bad_token(tkn_ptr, "Expected form (D,S),Y");
break;
}
return NDSY_NUM;
}
sprintf(emsg, "Expected a closing '%c' character", tst);
bad_token(inp_ptr, emsg);
break;
}
else
{ /* -+ if opened with a '(' */
if ( *inp_ptr == ',' )
{ /* stop on a comma? */
char xy;
while ( ++inp_ptr,isspace(*inp_ptr) ); /* eat it and ws */
get_token(); /* pick up address mode */
xy = *token_pool;
xy = _toupper(xy);
if ( token_type == TOKEN_strng && token_value == 1 )
{
switch (xy)
{
case 'X':
return X_NUM; /* syntax is nn,X */
case 'Y':
return Y_NUM; /* syntax is nn,Y */
case 'S':
return DS_NUM; /* syntax is nn,S */
}
}
bad_token(am_ptr, (options[QUAL_P816]) ?
"Expected indexed form of nn,X or nn,Y or nn,S" :
"Expected indexed form of nn,X or nn,Y");
break;
} /* -- end on comma */
return 0; /* no am specified */
} /* -- didn't open on '(' */
}
else
{ /* -+ if MOS format */
if ( token_type == TOKEN_strng && *inp_ptr == ',' )
{
char c;
if ( squeak )
printf("opc65:do_operand() entry 1: token_type=TOKEN_strng, *inp_ptr == ',', amflag=0x%04lX\n", amflag);
if ( amflag != 0 )
{
bad_token(am_ptr, "Invalid address mode syntax");
break;
}
c = *token_pool;
c = _toupper(c);
if ( token_value == 1 )
{
if ( c == 'I' )
amdcdnum = I_NUM;
else if ( c == 'Z' )
amdcdnum = Z_NUM;
else if ( c == 'A' )
amdcdnum = A_NUM;
else if ( c == 'N' )
amdcdnum = N_NUM;
else if ( c == 'X' )
amdcdnum = X_NUM;
else if ( c == 'Y' )
amdcdnum = Y_NUM;
if ( squeak )
printf("opc65:do_operand() entry 2: token_value==1, Found c='%c', amdcdnum=%d\n", c, amdcdnum);
}
else
{
if ( token_value == 2 )
{
char c1;
c1 = *(token_pool + 1);
c1 = _toupper(c1);
if ( c == 'Z' )
{
if ( c1 == 'X' )
amdcdnum = ZX_NUM;
else if ( c1 == 'Y' )
amdcdnum = ZY_NUM;
}
else if ( c == 'A' )
{
if ( c1 == 'X' )
amdcdnum = AX_NUM;
else if ( c1 == 'Y' )
amdcdnum = AY_NUM;
else if ( c1 == 'N' )
amdcdnum = N_NUM;
}
else if ( c == 'N' )
{
if ( c1 == 'X' )
amdcdnum = NX_NUM;
else if ( c1 == 'Y' )
amdcdnum = NY_NUM;
}
if ( squeak )
printf("opc65:do_operand() entry 2: token_value==2, Found c='%c', c1='%c', amdcdnum=%d\n", c, c1, amdcdnum);
}
}
if ( amdcdnum < UNDEF_NUM )
{
bad_token(am_ptr, "Unknown address mode");
break;
}
++inp_ptr; /* eat the comma */
get_token(); /* pickup the next token */
amflag = MNBI; /* signal nothing else allowed */
}
else
{
if ( squeak )
printf("opc65:do_operand() entry 1: token_type=%d, *inp_ptr == '%s', amflag=0x%04lX\n", token_type, inp_ptr, amflag);
}
if ( exprs(1, &EXP1) < 1 )
break;
if ( (amflag & MNBI) == 0 )
{
if ( *inp_ptr == open_operand )
{
char xy;
++inp_ptr; /* eat the ( */
get_token();
xy = *token_pool;
xy = _toupper(xy);
if ( token_type == TOKEN_strng &&
token_value == 1 &&
(xy == 'X' || xy == 'Y') &&
*inp_ptr == close_operand )
{
++inp_ptr; /* eat closing ) */
if ( amflag & OPENAT )
return (xy == 'X') ? NX_NUM : NY_NUM;
return (xy == 'X') ? X_NUM : Y_NUM;
}
if ( amflag & OPENAT )
bad_token(am_ptr, "Illegal index syntax. Expected @n(X) or @n(Y)");
else
bad_token(am_ptr, "Illegal index syntax. Expected n(X) or n(Y)");
break;
} /* -- if not open '(' */
else if ( amflag == OPENAT )
return N_NUM;
} /* -- if MNBI */
return amdcdnum > UNDEF_NUM ? amdcdnum : UNDEF_NUM; /* give 'em an am */
} /* -- if MOS format */
} /* -- case default */
} /* -- switch(ct) */
EXP1.ptr = 0;
return -1; /* am nfg */
}
static int bad_amode(void)
{
EXP0SP->expr_code = EXPR_VALUE;
EXP0.ptr = 1;
if ( options[QUAL_P816] )
{
EXP0.tag_len = 1;
EXP0.tag = 'l';
EXP0.psuedo_value = EXP0SP->expr_value = 0xEAEAEAEA; /* give 'em a bunch of nop's */
}
else
{
EXP0.tag = 'x';
EXP0.tag_len = 24;
EXP0.psuedo_value = EXP0SP->expr_value = 0xEAEAEA; /* give 'em a bunch of nop's */
}
EXP1.ptr = 0; /* and no operands */
EXP2.ptr = 0; /* and no operands */
EXP3.ptr = 0; /* and no operands */
return 0; /* no address mode */
}
static int make_dpage_ref(EXP_stk *estk)
{
DPage *dp;
EXP_stk *dpstk;
EXPR_struct * src,*dst;
int i;
dp = *dpage + dpage_stack;
if ( dp == 0 )
return 0; /* nothing to do */
dst = estk->stack + estk->ptr;
dpstk = dp->exptr;
src = dpstk->stack;
i = dpstk->ptr + estk->ptr + 1;
if ( i >= EXPR_MAXDEPTH )
{
bad_token((char *)0, "Direct page expression too complex");
return 0;
}
memcpy(dst, src, dpstk->ptr * sizeof(EXPR_struct));
dst += dpstk->ptr;
dst->expr_code = EXPR_OPER;
dst->expr_value = '-';
estk->ptr += dpstk->ptr + 1;
estk->tag = 'c';
estk->ptr = compress_expr(estk);
return 0;
}
static int make_bank_ref(EXP_stk *estk)
{
Bank *bp;
EXP_stk *bpstk;
EXPR_struct * src,*dst;
bp = *(bank + bank_stack);
if ( bp == 0 )
return 0; /* nothing to do */
dst = estk->stack + estk->ptr;
bpstk = bp->exptr;
src = bpstk->stack;
if ( bpstk->ptr + estk->ptr + 1 >= EXPR_MAXDEPTH )
{
bad_token((char *)0, "Absolute address expression too complex");
return 0;
}
memcpy(dst, src, bpstk->ptr * sizeof(EXPR_struct));
dst += bpstk->ptr;
dst->expr_code = EXPR_OPER;
dst->expr_value = '-';
estk->ptr += bpstk->ptr + 1;
estk->ptr = compress_expr(estk);
return 0;
}
static int make_pbr_ref(EXP_stk *estk)
{
EXPR_struct *dst;
dst = estk->stack + estk->ptr;
if ( estk->ptr + 4 >= EXPR_MAXDEPTH )
{
bad_token((char *)0, "Absolute address expression too complex");
return 0;
}
dst->expr_code = EXPR_SEG;
dst->expr_value = 0;
(dst++)->expr_seg = current_section;
dst->expr_code = EXPR_VALUE;
(dst++)->expr_value = 0xFF0000;
dst->expr_code = EXPR_OPER;
(dst++)->expr_value = '&';
dst->expr_code = EXPR_OPER;
(dst++)->expr_value = '-';
estk->ptr += 4;
return 0;
}
static int check_4_dpage(EXP_stk *estk)
{
DPage *dp;
EXP_stk * testk,*dexp;
EXPR_struct * expr,*texpr,*tp;
expr = estk->stack;
dp = *(dpage + dpage_stack);
if ( estk->base_page_reference )
return 1; /* base page reference */
if ( squeak )
printf("check_4_dpage(): #expr:%d, sptr->code=%d, sptr->value=%08lX, fwd=%d\n", estk->ptr, expr->expr_code, expr->expr_value, estk->forward_reference);
if ( dp == 0 )
{
if ( (estk->ptr == 1) && /* or expression is < 256 */
(expr->expr_code == EXPR_VALUE) &&
((expr->expr_value & -256) == 0) &&
(estk->forward_reference == 0) )
return 1;
}
else
{
int i;
dexp = dp->exptr;
i = (dexp->ptr + estk->ptr + 1) * sizeof(EXPR_struct) + sizeof(EXP_stk);
if ( i > tmp_expr_size )
{
if ( tmp_expr != 0 )
MEM_free((char *)tmp_expr);
tmp_expr = (EXP_stk *)MEM_alloc(i + 8 * sizeof(EXPR_struct));
tmp_expr_size = i + 8 * sizeof(EXPR_struct);
}
testk = tmp_expr;
memcpy(testk, estk, sizeof(EXP_stk));
texpr = testk->stack = (EXPR_struct *)(testk + 1);
memcpy(texpr, expr, estk->ptr * sizeof(EXPR_struct));
tp = texpr + estk->ptr;
memcpy(tp, dexp->stack, dexp->ptr * sizeof(EXPR_struct));
tp += dexp->ptr;
tp->expr_code = EXPR_OPER;
tp->expr_value = '-';
testk->ptr += dexp->ptr + 1;
i = compress_expr(testk);
if ( i == 1
&& texpr->expr_code == EXPR_VALUE
&& !(texpr->expr_value & -256)
&& !testk->forward_reference
)
{
return 1;
}
}
return 0;
}
static int check_4_abs(EXP_stk *estk)
{
Bank *bp;
EXP_stk * testk,*bexp;
EXPR_struct * expr,*texpr,*tp;
expr = estk->stack;
bp = *(bank + bank_stack);
if ( bp == 0 )
{
if ( (estk->ptr == 1) && /* or expression is < 65535 */
(expr->expr_code == EXPR_VALUE) &&
((expr->expr_value & -65536L) == 0) &&
(estk->forward_reference == 0) )
return 1;
}
else
{
int i;
bexp = bp->exptr;
i = (bexp->ptr + estk->ptr + 1) * sizeof(EXPR_struct) + sizeof(EXP_stk);
if ( i > tmp_expr_size )
{
if ( tmp_expr != 0 )
MEM_free((char *)tmp_expr);
tmp_expr = (EXP_stk *)MEM_alloc(i + 8 * sizeof(EXPR_struct));
tmp_expr_size = i + 8 * sizeof(EXPR_struct);
}
testk = tmp_expr;
memcpy(testk, estk, sizeof(EXP_stk));
texpr = testk->stack = (EXPR_struct *)(testk + 1);
memcpy(texpr, expr, estk->ptr * sizeof(EXPR_struct));
tp = texpr + estk->ptr;
memcpy(tp, bexp->stack, bexp->ptr * sizeof(EXPR_struct));
tp += bexp->ptr;
tp->expr_code = EXPR_OPER;
tp->expr_value = '-';
testk->ptr += bexp->ptr + 1;
i = compress_expr(testk);
if ( i == 1 && (texpr->expr_code == EXPR_VALUE && (texpr->expr_value & -65536L) == 0) &&
!testk->forward_reference )
{
return 1;
}
}
return 0;
}
static int check_am(Opcode *opc, AModes amdcdnum, AModes forced_am_num)
{
long amdcd;
EXPR_struct *exp_ptr;
#if 0
if ( squeak )
printf("check_am(): opc=%s, amodes=%08lX, amdcdnum=%d, forced_am_num=%d\n", opc->op_name, opc->op_amode, amdcdnum, forced_am_num);
#endif
if ( forced_am_num != 0 )
{
if ( forced_am_num == DES_NUM )
{ /* he only wants long mode */
switch (amdcdnum)
{
case UNDEF_NUM:
case A_NUM:
case AL_NUM:
amdcdnum = AL_NUM;
break;
case X_NUM:
case AX_NUM:
case ALX_NUM:
amdcdnum = ALX_NUM;
break;
default:
bad_token((char *)0, "No \"LONG\" addressing for this address mode");
}
}
else if ( forced_am_num == D_NUM )
{
switch (amdcdnum)
{
case UNDEF_NUM:
case D_NUM:
amdcdnum = D_NUM;
break;
case X_NUM:
amdcdnum = ZX_NUM;
break;
case Y_NUM:
amdcdnum = ZY_NUM;
break;
default:
bad_token((char *)0, "No \"DIRECT\" addressing for this address mode");
}
}
else if ( forced_am_num == A_NUM )
{
switch (amdcdnum)
{
case UNDEF_NUM:
case A_NUM:
amdcdnum = A_NUM;
break;
case X_NUM:
amdcdnum = AX_NUM;
break;
case Y_NUM:
amdcdnum = AY_NUM;
break;
default:
bad_token((char *)0, "No \"ABSOLUTE\" addressing for this address mode");
}
}
else
{
if ( amdcdnum != UNDEF_NUM && amdcdnum != forced_am_num )
{
bad_token((char *)0, "Conflicting address modes selected.");
}
amdcdnum = forced_am_num;
}
}
if ( amdcdnum == AC_NUM )
{ /* if no operand supplied */
if ( ((1L << amdcdnum) & opc->op_amode) == 0 )
{
bad_token((char *)0, "Opcode requires an operand");
return bad_amode();
}
return amdcdnum;
}
if ( EXP1.ptr < 0 || amdcdnum < 0 )
{ /* if bad am or exprs found an error */
return bad_amode();
}
amdcd = amdcdnum ? (1L << amdcdnum) : 0;
exp_ptr = EXP1.stack;
if ( options[QUAL_P816] )
{
static struct
{
AModes numz, numa, numl;
long maskz, maska, maskl;
} *tp, test[] = { { Z_NUM, A_NUM, AL_NUM, Z, A, AL },
{ ZX_NUM, AX_NUM, ALX_NUM, ZX, AX, ALX },
{ ZY_NUM, AY_NUM, 0, ZY, AY, 0 },
{ ND_NUM, N_NUM, 0, ND, N, 0 } };
if ( amdcdnum <= UNDEF_NUM )
tp = test;
else if ( amdcdnum == X_NUM )
tp = test + 1;
else if ( amdcdnum == Y_NUM )
tp = test + 2;
else if ( amdcdnum == N_NUM )
tp = test + 3;
else
tp = 0;
if ( tp != 0 )
do
{
if ( opc->op_amode & tp->maskz )
{ /* direct page */
if ( check_4_dpage(&EXP1) ||
((opc->op_amode & (tp->maska + tp->maskl)) == 0 ||
(!check_4_abs(&EXP1) && cpu_defam == CPU_DEFDIR)) )
{
amdcd = tp->maskz;
amdcdnum = tp->numz;
break;
}
}
if ( opc->op_amode & tp->maska )
{ /* absolute */
if ( check_4_abs(&EXP1) ||
((opc->op_amode & tp->maskl) == 0 || cpu_defam == CPU_DEFABS) )
{
amdcd = tp->maska;
amdcdnum = tp->numa;
break;
}
}
if ( opc->op_amode & tp->maskl )
{ /* long */
amdcd = tp->maskl;
amdcdnum = tp->numl;
}
} while ( 0 );
}
else
{
if ( amdcd == 0 )
{ /* if no address mode specified */
int maybeZPage=(opc->op_amode & Z); /* If Z page is even legal */
int ama=10;
if ( maybeZPage && options[QUAL_2_PASS] && pass )
{
/* We're running in 2 pass mode and this is pass 1 */
if ( (ama=getAMATag(current_fnd)) >= 0 )
maybeZPage = 0;
}
if ( squeak )
{
printf("check_am(): pass=%d, Checking for Z vs. A mode. maybeZPage=%d, ama=%d, EXP1.ptr=%d, EXP1.fwd=%d, exp->code=%d, exp->value=%08lX\n",
pass,
maybeZPage,
ama,
EXP1.ptr,
EXP1.forward_reference,
exp_ptr->expr_code,
exp_ptr->expr_value
);
}
if ( maybeZPage /* if can set Z mode */
&& (
(
(edmask & ED_AMA) == 0) /* not AMA mode (forced Z page) */
|| (EXP1.base_page_reference != 0) /* expression base page (forced Z page) */
|| (
(EXP1.ptr == 1) /* or expression is < 256 */
&& (exp_ptr->expr_code == EXPR_VALUE)
&& ((exp_ptr->expr_value & -256) == 0)
&& (EXP1.forward_reference == 0)
)
)
)
{
amdcd = Z; /* force it direct page */
amdcdnum = Z_NUM;
}
else
{
if ( !(opc->op_amode & A) )
{ /* if normal absolute not allowed */
amdcd = AL; /* force it absolute long */
amdcdnum = AL_NUM;
}
else
{
amdcd = A; /* else force it absolute */
amdcdnum = A_NUM;
}
if ( options[QUAL_2_PASS] && !pass && EXP1.forward_reference)
setAMATag(current_fnd,1); /* Say we chose a word for this instruction because of fwd reference */
}
}
else
{
if ( (amdcd & (X + Y)) )
{