Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: latin-1 -*-
- """
- bin2cas.py
- ~~~~~~~~~~
- converter binary files to .cas used on CoCo2/drgaon 32 emulators like XRoar
- origin code from: http://pastebin.com/GCw1NAeY
- see: http://www.coco3.com/community/2010/12/converting-binary-files-to-coco2-cas-files/
- cleanup 2013 by Jens Diemer
- """
- import os, sys
- def hexvl(a_st):
- a_st = a_st.lower()
- tmpr = 0
- hx_st = "0123456789abcdef"
- hx_st = hx_st.lower()
- for i in range(0, len(a_st), 1):
- tmpr = (tmpr * 16) + hx_st.find(a_st[i])
- return tmpr
- finp_st = sys.argv[1]
- fout_st = finp_st + ".cas"
- if finp_st.lower() == "--help".lower():
- print "bin2cas - 201012221554 - Paulo Silva (GPL licence)"
- print "converts a binary file into a CoCo2 .tap file"
- print "usage: python bin2cas.py yourfile.bin"
- print "the result will be a neighbour file named yourfile.bin.cas"
- print "this converter were made based on information from http://www.cs.unc.edu/~yakowenk/coco/text/tapeformat.html"
- sys.exit()
- finp_fl = open(finp_st, "r")
- fout_fl = open(fout_st, "w")
- lfl = os.path.getsize(finp_st) # lfl=lof(1)
- for i in range(0, 128, 1): # - 0x55 byte synchronism, 128 times...
- fout_fl.write(chr(0x55))
- fout_fl.write(chr(0x55)) # - two "magic bytes"
- fout_fl.write(chr(0x3C))
- fout_fl.write(chr(0x00)) # - block type: filename
- fout_fl.write(chr(0x0F)) # - data lenght
- infnam_st = "ABCDEFGH"
- infnam_st = infnam_st + " "
- infnam_st = infnam_st[:8] # - filename - 8 characters
- for i in infnam_st:
- fout_fl.write(i)
- # fout_fl.write(infnam_st[i])
- fout_fl.write(chr(0x02)) # - machine code
- fout_fl.write(chr(0x00)) # - ascii flag: binary
- fout_fl.write(chr(0x00)) # - gap flag
- fout_fl.write(chr(0x0C)) # - machine code starting address
- fout_fl.write(chr(0x00))
- fout_fl.write(chr(0x0C)) # - machine code loading address
- fout_fl.write(chr(0x00))
- fout_fl.write(chr(0x4D)) # - checksum (bitwiseand 255)
- fout_fl.write(chr(0x55)) # - "magic byte"
- for i in range(0, 128, 1): # - 0x55 byte synchronism, 128 times...
- fout_fl.write(chr(0x55))
- while True:
- if lfl < 0:break
- fout_fl.write(chr(0x55)) # - two "magic bytes"
- fout_fl.write(chr(0x3C))
- fout_fl.write(chr(0x01)) # - block type: data
- rlf = 0xFF
- if lfl < rlf:rlf = lfl
- # print rlf
- fout_fl.write(chr(rlf)) # - data lenght
- cfl = 255
- if lfl < 255:cfl = lfl
- cksm = cfl + 1
- for i in range(0, 255, 1):
- byte_st = finp_fl.read(1)
- if len(byte_st) == 0:break
- u = ord(byte_st)
- cksm = (cksm + u) % 256 # - cksm=(cksm+u) mod 256
- fout_fl.write(chr(u)) # - writing byte
- fout_fl.write(chr(cksm)) # - writing checksum
- lfl = lfl - 255
- fout_fl.write(chr(0x55)) # - "magic byte"
- fout_fl.write(chr(0x55)) # - two "magic bytes"
- fout_fl.write(chr(0x3C))
- fout_fl.write(chr(0xFF)) # - eof
- fout_fl.write(chr(0x00)) # - data lenght
- fout_fl.write(chr(0xFF)) # - checksum
- fout_fl.write(chr(0x55)) # - "magic byte"
- finp_fl.close()
- fout_fl.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement