This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-c64.js
1475 lines (1212 loc) · 45.8 KB
/
build-c64.js
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
var child_process = require('child_process');
var execSync = child_process.execSync;
var exec = child_process.exec;
var spawn = child_process.spawn;
var spawnSync = child_process.spawnSync;
var fs = require('graceful-fs');
var readline = require('readline');
var walk = require('walk');
var shellEscape = require('shell-escape');
var i;
var files;
var hash2ChunkCount;
var remainder = 16384;
var matchOperatorsRe = /[|\\{}()\[\]^$+*?.]/g;
var replaceAll = (str, mapObj) => str.replace(
new RegExp(
Object.
keys(mapObj).
map(x => "\\b"+(x.replace(matchOperatorsRe, "\\$&"))+"\\b").
join("|"),
"gi"
),
matched => mapObj[matched]
);
var hashMap = {};
var passPostfix = "";
/*functionsList = A mapping of function names to the memory page that
contains them, the address within that page and which trampoline function
in the $C000 range to call to set them up correctly*/
var functionsList = [
["BASIC_FAC_to_string", 1, 0xBDDD, "FUNC0"], /* in: FAC value out: str at $0100 a/y ptr to str */
["BASIC_string_to_FAC", 1, 0xB7B5, "FUNC0"], /* in: $22/$23 ptr to str,a=strlen out: FAC value */
["BASIC_s8_to_FAC", 1, 0xBC3C, "FUNC0"], /* a: value */
["BASIC_u8_to_FAC", 1, 0xB3A2, "FUNC0"], /* y: value */
["BASIC_u16_to_FAC", 1, 0xBC49, "FUNC0"], /* a/y:lo/hi value (sta $62 sty $63 sec ldx#$90 jsr...) */
["BASIC_s16_to_FAC", 1, 0xB395, "FUNC0"], /* a/y:lo/hi value */
["BASIC_FAC_to_u16", 1, 0xBC9B, "FUNC0"], /* in:FAC out: y/a:lo/hi value */
/*------------------------------------------------------------------------- */
/* these functions take one arg (in FAC) and return result (in FAC) as well */
/*------------------------------------------------------------------------- */
["__fabs", 1, 0xBC58, "FUNC1"], /* in/out: FAC */
["__fatn", 1, 0xE30E, "FUNC1"], /* in/out: FAC */
["__fcos", 1, 0xE264, "FUNC1"], /* in/out: FAC */
["__fexp", 1, 0xBFED, "FUNC1"], /* in/out: FAC */
/* __ffre: FUNC1 BASIC_FAC_Fre */
["__fint", 1, 0xBCCC, "FUNC1"], /* in/out: FAC */
["__flog", 1, 0xB9EA, "FUNC1"], /* in/out: FAC */
/* __fpos: FUNC1 BASIC_FAC_Pos */
["__frnd", 1, 0xE097, "FUNC1"], /* in/out: FAC */
["__fsgn", 1, 0xBC39, "FUNC1"], /* in/out: FAC */
["__fsin", 1, 0xE26B, "FUNC1"], /* in/out: FAC */
["__fsqr", 1, 0xBF71, "FUNC1"], /* in/out: FAC */
["__ftan", 1, 0xE2B4, "FUNC1"], /* in/out: FAC */
["__fnot", 1, 0xAED4, "FUNC1"], /* in/out: FAC */
["__fround", 1, 0xBC1B, "FUNC1"], /* in/out: FAC */
/*-------------------------------------------------------------------------- */
/* these functions take two args (in FAC and ARG) and return result (in FAC) */
/*-------------------------------------------------------------------------- */
["__fadd", 1, 0xB86A, "FUNC2"], /* in: ARG,FAC out:FAC */
["__fsub", 1, 0xB853, "FUNC2"], /* in: ARG,FAC out:FAC */
["__fmul", 1, 0xBA2B, "FUNC2"], /* in: ARG,FAC out:FAC */
["__fdiv", 1, 0xBB12, "FUNC2"], /* in: ARG,FAC out:FAC */
["__fpow", 1, 0xBF7B, "FUNC2"], /* in: ARG,FAC out:FAC */
["__fand", 1, 0xAFE9, "FUNC3"], /* in: ARG,FAC out:FAC */
["__for", 1, 0xAFE6, "FUNC3"], /* in: ARG,FAC out:FAC */
["BASIC_FAC_cmp", 1, 0xBC5B, "FUNC0"], /* in: FAC(x1) a/y ptr lo/hi to x2 out: a=0 (x1=x2) a=1 (x1>x2) a=255 (x1<x2) */
["BASIC_FAC_testsgn", 1, 0xBC2B, "FUNC0"], /* in: FAC(x1) out: a=0 (x1=0) a=1 (x1>0) a=255 (x1<0) */
["BASIC_FAC_Poly1", 1, 0xE059, "FUNC0"], /* in: FAC x a/y ptr to poly (1byte grade,5bytes per coefficient) */
/*normally these functions are wrapped by more code, but we need to be able
to call them in a raw form */
["BASIC_FAC_Atn", 1, 0xE30E, "FUNC0"], /* in/out: FAC */
["BASIC_ARG_FAC_Div", 1, 0xBB12, "FUNC0"], /* in: ARG,FAC out:FAC */
["BASIC_ARG_FAC_Add", 1, 0xB86A, "FUNC0"], /* in: ARG,FAC out:FAC */
];
/* these are unused */
/*BASIC_FAC_Poly2 = $e043 ; in: FAC x a/y ptr to poly (1byte grade,5bytes per coefficient) */
/*BASIC_LoadARG = $babc ; a/y:lo/hi ptr to 5-byte float */
/*BASIC_LoadFAC = $bba2 ; a/y:lo/hi ptr to 5-byte float */
var floatlibFunctionsList = [
["__ftostr", 1, 0x0001, "farcall"],
["__strtof", 1, 0x0001, "farcall"],
["__ctof", 1, 0x0001, "farcall"],
["__utof", 1, 0x0001, "farcall"],
["__stof", 1, 0x0001, "farcall"],
["__itof", 1, 0x0001, "farcall"],
["__ftoi", 1, 0x0001, "farcall"],
["__fcmp", 1, 0x0001, "farcall"],
["__ftestsgn", 1, 0x0001, "farcall"],
["__fneg", 1, 0x0001, "farcall"],
["__fpoly1", 1, 0x0001, "farcall"],
["__fpoly2", 1, 0x0001, "farcall"],
["__fatan2", 1, 0x0001, "farcall"],
];
var clibFunctionsList = [
/* standard C library functions */
["_strtod", 1, 0x0001, "farcall"],
["_calloc", 2, 0x0001, "farcall2"],
["_clearerr", 2, 0x0001, "farcall2"],
["_fclose", 2, 0x0001, "farcall2"],
["_feof", 2, 0x0001, "farcall2"],
["_ferror", 2, 0x0001, "farcall2"],
["_fflush", 2, 0x0001, "farcall2"],
["_fgetc", 2, 0x0001, "farcall2"],
["_fgets", 2, 0x0001, "farcall2"],
["_fopen", 2, 0x0001, "farcall2"],
["_fputc", 2, 0x0001, "farcall2"],
["_fread", 2, 0x0001, "farcall2"],
["_free", 2, 0x0001, "farcall2"],
["_fwrite", 2, 0x0001, "farcall2"],
["_ltoa", 2, 0x0001, "farcall2"],
["_malloc", 2, 0x0001, "farcall2"],
["_memcpy", 2, 0x0001, "farcall2"],
["_memmove", 2, 0x0001, "farcall2"],
["_memset", 2, 0x0001, "farcall2"],
["_realloc", 2, 0x0001, "farcall2"],
["_sprintf", 2, 0x0001, "farcall2"],
["_strcat", 2, 0x0001, "farcall2"],
["_strcmp", 2, 0x0001, "farcall2"],
["_strcpy", 2, 0x0001, "farcall2"],
["_strlen", 2, 0x0001, "farcall2"],
["_strncat", 2, 0x0001, "farcall2"],
["_strstr", 2, 0x0001, "farcall2"],
["_ultoa", 2, 0x0001, "farcall2"],
["_ungetc", 2, 0x0001, "farcall2"],
["_vsnprintf", 2, 0x0001, "farcall2"],
["_vsprintf", 2, 0x0001, "farcall2"],
//other special functions we'll need
["_isCombiningChar", 0, 0x0001, "farcall2"],
["_in_word_set_a", 0, 0x0001, "farcall2"],
["_in_word_set_b", 0, 0x0001, "farcall2"],
["_in_word_set_c", 0, 0x0001, "farcall2"]
];
var defines = {
"_stderr": 0,
"_stdin": 0,
"_stdout": 0,
"zerobss2": 0,
"initlib2": 0,
"donelib2": 0,
"___float_fac_to_float": 0,
"___float_float_to_fac": 0,
"___float_float_to_fac_arg": 0,
"aRegBackup": 0,
"spRegBackup": 0,
"xRegBackup": 0
};
var rodataLabels = [];
if(fs.existsSync("querycsv.c")) {
/* The first action to initiate */
start();
}
function start() {
console.log("start");
/* create the necessary folders */
execSync(
"mkdir -p build;"+
"mkdir -p build/bin;"+
"mkdir -p build/s;"+
"mkdir -p build/g;"+
"mkdir -p build/ro;"+
"mkdir -p build/obj;"+
"mkdir -p build/obj2"
);
createFloatLibInclude();
}
/* create the cc65-floatlib include */
function createFloatLibInclude() {
console.log('createFloatLibInclude');
var tables = fs.createWriteStream('build/floatlib.inc');
for(i = 0; i < functionsList.length; i++) {
hashMap[functionsList[i][0]] = i;
tables.write(
functionsList[i][0] + ":\n" +
" stx xRegBackup\n" +
" ldx #$"+("00"+(i.toString(16).toUpperCase())).substr(-2)+"\n" +
" jmp "+functionsList[i][3]+"\n"
);
}
tables.write("\nhighAddressTable:\n");
for(i = 0; i < functionsList.length; i++) {
tables.write(
".byte $"+
("0000"+(functionsList[i][2]-1).toString(16).toUpperCase()).
substr(-4).
substring(0,2)+
"\n"
);
}
tables.write("\nlowAddressTable:\n");
for(i = 0; i < functionsList.length; i++) {
tables.write(
".byte $" +
("00"+(functionsList[i][2]-1).toString(16).toUpperCase()).
substr(-2)+
"\n"
);
}
for(i = 0; i < floatlibFunctionsList.length; i++) {
tables.write(".export "+floatlibFunctionsList[i][0]+"\n");
}
tables.end(compileFloatLib);
}
/* compile cc65-floatlib. */
function compileFloatLib() {
console.log('compileFloatLib');
/* compile the floatlib portion written in C (strtod) */
execSync("cc65 -T -t c64 -I ./floatlib floatlib.c -o build/floatlib.s");
/* link into a binary */
execSync("cl65 -T -t c64 "+
"-I ./floatlib "+
"-Ln build/floatlib.lbl "+
"--config " + (passPostfix === ""?"":"build/") + "floatlib"+passPostfix+".cfg "+
"floatlib/float.s build/floatlib.s"
);
/*create a zeroed binary the same size as the total ram used */
execSync(
"("+
"echo -n \"ibase=16;scale=16;\" && "+
"((grep __RAM2_LAST__ build/floatlib.lbl|"+
"sed -n \"s/al \\([^ ]*\\).*/\\1/p\")|tr -d '\\n')"+
" && echo -n \"-\" && "+
"grep __RAM2_START__ build/floatlib.lbl|"+
"sed -n \"s/al \\([^ ]*\\).*/\\1/p\""+
")|bc|"+
"xargs -I {} dd if=/dev/zero bs=1 count={} of=build/floatlibdata.bin"
);
/* Create a combined binary of the data and bss
segments so that they can be copied as one blob */
execSync(
"dd if=build/floatlibdata2.bin of=build/floatlibdata.bin conv=notrunc;"+
"rm build/floatlibdata2.bin"
);
if(passPostfix === "") {
/* add the float lib functions to the hashmap */
floatlibFunctionsList.forEach(function(v) {
hashMap[v[0]] = this.length;
this.push(v);
}, functionsList);
clibFunctionsList.forEach(function(v) {
hashMap[v[0]] = this.length;
this.push(v);
}, functionsList);
hash2ChunkCount = 15;
for(i = 0; i < hash2ChunkCount; i++) {
/*
put an entry in the functions list for each file.
as the code in each of them doesn't need to call any other page
we can probably use farret2
*/
var name = '_isInHash2_'+i;
hashMap[name] = functionsList.length;
functionsList.push([
name,
0,
0,
"farcall2"
]);
}
}
/* read the label file and use its contents to
update each function address in the hashmap */
var lineReader = readline.createInterface({
input: fs.createReadStream('build/floatlib.lbl')
});
lineReader.on('line', updateFunctionAddress.bind(1));
/* when finished, start the next step (compiling libc) */
lineReader.on('close', compileLibC);
}
/* compile cc65 standard library */
function compileLibC() {
console.log('compileLibC');
/* compile a fake program that uses c library */
execSync(
"cl65 -T -t c64 -O -Os "+
"-Ln build/libc.lbl "+
"--static-locals "+
"--config " + (passPostfix === ""?"":"build/") + "libc"+passPostfix+".cfg "+
"libc.c"
);
/*create an zeroed binary the same size as the total ram used */
execSync(
"("+
"echo -n \"ibase=16;scale=16;\" && "+
"((grep __RAM2_LAST__ build/libc.lbl|"+
"sed -n \"s/al \\([^ ]*\\).*/\\1/p\")|tr -d '\n')"+
" && echo -n \"-\" && "+
"grep __RAM2_START__ build/libc.lbl|"+
"sed -n \"s/al \\([^ ]*\\).*/\\1/p\""+
")|bc|"+
"xargs -I {} dd if=/dev/zero bs=1 count={} of=build/libcdata.bin"
);
/* Create a combined binary of the ram segments
so that they can be copied as one blob */
execSync(
"dd if=build/libcdata2.bin of=build/libcdata.bin conv=notrunc;"+
"rm build/libcdata2.bin"
);
/* read the label file and use its contents to
update each function address in the hashmap */
var lineReader = readline.createInterface({
input: fs.createReadStream('build/libc.lbl')
});
lineReader.on('line', updateFunctionAddress.bind(2));
/* when finished, start the next step (compiling the functions that
make up my program) */
lineReader.on('close', passPostfix===""?compileLexer:compileData);
}
//compile and split up lexer
function compileLexer() {
console.log('compileLexer');
execSync(
'sed "'+
's/struct yy_trans_info/flex_int8_t yy_accept2\(unsigned int offset\);'+
'flex_uint16_t yy_nxt2\(unsigned int offset\);'+
'flex_int16_t yy_chk2\(unsigned int offset\);'+
'YY_CHAR yy_ec2\(unsigned int offset\);'+
'struct yy_trans_info/g;'+
's/%s/%S/g;'+
's/yyconst char msg\\[\\]/yyconst char *msg/g;'+
's/flex_int32_t yy_rule_can_match_eol/flex_int8_t yy_rule_can_match_eol/g;'+
's/flex_int16_t yy_accept/flex_int8_t yy_accept/g;'+
's/yy_ec\\[YY_SC_TO_UI(\\*yy_cp)\\]/yy_ec2(YY_SC_TO_UI(*yy_cp))/g;'+
's/yy_nxt\\[yy_base\\[yy_current_state\\] + (flex_int16_t) yy_c\\]/yy_nxt2\\(yy_base\\[yy_current_state\\] + (flex_int16_t) yy_c)/g;'+
's/yy_chk\\[yy_base\\[yy_current_state\\] + yy_c\\]/yy_chk2\\(yy_base\\[yy_current_state\\] + yy_c\\)/g;'+
's/yy_accept\\[yy_current_state\\]/yy_accept2\\(yy_current_state\\)/g;'+
'" lexer.c > build/lexer2.h'
);
/* compile functions into assembly language. use our own
patched cc65 executable that does "jmp farret" instead of "rts" */
execSync('./cc65_2 -T -t c64 lexer2.c -O -Os --static-locals --writable-strings -o build/lexer2.s');
splitUpFunctions("lexer2", compileParser);
}
function compileParser() {
console.log('compileParser');
execSync(
'sed -e"'+
's/YY_INITIAL_VALUE \(static YYSTYPE yyval_default;\)//g;'+
's/yycheck\\[\\(.[^]]*\\)\\]/yycheck2(\\1)/g;' +
's/#define YY_LAC_ESTABLISH/yytype_int16 yycheck2(int offset);\\n#define YY_LAC_OESTABLISH/g;'+
's/%s/%S/g;'+
's/%d/%D/g;'+
's/%lu/%LU/g;'+
's/\'s\'/\'S\'/g;'+
's/YYSTYPE yylval YY_INITIAL_VALUE \(= yyval_default\);/static YYSTYPE yylval;/g;'+
's/static const yytype_int16 yypact\\[\\]/yytype_int16 yypact2\(int offset\);static const yytype_int16 yypact[]/g;'+
'" sql.c > build/sql2.h');
execSync(
'sed -i -r "'+
's/yypact\\[([^]]+)\\]/yypact2(\\1)/gi;'+
'" build/sql2.h');
execSync('./cc65_2 -T -t c64 -O -Os sql2.c --static-locals --writable-strings -o build/sql2.s');
//split parser up into 1 function per .s file
//(including all rodata. add all data vars to a single .s file)
splitUpFunctions("sql2", compileC64, true);
}
function compileC64() {
console.log('compileC64');
execSync("./cc65_2 -T -t c64 c64.c --writable-strings -o build/c64.s");
splitUpFunctions("c64", compileQueryCSV, true);
}
function compileQueryCSV() {
console.log('compileQueryCSV');
execSync("./cc65_2 -T -t c64 querycsv.c --writable-strings -o build/querycsv.s");
splitUpFunctions("querycsv", compileData, true);
}
/* compile the data segment to a memory page.
(this will be copied to ram on startup) */
function compileData() {
console.log('compileData');
/* compile data and bss segments. generate an assembly
language include of all the labels */
execSync(
"cl65 "+
"-Ln build/data.lbl "+
"-C " + (passPostfix === ""?"":"build/") + "data"+passPostfix+".cfg "+
"build/data.s -vm"
);
execSync(
"("+
"echo -n \"ibase=16;scale=16;\" && "+
"((grep __RAM2_LAST__ build/data.lbl|"+
"sed -n \"s/al \\([^ ]*\\).*/\\1/p\")|tr -d '\n')"+
" && echo -n \"-\" && "+
"grep __RAM2_START__ build/data.lbl|"+
"sed -n \"s/al \\([^ ]*\\).*/\\1/p\""+
")|bc|"+
"xargs -I {} dd if=/dev/zero bs=1 count={} of=build/data.bin"
);
/*TODO: confirm this: Create a combined binary of the ram segments
so that they can be copied as one blob? */
execSync("dd if=build/data2.bin of=build/data.bin conv=notrunc;rm build/data2.bin");
var lineReader2 = readline.createInterface({
input: fs.createReadStream('build/data.lbl')
});
var labels = fs.createWriteStream('build/labels.s');
/* read the resultant memory locations and make an assembly include
containing just their addresses */
lineReader2.on('line', line => {
var name = line.replace(/^al\s+[0-9A-F]+ \./, "");
if(name.match(/l[0-9a-f]{4}/)) {
var address = parseInt(line.match(/[0-9A-F]+/), 16);
labels.write(
name+" = $"+
("0000"+address.toString(16).toUpperCase()).substr(-4)+
"\n"
);
}
//starts with an underscore (name defined by the c source code)
else if(name.match(/\b_[0-9a-zA-Z]/) || name === "jmpvec") {// && name !== '_main') {
var address = parseInt(line.match(/[0-9A-F]+/), 16);
labels.write(".export "+name+"\n");
labels.write(
name+" = $"+
("0000"+address.toString(16).toUpperCase()).substr(-4)+
"\n"
);
}
});
lineReader2.on('close', () => {
labels.end(compileHash2);
});
}
function compileHash2() {
var i, name;
console.log("compileHash2");
for(i = 0; fs.existsSync('./hash2in'+i+'.c'); i++) {
execSync(
"cl65 -T -t c64 "+
"-o build/obj2/hash2in"+i+".bin "+
"-Ln build/obj2/hash2in"+i+".lbl "+
"-C rodata-page.cfg "+
"hash2in"+i+".c build/labels.s;"+
"rm *.o"
);
/*
put an entry in the functions list for each file.
as the code in each of them doesn't need to call any other page
we can probably use farret2
*/
name = '_isInHash2_'+i;
functionsList[hashMap[name]][2] = parseInt(execSync(
'sh -c "(echo -n \\"ibase=16;scale=16;\\" && (grep _isInHash2_'+
i+
' build/obj2/hash2in'+
i+
'.lbl|sed -n \\"s/al \\([^ ]*\\).*/\\1/p\\"))|bc"'
).toString(), 10);
}
if(passPostfix === "") {
/* allow the hash2 function to be packed also */
execSync('./cc65_2 -T -t c64 hash2out.c -o build/hash2out.s');
splitUpFunctions("hash2out", compileHash3And4, true);
}
else {
compileHash3And4();
}
}
function compileHash3And4() {
console.log("compileHash3And4");
execSync(
"cl65 -T -t c64 "+
"-o build/obj2/hash3.bin "+
"-Ln build/obj2/hash3.lbl "+
"-C rodata-page.cfg "+
"hash3.c build/labels.s;"+
"rm *.o"
);
functionsList[hashMap['_isCombiningChar']][2] = parseInt(execSync(
'sh -c "(echo -n \\"ibase=16;scale=16;\\" && (grep _isCombiningChar'+
' build/obj2/hash3.lbl|sed -n \\"s/al \\([^ ]*\\).*/\\1/p\\"))|bc"'
).toString(), 10);
execSync(
'sed -e"'+
's/static struct hash4Entry/static const struct hash4Entry/gi;' +
's/static unsigned short/static const unsigned short/gi;' +
's/return \\&wordlist\\[key\\];/{hash4export.script = wordlist[key].script;hash4export.index = wordlist[key].index;hash4export.islower = wordlist[key].islower;return \\&hash4export;}/gi;' +
'" hash4a.h > hash4a2.h');
execSync(
"cl65 -T -t c64 "+
"-o build/obj2/hash4a.bin "+
"-Ln build/obj2/hash4a.lbl "+
"-C rodata-page.cfg "+
"hash4a.c build/labels.s;"+
"rm *.o");
functionsList[hashMap['_in_word_set_a']][2] = parseInt(execSync(
'sh -c "(echo -n \\"ibase=16;scale=16;\\" && (grep _in_word_set_a'+
' build/obj2/hash4a.lbl|sed -n \\"s/al \\([^ ]*\\).*/\\1/p\\"))|bc"'
).toString(), 10);
execSync(
'sed -e"'+
's/static struct hash4Entry/static const struct hash4Entry/gi;' +
's/static unsigned short/static const unsigned short/gi;' +
's/return \\&wordlist\\[key\\];/{hash4export.script = wordlist[key].script;hash4export.index = wordlist[key].index;hash4export.islower = wordlist[key].islower;return \\&hash4export;}/gi;' +
'" hash4b.h > hash4b2.h');
execSync(
"cl65 -T -t c64 "+
"-o build/obj2/hash4b.bin "+
"-Ln build/obj2/hash4b.lbl "+
"-C rodata-page.cfg "+
"hash4b.c build/labels.s;"+
"rm *.o"
);
functionsList[hashMap['_in_word_set_b']][2] = parseInt(execSync(
'sh -c "(echo -n \\"ibase=16;scale=16;\\" && (grep _in_word_set_b'+
' build/obj2/hash4b.lbl|sed -n \\"s/al \\([^ ]*\\).*/\\1/p\\"))|bc"'
).toString(), 10);
execSync(
'sed -e"'+
's/static struct hash4Entry/static const struct hash4Entry/gi;' +
's/static unsigned short/static const unsigned short/gi;' +
's/return \\&wordlist\\[key\\];/{hash4export.script = wordlist[key].script;hash4export.index = wordlist[key].index;hash4export.islower = wordlist[key].islower;return \\&hash4export;}/gi;' +
'" hash4c.h > hash4c2.h');
execSync(
"cl65 -T -t c64 "+
"-o build/obj2/hash4c.bin "+
"-Ln build/obj2/hash4c.lbl "+
"-C rodata-page.cfg "+
"hash4c.c build/labels.s;"+
"rm *.o"
);
functionsList[hashMap['_in_word_set_c']][2] = parseInt(execSync(
'sh -c "(echo -n \\"ibase=16;scale=16;\\" && (grep _in_word_set_c'+
' build/obj2/hash4c.lbl|sed -n \\"s/al \\([^ ]*\\).*/\\1/p\\"))|bc"'
).toString(), 10);
createTrampolinesInclude();
}
function createTrampolinesInclude() {
console.log("createTrampolinesInclude");
var tables = fs.createWriteStream('build/tables.inc');
for(i in defines) {
tables.write(
".export "+i+"\n"+
i+" = $"+
("0000"+((defines[i]).toString(16).toUpperCase())).substr(-4)+
"\n"
);
}
for(i = 0; i < functionsList.length; i++) {
let secondTable = i > 255;
if(secondTable && functionsList[i][3] == 'farcall') {
functionsList[i][3] = 'farcall3';
}
tables.write(
".export "+functionsList[i][0]+"\n"+
functionsList[i][0]+":\n"+
" stx xRegBackup\n"+
" ldx #$"+("00"+(i.toString(16).toUpperCase())).substr(-2)+"\n"+
" jmp "+functionsList[i][3]+"\n"
);
}
tables.write("\nhighAddressTable:\n");
for(i = 0; i < Math.min(256, functionsList.length); i++) {
tables.write(
".byte $"+
("0000"+((functionsList[i][2]-1).toString(16).toUpperCase())).
substr(-4).
substring(0,2)+
" ;"+functionsList[i][0]+
"\n"
);
}
tables.write("\nlowAddressTable:\n");
for(i = 0; i < Math.min(256, functionsList.length); i++) {
tables.write(
".byte $"+
("00"+((functionsList[i][2]-1).toString(16).toUpperCase())).
substr(-2)+
" ;"+functionsList[i][0]+
"\n"
);
}
tables.write("\nbankTable:\n");
for(i = 0; i < Math.min(256, functionsList.length); i++) {
tables.write(
".byte $"+
("00"+(functionsList[i][1].toString(16).toUpperCase())).
substr(-2)+
" ;"+functionsList[i][0]+
"\n"
);
}
tables.write("\nhighAddressTable2:\n");
for(i = 256; i < functionsList.length; i++) {
tables.write(
".byte $"+
("0000"+((functionsList[i][2]-1).toString(16).toUpperCase())).
substr(-4).
substring(0,2)+
" ;"+functionsList[i][0]+
"\n"
);
}
tables.write("\nlowAddressTable2:\n");
for(i = 256; i < functionsList.length; i++) {
tables.write(
".byte $"+
("00"+((functionsList[i][2]-1).toString(16).toUpperCase())).
substr(-2)+
" ;"+functionsList[i][0]+
"\n"
);
}
tables.write("\nbankTable2:\n");
for(i = 256; i < functionsList.length; i++) {
tables.write(
".byte $"+
("00"+(functionsList[i][1].toString(16).toUpperCase())).
substr(-2)+
" ;"+functionsList[i][0]+
"\n"
);
}
tables.end(compileMain);
}
function compileMain() {
console.log("compileMain");
execSync("cl65 -T -t c64 -Ln build/main.lbl --config "+(passPostfix===""?"":"build/")+"main"+(passPostfix===""?"":"a")+".cfg crt0.s");
execSync(
"("+
"echo -n \"ibase=16;scale=16;\" && "+
"((grep __RAM2_LAST__ build/main.lbl|"+
"sed -n \"s/al \\([^ ]*\\).*/\\1/p\")|tr -d '\\n')"+
" && echo -n \"-\" && "+
"grep __RAM2_START__ build/main.lbl|"+
"sed -n \"s/al \\([^ ]*\\).*/\\1/p\""+
")|bc|"+
"xargs -I {} dd if=/dev/zero bs=1 count={} of=build/maindata.bin"
);
/*read the label file and update each function address */
var lineReader = readline.createInterface({
input: fs.createReadStream('build/main.lbl')
});
execSync("cat build/labels.s > build/labels2.s");
var labels = fs.createWriteStream('build/labels2.s', { flags: 'a' });
for(i in defines) {
labels.write(
i+" = $"+
("0000"+((defines[i]).toString(16).toUpperCase())).substr(-4)+
"\n"
);
}
labels.write(".export _main\n");
labels.write(".import pushl0\n");
lineReader.on('line', function(line) {
var name = line.replace(/^al\s+[0-9A-F]+ \./, "");
var address = parseInt(line.match(/[0-9A-F]+/), 16);
if(name == "farret") {
labels.write(
"farret = $"+
("0000"+(address.toString(16).toUpperCase())).substr(-4)+
"\n"
);
}
if(name == "_exit") {
labels.write(
"_exit = $"+
("0000"+(address.toString(16).toUpperCase())).substr(-4)+
"\n"
);
}
if(hashMap.hasOwnProperty(name)) {
labels.write(
name+" = $"+
("0000"+(address.toString(16).toUpperCase())).substr(-4)+
"\n"
);
}
});
lineReader.on('close', function() {
if(passPostfix === "") {
labels.end(calculateSizes);
}
else if(passPostfix === "a") {
labels.end(compileYYParse);
}
else {
labels.end(glueTogetherBinary);
}
});
}
/* compile yyparse twice, first to get its size, then to locate it in the proper place */
function calculateSizes() {
console.log("calculateSizes");
updateName(["_yyparse.s", fs.readFileSync("build/s/_yyparse.s", {encoding: 'utf8'})]);
execSync(
"echo \".include \\\"../../header.inc\\\"\" > ./build/g/_yyparse.s;"+
"echo \".include \\\"../labels2.s\\\"\" >> ./build/g/_yyparse.s;"+
"cat ./build/s/_yyparse.s >> ./build/g/_yyparse.s;"+
"cl65 -T -t c64 "+
"-o ./build/obj2/yyparse.bin "+
"-Ln ./build/obj2/yyparse.lbl "+
"-C ./yyparse.cfg ./build/g/_yyparse.s"
);
passPostfix = "a";
var yyparse_size = Math.max(0x2000, fs.statSync("build/obj2/yyparse.bin").size);
var yyparse_start = 0xC000 - yyparse_size;
var data_size = fs.statSync("build/data.bin").size;
var data_start = yyparse_start - data_size;
var libc_size = fs.statSync("build/libcdata.bin").size;
var libc_start = data_start - libc_size;
var floatlib_size = fs.statSync("build/floatlibdata.bin").size;
var floatlib_start = libc_start - floatlib_size;
var main_size = fs.statSync("build/maindata.bin").size;
var main_start = floatlib_start - main_size;
var heap_size = main_start - 0x0800;
remainder -= (yyparse_size+data_size+libc_size+floatlib_size+main_size);
execSync(
"sed 's/ROMH: file = %O, start = $8000, size = $4000/"+
"ROMH: file = %O, start = $"+
(yyparse_start.toString(16).toUpperCase())+
", size = $"+
(yyparse_size.toString(16).toUpperCase())+
"/g;"+
's/RAM: file = "", start = $0800, size = $9800/'+
'RAM: file = "", start = $0800, size = $'+
(heap_size.toString(16).toUpperCase())+"/g;"+
"' yyparse.cfg > build/yyparsea.cfg"
);
execSync(
"sed 's/data2.bin\\\", start = $0800, size = $9800/"+
"data2.bin\\\", start = $"+
(data_start.toString(16).toUpperCase())+
", size = $"+
(data_size.toString(16).toUpperCase())+
"/g;"+
's/RAM: file = "", start = $0800, size = $9800/'+
'RAM: file = "", start = $0800, size = $'+
(heap_size.toString(16).toUpperCase())+"/g;"+
"' data.cfg > build/dataa.cfg"
);
execSync(
"sed 's/floatlibdata2.bin\\\", start = \$0800, size = \$9800/"+
"floatlibdata2.bin\\\", start = $"+
(floatlib_start.toString(16).toUpperCase())+
", size = $"+
(floatlib_size.toString(16).toUpperCase())+
"/g;"+
's/RAM: file = "", start = $0800, size = $9800/'+
'RAM: file = "", start = $0800, size = $'+
(heap_size.toString(16).toUpperCase())+"/g;"+
"' floatlib.cfg > build/floatliba.cfg"
);
execSync(
"sed 's/libcdata2.bin\\\", start = \$0800, size = \$9800/"+
"libcdata2.bin\\\", start = $"+
(libc_start.toString(16).toUpperCase())+
", size = $"+
(libc_size.toString(16).toUpperCase())+
"/g;"+
's/RAM: file = "", start = $0800, size = $9800/'+
'RAM: file = "", start = $0800, size = $'+
(heap_size.toString(16).toUpperCase())+"/g;"+
"' libc.cfg > build/libca.cfg"
);
execSync(
"sed 's/RAM2: file = \\\"\\\", start = $0800, size = $9800/"+
"RAM2: file = \\\"\\\", start = $"+
(main_start.toString(16).toUpperCase())+
", size = $"+
(main_size.toString(16).toUpperCase())+
"/g;"+
's/RAM: file = "", start = $0800, size = $9800/'+
'RAM: file = "", start = $0800, size = $'+
(heap_size.toString(16).toUpperCase())+"/g;"+
"' main.cfg > build/maina.cfg"
);
execSync(
"sed '"+
's/RAM: file = "", start = $0800, size = $9800/'+
'RAM: file = "", start = $0800, size = $'+
(heap_size.toString(16).toUpperCase())+"/g;"+
"' page.cfg > build/pagea.cfg"
);
compileFloatLib();
}
function compileYYParse() {
console.log("compileYYParse");
/*yyparse goes directly into an oversized page of its own */
execSync(
"cl65 -T -t c64 "+
"-o ./build/obj2/yyparse.bin "+
"-Ln ./build/obj2/yyparse.lbl "+
"-C ./build/yyparsea.cfg ./build/g/_yyparse.s"
);
execSync("mv build/s/_yyparse.s ./build/_yyparse.s");
/* read the label file and use its contents to
update each function address in the hashmap */
var lineReader = readline.createInterface({
input: fs.createReadStream('./build/obj2/yyparse.lbl')
});
lineReader.on('line', updateFunctionAddress.bind(3));
/* when finished, start the next step (compiling the functions that
make up my program) */
lineReader.on('close', addROData);
}
/* pack most function code into a set of 8k memory pages */
function addROData() {
console.log("addROData");
var list = [];
var walker = walk.walk('./build/s', {});
walker.on("file", (root, fileStats, next) => {
list.push([fileStats.name, fs.readFileSync("build/s/"+fileStats.name, {encoding: 'utf8'})]);
next();
});
walker.on("errors", (root, nodeStatsArray, next) => {
next();
});
walker.on("end", () => {
list.forEach(updateName);
packPages();
});
}
function packPages() {
console.log("packPages");
/*patch compareCodepoints into the functions that need it (so the table is
always in the same page) */
execSync("sed -i 's/jmp farret/rts/g;s/__compareCodepoints/_compareCodepoints/g;' build/s/_compareCodepoints.s");
execSync("cat build/s/_compareCodepoints.s >> build/s/_getBytesCP1252.s;sed -i 's/_compareCodepoints/_compareCP1252/g;s/querycsv/querycsv1/g;' build/s/_getBytesCP1252.s");
execSync("cat build/s/_compareCodepoints.s >> build/s/_getBytesCommon.s;sed -i 's/_compareCodepoints/_compareCommon/g;s/querycsv/querycsv2/g;' build/s/_getBytesCommon.s");
execSync("cat build/s/_compareCodepoints.s >> build/s/_getBytesPetscii.s;sed -i 's/_compareCodepoints/_comparePetscii/g;s/querycsv/querycsv3/g;' build/s/_getBytesPetscii.s");
execSync("cat build/s/_compareCodepoints.s >> build/s/_getBytesAtariST.s;sed -i 's/_compareCodepoints/_compareAtariST/g;s/querycsv/querycsv4/g;' build/s/_getBytesAtariST.s");
execSync("rm build/s/_compareCodepoints.s");
execSync(
"pushd build/s;"+
"find * -name \"*.s\" ! -name build/_yyparse.s -print0|"+
"sed -z \"s/\\.s$//g\"|"+
"xargs -0 -I {} sh -c '"+
"echo \".include \\\"../../header.inc\\\"\" > ../g/'{}'.s;"+
"echo \".include \\\"../labels2.s\\\"\" >> ../g/'{}'.s;"+
"cat '{}'.s >> ../g/'{}'.s;"+
""+
"cl65 -T -t c64 "+
"-o ../obj/'{}'.bin "+