raw2cas.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. # Written by Pedro Gimeno
  3. # args: infile outfile savename startaddr [execaddr]
  4. # execaddr defaults to startaddr
  5. # For lack of knowledge, currently only executable binaries are supported.
  6. import sys, struct
  7. def main():
  8. if len(sys.argv) < 5:
  9. sys.stderr.write("Usage: raw2cas.py infile outfile filename6chars startaddr"
  10. " [execaddr]\n")
  11. return
  12. start = int(sys.argv[4], 0) & 0xFFFF
  13. if len(sys.argv) > 5:
  14. exe = int(sys.argv[5], 0)
  15. else:
  16. exe = start
  17. h = b'\x1F\xA6\xDE\xBA\xCC\x13\x7D\x74'
  18. if sys.argv[1] != '-':
  19. f = open(sys.argv[1], 'rb')
  20. try:
  21. data = f.read()
  22. finally:
  23. f.close()
  24. else:
  25. data = sys.stdin.read()
  26. end = start + len(data) - 1
  27. nam = ('%-6.6s' % sys.argv[3]).encode('latin1')
  28. if sys.argv[2] != '-':
  29. g = open(sys.argv[2], 'wb')
  30. else:
  31. g = sys.stdout.buffer
  32. try:
  33. # Block 1
  34. g.write(h)
  35. g.write(b'\xD0' * 10)
  36. g.write(nam)
  37. # Block 2
  38. g.write(h)
  39. g.write(struct.pack('<HHH', start, end, exe))
  40. g.write(data)
  41. g.write(b'\0' * (-(len(data) + 6) & 7))
  42. finally:
  43. if sys.argv[2] != '-':
  44. g.close()
  45. main()