-
Notifications
You must be signed in to change notification settings - Fork 0
/
assp4.c
1591 lines (1491 loc) · 47.2 KB
/
assp4.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
/*:ts=4 assp4.c
*
* cp4 - Commodore C+4 emulator
* Copyright (C) 1998 Gáti Gergely
*
* 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 2 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, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* e-mail: [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "macros.h"
#include "common.h"
#include "licence.h"
#undef INLINE
#define INLINE
#define ierr() fprintf(stderr,"%s: ***internal error in `%s' at line %d\n",progname,__FILE__,__LINE__)
/*
* Error codes
*/
#define ER_UNKNOWN 1
#define ER_BRANCHOUTOFRANGE 2
#define ER_LABELNOTFOUND 3
#define ER_INVALIDADDRESSING 4
#define ER_NOADDRESS 5
#define ER_TOOMANYMACRO 6
#define ER_NOMEM 7
#define ER_MACRONOTFOUND 8
char *VER="\0$VER: assp4 1.8 (22.12.99) Copyright © by Gáti Gergely";
char *errstr[]={
"ok",
"unknown",
"branch out of range",
"label not found: ",
"invalid addressing mode",
"no address part of symbol",
"too many use of macro",
"not enough memory",
"macro not found",
NULL
};
/*
* internal mnemonics
*/
char *imnm[]={
"DAT", // egy byte elhelyezése ( DAT [#]$12 )
"BSS", // sok byte elhelyezése ( BSS [#]$12[,$x] )
"STR", // sorvégéig string elhelyezése (petscii)
"MAC", // macro start
"<<<", // endmacro
"USE", // usemacro (nem kötelezõ)
"EQU", // define-hoz
"INL", // include
"XDF", // kívûlrõl is látható labelekhez
NULL
};
#define DAT 0
#define BSS 1
#define STR 2
#define MAC 3
#define END 4
#define USE 5
#define EQU 6
#define INL 7
#define XDF 8
/*
* Addressing modes
*/
#define ILL 0
#define BYTE 1
#define ABS 2
#define ZP 3
#define ACC 4
#define IMP 5
#define IZPX 6
#define IZPY 7
#define ZPX 8
#define ABSX 9
#define ABSY 10
#define REL 11
#define IABS 12
#define ZPY 13
/*
* Statements defines / names
*/
#define ADC 0
#define AND 1
#define ASL 2
#define BCC 3
#define BCS 4
#define BEQ 5
#define BIT 6
#define BMI 7
#define BNE 8
#define BPL 9
#define BRK 10
#define BVC 11
#define BVS 12
#define CLC 13
#define CLD 14
#define CLI 15
#define CLV 16
#define CMP 17
#define CPX 18
#define CPY 19
#define DEC 20
#define DEX 21
#define DEY 22
#define EOR 23
#define INC 24
#define INX 25
#define INY 26
#define JMP 27
#define JSR 28
#define LDA 29
#define LDX 30
#define LDY 31
#define LSR 32
#define NOP 33
#define ORA 34
#define PHA 35
#define PHP 36
#define PLA 37
#define PLP 38
#define ROL 39
#define ROR 40
#define RTI 41
#define RTS 42
#define SBC 43
#define SEC 44
#define SED 45
#define SEI 46
#define STA 47
#define STX 48
#define STY 49
#define TAX 50
#define TAY 51
#define TSX 52
#define TXA 53
#define TXS 54
#define TYA 55
#define XXX 56
#define LAB_GLB 0
#define LAB_SUB 1
// label-table
struct lnode;
struct macro;
struct ltab {
struct lnode *head;
struct fill *fhd;
};
struct lnode {
struct lnode *next;
char *name;
int addr; // normal (-1 -> xdef)
int lineno;
struct macro *mac; // MAC
int val; // EQU ($0000-$ffff) -1 invalid
};
struct fill {
struct fill *next;
char *label;
long filep;
int type;
int lineno;
int addr; // real address of the statement of the fill
struct macro *mac;
};
// `type' mezõhöz:
#define F_REL 0 // 1 byte
#define F_WORD 1 // 2 byte (lohi)
#define F_HI 2 // 1 byte (hi)
#define F_LO 3 // 1 byte (lo)
#define F_MACRO 4 // x byte
#define F_LOCAL 128 // 7. bit local hivatkozáshoz
struct macro {
struct macro *next;
char *name;
int len;
int lineno;
unsigned char *data;
int usecount;
struct ltab ltab;
};
struct mac {
int on; // macro mode? (1-ya)
unsigned char *mem; // alloced mem for macros
int size; // alloced mem size
int pnt; // size of macro
struct macro *first;// first macro
};
#define MACROMEMCHUNK 1024
struct global {
struct mac macros;
int startaddr;
struct lnode *actlab;
int foffs;
};
int assemble(FILE *fin,FILE *fp,struct ltab *lt,int startaddr);
// statement struct
struct stmn {
char mnm[4];
int from;
};
struct stmn statements[]={
{"ADC",0},{"AND",8},{"ASL",16},{"BCC",21},{"BCS",22},{"BEQ",23},{"BIT",24},{"BMI",26},
{"BNE",27},{"BPL",28},{"BRK",29},{"BVC",30},{"BVS",31},{"CLC",32},{"CLD",33},{"CLI",34},
{"CLV",35},{"CMP",36},{"CPX",44},{"CPY",47},{"DEC",50},{"DEX",54},{"DEY",55},{"EOR",56},
{"INC",64},{"INX",68},{"INY",69},{"JMP",70},{"JSR",72},{"LDA",73},{"LDX",81},{"LDY",86},
{"LSR",91},{"NOP",96},{"ORA",97},{"PHA",105},{"PHP",106},{"PLA",107},{"PLP",108},{"ROL",109},
{"ROR",114},{"RTI",119},{"RTS",120},{"SBC",121},{"SEC",129},{"SED",130},{"SEI",131},{"STA",132},
{"STX",139},{"STY",142},{"TAX",145},{"TAY",146},{"TSX",147},{"TXA",148},{"TXS",149},{"TYA",150},{"",-1}
};
// machine struct
struct asmstat {
int name;
int mode;
int len;
int code;
};
struct asmstat stat[]={
// NAME,MODE LEN CODE
//-------------------------------
{ ADC,ABS, 3, 109 },
{ ADC,ABSX, 3, 125 },
{ ADC,ABSY, 3, 121 },
{ ADC,BYTE, 2, 105 },
{ ADC,IZPX, 2, 97 },
{ ADC,IZPY, 2, 113 },
{ ADC,ZP, 2, 101 },
{ ADC,ZPX, 2, 117 },
{ AND,ABS, 3, 45 },
{ AND,ABSX, 3, 61 },
{ AND,ABSY, 3, 57 },
{ AND,BYTE, 2, 41 },
{ AND,IZPX, 2, 33 },
{ AND,IZPY, 2, 49 },
{ AND,ZP, 2, 37 },
{ AND,ZPX, 2, 53 },
{ ASL,ABS, 3, 14 },
{ ASL,ABSX, 2, 30 },
{ ASL,ACC, 1, 10 },
{ ASL,ZP, 2, 6 },
{ ASL,ZPX, 2, 22 },
{ BCC,REL, 2, 144 },
{ BCS,REL, 2, 176 },
{ BEQ,REL, 2, 240 },
{ BIT,ABS, 2, 44 },
{ BIT,ZP, 2, 36 },
{ BMI,REL, 2, 48 },
{ BNE,REL, 2, 208 },
{ BPL,REL, 2, 16 },
{ BRK,IMP, 1, 0 },
{ BVC,REL, 2, 80 },
{ BVS,REL, 2, 112 },
{ CLC,IMP, 1, 24 },
{ CLD,IMP, 1, 216 },
{ CLI,IMP, 1, 88 },
{ CLV,IMP, 1, 184 },
{ CMP,ABS, 3, 205 },
{ CMP,ABSX, 3, 221 },
{ CMP,ABSY, 3, 217 },
{ CMP,BYTE, 2, 201 },
{ CMP,IZPX, 2, 193 },
{ CMP,IZPY, 2, 209 },
{ CMP,ZP, 2, 197 },
{ CMP,ZPX, 2, 213 },
{ CPX,ABS, 3, 236 },
{ CPX,BYTE, 2, 224 },
{ CPX,ZP, 2, 228 },
{ CPY,ABS, 3, 204 },
{ CPY,BYTE, 2, 192 },
{ CPY,ZP, 2, 196 },
{ DEC,ABS, 3, 206 },
{ DEC,ABSX, 3, 222 },
{ DEC,ZP, 2, 198 },
{ DEC,ZPX, 2, 214 },
{ DEX,IMP, 1, 202 },
{ DEY,IMP, 1, 136 },
{ EOR,ABS, 3, 77 },
{ EOR,ABSX, 3, 93 },
{ EOR,ABSY, 3, 89 },
{ EOR,BYTE, 2, 73 },
{ EOR,IZPX, 2, 65 },
{ EOR,IZPY, 2, 81 },
{ EOR,ZP, 2, 69 },
{ EOR,ZPX, 2, 85 },
{ INC,ABS, 3, 238 },
{ INC,ABSX, 3, 254 },
{ INC,ZP, 2, 230 },
{ INC,ZPX, 2, 246 },
{ INX,IMP, 1, 232 },
{ INY,IMP, 1, 200 },
{ JMP,ABS, 3, 76 },
{ JMP,IABS, 3, 108 },
{ JSR,ABS, 3, 32 },
{ LDA,ABS, 3, 173 },
{ LDA,ABSX, 3, 189 },
{ LDA,ABSY, 3, 185 },
{ LDA,BYTE, 2, 169 },
{ LDA,IZPX, 2, 161 },
{ LDA,IZPY, 2, 177 },
{ LDA,ZP, 2, 165 },
{ LDA,ZPX, 2, 181 },
{ LDX,ABS, 3, 174 },
{ LDX,ABSY, 3, 190 },
{ LDX,BYTE, 2, 162 },
{ LDX,ZP, 2, 166 },
{ LDX,ZPY, 2, 182 },
{ LDY,ABS, 3, 172 },
{ LDY,ABSX, 3, 188 },
{ LDY,BYTE, 2, 160 },
{ LDY,ZP, 2, 164 },
{ LDY,ZPX, 2, 180 },
{ LSR,ABS, 3, 78 },
{ LSR,ABSX, 3, 94 },
{ LSR,ACC, 1, 74 },
{ LSR,ZP, 2, 70 },
{ LSR,ZPX, 2, 86 },
{ NOP,IMP, 1, 234 },
{ ORA,ABS, 3, 13 },
{ ORA,ABSX, 3, 29 },
{ ORA,ABSY, 3, 25 },
{ ORA,BYTE, 2, 9 },
{ ORA,IZPX, 2, 1 },
{ ORA,IZPY, 2, 17 },
{ ORA,ZP, 2, 5 },
{ ORA,ZPX, 2, 21 },
{ PHA,IMP, 1, 72 },
{ PHP,IMP, 2, 8 },
{ PLA,ACC, 1, 104 },
{ PLP,IMP, 1, 40 },
{ ROL,ABS, 3, 46 },
{ ROL,ABSX, 3, 62 },
{ ROL,ACC, 1, 42 },
{ ROL,ZP, 2, 38 },
{ ROL,ZPX, 2, 54 },
{ ROR,ABS, 3, 110 },
{ ROR,ABSX, 3, 126 },
{ ROR,ACC, 1, 106 },
{ ROR,ZP, 2, 102 },
{ ROR,ZPX, 2, 118 },
{ RTI,IMP, 1, 64 },
{ RTS,IMP, 2, 96 },
{ SBC,ABS, 3, 237 },
{ SBC,ABSX, 3, 253 },
{ SBC,ABSY, 3, 249 },
{ SBC,BYTE, 2, 233 },
{ SBC,IZPX, 2, 225 },
{ SBC,IZPY, 2, 241 },
{ SBC,ZP, 2, 229 },
{ SBC,ZPX, 3, 245 },
{ SEC,IMP, 1, 56 },
{ SED,IMP, 1, 248 },
{ SEI,IMP, 1, 120 },
{ STA,ABS, 3, 141 },
{ STA,ABSX, 3, 157 },
{ STA,ABSY, 3, 153 },
{ STA,IZPX, 2, 129 },
{ STA,IZPY, 2, 145 },
{ STA,ZP, 2, 133 },
{ STA,ZPX, 2, 149 },
{ STX,ABS, 3, 142 },
{ STX,ZP, 2, 134 },
{ STX,ZPY, 2, 150 },
{ STY,ABS, 3, 140 },
{ STY,ZP, 2, 132 },
{ STY,ZPX, 2, 148 },
{ TAX,IMP, 1, 170 },
{ TAY,IMP, 1, 168 },
{ TSX,IMP, 1, 186 },
{ TXA,IMP, 1, 138 },
{ TXS,IMP, 1, 154 },
{ TYA,IMP, 1, 152 }
};
/*
* Globals
* =======
*/
static char *progname; // argv[0]
static int branch[]={ BCC,BCS,BMI,BPL,BEQ,BNE,BVC,BVS,-1 };
static struct global *globals; // reentrant support
/* Options
*/
static int o_sym=0; // symbol-printing? (0=no)
/*
* összehasonlít két stringet (case insensitive)
*/
INLINE int istrcmp(char *s1,char *s2) {
while(toupper(*s1)==toupper(*s2)&&toupper(*s1)) {
s1++; s2++;
}
return(toupper(*s1)-toupper(*s2));
} // istrcmp()
/*
* összehasonlít két stringet (csak n char, case insensitive)
*/
INLINE int istrncmp(char *s1,char *s2,int n) {
while(toupper(*s1)==toupper(*s2)&&toupper(*s1)&&--n>0) {
s1++; s2++;
}
return(toupper(*s1)-toupper(*s2));
} // istrncmp()
/*
* kikeres egy mnemonikot a fenti tömbbõl
*/
INLINE struct asmstat *lookup(char *mnm) {
int i;
for(i=0;statements[i].from!=-1&&istrcmp(statements[i].mnm,mnm)!=0;i++);
if(statements[i].from==-1) return(NULL);
return(&stat[statements[i].from]);
} // lookup()
/*
* newtab
*/
struct ltab *newtab(void) {
struct ltab *t;
if(NULL==(t=malloc(sizeof(struct ltab)))) return(NULL);
t->head=NULL;
t->fhd=NULL;
return(t);
} // newtab()
/*
* droptab
*/
void droptab(struct ltab *lt) {
struct macro *mc,*mx;
struct lnode *n,*x;
struct fill *f,*g;
int numsym=0,i;
if(lt==NULL) return;
if(o_sym!=0) {
printf("\n\n TYPE VALUE LINE NAME\n");
for(numsym=0,n=lt->head;n!=NULL;n=x,numsym++) x=n->next;
for(;numsym>0;numsym--) {
for(i=1,n=lt->head;i<numsym;n=x,i++) x=n->next;
if(n->mac!=NULL) printf(" Macro - %4d `%s'\n",n->lineno,n->name);
else if(n->val>=0) printf(" Const $%04x - %4d `%s'\n",n->val,n->lineno,n->name);
else if(n->addr<0) printf(" Globl - %4d `%s'\n",n->lineno,&n->name[1]);
else printf(" Label $%04x - %4d `%s'\n",n->addr,n->lineno,n->name);
}
}
for(mc=globals->macros.first;mc!=NULL;mc=mx) {
mx=mc->next;
if(mc->name!=NULL) free(mc->name);
if(mc->data!=NULL) free(mc->data);
for(f=mc->ltab.fhd;f!=NULL;f=g) {
g=f->next;
if(f->label!=NULL) free(f->label);
free(f);
}
for(n=mc->ltab.head;n!=NULL;n=x) {
x=n->next;
if(n->name!=NULL) free(n->name);
free(n);
}
}
for(n=lt->head;n!=NULL;n=x) {
x=n->next;
if(n->name!=NULL) free(n->name);
free(n);
}
for(f=lt->fhd;f!=NULL;f=g) {
g=f->next;
if(f->label!=NULL) free(f->label);
free(f);
}
free(lt);
} // droptab()
/* lokális nevet kreál ->
* n1="hello"
* n2=".name"
* ret="hello/name"
*/
INLINE char *createlocname(char *n) {
char *r;
if(n[0]=='.') {
if(globals->actlab==NULL) return(NULL);
if(NULL==(r=malloc(strlen(n)+strlen(globals->actlab->name)))) return(r);
strcpy(r,globals->actlab->name);
strcat(r,"/");
strcat(r,n+1);
} else {
if(NULL==(r=malloc(strlen(n)+1))) return(r);
strcpy(r,n);
}
return(r);
}
/* macro bejegyzés kezdete
*/
int macstart(struct lnode *lb) {
struct macro *nm;
if(globals->macros.on!=0) return(-1);
if(NULL==(nm=malloc(sizeof(struct macro)))) return(-1);
nm->next=globals->macros.first;
if(NULL==(nm->name=malloc(strlen(lb->name)+1))) { free(nm); return(-1); }
strcpy(nm->name,lb->name);
nm->len=-1;
nm->lineno=0;
nm->data=NULL;
nm->ltab.fhd=NULL;
nm->usecount=0;
nm->ltab.head=NULL;
if(NULL==(globals->macros.mem=malloc(MACROMEMCHUNK))) { free(nm->name); free(nm); return(-1); }
globals->macros.size=MACROMEMCHUNK;
globals->macros.pnt=0;
globals->macros.first=nm;
globals->macros.on=1;
lb->mac=nm;
return(0);
}
/* macro bejegyzés vége
*/
void macend(void) {
if(globals->macros.on==0) return;
globals->macros.on=0;
globals->macros.first->len=globals->macros.pnt;
globals->macros.first->lineno=0;
globals->macros.first->data=globals->macros.mem;
globals->macros.mem=NULL;
globals->macros.size=0;
globals->macros.pnt=0;
}
INLINE int outbyte(int byte, FILE *fp) {
int ret=0;
if(globals->macros.on!=0) {
if(globals->macros.mem==NULL) return(-1);
if(globals->macros.pnt>=globals->macros.size) {
globals->macros.mem=realloc(globals->macros.mem,globals->macros.size+MACROMEMCHUNK);
if(NULL==globals->macros.mem) {
globals->macros.mem=NULL;
return(-1);
}
globals->macros.size+=MACROMEMCHUNK;
}
globals->macros.mem[globals->macros.pnt++]=(unsigned char)byte;
} else ret=fputc(byte,fp);
return(ret);
}
/* kikeresi a macrok közül a `name' nevût (vagy NULL)
*/
INLINE struct macro *macseek(char *name) {
struct macro *m;
char *n;
n=createlocname(name);
for(m=globals->macros.first;m!=NULL;m=m->next) if(strcmp(m->name,n)==0) break;
free(n);
return(m);
}
/* insert macro `name' into `fp'
* beszúr annyi üres byte-ot, amennyit a macro elfoglal
* ret=-1 if error
* >0 byte count in fp
*/
INLINE int macins(char *name,FILE *fp) {
struct macro *mc;
int i;
if(NULL==(mc=macseek(name))) return(-1);
if(mc->len<0) return(-1);
if(mc->len==0) return(0);
for(i=0;i<mc->len;i++) outbyte(mc->data[i],fp);
return(i);
}
/*
* labsea
*/
INLINE struct lnode *labsea(struct ltab *lt,char *lab) {
struct lnode *n;
if(lt==NULL||lab==NULL) return(NULL);
for(n=lt->head;n!=NULL&&strcmp(lab,n->name)!=0;n=n->next);
return(n);
} // labsea()
/*
* addlabel
*
* mode==LAB_GLB | LAB_SUB
*
* 0-OK, <0 hiba
*/
INLINE int addlabel(struct ltab *lt,char *lab,int addr,int lineno) {
struct lnode *n;
if(lt==NULL||lab==NULL) return(-1);
if(NULL==(n=malloc(sizeof(struct lnode)))) return(-1);
n->addr=addr;
n->lineno=lineno;
n->mac=NULL;
n->val=-1;
if(*lab!='.') globals->actlab=n;
if(NULL==(n->name=createlocname(lab))) { free(n); return(-1); }
if(NULL!=labsea(lt,n->name)) { free(n->name); free(n); return(-1); }
n->next=lt->head;
lt->head=n;
return(0);
} // addlabel()
/*
* addfill
*/
/* plusz:
ha macron belül van és lokális labelre hivatkozik:
akkor a hivatkozott label típusa
módosul: kap egy bitet, és plusz 4 chart a végére
*/
INLINE void addfill(struct ltab *lt,FILE *fp,int type,char *lab,int lineno, int addr) {
struct fill *nf;
struct macro *mc;
if(lt==NULL||fp==NULL) return;
if(NULL==(nf=malloc(sizeof(struct fill)))) return;
if(globals->macros.on==0) {
nf->next=lt->fhd;
nf->filep=ftell(fp);
nf->addr=addr;
} else {
nf->next=globals->macros.first->ltab.fhd;
nf->filep=globals->macros.pnt;
nf->addr=-1;
}
nf->type=type;
if(globals->macros.on!=0&&*lab=='.') nf->type|=F_LOCAL;
nf->lineno=lineno;
if(NULL==(nf->label=createlocname(lab))) { free(nf); return; }
if(globals->macros.on!=0&&*lab=='.') {
char *n;
if(NULL==(n=malloc(strlen(nf->label)+5))) { free(nf->label); free(nf); return; }
strcpy(n,nf->label);
strcat(n,"xxxx");
free(nf->label);
nf->label=n;
}
if(type==F_MACRO) {
mc=macseek(nf->label);
if(mc==NULL) { free(nf->label); free(nf); return; }
nf->mac=mc;
} else nf->mac=NULL;
if(globals->macros.on==0) lt->fhd=nf;
else globals->macros.first->ltab.fhd=nf;
} // addfill()
/*
* parseoperand
* operandust ismer fel,
* return: a megfelelõ asmstat bejegyzés, ha OK (NULL, hiba)
* input: az elsõ asmstat bejegyzés erre a parancsra
*/
struct asmstat *parseoperand(char *op,struct asmstat *st) {
struct asmstat *ret=NULL;
int nm;
switch(*op) {
case '$' : // ABSOLUTE
// abszolút
switch(strlen(op)) {
case 3 :
// ZP
for(nm=st->name;st->name==nm&&st->mode!=ZP;st++);
if(st->name==nm) ret=st;
break;
case 5 :
// ABS ZP,X ZP,Y
switch(op[strlen(op)-1]) {
case 'x' :
case 'X' :
if(op[strlen(op)-2]!=',') return(NULL);
for(nm=st->name;st->name==nm&&st->mode!=ZPX;st++);
if(st->name==nm) ret=st;
break;
case 'y' :
case 'Y' :
if(op[strlen(op)-2]!=',') return(NULL);
for(nm=st->name;st->name==nm&&st->mode!=ZPY;st++);
if(st->name==nm) ret=st;
break;
default :
// ABS, vagy REL ?
for(nm=0;branch[nm]!=-1&&branch[nm]!=st->name;nm++);
if(branch[nm]==-1) {
// ABS
for(nm=st->name;st->name==nm&&st->mode!=ABS;st++);
if(st->name==nm) ret=st;
} else {
// REL
for(nm=st->name;st->name==nm&&st->mode!=REL;st++);
if(st->name==nm) ret=st;
}
break;
}
break;
case 7 :
// ABS,X ABS,Y
switch(op[strlen(op)-1]) {
case 'x' :
case 'X' :
if(op[strlen(op)-2]!=',') return(NULL);
for(nm=st->name;st->name==nm&&st->mode!=ABSX;st++);
if(st->name==nm) ret=st;
break;
case 'y' :
case 'Y' :
if(op[strlen(op)-2]!=',') return(NULL);
for(nm=st->name;st->name==nm&&st->mode!=ABSY;st++);
if(st->name==nm) ret=st;
break;
}
break;
}
break;
case '(' : // INDIRECT
// (ZP,X) (ZP),Y (ABS) (label)
if(op[1]=='$') {
// ($ZP,X) ($ZP),Y ($ABS)
switch(op[strlen(op)-2]) {
case 'x' :
case 'X' :
// (ZP,X)
if(op[strlen(op)-1]!=')') return(NULL);
for(nm=st->name;st->name==nm&&st->mode!=IZPX;st++);
if(st->name==nm) ret=st;
break;
case ',' :
// (ZP),Y
if(toupper(op[strlen(op)-1])!='Y') return(NULL);
for(nm=st->name;st->name==nm&&st->mode!=IZPY;st++);
if(st->name==nm) ret=st;
break;
default :
// (ABS)
for(nm=st->name;st->name==nm&&st->mode!=IABS;st++);
if(st->name==nm) ret=st;
break;
}
} else {
// (label) --> IABS
for(nm=st->name;st->name==nm&&st->mode!=IABS;st++);
if(st->name==nm) ret=st;
break;
}
break;
case '#' : // BYTE DATA
for(nm=st->name;st->name==nm&&st->mode!=BYTE;st++);
if(st->name==nm) ret=st;
break;
case 'a' : // ACCU
case 'A' :
if(strlen(op)==1) {
for(nm=st->name;st->name==nm&&st->mode!=ACC;st++);
if(st->name==nm) ret=st;
break;
}
default :
// label
// lehet 1 byte, ha Bxx
// vagy 2 byte, ha más
// el kell dönteni, hogy mi ez:
// label : ABS
// label,X : ABS,X
// label,Y : ABS,Y
if(op[strlen(op)-2]==',') {
// label,X label,Y
switch(op[strlen(op)-1]) {
case 'x' :
case 'X' :
// label,X
for(nm=st->name;st->name==nm&&st->mode!=ABSX;st++);
if(st->name==nm) ret=st;
break;
case 'y' :
case 'Y' :
// label,Y
for(nm=st->name;st->name==nm&&st->mode!=ABSY;st++);
if(st->name==nm) ret=st;
break;
default :
return(NULL);
break;
}
} else {
// label
for(nm=st->name;st->name==nm&&st->mode!=ABS&&st->mode!=REL;st++);
if(st->name==nm) ret=st;
}
break;
}
return(ret);
} // parseoperand()
/* beolvas egy számot (vagy equ értéket)
* ret: ahol az olvasás végetért
*/
INLINE char *getnumber(struct ltab *lt,char *pre,char *str,char *post, int *val) {
struct lnode *ln;
int pl=0,i;
char *bf,*n;
char t;
if(pre!=NULL) {
if(0!=(istrncmp(str,pre,strlen(pre)))) return(NULL);
str+=strlen(pre);
}
if(post!=NULL) pl=strlen(post);
if(*str=='$') {
if(NULL==(bf=malloc(pl+5))) return(NULL);
strcpy(bf,"$%x");
if(post!=NULL) strcat(bf,post);
sscanf(str,bf,val);
free(bf);
} else if(isdigit(*str)) {
// decimal?
*val=atoi(str);
} else {
// equ-value?
if(post==NULL) t='\0';
else t=post[0];
if(NULL==(bf=malloc(strlen(str)+1))) return(NULL);
for(i=0;str[i]!=t;i++) bf[i]=str[i];
bf[i]='\0';
if(NULL==(n=createlocname(bf))) { free(bf); return(NULL); }
ln=labsea(lt,n);
if(ln==NULL||ln->val<0) { free(n); free(bf); return(NULL); }
*val=ln->val;
free(n);
free(bf);
}
return(str);
}
/*
* output
* fp-be kiírja a byteokat, ahol cimkék vannak, oda felvesz egy 'fill'-t
* ret: 0, vagy error code (errstr index)
*/
int output(struct ltab *lt,struct asmstat *st,char *op,FILE *fp,int addr,int lineno) {
int o1;
if(st!=NULL) {
outbyte(st->code,fp);
switch(st->mode) {
case ACC :
// 0
break;
case IMP :
// 0
break;
case BYTE :
// 1
switch(op[1]) {
case '$' : // normal
getnumber(lt,"#",op,0,&o1);
outbyte(o1,fp);
break;
case '<' : // lo
addfill(lt,fp,F_LO,op+2,lineno,addr);
outbyte(0,fp);
break;
case '>' : // hi
addfill(lt,fp,F_HI,op+2,lineno,addr);
outbyte(0,fp);
break;
case 39 : // (') char
outbyte(op[2],fp);
break;
default :
if(NULL!=getnumber(lt,"#",op,0,&o1)) outbyte(o1,fp);
else return(ER_INVALIDADDRESSING);
break;
}
break;
case ZP :
// 1
if(NULL!=getnumber(lt,0,op,0,&o1)) outbyte(o1,fp);
else return(ER_INVALIDADDRESSING);
break;
case ZPX :
// 1
if(NULL!=getnumber(lt,0,op,",x",&o1)) outbyte(o1,fp);
else return(ER_INVALIDADDRESSING);
break;
case ZPY :
// 1
if(NULL!=getnumber(lt,0,op,",y",&o1)) outbyte(o1,fp);
else return(ER_INVALIDADDRESSING);
break;
case IZPX :
// 1
if(NULL!=getnumber(lt,"(",op,",x)",&o1)) outbyte(o1,fp);
else return(ER_INVALIDADDRESSING);
break;
case IZPY :
// 1
if(NULL!=getnumber(lt,"(",op,"),y",&o1)) outbyte(o1,fp);
else return(ER_INVALIDADDRESSING);
break;
case REL :
// 1
addr+=2;
if(*op=='$') {
// count
getnumber(lt,0,op,0,&o1);
o1=o1-addr;
if(o1>127||o1<-128) return(ER_BRANCHOUTOFRANGE);
outbyte(o1,fp);
} else {
// label
addfill(lt,fp,F_REL,op,lineno,addr);
outbyte(0,fp);
}
break;
case ABS :
// 2
if(*op=='$') {
getnumber(lt,0,op,0,&o1);
outbyte(o1&0xff,fp);
outbyte(o1>>8,fp);
} else {
// label
addfill(lt,fp,F_WORD,op,lineno,addr);
outbyte(0,fp);
outbyte(0,fp);
}
break;
case ABSX :
case ABSY :
if(*op=='$') {
getnumber(lt,0,op,0,&o1);
outbyte(o1&0xff,fp);
outbyte(o1>>8,fp);