bas2cas.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python3
  2. # Written by Pedro Gimeno
  3. # args: infile outfile savename
  4. # execaddr defaults to startaddr
  5. import sys, struct
  6. def main():
  7. if len(sys.argv) < 4:
  8. sys.stderr.write("Usage: bas2cas.py infile outfile filename6chars\n")
  9. return
  10. h = b'\x1F\xA6\xDE\xBA\xCC\x13\x7D\x74'
  11. if sys.argv[1] != '-':
  12. f = open(sys.argv[1], 'rb')
  13. try:
  14. data = f.read()
  15. finally:
  16. f.close()
  17. else:
  18. data = sys.stdin.buffer.read()
  19. # Pad it to a multiple of 256 bytes, adding at least one EOF
  20. data += b'\x1A'
  21. data += b'\x1A' * (-len(data) % 256)
  22. # Not the right encoding, so just in case, don't use special characters
  23. nam = ('%-6.6s' % sys.argv[3]).encode('latin1')
  24. if sys.argv[2] != '-':
  25. g = open(sys.argv[2], 'wb')
  26. else:
  27. g = sys.stdout.buffer
  28. try:
  29. # Block 1 (header)
  30. g.write(h)
  31. g.write(b'\xEA' * 10)
  32. g.write(nam)
  33. # Subsequent blocks
  34. for i in range(0, len(data), 256):
  35. g.write(h)
  36. g.write(data[i:i+256])
  37. finally:
  38. if sys.argv[2] != '-':
  39. g.close()
  40. main()