-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
669 lines (622 loc) · 15.2 KB
/
main.cpp
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
#if !defined(INTERFACE_CLI) && !defined(INTERFACE_LIB)
#error Please pick a frontend with -DINTERFACE_CLI or -DINTERFACE_LIB.
#endif
//#include <map>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "libsmw.h"
#include "libstr.h"
#include "scapegoat.hpp"
#include "autoarray.h"
#include "asar.h"
extern const int asarver_maj=1;
extern const int asarver_min=3;
extern const int asarver_bug=6;
extern const bool asarver_beta=false;
#ifdef _I_RELEASE
extern char blockbetareleases[(!asarver_beta)?1:-1];
#endif
#ifdef _I_DEBUG
extern char blockreleasedebug[(asarver_beta)?1:-1];
#endif
unsigned const char * romdata_r;
int romlen_r;
int64_t math(const char * mystr, const char ** e);
void initmathcore();
int pass;
int optimizeforbank=-1;
const char * thisfilename;
int thisline;
const char * thisblock;
const char * callerfilename=NULL;
int callerline=-1;
bool errored=false;
volatile int recursioncount=0;
unsigned int labelval(const char ** rawname, bool update);
unsigned int labelval(char ** rawname, bool update);
unsigned int labelval(string name, bool define);
bool labelval(const char ** rawname, unsigned int * rval, bool update);
bool labelval(char ** rawname, unsigned int * rval, bool update);
bool labelval(string name, unsigned int * rval, bool define);
void startmacro(const char * line);
void tomacro(const char * line);
void endmacro(bool insert);
void callmacro(const char * data);
bool setmapper()
{
int maxscore=-99999;
mapper_t bestmap=lorom;
mapper_t maps[]={lorom, hirom};
for (size_t mapid=0;mapid<sizeof(maps)/sizeof(maps[0]);mapid++)
{
mapper=maps[mapid];
int score=0;
int highbits=0;
bool foundnull=false;
for (int i=0;i<21;i++)
{
unsigned char c=romdata[snestopc(0x00FFC0+i)];
if (foundnull && c) score-=4;//according to some documents, NUL terminated names are possible - but they shouldn't appear in the middle of the name
if (c>=128) highbits++;
else if (isupper(c)) score+=3;
else if (c==' ') score+=2;
else if (isdigit(c)) score+=1;
else if (islower(c)) score+=1;
else if (c=='-') score+=1;
else if (!c) foundnull=true;
else score-=3;
}
if (highbits>0 && highbits<=14) score-=21;//high bits set on some, but not all, bytes = unlikely to be a ROM
if ((romdata[snestopc(0x00FFDE)]^romdata[snestopc(0x00FFDC)])!=0xFF ||
(romdata[snestopc(0x00FFDF)]^romdata[snestopc(0x00FFDD)])!=0xFF) score=-99999;//checksum doesn't match up to 0xFFFF? Not a ROM.
//too lazy to check the real checksum
if (score>maxscore)
{
maxscore=score;
bestmap=mapper;
}
}
mapper=bestmap;
//detect oddball mappers
int mapperbyte=romdata[snestopc(0x00FFD5)];
int romtypebyte=romdata[snestopc(0x00FFD6)];
if (mapper==lorom)
{
if (mapperbyte==0x23 && (romtypebyte==0x32 || romtypebyte==0x34 || romtypebyte==0x35)) mapper=sa1rom;
}
return (maxscore>=0);
}
string getdecor()
{
string e;
if (thisfilename)
{
e+=S thisfilename;
if (thisline!=-1) e+=S ":"+dec(thisline+1);
if (callerfilename) e+=S" (called from "+callerfilename+":"+dec(callerline+1)+")";
e+=": ";
}
return e;
}
//modified from somewhere in nall (license: ISC)
string dir(char const *name) {
string result = name;
for(signed i = strlen(result); i >= 0; i--) {
if(result[i] == '/' || result[i] == '\\') {
result[i + 1] = 0;
break;
}
if(i == 0) result = "";
}
return result;
}
//extern lightweight_map<string, unsigned int> labels;
extern autoarray<int> poslabels;
extern autoarray<int> neglabels;
extern int freespaceextra;
extern int freespaceid;
extern int freespacepos[256];
void checkbankcross();
extern bool warnxkas;
extern bool emulatexkas;
static bool freespaced;
static int getlenforlabel(int snespos, int thislabel, bool exists)
{
if (warnxkas && (((thislabel^snespos)&0xFFFF0000)==0))
warn1("xkas compatibility warning: label access is always 24bit in emulation mode, but may be 16bit in native mode");
if (!exists)
{
if (!freespaced) freespaceextra++;
freespaced=true;
return 2;
}
else if (optimizeforbank>=0)
{
if (thislabel&0xFF000000) return 3;
else if ((thislabel>>16)==optimizeforbank) return 2;
else return 3;
}
else if((thislabel >> 16) == 0x7E && (thislabel & 0xFFFF) < 0x100)
{
return 1;
}
else if((thislabel >> 16) == 0x7E && (thislabel & 0xFFFF) < 0x2000)
{
return 2;
}
else if(((thislabel >> 16) & 0x7F) < 0x40 && (thislabel & 0xFFFF) < 0x8000)
{
return 2;
}
else if ((thislabel|snespos)&0xFF000000)
{
if ((thislabel^snespos)&0xFF000000) return 3;
else return 2;
}
else if ((thislabel^snespos)&0xFF0000) return 3;
else return 2;
}
int getlen(const char * orgstr, bool optimizebankextraction)
{
const char * str=orgstr;
freespaced=false;
if (str[0]=='+' || str[0]=='-')
{
int i;
for (i=0;str[i];i++)
{
if (str[i]!=str[0]) goto notposneglabel;
}
if (!pass) return 2;
unsigned int labelpos=31415926;
bool found;
if (str[0]=='+') found=labelval(S":pos_"+dec(i)+"_"+dec(poslabels[i]), &labelpos);
else found=labelval(S":neg_"+dec(i)+"_"+dec(neglabels[i]), &labelpos);
return getlenforlabel(snespos, labelpos, found);
}
notposneglabel:
int len=0;
while (*str)
{
int thislen=0;
bool maybebankextraction=(str==orgstr);
if (*str=='$')
{
str++;
int i;
for (i=0;isxdigit(str[i]);i++);
//if (i&1) warn(S dec(i)+"-digit hex value");//blocked in getnum instead
thislen=(i+1)/2;
str+=i;
}
else if (*str=='%')
{
str++;
int i;
for (i=0;str[i]=='0' || str[i]=='1';i++);
//if (i&7) warn(S dec(i)+"-digit binary value");
thislen=(i+7)/8;
str+=i;
}
else if (str[0]=='\'' && str[2]=='\'')
{
thislen=1;
str+=3;
}
else if (isdigit(*str))
{
int val=strtol(str, (char**)&str, 10);
if (val>=0) thislen=1;
if (val>=256) thislen=2;
if (val>=65536) thislen=3;
if (val>=16777216) thislen=4;
}
else if (isalpha(*str) || *str=='.' || *str=='?')
{
unsigned int thislabel;
bool exists=labelval(&str, &thislabel);
thislen=getlenforlabel(snespos, thislabel, exists);
}
else str++;
if (optimizebankextraction && maybebankextraction &&
(!strcmp(str, ">>16") || !strcmp(str, "/65536") || !strcmp(str, "/$10000")))
return 1;
if (thislen>len) len=thislen;
}
if (len>3) return 3;
return len;
}
extern bool foundlabel;
extern bool forwardlabel;
int getnum(const char * str)
{
// if (str[0]=='+' || str[0]=='-')
// {
// int i;
// for (i=0;str[i];i++)
// {
// if (str[i]!=str[0]) goto notposneglabel;
// }
// if (!str[i])
// {
// foundlabel=true;
// if (str[0]=='+') forwardlabel=true;
// if (str[0]=='+') return labelval(S":pos_"+dec(i)+"_"+dec(poslabels[i]))&0xFFFFFF;
// else return labelval(S":neg_"+dec(i)+"_"+dec(neglabels[i]))&0xFFFFFF;
// return 0;
// }
// }
//notposneglabel:
const char * e;
//int num=math(str, &e);
unsigned int num=math(str, &e);
if (e)
{
error<errblock>(1, e);
}
return num;
}
struct strcompare {
bool operator() (const char * lhs, const char * rhs) const
{
return strcmp(lhs, rhs)<0;
}
};
struct stricompare {
bool operator() (const char * lhs, const char * rhs) const
{
return stricmp(lhs, rhs)<0;
}
};
lightweight_map<string, char**> filecontents;
lightweight_map<string, string> defines;
void assembleblock(const char * block);
void resolvedefines(string& out, const char * start)
{
recurseblock rec;
const char * here=start;
while (*here)
{
if (*here=='"' && emulatexkas)
{
out+=*here++;
while (*here && *here!='"') out+=*here++;
out+=*here++;
}
else if (*here=='!')
{
bool first=(here==start || (here>=start+4 && here[-1]==' ' && here[-2]==':' && here[-3]==' '));//check if it's the start of a block
string defname;
here++;
if (*here=='{')
{
here++;
string unprocessedname;
int braces=1;
while (true)
{
if (*here=='{') braces++;
if (*here=='}') braces--;
if (!*here) error<errline>(0, "Unmatched braces.");
if (!braces) break;
unprocessedname+=*here++;
}
here++;
resolvedefines(defname, unprocessedname);
bool bad=false;
if (!defname[0]) bad=true;
for (int i=0;defname[i];i++)
{
if (!isualnum(defname[i])) bad=true;
}
if (bad) error<errline>(0, "Invalid define name.");
}
else
{
while (isualnum(*here)) defname+=*here++;
}
if (warnxkas && here[0]=='(' && here[1]==')')
warn0("xkas compatibility warning: Unlike xkas, Asar does not eat parentheses after defines");
//if (emulatexkas && here[0]=='(' && here[1]==')') here+=2;
if (first)
{
enum {
null,
append,
expand,
domath,
setifnotset,
} mode;
if(0);
else if (stribegin(here, " = ")) { here+=3; mode=null; }
else if (stribegin(here, " += ")) { here+=4; mode=append; }
else if (stribegin(here, " := ")) { here+=4; mode=expand; }
else if (stribegin(here, " #= ")) { here+=4; mode=domath; }
else if (stribegin(here, " ?= ")) { here+=4; mode=setifnotset; }
else goto notdefineset;
if (emulatexkas && mode!=null) warn0("Convert the patch to native Asar format instead of making an Asar-only xkas patch.");
//else if (stribegin(here, " equ ")) here+=5;
string val;
if (*here=='"')
{
here++;
while (true)
{
if (*here=='"')
{
if (!here[1] || here[1]==' ') break;
else if (here[1]=='"') here++;
else error<errline>(0, "Broken define declaration");
}
val+=*here++;
}
here++;
}
else
{
while (*here && *here!=' ') val+=*here++;
}
//if (strqchr(val.str, ';')) *strqchr(val.str, ';')=0;
if (*here && !stribegin(here, " : ")) error<errline>(0, "Broken define declaration");
clean(val);
switch (mode)
{
case null:
{
defines.insert(defname, val);
break;
}
case append:
{
string oldval;
if (!defines.find(defname, oldval)) error<errnull>(0, "Appending to an undefined define");
val=oldval+val;
defines.insert(defname, val);
break;
}
case expand:
{
string newval;
resolvedefines(newval, val);
defines.insert(defname, newval);
break;
}
case domath:
{
string newval;
resolvedefines(newval, val);
int num=getnum(newval);
if (foundlabel) error<errline>(0, "!Define #= Label is not allowed");
defines.insert(defname, dec(num));
break;
}
case setifnotset:
{
string idontcare;
if (!defines.find(defname, idontcare)) defines.insert(defname, val);
break;
}
}
}
else
{
notdefineset:
if (!defname) out+="!";
else
{
string thisone;
if (!defines.find(defname, thisone)) error<errline>(0, S"Define !"+defname+" not found");
else resolvedefines(out, thisone);
}
}
}
else out+=*here++;
}
}
int repeatnext=1;
bool autocolon=false;
bool moreonline;
bool asarverallowed;
bool istoplevel;
extern int numtrue;
extern int numif;
extern int macrorecursion;
void assembleline(const char * fname, int linenum, const char * line)
{
recurseblock rec;
bool moreonlinetmp=moreonline;
thisfilename=fname;
thisline=linenum;
thisblock=NULL;
try
{
string tmp=line;
if (!confirmquotes(tmp)) error<errline>(0, "Mismatched quotes");
clean(tmp);
string out;
if (numif==numtrue) resolvedefines(out, tmp);
else out=tmp;
out.qreplace(": :", ": :", true);
//puts(out);
autoptr<char**> blocks=qsplit(out.str, " : ");
moreonline=true;
for (int block=0;moreonline;block++)
{
moreonline=blocks[block+1];
int repeatthis=repeatnext;
repeatnext=1;
for (int i=0;i<repeatthis;i++)
{
try
{
itrim(blocks[block], " ", " ", true);
thisfilename=fname;
thisline=linenum;//do not optimize, this one is recursive
thisblock=blocks[block];
assembleblock(blocks[block]);
}
catch (errblock&) {}
if (blocks[block][0]!='\0' && blocks[block][0]!='@') asarverallowed=false;
}
}
}
catch (errline&) {}
moreonline=moreonlinetmp;
}
extern int numif;
extern int numtrue;
int incsrcdepth=0;
void assemblefile(const char * filename, bool toplevel)
{
incsrcdepth++;
thisfilename=filename;
thisline=-1;
thisblock=NULL;
char ** lines;
int startif=numif;
if (!filecontents.find(filename, lines))
{
char * temp=readfile(filename);
if (!temp)
{
error<errnull>(0, "Couldn't open file");
return;
}
lines=split(temp, "\n");
for (int i=0;lines[i];i++)
{
char * line=lines[i];
char * comment=line;
while ((comment=strqchr(comment, ';')))
{
if (comment[1]!='@') comment[0]='\0';
else
{
if (strncasecmp(comment+2, "xkas", 4)) comment[1]=' ';
comment[0]=' ';
}
}
while (strqchr(line, '\t')) *strqchr(line, '\t')=' ';
if (!confirmquotes(line)) { thisline=i; thisblock=line; error<errnull>(0, "Mismatched quotes"); line[0]='\0'; }
itrim(line, " ", " ", true);
for (int j=1;strqchr(line, ',') && !strqchr(line, ',')[1] && lines[i+j];j++)
{
strcat(line, lines[i+j]);
static char nullstr[]="";
lines[i+j]=nullstr;
}
}
filecontents.insert(filename, lines);
}
bool inmacro=false;
asarverallowed=true;
for (int i=0;lines[i];i++)
{
try
{
thisfilename=filename;
thisline=i;
thisblock=NULL;
istoplevel=toplevel;
if (stribegin(lines[i], "macro ") && numif==numtrue)
{
if (inmacro) error<errline>(0, "Nested macro definition");
inmacro=true;
if (!pass) startmacro(lines[i]+6);
}
else if (!stricmp(lines[i], "endmacro") && numif==numtrue)
{
if (!inmacro) error<errline>(0, "Misplaced endmacro");
inmacro=false;
if (!pass) endmacro(true);
}
else if (inmacro)
{
if (!pass) tomacro(lines[i]);
}
else assembleline(filename, i, lines[i]);
}
catch (errline&) {}
asarverallowed=false;
}
thisline++;
thisblock=NULL;
checkbankcross();
if (inmacro)
{
error<errnull>(0, "Unclosed macro");
if (!pass) endmacro(false);
}
if (repeatnext!=1)
{
repeatnext=1;
error<errnull>(0, "rep at the end of a file");
}
if (numif!=startif)
{
numif=startif;
numtrue=startif;
error<errnull>(0, "Unclosed if statement");
}
incsrcdepth--;
}
bool checksum=true;
extern lightweight_map<string, unsigned int> labels;
extern autoarray<string> sublabels;
extern string ns;
struct macrodata
{
autoarray<string> lines;
int numlines;
int startline;
const char * fname;
const char ** arguments;
int numargs;
};
extern lightweight_map<string, macrodata*> macros;
#define cfree(x) free((void*)x)
static void clearmacro(const string & key, macrodata* & macro)
{
macro->lines.~autoarray();
cfree(macro->fname);
cfree(macro->arguments[0]);
cfree(macro->arguments);
cfree(macro);
}
static void clearfile(const string & key, char** & filecontent)
{
cfree(*filecontent);
cfree(filecontent);
}
void reseteverything()
{
string str;
labels.clear();
defines.clear();
macros.traverse(clearmacro);
macros.clear();
//while (str=macros.rootKey())
//{
// macrodata * macro;
// macros.find(str, macro);
// macro->lines.~autoarray();
// free(macro->fname);
// free(macro->arguments[0]);
// free(macro->arguments);
// free(macro);
// macros.remove(str);
//}
filecontents.traverse(clearfile);
filecontents.clear();
//while (str=filecontents.rootKey())
//{
// char ** filecontent;
// filecontents.find(str, filecontent);
// free(*filecontent);
// free(filecontent);
// filecontents.remove(str);
//}
optimizeforbank=-1;
#undef free
}