Add bin_to_tok and tok_to_bin for applesoft native format (via dos33 utility)
authorNick Downing <nick@ndcode.org>
Wed, 18 May 2022 05:06:38 +0000 (15:06 +1000)
committerNick Downing <nick@ndcode.org>
Wed, 18 May 2022 05:06:38 +0000 (15:06 +1000)
bin_to_tok.py [new file with mode: 0755]
tok_to_bin.py [new file with mode: 0755]

diff --git a/bin_to_tok.py b/bin_to_tok.py
new file mode 100755 (executable)
index 0000000..71b1ccc
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+import os
+import sys
+
+bin = list(os.read(sys.stdin.fileno(), 0x10000))
+
+i = 0
+def byte():
+  global i
+  value = bin[i]
+  i += 1
+  return value 
+def word():
+  global i
+  value = bin[i] + (bin[i + 1] << 8)
+  i += 2
+  return value
+tail = word()
+assert tail == len(bin) - 2
+while i < tail:
+  link = word() - 0x800
+  line_number = word()
+  assert link >= i
+  line = bin[i:link]
+  i = link
+  assert byte() == 0
+  sys.stdout.write(
+    '{0:d}{1:s}\n'.format(
+      line_number,
+      ''.join([chr(j) for j in line])
+    )
+  )
+assert i == tail
+assert word() == 0
diff --git a/tok_to_bin.py b/tok_to_bin.py
new file mode 100755 (executable)
index 0000000..1e7033c
--- /dev/null
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+import os
+import sys
+
+bin = []
+def byte(value):
+  bin.append(value & 0xff)
+def word(value):
+  bin.extend([value & 0xff, (value >> 8) & 0xff])
+
+word(0)
+for line in sys.stdin:
+  line = [ord(i) for i in line]
+  assert line.pop() == 0xa
+
+  i = 0
+  line_number = 0
+  while i < len(line) and line[i] >= 0x30 and line[i] < 0x3a:
+    line_number = line_number * 10 + (line[i] & 0xf)
+    i += 1
+  line = line[i:]
+
+  word(len(bin) + 4 + len(line) + 0x800)
+  word(line_number)
+  bin.extend(line)
+  byte(0)
+bin[0] = len(bin) & 0xff
+bin[1] = (len(bin) >> 8) & 0xff
+word(0)
+assert os.write(sys.stdout.fileno(), bytes(bin)) == len(bin)