cld2cas.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. # Written by Pedro Gimeno
  3. # args: infile outfile savename
  4. import sys, struct
  5. class FileFormatError(Exception):
  6. pass
  7. def main():
  8. if len(sys.argv) < 4:
  9. sys.stderr.write("Usage: bas2cas.py infile outfile filename6chars\n")
  10. return
  11. h = b'\x1F\xA6\xDE\xBA\xCC\x13\x7D\x74'
  12. if sys.argv[1] != '-':
  13. f = open(sys.argv[1], 'rb')
  14. try:
  15. data = f.read()
  16. finally:
  17. f.close()
  18. else:
  19. data = sys.stdin.buffer.read()
  20. if data[0:1] != b'\xFF':
  21. raise FileFormatError("The binary BASIC program does not start with 0FFh")
  22. data = data[1:] + b'\0\0\0\0\0\0\0'
  23. # Pad to a multiple of 8 bytes
  24. data = data + b'\0' * (-len(data) % 8)
  25. # Not the right encoding, so just in case, don't use special characters
  26. nam = ('%-6.6s' % sys.argv[3]).encode('latin1')
  27. if sys.argv[2] != '-':
  28. g = open(sys.argv[2], 'wb')
  29. else:
  30. g = sys.stdout.buffer
  31. try:
  32. # Block 1 (header)
  33. g.write(h)
  34. g.write(b'\xD3' * 10)
  35. g.write(nam)
  36. # Block 2 (data)
  37. g.write(h)
  38. g.write(data)
  39. finally:
  40. if sys.argv[2] != '-':
  41. g.close()
  42. main()