-
Notifications
You must be signed in to change notification settings - Fork 24
/
asm6f.c
2959 lines (2688 loc) · 70.4 KB
/
asm6f.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
/* asm6f - asm6 with modifications for NES/Famicom development */
/* asm6f History:
1.6 + f003
* [controllerhead] +/- labels do not break @local scope
* [controllerhead] Added support for newer/older Mesen-compatible (.mlb) label export.
* [dttdndn] fix for famistudio sound engine
* [morskoyzmey] Absolute addressing support for zero page addresses.
1.6 + f002 (March 10, 2018)
* [nicklausw] Added new directives for INES header generation.
* [nicklausw] Put unstable/highly unstable opcode use behind directives,
instead of requiring separate executables.
* [nicklausw] Add support for Lua symbol file generation.
* [Sour] Add support for .cdl file generation, for use with FCEUX and Mesen.
* [Sour] Add support for Mesen-compatible (.mlb) label export.
* [freem] Fixed issue where the last symbol would not appear in an .nl file.
1.6 + freem modifications (June 24, 2015)
Added preliminary support for undocumented/illegal opcodes.
Added sonder's code to export the .nl files for FCEUX symbolic debugging.
*/
/* asm6 History:
1.6
Prevent error overload by emitting 2 bytes when branch instructions fail to parse
Bugfix for negative numbers being parsed incorrectly after too many passes are made
Compatible with big-endian and 64-bit machines
-q (quiet mode) command line option added
Lots of miscellaneous code cleanup
1.51
Added extra INCBIN args
1.5
Added local labels
Added DL, DH directives
Added ERROR directive
Bugfix for ACC opcode type
1.4
Decided to start keeping a history.
Added some special handling of IFDEF in expandline()
Changed macro usage: no parentheses around arg list, and args must be comma separated.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <stdarg.h>
#include <limits.h>
#define VERSION "1.6"
#define addr firstlabel.value // '$' value
#define NOORIGIN -0x40000000 // nice even number so aligning works before origin is defined
#define INITLISTSIZE 128 // initial label list size
#define BUFFSIZE 8192 // file buffer (inputbuff, outputbuff) size
#define WORDMAX 128 // used with getword()
#define LINEMAX 2048 // plenty of room for nested equates
#define MAXPASSES 7 // # of tries before giving up
#define IFNESTS 32 // max nested IF levels
#define DEFAULTFILLER 0 // default fill value
#define LOCALCHAR '@'
static void* true_ptr = &true_ptr;
enum labeltypes {LABEL,VALUE,EQUATE,MACRO,RESERVED};
enum cdltypes {NONE=0,CODE=1,DATA=2};
// LABEL: known address
// VALUE: defined with '='
// EQUATE: made with EQU
// MACRO: macro (duh)
// RESERVED: reserved word
typedef struct {
const char *name; //label name
// ptrdiff_t so it can hold function pointer on 64-bit machines
ptrdiff_t value; //PC (label), value (equate), param count (macro), funcptr (reserved)
// [freem addition (from asm6_sonder.c)]
int pos; // location in file; used to determine bank when exporting labels
char *line; //for macro or equate, also used to mark unknown label
//*next:text->*next:text->..
//for macros, the first <value> lines hold param names
//for opcodes (reserved), this holds opcode definitions, see initlabels
int type; //labeltypes enum (see above)
int used; //for EQU and MACRO recursion check
int pass; //when label was last defined
int scope; //where visible (0=global, nonzero=local)
int ignorenl; //[freem addition] output this label in .nl files? (0=yes, nonzero=no)
void *link; //labels that share the same name (local labels) are chained together
} label;
label firstlabel={ //'$' label
"$",//*name
0,//value
0,//[freem edit (from asm6_sonder.c)] pos
(char*)&true_ptr,//*line
VALUE,//type
0,//used
0,//pass
0,//scope
0,//[freem addition] ignorenl
0,//link
};
typedef struct {
char *text;
int pos;
} comment;
typedef unsigned char byte;
typedef void (*icfn)(label*,char**);
#define fatal_error(fmt, args...) { fprintf(stderr, "\nError: " fmt "\n\n", ## args); DIE(); }
#define message(fmt, args...) if (verbose) { printf(fmt, ## args); }
//unstable instruction allowance
int allowunstable = 0;
int allowhunstable = 0;
//[nicklausw] ines stuff
int ines_include = 0;
int inesprg_num = 0;
int ineschr_num = 0;
int inesmir_num = 0;
int inesmap_num = 0;
int use_nes2 = 0;
int nes2chr_num = 0;
int nes2prg_num = 0;
int nes2sub_num = 0;
int nes2tv_num = 0;
int nes2vs_num = 0;
int nes2wram_num = 0;
int nes2bram_num = 0;
int nes2chrbram_num = 0;
void inesprg(label*, char**);
void ineschr(label*, char**);
void inesmir(label*, char**);
void inesmap(label*, char**);
void nes2chrram(label*, char**);
void nes2prgram(label*, char**);
void nes2sub(label*, char**);
void nes2tv(label*, char**);
void nes2vs(label*, char**);
void nes2bram(label*, char**);
void nes2chrbram(label*, char**);
void DIE(void);
label *findlabel(const char*);
void initlabels(void);
label *newlabel(void);
void getword(char*,char**,int);
int getvalue(char**);
int getoperator(char**);
int eval(char**,int);
label *getreserved(char**);
int getlabel(char*,char**);
void processline(char*,char*,int);
void listline(char*,char*);
void endlist(void);
void opcode(label*,char**);
void org(label*,char**);
void base(label*,char**);
void pad(label*,char**);
void equ(label*,char**);
void equal(label*,char**);
void nothing(label*,char**);
void include(label*,char**);
void incbin(label*,char**);
void dw(label*,char**);
void db(label*,char**);
void dl(label*,char**);
void dh(label*,char**);
void hex(label*,char**);
void dsw(label*,char**);
void dsb(label*,char**);
void align(label*,char**);
void _if(label*,char**);
void ifdef(label*,char**);
void ifndef(label*,char**);
void elseif(label*,char**);
void _else(label*,char**);
void endif(label*,char**);
void macro(label*,char**);
void endm(label*,char**);
void endr(label*,char**);
void rept(label*,char**);
void _enum(label*,char**);
void ende(label*,char**);
void ignorenl(label*,char**); // [freem addition] "ignorenl"
void endinl(label*,char**); // [freem addition] "endinl"
void fillval(label*,char**);
void expandmacro(label*,char**,int,char*);
void expandrept(int,char*);
void make_error(label*,char**);
void unstable(label*,char**);
void hunstable(label*,char**);
void *my_malloc(size_t);
char *my_strupr(char*);
int hexify(int);
char *strend(char*,char*);
void getfilename(char*,char**);
char *expandline(char*,char*);
int eatchar(char**,char);
void reverse(char*,char *);
void export_labelfiles(void);
void export_lua(void);
int comparelabels(const void* arg1, const void* arg2);
int comparecomments(const void* arg1, const void* arg2);
void export_mesenlabels(void);
void addlabel(char*,int);
void initcomments(void);
void growcommentlist(void);
void addcomment(char*);
void growlist(void);
void showerror(char*,int);
void processfile(FILE*,char*);
void showhelp(void);
void output(byte*,int,int);
void filler(int,char**);
// [freem addition (from asm6_sonder.c)]
int filepos=0;
enum optypes {ACC,IMM,IND,INDX,INDY,ZPX,ZPY,ABSX,ABSY,ZP,ABS,REL,IMP};
int opsize[]={0,1,2,1,1,1,1,2,2,1,2,1,0};
char ophead[]={0,'#','(','(','(',0,0,0,0,0,0,0,0};
const char *optail[]={"A","",")",",X)","),Y",",X",",Y",",X",",Y","","","",""};
byte brk[]={0x00,IMM,0x00,ZP,0x00,IMP,-1,-1};
byte ora[]={0x09,IMM,0x01,INDX,0x11,INDY,0x15,ZPX,0x1d,ABSX,0x19,ABSY,0x05,ZP,0x0d,ABS,-1,-1};
byte asl[]={0x0a,ACC,0x16,ZPX,0x1e,ABSX,0x06,ZP,0x0e,ABS,0x0a,IMP,-1,-1};
byte php[]={0x08,IMP,-1,-1};
byte bpl[]={0x10,REL,-1,-1};
byte clc[]={0x18,IMP,-1,-1};
byte jsr[]={0x20,ABS,-1,-1};
byte and[]={0x29,IMM,0x21,INDX,0x31,INDY,0x35,ZPX,0x3d,ABSX,0x39,ABSY,0x25,ZP,0x2d,ABS,-1,-1};
byte bit[]={0x24,ZP,0x2c,ABS,-1,-1};
byte rol[]={0x2a,ACC,0x36,ZPX,0x3e,ABSX,0x26,ZP,0x2e,ABS,0x2a,IMP,-1,-1};
byte plp[]={0x28,IMP,-1,-1};
byte bmi[]={0x30,REL,-1,-1};
byte sec[]={0x38,IMP,-1,-1};
byte rti[]={0x40,IMP,-1,-1};
byte eor[]={0x49,IMM,0x41,INDX,0x51,INDY,0x55,ZPX,0x5d,ABSX,0x59,ABSY,0x45,ZP,0x4d,ABS,-1,-1};
byte lsr[]={0x4a,ACC,0x56,ZPX,0x5e,ABSX,0x46,ZP,0x4e,ABS,0x4a,IMP,-1,-1};
byte pha[]={0x48,IMP,-1,-1};
byte jmp[]={0x6c,IND,0x4c,ABS,-1,-1};
byte bvc[]={0x50,REL,-1,-1};
byte cli[]={0x58,IMP,-1,-1};
byte rts[]={0x60,IMP,-1,-1};
byte adc[]={0x69,IMM,0x61,INDX,0x71,INDY,0x75,ZPX,0x7d,ABSX,0x79,ABSY,0x65,ZP,0x6d,ABS,-1,-1};
byte ror[]={0x6a,ACC,0x76,ZPX,0x7e,ABSX,0x66,ZP,0x6e,ABS,0x6a,IMP,-1,-1};
byte pla[]={0x68,IMP,-1,-1};
byte bvs[]={0x70,REL,-1,-1};
byte sei[]={0x78,IMP,-1,-1};
byte sta[]={0x81,INDX,0x91,INDY,0x95,ZPX,0x9d,ABSX,0x99,ABSY,0x85,ZP,0x8d,ABS,-1,-1};
byte sty[]={0x94,ZPX,0x84,ZP,0x8c,ABS,-1,-1};
byte stx[]={0x96,ZPY,0x86,ZP,0x8e,ABS,-1,-1};
byte dey[]={0x88,IMP,-1,-1};
byte txa[]={0x8a,IMP,-1,-1};
byte bcc[]={0x90,REL,-1,-1};
byte tya[]={0x98,IMP,-1,-1};
byte txs[]={0x9a,IMP,-1,-1};
byte ldy[]={0xa0,IMM,0xb4,ZPX,0xbc,ABSX,0xa4,ZP,0xac,ABS,-1,-1};
byte lda[]={0xa9,IMM,0xa1,INDX,0xb1,INDY,0xb5,ZPX,0xbd,ABSX,0xb9,ABSY,0xa5,ZP,0xad,ABS,-1,-1};
byte ldx[]={0xa2,IMM,0xb6,ZPY,0xbe,ABSY,0xa6,ZP,0xae,ABS,-1,-1};
byte tay[]={0xa8,IMP,-1,-1};
byte tax[]={0xaa,IMP,-1,-1};
byte bcs[]={0xb0,REL,-1,-1};
byte clv[]={0xb8,IMP,-1,-1};
byte tsx[]={0xba,IMP,-1,-1};
byte cpy[]={0xc0,IMM,0xc4,ZP,0xcc,ABS,-1,-1};
byte cmp[]={0xc9,IMM,0xc1,INDX,0xd1,INDY,0xd5,ZPX,0xdd,ABSX,0xd9,ABSY,0xc5,ZP,0xcd,ABS,-1,-1};
byte dec[]={0xd6,ZPX,0xde,ABSX,0xc6,ZP,0xce,ABS,-1,-1};
byte iny[]={0xc8,IMP,-1,-1};
byte dex[]={0xca,IMP,-1,-1};
byte bne[]={0xd0,REL,-1,-1};
byte cld[]={0xd8,IMP,-1,-1};
byte cpx[]={0xe0,IMM,0xe4,ZP,0xec,ABS,-1,-1};
byte sbc[]={0xe9,IMM,0xe1,INDX,0xf1,INDY,0xf5,ZPX,0xfd,ABSX,0xf9,ABSY,0xe5,ZP,0xed,ABS,-1,-1};
byte inc[]={0xf6,ZPX,0xfe,ABSX,0xe6,ZP,0xee,ABS,-1,-1};
byte inx[]={0xe8,IMP,-1,-1};
byte nop[]={0xea,IMP,-1,-1};
byte beq[]={0xf0,REL,-1,-1};
byte sed[]={0xf8,IMP,-1,-1};
// asm6f addition:
/* Undocumented/Illegal Opcodes (NMOS 6502 only!) */
// names/information taken from http://www.oxyron.de/html/opcodes02.html
byte slo[]={0x03,INDX,0x13,INDY,0x17,ZPX,0x1f,ABSX,0x1b,ABSY,0x07,ZP,0x0f,ABS,-1,-1};
byte rla[]={0x23,INDX,0x33,INDY,0x37,ZPX,0x3f,ABSX,0x3b,ABSY,0x27,ZP,0x2f,ABS,-1,-1};
byte sre[]={0x43,INDX,0x53,INDY,0x57,ZPX,0x5f,ABSX,0x5b,ABSY,0x47,ZP,0x4f,ABS,-1,-1};
byte rra[]={0x63,INDX,0x73,INDY,0x77,ZPX,0x7f,ABSX,0x7b,ABSY,0x67,ZP,0x6f,ABS,-1,-1};
byte sax[]={0x83,INDX,0x97,ZPY,0x87,ZP,0x8f,ABS,-1,-1};
byte lax[]={0xa3,INDX,0xb3,INDY,0xb7,ZPY,0xbf,ABSY,0xa7,ZP,0xaf,ABS,-1,-1};
byte dcp[]={0xc3,INDX,0xd3,INDY,0xd7,ZPX,0xdf,ABSX,0xdb,ABSY,0xc7,ZP,0xcf,ABS,-1,-1};
byte isc[]={0xe3,INDX,0xf3,INDY,0xf7,ZPX,0xff,ABSX,0xfb,ABSY,0xe7,ZP,0xef,ABS,-1,-1};
byte anc[]={0x0b,IMM,-1,-1}; // has duplicate at 0x2b
byte alr[]={0x4b,IMM,-1,-1};
byte arr[]={0x6b,IMM,-1,-1};
byte axs[]={0xcb,IMM,-1,-1};
byte las[]={0xbb,ABSY,-1,-1};
// "unstable in certain matters":
byte ahx[]={0x93,INDY,0x9f,ABSY,-1,-1};
byte shy[]={0x9c,ABSX,-1,-1};
byte shx[]={0x9e,ABSY,-1,-1};
byte tas[]={0x9b,ABSY,-1,-1};
// "highly unstable (results are not predictable on some machines)":
byte xaa[]={0x8b,IMM,-1,-1};
//byte lax[]={0xab,IMM,-1,-1};
const void *rsvdlist[]={ //all reserved words
"BRK",brk,
"PHP",php,
"BPL",bpl,
"CLC",clc,
"JSR",jsr,
"PLP",plp,
"BMI",bmi,
"SEC",sec,
"RTI",rti,
"PHA",pha,
"BVC",bvc,
"CLI",cli,
"RTS",rts,
"PLA",pla,
"BVS",bvs,
"SEI",sei,
"DEY",dey,
"BCC",bcc,
"TYA",tya,
"LDY",ldy,
"TAY",tay,
"BCS",bcs,
"CLV",clv,
"CPY",cpy,
"INY",iny,
"BNE",bne,
"CLD",cld,
"CPX",cpx,
"INX",inx,
"BEQ",beq,
"SED",sed,
"ORA",ora,
"AND",and,
"EOR",eor,
"ADC",adc,
"STA",sta,
"LDA",lda,
"CMP",cmp,
"SBC",sbc,
"ASL",asl,
"ROL",rol,
"LSR",lsr,
"ROR",ror,
"TXA",txa,
"TXS",txs,
"LDX",ldx,
"TAX",tax,
"TSX",tsx,
"DEX",dex,
"NOP",nop,
"BIT",bit,
"JMP",jmp,
"STY",sty,
"STX",stx,
"DEC",dec,
"INC",inc,
/* begin undocumented/illegal opcodes */
"SLO",slo,
"RLA",rla,
"SRE",sre,
"RRA",rra,
"SAX",sax,
"LAX",lax,
"DCP",dcp,
"ISC",isc,
"ANC",anc,
"ALR",alr,
"ARR",arr,
"AXS",axs,
"LAS",las,
/* somewhat unstable instructions */
"AHX",ahx,
"SHY",shy,
"SHX",shx,
"TAS",tas,
/* highly unstable instructions */
"XAA",xaa,
/* end list */
0, 0
};
const char *unstablelist[]={
"AHX", "SHY", "SHX", "TAS"
};
struct {
const char* name;
void (*func)( label*, char** );
} directives[]={
{"",nothing},
{"IF",_if},
{"ELSEIF",elseif},
{"ELSE",_else},
{"ENDIF",endif},
{"IFDEF",ifdef},
{"IFNDEF",ifndef},
{"=",equal},
{"EQU",equ},
{"ORG",org},
{"BASE",base},
{"PAD",pad},
{"INCLUDE",include},{"INCSRC",include},
{"INCBIN",incbin},{"BIN",incbin},
{"HEX",hex},
{"WORD",dw},{"DW",dw},{"DCW",dw},{"DC.W",dw},
{"BYTE",db},{"DB",db},{"DCB",db},{"DC.B",db},
{"DSW",dsw},{"DS.W",dsw},
{"DSB",dsb},{"DS.B",dsb},
{"ALIGN",align},
{"MACRO",macro},
{"REPT",rept},
{"ENDM",endm},
{"ENDR",endr},
{"ENUM",_enum},
{"ENDE",ende},
{"IGNORENL",ignorenl},
{"ENDINL",endinl},
{"FILLVALUE",fillval},
{"DL",dl},
{"DH",dh},
{"ERROR",make_error},
{"INESPRG",inesprg},
{"INESCHR",ineschr},
{"INESMIR",inesmir},
{"INESMAP",inesmap},
{"NES2CHRRAM",nes2chrram},
{"NES2PRGRAM",nes2prgram},
{"NES2SUB",nes2sub},
{"NES2TV",nes2tv},
{"NES2VS",nes2vs},
{"NES2BRAM",nes2bram},
{"NES2CHRBRAM",nes2chrbram},
{"UNSTABLE",unstable},
{"HUNSTABLE",hunstable},
{0, 0}
};
char OutOfRange[]="Value out of range.";
char SeekOutOfRange[]="Seek position out of range.";
char BadIncbinSize[]="INCBIN size is out of range.";
char NotANumber[]="Not a number.";
char UnknownLabel[]="Unknown label.";
char Illegal[]="Illegal instruction.";
char IncompleteExp[]="Incomplete expression.";
char LabelDefined[]="Label already defined.";
char MissingOperand[]="Missing operand.";
char DivZero[]="Divide by zero.";
char BadAddr[]="Can't determine address.";
char NeedName[]="Need a name.";
char CantOpen[]="Can't open file.";
char CantRead[]="Can't read file (possible I/O error).";
char ExtraENDM[]="ENDM without MACRO.";
char ExtraENDR[]="ENDR without REPT.";
char ExtraENDE[]="ENDE without ENUM.";
char ExtraENDINL[]="ENDINL without IGNORENL.";
char RecurseMACRO[]="Recursive MACRO not allowed.";
char RecurseEQU[]="Recursive EQU not allowed.";
char NoENDIF[]="Missing ENDIF.";
char NoENDM[]="Missing ENDM.";
char NoENDR[]="Missing ENDR.";
char NoENDE[]="Missing ENDE.";
char NoENDINL[]="Missing ENDINL.";
char IfNestLimit[]="Too many nested IFs.";
char undefinedPC[]="PC is undefined (use ORG first)";
char whitesp[]=" \t\r\n:"; //treat ":" like whitespace (for labels)
char whitesp2[]=" \t\r\n\""; //(used for filename processing)
char tmpstr[LINEMAX]; //all purpose big string
int pass=0;
int scope;//current scope, 0=global
int nextscope;//next nonglobal scope (increment on each new block of localized code)
int lastchance=0;//set on final attempt
int needanotherpass;//still need to take care of some things..
int error=0;//hard error (stop assembly after this pass)
char **makemacro=0;//(during macro creation) where next macro line will go. 1 to skip past macro
char **makerept;//like makemacro.. points to end of string chain
int reptcount=0;//counts rept statements during rept string storage
int iflevel=0;//index into ifdone[],skipline[]
int ifdone[IFNESTS];//nonzero if current IF level has been true
int skipline[IFNESTS];//1 on an IF statement that is false
const char *errmsg;
char *inputfilename=0;
char *outputfilename=0;
char *listfilename=0;
char *cdlfilename=0;
int verboselisting=0;//expand REPT loops in listing
int genfceuxnl=0;//[freem addition] generate FCEUX .nl files for symbolic debugging
int genmesenlabels=0; //generate label files for use with Mesen
int gencdl=0; //generate CDL file
int genlua=0;//generate lua symbol file
const char *listerr=0;//error message for list file
label *labelhere;//points to the label being defined on the current line (for EQU, =, etc)
FILE *listfile=0;
FILE *outputfile=0;
FILE *cdlfile=0;
byte outputbuff[BUFFSIZE];
byte inputbuff[BUFFSIZE];
int outcount;//bytes waiting in outputbuff
label **labellist; //array of label pointers (list starts from center and grows outward)
int labels;//# of labels in labellist
int maxlabels;//max # of labels labellist can hold
int labelstart;//index of first label
int labelend;//index of last label
label *lastlabel;//last label created
comment **comments;
int commentcount;
int commentcapacity;
int lastcommentpos = -1;
int nooutput=0;//supress output (use with ENUM)
int nonl=0;//[freem addition] supress output to .nl files
int defaultfiller;//default fill value
int insidemacro=0;//macro/rept is being expanded
int verbose=1;
static void* ptr_from_bool( int b )
{
if ( b )
return true_ptr;
return NULL;
}
// Prints printf-style message to stderr then exits.
// Used by fatal_error() macro.
void DIE(void)
{
if ( outputfile != NULL ) {
fclose( outputfile );
remove( outputfilename );
}
exit( EXIT_FAILURE );
}
// Same as malloc(), but prints error and exits if allocation fails
void* my_malloc( size_t s )
{
char* p = malloc( s ? s : 1 );
if ( p == NULL )
fatal_error( "out of memory" );
return p;
}
// Same as common strdup(), but prints error and exits if allocation fails
static char* my_strdup(const char *in)
{
size_t size = strlen( in ) + 1;
char* out = my_malloc( size );
memcpy( out, in, size );
return out;
}
//-------------------------------------------------------
//parsing functions
//-------------------------------------------------------
// Not all systems support this, so we implement our own always.
// More problematic to try to use the system's version rather than
// ours in all cases.
char *my_strupr(char *string)
{
char *s;
if (string == NULL) {
return (char *)NULL;
}
for (s = string; *s; ++s) {
*s = toupper((unsigned char) *s);
}
return string;
}
int hexify(int i) {
if(i>='0' && i<='9') {
return i-'0';
} else if(i>='a' && i<='f') {
return i-('a'-10);
} else if(i>='A' && i<='F') {
return i-('A'-10);
} else {
errmsg=NotANumber;
return 0;
}
}
#define eatwhitespace(str) (*str+=strspn(*str,whitesp))
//find end of str, excluding any chars in whitespace
char *strend(char *str, char *whitespace) {
char c;
char *w=whitespace;
char *end=str+strlen(str);
while(*w && end!=str) {
for(w=whitespace, c=end[-1]; *w; w++) {
if(*w==c) {
end--;
break;
}
}
}
return end;
}
//decode str into a number
//set errmsg on error
char gvline[WORDMAX];
int dependant;//set to nonzero if symbol couldn't be resolved
int chars; // [morskoyzmey absolute addressing change]
int absolute = 0; // [morskoyzmey absolute addressing change]
int getvalue(char **str) {
char *s,*end;
int ret,j;
label *p;
getword(gvline,str,1);
s=gvline;
if(!*s) {
errmsg=MissingOperand;
return 0;
}
ret=chars=0;
if(*s=='$') { //hex---------------------
s++;
if(!*s) {
ret=addr;//$ by itself is the PC
} else do {
hexi: j=hexify(*s);
s++;
chars++;
ret=(ret<<4)|j;
} while(*s);
if(chars>8)
errmsg=OutOfRange;
} else if(*s=='%') { //binary----------------------
s++;
do {
bin: j=*s;
s++;
chars++;
j-='0';
if(j>1) {
errmsg=NotANumber;
}
ret=(ret<<1)|j;
} while(*s);
if(chars>32)
errmsg=OutOfRange;
} else if(*s=='\'') { //char-----------------
s++;
if(*s=='\\') s++;
ret=*s;
s++;
if(*s!='\'')
errmsg=NotANumber;
} else if(*s=='"') { //char 2-----------------
s++;
if(*s=='\\') s++;
ret=*s;
s++;
if(*s!='"')
errmsg=NotANumber;
} else if(*s>='0' && *s<='9') {//number--------------
end=s+strlen(s)-1;
if(strspn(s,"0123456789")==strlen(s))
ret=atoi(s);
else if(*end=='b' || *end=='B') {
*end=0;
goto bin;
} else if(*end=='h' || *end=='H') {
*end=0;
goto hexi;
} else
errmsg=NotANumber;
} else { //label---------------
p=findlabel(gvline);
if(!p) {//label doesn't exist (yet?)
needanotherpass=dependant=1;
if(lastchance) {//only show error once we're certain label will never exist
errmsg=UnknownLabel;
}
} else {
dependant|=!p->line;
needanotherpass|=!p->line;
if(p->type==LABEL || p->type==VALUE) {
ret=p->value;
} else if(p->type==MACRO) {
errmsg="Can't use macro in expression.";
} else {//what else is there?
errmsg=UnknownLabel;
}
}
}
return ret;
}
char mathy[]="!^&|+-*/%()<>=,";
enum prectypes {WHOLEEXP,ORORP,ANDANDP,ORP,XORP,ANDP,EQCOMPARE,COMPARE,SHIFT,PLUSMINUS,MULDIV,UNARY};//precedence levels
enum operators {NOOP,EQUAL,NOTEQUAL,GREATER,GREATEREQ,LESS,LESSEQ,PLUS,MINUS,MUL,DIV,MOD,AND,XOR,OR,ANDAND,OROR,LEFTSHIFT,RIGHTSHIFT};//all operators
char prec[]={WHOLEEXP,EQCOMPARE,EQCOMPARE,COMPARE,COMPARE,COMPARE,COMPARE,PLUSMINUS,PLUSMINUS,MULDIV,MULDIV,MULDIV,ANDP,XORP,ORP,ANDANDP,ORORP,SHIFT,SHIFT};//precedence of each operator
//get operator from str and advance str
int getoperator(char **str) {
*str+=strspn(*str,whitesp); //eatwhitespace
(*str)++;
switch(*(*str-1)) {
case '&':
if(**str=='&') {
(*str)++;
return ANDAND;
} else
return AND;
case '|':
if(**str=='|') {
(*str)++;
return OROR;
} else
return OR;
case '^':
return XOR;
case '+':
return PLUS;
case '-':
return MINUS;
case '*':
return MUL;
case '%':
return MOD;
case '/':
return DIV;
case '=':
if(**str=='=')
(*str)++;
return EQUAL;
case '>':
if(**str=='=') {
(*str)++;
return GREATEREQ;
} else if(**str=='>') {
(*str)++;
return RIGHTSHIFT;
} else
return GREATER;
case '<':
if(**str=='=') {
(*str)++;
return LESSEQ;
} else if(**str=='>') {
(*str)++;
return NOTEQUAL;
} else if(**str=='<') {
(*str)++;
return LEFTSHIFT;
} else
return LESS;
case '!':
if(**str=='=') {
(*str)++;
return NOTEQUAL;
}
// fall through
default:
(*str)--;
return NOOP;
}
}
//evaluate expression in str and advance str
int eval(char **str,int precedence) {
char unary;
char *s,*s2;
int ret,val2;
int op;
s=*str+strspn(*str,whitesp); //eatwhitespace
unary=*s;
switch(unary) {
// [morskoyzmey absolute addressing change]
case 'a':
if(*(s+1)==':'){
s+=2;
absolute=1;
ret=eval(&s,WHOLEEXP);
}
else{
ret=getvalue(&s);
}
break;
case '(':
s++;
ret=eval(&s,WHOLEEXP);
s+=strspn(s,whitesp); //eatwhitespace
if(*s==')')
s++;
else
errmsg=IncompleteExp;
break;
case '#':
s++;
ret=eval(&s,WHOLEEXP);
break;
case '~':
s++;
ret=~eval(&s,UNARY);
break;
case '!':
s++;
ret=!eval(&s,UNARY);
break;
case '<':
s++;
ret=eval(&s,UNARY)&0xff;
break;
case '>':
s++;
ret=(eval(&s,UNARY)>>8)&0xff;
break;
case '+':
case '-':
//careful.. might be +-label
s2=s;
s++;
op=dependant;//eval() is reentrant so don't mess up dependant
val2=needanotherpass;
dependant=0;
ret=getvalue(&s2);
if (errmsg == UnknownLabel)
errmsg=0;
if(!dependant || s2==s) {//found something or single + -
s=s2;
s2=0;//flag that we got something
dependant|=op;
} else {//not a label after all..
dependant=op;
needanotherpass=val2;
}
if(s2) {//if it wasn't a +-label
ret=eval(&s,UNARY);
if(unary=='-') ret=-ret;
}
break;
default:
ret=getvalue(&s);
}
do {
*str=s;
op=getoperator(&s);
if(precedence<prec[op]) {
val2=eval(&s,prec[op]);
if(!dependant) switch(op) {
case AND:
ret&=val2;
break;
case ANDAND:
ret=ret&&val2;
break;
case OR:
ret|=val2;
break;
case OROR:
ret=ret||val2;
break;
case XOR:
ret^=val2;
break;
case PLUS:
ret+=val2;
break;
case MINUS:
ret-=val2;
break;
case MUL:
ret*=val2;
break;
case DIV:
if(!val2) errmsg=DivZero;
else ret/=val2;
break;
case MOD:
if(!val2) errmsg=DivZero;
else ret%=val2;
break;
case EQUAL:
ret=(ret==val2);
break;
case NOTEQUAL:
ret=(ret!=val2);
break;
case GREATER:
ret=ret>val2;
break;
case GREATEREQ:
ret=ret>=val2;
break;
case LESS:
ret=ret<val2;
break;
case LESSEQ:
ret=ret<=val2;
break;
case LEFTSHIFT:
ret<<=val2;
break;
case RIGHTSHIFT:
ret>>=val2;
break;
} else ret=0;
}
} while(precedence<prec[op] && !errmsg);
return ret;
}
//copy next word from src into dst and advance src
//mcheck=0 to crop nothing
//mcheck=1 to crop mathy stuff
//mcheck=2 to crop comma (e.g. for incbin)
void getword(char *dst,char **src,int mcheck) {
*src+=strspn(*src,whitesp);//eatwhitespace
strncpy(dst,*src,WORDMAX-1);
dst[WORDMAX-1]=0;
strtok(dst,whitesp);//no trailing whitespace
if(mcheck==1) strtok(dst,mathy);
else if(mcheck==2) strtok(dst,",");
*src+=strlen(dst);
if(**src==':') (*src)++;//cheesy fix for rept/macro listing
}
//grab string with optional quotes
void getfilename(char *dst, char **next) {
char *s,*end;
int len;
eatwhitespace(next);
if(**next=='"') { //look for end quote, grab everything inside
s=*next+1;
end=strchr(s,'"');
if(end) {
len=(int)(end-s);
memcpy(dst,s,len); dst[len]=0;
*next=end+1;
} else { //no end quote.. grab everything minus trailing whitespace
end=strend(s,whitesp);
len=(int)(end-s);
memcpy(dst,s,len); dst[len]=0;
*next=end;
}
} else {
getword(dst,next,2);
}
}
//get word in src, advance src, and return reserved label*
label *getreserved(char **src) {
char dst[WORDMAX];
char upp[WORDMAX];
label *p;
*src+=strspn(*src,whitesp);//eatwhitespace
if(**src=='=') {//special '=' reserved word
upp[0]='=';
upp[1]=0;
(*src)++;
} else {
if(**src=='.')//reserved words can start with "."
(*src)++;
getword(dst,src,1);
strcpy(upp,dst);
my_strupr(upp);
}
p=findlabel(upp);//case insensitive reserved word