Advertisement
Guest User

bin2cas.py

a guest
Dec 22nd, 2010
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: latin-1 -*-
  3. import os,sys
  4. def hexvl(a_st):
  5.   a_st=a_st.lower();tmpr=0;hx_st="0123456789abcdef";hx_st=hx_st.lower()
  6.   for i in range(0,len(a_st),1):
  7.     tmpr=(tmpr*16)+hx_st.find(a_st[i])
  8.   return tmpr
  9. finp_st=sys.argv[1];fout_st=finp_st+".cas"
  10. if finp_st.lower()=="--help".lower():
  11.   print "bin2cas - 201012221554 - Paulo Silva (GPL licence)"
  12.   print "converts a binary file into a CoCo2 .tap file"
  13.   print "usage: python bin2cas.py yourfile.bin"
  14.   print "the result will be a neighbour file named yourfile.bin.cas"
  15.   print "this converter were made based on information from http://www.cs.unc.edu/~yakowenk/coco/text/tapeformat.html"
  16. else:
  17.   finp_fl=open(finp_st,"r");fout_fl=open(fout_st,"w")
  18.   lfl=os.path.getsize(finp_st) # lfl=lof(1)
  19.   for i in range(0,128,1):   #- 0x55 byte synchronism, 128 times...
  20.     fout_fl.write(chr(0x55))
  21.   fout_fl.write(chr(0x55))    #- two "magic bytes"
  22.   fout_fl.write(chr(0x3C))
  23.   fout_fl.write(chr(0x00))    #- block type: filename
  24.   fout_fl.write(chr(0x0F))    #- data lenght
  25.   infnam_st="ABCDEFGH"
  26.   infnam_st=infnam_st+"        "
  27.   infnam_st=infnam_st[:8]     #- filename - 8 characters
  28.   for i in infnam_st:
  29.     fout_fl.write(i)
  30.     #fout_fl.write(infnam_st[i])
  31.   fout_fl.write(chr(0x02)) #- machine code
  32.   fout_fl.write(chr(0x00)) #- ascii flag: binary
  33.   fout_fl.write(chr(0x00)) #- gap flag
  34.   fout_fl.write(chr(0x0C)) #- machine code starting address
  35.   fout_fl.write(chr(0x00))
  36.   fout_fl.write(chr(0x0C)) #- machine code loading address
  37.   fout_fl.write(chr(0x00))
  38.   fout_fl.write(chr(0x4D)) #- checksum (bitwiseand 255)
  39.   fout_fl.write(chr(0x55)) #- "magic byte"
  40.   for i in range(0,128,1): #- 0x55 byte synchronism, 128 times...
  41.     fout_fl.write(chr(0x55))
  42.   while True:
  43.     if lfl<0:break
  44.     fout_fl.write(chr(0x55)) #- two "magic bytes"
  45.     fout_fl.write(chr(0x3C))
  46.     fout_fl.write(chr(0x01)) #- block type: data
  47.     rlf=0xFF
  48.     if lfl<rlf:rlf=lfl
  49.     #print rlf
  50.     fout_fl.write(chr(rlf))  #- data lenght
  51.     cfl=255
  52.     if lfl<255:cfl=lfl
  53.     cksm=cfl+1
  54.     for i in range(0,255,1):
  55.       byte_st=finp_fl.read(1)
  56.       if len(byte_st)==0:break
  57.       u=ord(byte_st)
  58.       cksm=(cksm+u)%256 #- cksm=(cksm+u) mod 256
  59.       fout_fl.write(chr(u)) #- writing byte
  60.     fout_fl.write(chr(cksm)) #- writing checksum
  61.     lfl=lfl-255
  62.   fout_fl.write(chr(0x55)) #- "magic byte"
  63.   fout_fl.write(chr(0x55)) #- two "magic bytes"
  64.   fout_fl.write(chr(0x3C))
  65.   fout_fl.write(chr(0xFF)) #- eof
  66.   fout_fl.write(chr(0x00)) #- data lenght
  67.   fout_fl.write(chr(0xFF)) #- checksum
  68.   fout_fl.write(chr(0x55)) #- "magic byte"
  69.   finp_fl.close();fout_fl.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement