Advertisement
Guest User

bin2cas.py

a guest
Dec 21st, 2010
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 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 - 201012211921 - 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. else:
  16.   finp_fl=open(finp_st,"r");fout_fl=open(fout_st,"w")
  17.   lfl=os.path.getsize(finp_st) # lfl=lof(1)
  18.   for i in range(0,128,1):   #- 0x55 byte synchronism, 128 times...
  19.     fout_fl.write(chr(0x55))
  20.   fout_fl.write(chr(0x55))    #- two "magic bytes"
  21.   fout_fl.write(chr(0x3C))
  22.   fout_fl.write(chr(0x00))    #- block type: filename
  23.   fout_fl.write(chr(0x0F))    #- data lenght
  24.   infnam_st="ABCDEFGH"
  25.   infnam_st=infnam_st+"        "
  26.   infnam_st=infnam_st[:8]     #- filename - 8 characters
  27.   for i in infnam_st:
  28.     fout_fl.write(i)
  29.     #fout_fl.write(infnam_st[i])
  30.   fout_fl.write(chr(0x02)) #- machine code
  31.   fout_fl.write(chr(0x00)) #- ascii flag: binary
  32.   fout_fl.write(chr(0x00)) #- gap flag
  33.   fout_fl.write(chr(0x0C)) #- machine code starting address
  34.   fout_fl.write(chr(0x00))
  35.   fout_fl.write(chr(0x0C)) #- machine code loading address
  36.   fout_fl.write(chr(0x00))
  37.   fout_fl.write(chr(0x4D)) #- checksum (bitwiseand 255)
  38.   fout_fl.write(chr(0x55)) #- "magic byte"
  39.   for i in range(0,128,1): #- 0x55 byte synchronism, 128 times...
  40.     fout_fl.write(chr(0x55))
  41.   while True:
  42.     if lfl<0:break
  43.     fout_fl.write(chr(0x55)) #- two "magic bytes"
  44.     fout_fl.write(chr(0x3C))
  45.     fout_fl.write(chr(0x01)) #- block type: data
  46.     rlf=0xFF
  47.     if lfl<rlf:rlf=lfl
  48.     #print rlf
  49.     fout_fl.write(chr(rlf))  #- data lenght
  50.     lfl=lfl-255
  51.     cksm=0
  52.     for i in range(0,255,1):
  53.       byte_st=finp_fl.read(1)
  54.       if len(byte_st)==0:break
  55.       u=ord(byte_st)
  56.       cksm=(cksm+u)%256 #- cksm=(cksm+u) mod 256
  57.       fout_fl.write(chr(u)) #- writing byte
  58.     fout_fl.write(chr(cksm)) #- writing checksum
  59.   fout_fl.write(chr(0x55)) #- "magic byte"
  60.   fout_fl.write(chr(0x55)) #- two "magic bytes"
  61.   fout_fl.write(chr(0x3C))
  62.   fout_fl.write(chr(0xFF)) #- eof
  63.   fout_fl.write(chr(0x00)) #- data lenght
  64.   fout_fl.write(chr(0xFF)) #- checksum
  65.   fout_fl.write(chr(0x55)) #- "magic byte"
  66.   finp_fl.close();fout_fl.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement