-
Notifications
You must be signed in to change notification settings - Fork 0
/
cranktheprint
executable file
·828 lines (761 loc) · 31.8 KB
/
cranktheprint
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
#!/usr/bin/env python3
# Crank the PRINT!
# Because in 2022, the C64 PRINT needs a speed update.
# https://github.com/c1570/CrankThePRINT
import csv
import os.path
import sys
input_file = sys.argv[1] if len(sys.argv) > 1 else "input.prg"
output_file = "output.prg"
output_html_file = "output.html"
table_filename_template = "table_$1.prg"
str_table_buckets = [
(0xe000, 0xfff0),
(0xa000, 0xafff),
(0xc800, 0xcdff)
]
helper_addr = 0xce00
def line_whitelisted(number):
return (number < 502 or number > 532)
TOKENS = ["END", "FOR", "NEXT", "DATA", "INPUT#", "INPUT", "DIM", "READ",
"LET", "GOTO", "RUN", "IF", "RESTORE", "GOSUB", "RETURN", "REM",
"STOP", "ON", "WAIT", "LOAD", "SAVE", "VERIFY", "DEF", "POKE",
"PRINT#", "PRINT", "CONT", "LIST", "CLR", "CMD", "SYS", "OPEN",
"CLOSE", "GET", "NEW", "TAB(", "TO", "FN", "SPC(", "THEN", "NOT",
"STEP", "+", "-", "*", "/", "^", "AND", "OR", ">", "=", "<",
"SGN", "INT", "ABS", "USR", "FRE", "POS", "SQR", "RND", "LOG",
"EXP", "COS", "SIN", "TAN", "ATN", "PEEK", "LEN", "STR$", "VAL",
"ASC", "CHR$", "LEFT$", "RIGHT$", "MID$", "GO"]
TOK_TO_STR = {}
val = 128
for tok in TOKENS:
TOK_TO_STR[val] = tok
val += 1
def prg_line_to_ascii(data: bytearray):
def convert(c):
return TOK_TO_STR[c] if c in TOK_TO_STR else chr(c)
res = ""
in_quotes = False
for c in data:
if c == 34:
in_quotes = not in_quotes
res += convert(c) if not in_quotes else chr(c)
return res
class Element:
def __init__(self, data):
self.data = data
def get_bytes(self):
return self.data
def __str__(self):
return f"Element {self.data}"
class ElementPrint(Element):
def __init__(self, data):
self.data = data
data = data.replace(b'""',
b'') # this replaces "" only in internal representation, will not get serialized later
self.is_constant = data[1:2] == b'"' and (data[-2:] == b'";' or data[-1:] == b'"') and b'"' not in data[2:-2]
if self.is_constant:
if data[-2:] == b'";':
self.constant = data[2:-2]
else:
self.constant = data[2:-1] + b'\x0d'
def __str__(self):
return f"PRINT {self.data}"
class ElementSys(Element):
def __init__(self, data: bytearray = None, addr: int = None):
if data is not None:
self.data = data
elif addr is not None:
self.data = bytearray([158] + bytes(str(addr), "ascii"))
else:
assert False
self.is_sysq = False
self.is_simple_sysq = False
if self.data[1] != 0x51:
return
pars = self.data[1:].split(b',')
if len(pars) < 4:
return
self.is_sysq = pars[0] == b'Q'
self.sysq_pars = pars[1:3]
str_par = self.data[len(pars[0]) + len(pars[1]) + len(pars[2]) + 4:]
str_par = str_par.replace(b'""',
b'') # this replaces "" only in internal representation, will not get serialized later
self.is_simple_sysq = self.is_sysq and str_par[0:1] == b'"' and (
str_par[-2:] == b'";' or str_par[-1:] == b'"') and b'"' not in str_par[2:-2]
self.sysq_pars.append(str_par)
if self.is_simple_sysq:
if str_par[-2:] == b'";':
str_par = str_par[1:-2]
else:
str_par = str_par[1:-1] + b'\x0d'
self.constant = str_par
print(f"SIMPLE sysq_pars: {self.sysq_pars}")
else:
print(f"COMPLEX sysq_pars: {self.sysq_pars}")
class ElementIf(Element):
pass
def create_element(data):
if len(data) > 0:
if data[0] == 153:
return ElementPrint(data)
elif data[0] == 158:
return ElementSys(data)
elif data[0] == 139:
return ElementIf(data)
return Element(data)
CTRLCODE_TO_TITLE = {
3: "run/stop",
5: "white",
8: "disable toggle charset",
9: "enable toggle charset",
13: "return",
14: "toggle charset",
17: "down",
18: "reverse on",
19: "home",
20: "del",
28: "red",
29: "right",
30: "green",
31: "blue",
129: "orange",
131: "shift run/stop",
133: "f1",
134: "f3",
135: "f5",
136: "f7",
137: "f2",
138: "f4",
139: "f6",
140: "f8",
141: "shift return",
142: "upper caps/graphics",
144: "black",
145: "up",
146: "reverse off",
147: "clear",
148: "insert",
149: "brown",
150: "light red",
151: "grey 1",
152: "grey 2",
153: "light green",
154: "light blue",
155: "grey 3",
156: "purple",
157: "left",
158: "yellow",
159: "cyan"
}
def get_var_branch_info(data: bytearray, branch_targets: list = [], jump_sources: list = None) -> str:
if len(data) <= 2:
return ""
in_data = False
in_quotes = False
in_rem = False
in_branch = False
in_gosub = False
line_has_if = False
starting_token = -1
line_contains_unconditional_branch = False
line_number = data[0] + (data[1] << 8)
var_name = bytearray()
branch_target = bytearray()
line_title = labels["sub"].get(str(line_number), "")
if line_title != "":
line_title = line_title + " "
j_sources = " ".join([f"GOTO from {j}" if j >= 0 else f"GOSUB from {abs(j)}" for j in jump_sources]) if jump_sources is not None else ""
is_subroutine = min(jump_sources) < 0 if jump_sources is not None and len(jump_sources) > 0 else False
line_number_style = "color: green" if is_subroutine else ("color: grey" if jump_sources is not None and len(jump_sources) > 0 else "")
html = f"<div class='fontc64' id='line_{line_number}'><span title='{line_title}{j_sources}' style='{line_number_style}'>{line_number}</span> "
def get_var_id(var_name):
return "var_" + var_name.replace(" ", "_").replace("$", "str").replace("%", "int").replace("(", "arr")
def add_var_name_to_html():
nonlocal html
if len(var_name) > 0:
var_label = var_name.decode('ascii')
#print(f"Variable: {var_label}")
var_name.clear()
global labels
var_id = get_var_id(var_label)
var_title = labels["var"].get(var_label, "")
#if var_title == "":
# print(f"var,{var_label},")
html = html + f"<span class='{var_id}' title='{var_title}' onclick='highlightClass(\"{var_id}\")'>{var_label}</span>"
def add_branch_target_to_html():
nonlocal html, in_gosub
if len(branch_target) > 0:
target = int(branch_target.decode('ascii'))
#print(f"Branch target: {target}")
branch_title = labels["sub"].get(str(target), "")
html = html + f"<a href='#line_{target}' title='{branch_title}'>{target}</a>"
if in_gosub:
target = -target
branch_targets.append(target)
branch_target.clear()
def add_char_to_html(c: int, detokenize: bool):
nonlocal html
if c == 32:
html = html + " "
elif 31 < c < 91:
html = html + chr(c)
elif detokenize:
if c in TOK_TO_STR:
token = TOK_TO_STR[c]
if len(token) > 1:
html = html + f"<a class='nolink' href='https://c64-wiki.de/wiki/{token}'>{token}</a>"
else:
html = html + token
else:
html = html + f"î{c:2x};"
else:
sc = ([0x80, 0x20, 0x00, 0x40, 0xC0, 0x60, 0x40, 0x60][c >> 5] + (c & 0x1f)) if c != 255 else 94
char = f"î{sc:2x};"
if c in CTRLCODE_TO_TITLE:
char = f"<span title='{CTRLCODE_TO_TITLE[c]}'>{char}</span>"
html = html + char
for c in data[2:]:
if in_branch and c > 127:
add_branch_target_to_html()
in_branch = False
in_gosub = False
if not in_quotes and not in_data and not in_rem and c > 127:
if c == 137 and not starting_token == 145: # GOTO but not ON...GOTO
line_contains_unconditional_branch = True
if c in [128, 142]: # END, RETURN
line_contains_unconditional_branch = True
if starting_token == -1:
starting_token = c
if in_quotes or c == 34: # "
if c == 34:
add_var_name_to_html()
in_quotes = not in_quotes
add_char_to_html(c, False)
elif in_data:
if c == 58: # :
in_data = False
starting_token = -1
add_char_to_html(c, False)
elif in_rem:
add_char_to_html(c, True)
continue
elif c == 131: # DATA
add_var_name_to_html()
add_char_to_html(c, True)
in_data = True
elif c == 143: # REM
add_var_name_to_html()
add_char_to_html(c, True)
in_rem = True
elif c in [137, 138, 141, 155, 167]: # GOTO, RUN, GOSUB, LIST, THEN
add_var_name_to_html()
in_gosub = c == 141
in_branch = True
add_char_to_html(c, True)
elif c == 40: # (
if len(var_name) > 0:
var_name.append(c)
add_var_name_to_html()
else:
add_char_to_html(c, True)
elif (64 < c < 91) and len(var_name) == 0: # A..Z
add_branch_target_to_html()
in_branch = False
in_gosub = False
var_name.append(c)
elif (64 < c < 91) or (47 < c < 58) and len(var_name) > 0: # A..Z0..9
if var_name[-1] in [36, 37]: # %, $
add_var_name_to_html()
var_name.append(c)
elif (47 < c < 58) and in_branch:
branch_target.append(c)
elif (c in [36, 37]) and len(var_name) > 0: # %, $
var_name.append(c)
add_branch_target_to_html()
elif c == 32: # spaces are not significant but possibly _important_ ("F N")
if len(var_name) > 0:
var_name.append(c)
continue
else:
if in_branch:
if c == 44: # ,
add_branch_target_to_html()
elif c == 58: # :
add_branch_target_to_html()
in_branch = False
in_gosub = False
add_var_name_to_html()
add_char_to_html(c, True)
if c == 58: # :
starting_token = -1
elif c == 139: # IF
line_has_if = True
add_var_name_to_html()
add_branch_target_to_html()
html = html + "</div>"
if not line_has_if and line_contains_unconditional_branch:
html = html + "<br/>"
return html + "\n"
class Line:
def __init__(self, data):
self.number = data[0] + (data[1] << 8)
self.elements = []
data = data[2:]
print(f"{self.number} {prg_line_to_ascii(data)} --- {data}")
off = 0
in_quotes = False
in_data = False
while off < len(data):
if data[off] == 34: # "
in_quotes = not in_quotes
elif data[off] == 131: # DATA
in_data = True
elif data[off] == 58: # :
if not in_quotes:
self.elements.append(create_element(data[:off]))
self.elements.append(create_element(data[off:off + 1])) # TODO should include whitespace here
data = data[off + 1:]
in_data = False
off = -1
elif data[off] == 167: # THEN
if not in_quotes and not in_data:
self.elements.append(create_element(data[:off + 1]))
data = data[off + 1:]
off = -1
off += 1
if len(data) > 0:
self.elements.append(create_element(data))
def get_bytes(self, branch_table=[]):
out = bytearray([self.number & 255, (self.number >> 8) & 255])
empty_line = True
for el in self.elements:
el_bytes = bytes(el.get_bytes())
if el_bytes != b':' and el_bytes != b'':
empty_line = False
out.extend(el_bytes)
if empty_line:
if self.number in branch_table and len(branch_table[self.number]) > 0:
print(f"{self.number} is in branch_table! len(out) is {len(out)}")
if len(out) == 2:
out.extend(b'\x8f') # all elements got removed but line is target of a branch: add REM
else:
return bytearray() # empty line that's not target of a branch: just ignore
out.extend(b'\00')
return out
def __str__(self):
return f"{self.number} {prg_line_to_ascii(self.get_bytes()[2:-1])} --- {len(self.elements)} elements"
class Program:
def __init__(self, data):
self.startaddr = data[0] + (data[1] << 8)
self.lines = []
curoff = 2
while curoff < len(data):
if data[curoff] == 0 and data[curoff + 1] == 0:
return
line_data = data[curoff + 2:]
line_data = line_data[0:line_data[2:].find(b'\x00') + 2]
# print(line_data)
self.lines.append(Line(line_data))
curoff += len(line_data) + 3
def get_bytes(self, branch_table=[]):
outprg = bytearray([self.startaddr & 255, (self.startaddr >> 8) & 255])
curadr = self.startaddr
for line in self.lines:
line_bytes = line.get_bytes(branch_table)
if len(line_bytes) == 0:
continue
curadr += len(line_bytes) + 2
outprg.append(curadr & 255)
outprg.append((curadr >> 8) & 255)
outprg.extend(line_bytes)
outprg.extend([0, 0])
return outprg
def __str__(self):
return f"Program with {len(self.lines)} lines"
def analyze_program(program: Program) -> (dict, str):
line_number_to_jump_source = dict()
for line in program.lines:
branch_targets = []
get_var_branch_info(line.get_bytes()[:-1],
branch_targets=branch_targets)
for target in branch_targets:
sources = line_number_to_jump_source.setdefault(abs(target), [])
sources.append(line.number if target >= 0 else -line.number)
html = """
<!DOCTYPE html><html><head><meta charset="utf-8"/><style type="text/css">
@font-face {
font-family: "C64";
src: url("C64_Pro_Mono-STYLE.woff2") format("woff2");
}
.fontc64 { font-family: "C64"; white-space: nowrap; }
.nolink { text-decoration: none; }
</style>
<script>
function highlightClass(cls){
document.querySelectorAll('.marker').forEach(a => a.remove());
document.querySelectorAll('.highlighted').forEach(a => { a.classList.remove("highlighted"); a.style = "color: black"; });
for(let el of document.getElementsByClassName(cls)) {
el.style = "color: red";
el.classList.add("highlighted");
let d = document.createElement("div");
d.classList.add("marker");
d.innerHtml=" ";
let relative = (el.getBoundingClientRect().top + (window.pageYOffset || document.documentElement.scrollTop)) / document.body.scrollHeight * 100;
d.style.cssText="position: fixed; right: 0; border: 3px solid; border-color: red; width: 10px; top: " + relative + "%;";
document.body.appendChild(d);
}
};
</script>
</head><body>
"""
for line in program.lines:
html = html + get_var_branch_info(line.get_bytes()[:-1],
jump_sources=line_number_to_jump_source.setdefault(line.number, []))
html = html + "</body></html>"
return line_number_to_jump_source, html
petscii_to_color = {144: 0, 5: 1, 28: 2, 159: 3, 156: 4, 30: 5, 31: 6, 158: 7, 129: 8, 149: 9, 150: 10, 151: 11,
152: 12, 153: 13, 154: 14, 155: 15}
def get_fastcode_for_offset(off: int) -> bytearray:
res = bytearray()
while off != 0:
if off >= 0:
b = off if off <= 127 else 127
else:
b = off if off >= -128 else -128
res.extend([254, b & 0xff])
off -= b
return res
def get_term_fastcode(adj_x, adj_y: int) -> bytearray:
return bytearray([255,
adj_x & 0xff,
adj_y & 0xff]) # end marker
def convert_petscii_to_fastcode(data) -> (bytearray, int, int):
res = bytearray()
accumulated_offset = 0
adjust_x_pos = 0
adjust_y_pos = 0
cur_color = -1
c = 0
reversed = False
i = 0
while i < len(data):
c = data[i]
i += 1
if c == 255: # Pi
c = 94
elif c == 147:
assert False, "clr/home not supported"
elif c == 19:
assert False, "home not supported"
elif c == 29: # RIGHT
accumulated_offset += 1
adjust_x_pos += 1
continue
elif c == 157: # LEFT
accumulated_offset -= 1
adjust_x_pos -= 1
continue
elif c == 17: # DOWN
accumulated_offset += 40
adjust_y_pos += 1
continue
elif c == 145: # UP
accumulated_offset -= 40
adjust_y_pos -= 1
continue
elif c in petscii_to_color:
col_new = petscii_to_color[c]
if col_new != cur_color:
res.extend([252, col_new])
cur_color = col_new
continue
elif c == 18:
reversed = True
continue
elif c == 146:
reversed = False
continue
elif c == 13 and i == len(data):
continue
elif c in [133, 134, 135, 136, 137, 138, 139, 140, 20, 3, 8, 9, 13, 141, 148, 14, 142]:
assert False, "unsupported PETSCII {i}"
else:
c = [0x80, 0x20, 0x00, 0x40, 0xC0, 0x60, 0x40, 0x60][c >> 5] + (c & 0x1f)
if reversed:
c = c | 0x80
adjust_x_pos += 1
res.extend(get_fastcode_for_offset(accumulated_offset))
accumulated_offset = 0
if c >= 252:
res.extend([253, c]) # escape one char
else:
res.append(c)
# if reversed:
# assert False, "reversed still on"
res.extend(get_fastcode_for_offset(accumulated_offset))
return res, adjust_x_pos, adjust_y_pos
class StringTracker:
def __init__(self):
self.str_table = []
self.total_src_bytes = 0
self.total_dst_bytes = 0
self.tracked_elements = []
self.tracked_fastcode = bytearray()
self.start_x_pos = None
self.start_y_pos = None
self.cur_x_pos = None
self.cur_y_pos = None
self.x_adj = 0
self.y_adj = 0
self.buckets = []
def reset(self):
self.tracked_elements = []
self.tracked_fastcode = bytearray()
self.start_x_pos = None
self.start_y_pos = None
self.cur_x_pos = None
self.cur_y_pos = None
self.x_adj = 0
self.y_adj = 0
def add_string(self, el: Element, petscii: bytearray, start_x_pos: int = None, start_y_pos: int = None,
could_parse_start_pos: bool = True):
if start_y_pos is not None and self.cur_y_pos is None or \
start_x_pos is not None and self.cur_x_pos is None:
# have new absolute positioning but were relative before: flush first
self.flush_string(deinit_xy_tracking=True)
if self.cur_x_pos is not None and start_x_pos is not None:
if self.cur_y_pos is not None and start_y_pos is not None:
# have abs pos, got abs PRINT: move relative
self.tracked_fastcode.extend(
get_fastcode_for_offset(start_x_pos - self.cur_x_pos + (start_y_pos - self.cur_y_pos)*40)
)
self.x_adj += start_x_pos - self.cur_x_pos
self.y_adj += start_y_pos - self.cur_y_pos
self.cur_x_pos = start_x_pos
self.cur_y_pos = start_y_pos
if len(self.tracked_fastcode) == 0:
# just starting, init x/y tracking
if could_parse_start_pos:
if start_x_pos is None and self.cur_x_pos is not None:
start_x_pos = self.cur_x_pos # know X from previous
if start_y_pos is None and self.cur_y_pos is not None:
start_y_pos = self.cur_y_pos # know Y from previous
self.start_x_pos = start_x_pos
self.start_y_pos = start_y_pos
self.cur_x_pos = start_x_pos
self.cur_y_pos = start_y_pos
self.x_adj = 0
self.y_adj = 0
self.tracked_elements.append(el)
fastcode, adj_x, adj_y = convert_petscii_to_fastcode(petscii)
self.x_adj += adj_x
self.y_adj += adj_y
if self.cur_y_pos is not None:
self.cur_y_pos += adj_y
if self.cur_x_pos is not None:
self.cur_x_pos += adj_x
self.tracked_fastcode.extend(fastcode)
self.total_src_bytes += len(petscii)
if petscii.endswith(b'\x0d'):
if self.cur_x_pos is None and start_x_pos is None:
# were relative, have relative string that ends in CR: flush
self.x_adj = 0xff
self.flush_string()
else:
# we have absolut pos, just keep track of CR
if self.cur_y_pos is not None:
self.cur_y_pos += 1
self.y_adj += 1
self.x_adj -= self.cur_x_pos
self.tracked_fastcode.extend(
get_fastcode_for_offset(40 - self.cur_x_pos)
)
self.cur_x_pos = 0 # we do know that we're in column 0 now
def fill_tables(self, check_only: bool):
if not check_only:
self.buckets = [bytearray() for bucket in str_table_buckets]
self.buckets[0].extend(bytearray(len(self.str_table) * 2))
remaining_bytes = [bucket[1] - bucket[0] + 1 for bucket in str_table_buckets]
remaining_bytes[0] -= len(self.str_table) * 2 # offset table goes into bucket 0
for i_entry in range(len(self.str_table)):
entry = self.str_table[i_entry]
for i_bucket in range(len(remaining_bytes)):
len_entry = len(entry)
if remaining_bytes[i_bucket] > len_entry:
remaining_bytes[i_bucket] = remaining_bytes[i_bucket] - len_entry
if not check_only:
addr = str_table_buckets[i_bucket][0] + len(self.buckets[i_bucket])
# print(f"String nr. {i_entry} goes to {addr:04x}")
self.buckets[i_bucket].extend(entry)
self.buckets[0][i_entry * 2] = addr & 255
self.buckets[0][i_entry * 2 + 1] = addr >> 8
entry = None
break
if entry is not None:
return False # all bucket space exhausted
return True
def write_tables(self):
self.fill_tables(check_only=False)
for i_bucket in range(len(self.buckets)):
table_filename = table_filename_template.replace("$1", f"{str_table_buckets[i_bucket][0]:04x}".lower())
print(f"Table {table_filename} from {str_table_buckets[i_bucket][0]:04x} to {(str_table_buckets[i_bucket][0]+len(self.buckets[i_bucket])):04x}")
with open(table_filename, "wb") as tablefile:
tablefile.write(bytearray([str_table_buckets[i_bucket][0] & 255, str_table_buckets[i_bucket][0] >> 8]))
tablefile.write(self.buckets[i_bucket])
def flush_string(self, deinit_xy_tracking: bool = False):
if len(self.tracked_fastcode) == 0:
if deinit_xy_tracking:
self.reset()
return False
print("FLUSHING")
# end term marker to fastcode
self.tracked_fastcode.extend(get_term_fastcode(self.x_adj, self.y_adj))
# add fastcode to table/reuse existing entry
if self.tracked_fastcode not in self.str_table:
if len(self.str_table) >= 255:
print("ERROR: Available table entries exhausted")
return False
self.str_table.append(self.tracked_fastcode)
if not self.fill_tables(check_only=True):
self.str_table.pop()
print("ERROR: Table space exhausted")
self.reset()
return False
i_str = len(self.str_table) - 1
self.total_dst_bytes = self.total_dst_bytes + len(self.tracked_fastcode) + 2
print(f"Using fastcode nr. {i_str} from table")
else:
i_str = self.str_table.index(self.tracked_fastcode)
print(f"Fastcode already in table at index {i_str}, reusing")
# generate BASIC code
rep_code = bytearray()
# GotoXY
if self.start_x_pos is not None:
rep_code.extend(b'\x97211,') # poke211,
rep_code.extend(str(self.start_x_pos).encode("ascii"))
rep_code.extend(b':')
if self.start_y_pos is not None:
rep_code.extend(b'\x97214,') # poke214,
rep_code.extend(str(self.start_y_pos).encode("ascii"))
rep_code.extend(b':')
# POKEhelper,index:SYShelper+1
rep_code.extend(b'\x97' + str(helper_addr).encode("ascii") + b',' + str(i_str).encode("ascii")) # poke...
rep_code.extend(b':\x9e' + str(helper_addr + 1).encode("ascii")) # sys...
self.tracked_elements[0].data = rep_code # todo don't mutate
if len(self.tracked_elements) > 1:
# replace other tracked BASIC elements with NOPs
for el in self.tracked_elements[1:]:
print(f"Nuking {el.data}")
el.data = bytearray()
if deinit_xy_tracking:
self.reset()
else:
self.tracked_elements = []
self.tracked_fastcode = bytearray()
return True
def crank_the_print(program: Program, branch_table: dict):
string_tracker = StringTracker()
for line in program.lines:
if line.number in branch_table.keys() and len(branch_table[line.number]) > 0:
if string_tracker.flush_string(deinit_xy_tracking=True):
print(f"Flushed: Line {line.number} is jumped to from {' '.join([str(l) for l in branch_table[line.number]])}")
print(line)
i_el = -1
line_contains_branch = False
while i_el < len(line.elements) - 1:
i_el += 1
el = line.elements[i_el]
if isinstance(el, ElementIf):
line_contains_branch = True
if isinstance(el, ElementPrint):
if not el.is_constant or not line_whitelisted(line.number):
string_tracker.flush_string(deinit_xy_tracking=True)
continue
print(f"PRINT constant in line {line.number}: {el.constant}")
try:
if len(string_tracker.tracked_fastcode) + len(el.constant) < 10:
raise Exception("skipping, constant too short")
string_tracker.add_string(el, el.constant)
except Exception as e:
# non-convertable string
string_tracker.flush_string(deinit_xy_tracking=True)
print(e)
continue
if isinstance(el, ElementSys):
try:
if el.is_sysq:
print(f"SYSQ found in line {line.number}: {el.sysq_pars}")
rep_code = bytearray(b'\x97211,') # poke211,
rep_code.extend(el.sysq_pars[0])
rep_code.extend(b':\x97214,') # poke214,
rep_code.extend(el.sysq_pars[1])
rep_code.extend(b':')
el.data = rep_code # todo don't mutate
if el.is_simple_sysq and line_whitelisted(line.number):
try:
convert_petscii_to_fastcode(el.constant) # just checking suitability of string
x_pos = None
y_pos = None
try:
x_pos = int(el.sysq_pars[0].decode("ascii"))
y_pos = int(el.sysq_pars[1].decode("ascii"))
# Notify StringTracker of X/Y pos and mark generated GotoXY code for replacement.
string_tracker.add_string(el, el.constant, x_pos, y_pos)
# Just in case flush_string runs into trouble later, add standard code
el.data.extend(b'\x9e58732:\x99') # sys58732:print
el.data.extend(el.sysq_pars[2])
except Exception as e:
print("SYSQ doesn't use constants as X/Y, cannot track position")
# We keep the two POKEs in any case.
# Add a SYSgotoxy:PRINT placeholder element that might get replaced later.
rep_code = bytearray(b'\x9e58732:\x99')
rep_code.extend(el.sysq_pars[2])
next_el = Element(rep_code)
i_el += 1
line.elements.insert(i_el, next_el)
string_tracker.add_string(next_el, el.constant, could_parse_start_pos=False)
continue
except Exception as e:
# cannot convert, fall through to normal PRINT
print(e)
# combined sysq or non-compilable parameter, just PRINT
string_tracker.flush_string(deinit_xy_tracking=True)
el.data.extend(b'\x9e58732:\x99') # sys58732:print
el.data.extend(el.sysq_pars[2])
else:
string_tracker.flush_string(deinit_xy_tracking=True)
except Exception as e:
string_tracker.flush_string(deinit_xy_tracking=True)
print(e)
continue
if el.get_bytes() != b':':
# print(f"Unhandled element: {prg_line_to_ascii(el.get_bytes())} --- {el}")
string_tracker.flush_string(deinit_xy_tracking=True)
# end of line
if line_contains_branch:
if string_tracker.flush_string(deinit_xy_tracking=True):
print("Flushed: Line contained branch")
string_tracker.flush_string()
print("******* Done")
print(f"Number of strings: {len(string_tracker.str_table)}")
print(f"Input bytes: {string_tracker.total_src_bytes}")
print(f"Output bytes: {string_tracker.total_dst_bytes}")
string_tracker.write_tables()
with open(input_file, "rb") as infile:
inprg = bytes(infile.read())
labels = {"var": {}, "sub": {}}
if os.path.isfile("labels.csv"):
with open("labels.csv", newline="") as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
for row in csvreader:
if row[2] != "":
labels[row[0]][row[1]] = row[2]
input_program = Program(inprg)
branch_table, doc_html = analyze_program(input_program)
crank_the_print(input_program, branch_table)
outprg = input_program.get_bytes(branch_table)
with open(output_file, "wb") as outfile:
outfile.write(outprg)
with open(output_html_file, "w") as outfile:
outfile.write(doc_html)