Advertisement
Guest User

bin2cas.py

a guest
Aug 16th, 2013
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. # -*- coding: latin-1 -*-
  3.  
  4. """
  5.    bin2cas.py
  6.    ~~~~~~~~~~
  7.  
  8.    converter binary files to .cas used on CoCo2/drgaon 32 emulators like XRoar
  9.  
  10.    origin code from: http://pastebin.com/GCw1NAeY
  11.    see: http://www.coco3.com/community/2010/12/converting-binary-files-to-coco2-cas-files/
  12.  
  13.    cleanup 2013 by Jens Diemer
  14. """
  15.  
  16. import os, sys
  17.  
  18. def hexvl(a_st):
  19.     a_st = a_st.lower()
  20.     tmpr = 0
  21.     hx_st = "0123456789abcdef"
  22.     hx_st = hx_st.lower()
  23.     for i in range(0, len(a_st), 1):
  24.         tmpr = (tmpr * 16) + hx_st.find(a_st[i])
  25.     return tmpr
  26.  
  27. finp_st = sys.argv[1]
  28. fout_st = finp_st + ".cas"
  29.  
  30. if finp_st.lower() == "--help".lower():
  31.     print "bin2cas - 201012221554 - Paulo Silva (GPL licence)"
  32.     print "converts a binary file into a CoCo2 .tap file"
  33.     print "usage: python bin2cas.py yourfile.bin"
  34.     print "the result will be a neighbour file named yourfile.bin.cas"
  35.     print "this converter were made based on information from http://www.cs.unc.edu/~yakowenk/coco/text/tapeformat.html"
  36.     sys.exit()
  37.  
  38.  
  39. finp_fl = open(finp_st, "r")
  40. fout_fl = open(fout_st, "w")
  41.  
  42. lfl = os.path.getsize(finp_st) # lfl=lof(1)
  43.  
  44. for i in range(0, 128, 1): # - 0x55 byte synchronism, 128 times...
  45.     fout_fl.write(chr(0x55))
  46.  
  47. fout_fl.write(chr(0x55)) # - two "magic bytes"
  48. fout_fl.write(chr(0x3C))
  49. fout_fl.write(chr(0x00)) # - block type: filename
  50. fout_fl.write(chr(0x0F)) # - data lenght
  51.  
  52. infnam_st = "ABCDEFGH"
  53. infnam_st = infnam_st + "                "
  54. infnam_st = infnam_st[:8] # - filename - 8 characters
  55. for i in infnam_st:
  56.     fout_fl.write(i)
  57.     # fout_fl.write(infnam_st[i])
  58.  
  59. fout_fl.write(chr(0x02)) # - machine code
  60. fout_fl.write(chr(0x00)) # - ascii flag: binary
  61. fout_fl.write(chr(0x00)) # - gap flag
  62. fout_fl.write(chr(0x0C)) # - machine code starting address
  63. fout_fl.write(chr(0x00))
  64. fout_fl.write(chr(0x0C)) # - machine code loading address
  65. fout_fl.write(chr(0x00))
  66. fout_fl.write(chr(0x4D)) # - checksum (bitwiseand 255)
  67. fout_fl.write(chr(0x55)) # - "magic byte"
  68.  
  69. for i in range(0, 128, 1): # - 0x55 byte synchronism, 128 times...
  70.     fout_fl.write(chr(0x55))
  71.  
  72. while True:
  73.     if lfl < 0:break
  74.     fout_fl.write(chr(0x55)) # - two "magic bytes"
  75.     fout_fl.write(chr(0x3C))
  76.     fout_fl.write(chr(0x01)) # - block type: data
  77.     rlf = 0xFF
  78.     if lfl < rlf:rlf = lfl
  79.     # print rlf
  80.     fout_fl.write(chr(rlf)) # - data lenght
  81.     cfl = 255
  82.     if lfl < 255:cfl = lfl
  83.     cksm = cfl + 1
  84.     for i in range(0, 255, 1):
  85.         byte_st = finp_fl.read(1)
  86.         if len(byte_st) == 0:break
  87.         u = ord(byte_st)
  88.         cksm = (cksm + u) % 256 # - cksm=(cksm+u) mod 256
  89.         fout_fl.write(chr(u)) # - writing byte
  90.     fout_fl.write(chr(cksm)) # - writing checksum
  91.     lfl = lfl - 255
  92.  
  93. fout_fl.write(chr(0x55)) # - "magic byte"
  94. fout_fl.write(chr(0x55)) # - two "magic bytes"
  95. fout_fl.write(chr(0x3C))
  96. fout_fl.write(chr(0xFF)) # - eof
  97. fout_fl.write(chr(0x00)) # - data lenght
  98. fout_fl.write(chr(0xFF)) # - checksum
  99. fout_fl.write(chr(0x55)) # - "magic byte"
  100.  
  101. finp_fl.close()
  102. fout_fl.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement