Título: Python script para archivos bin Publicado por: samsaga2 en 15 de Febrero de 2011, 10:36:44 am Este script en python lo utilizo para volcar archivos binarios (tipo bsave) de pantallas de screen 5 a c o ensamblador. También puedes extraer fácilmente la paleta de pantallazos en screen 5 o tiles del screen 2.
Espero que le sea útil a alguien más. Ejemplo: Para extraer la paleta de colores de una pantalla screen 5: Código: python bsave.py -i pantallazo.sc5 -o pantallazo.asm -s 0x7680 -z 32 -v -f asm Obtenemos: Código: db 11h,01h,00h,00h,14h,02h,17h,04h,12h,02h db 52h,01h,44h,04h,21h,02h,21h,04h,41h,05h db 41h,03h,64h,03h,73h,06h,56h,06h,61h,04h db 77h,07h Código: import sys import getopt def read(input, start, size, verbose): bytes = input.read() if bytes[0] != 0xfe: print("Unknown input file format") sys.exit(1) header_start = bytes[2] * 256 + bytes[1] header_stop = bytes[4] * 256 + bytes[3] header_run = bytes[6] * 256 + bytes[5] if verbose: print("""Header: Start: %s Stop: %s Run: %s""" % (hex(header_start), hex(header_stop), hex(header_run))) if start != None: offset = start - header_start + 7 if offset < 0: print("Start addres out of bounds") sys.exit(1) else: offset = 0 if verbose: print("File offset %x" % (offset)) if size == None: size = len(bytes) output_bytes = [] for i in range(0, size): output_bytes.append(bytes[offset + i]) return output_bytes def dump_bin(output, bytes): with open(output, "w") as o: for x in bytes: o.write(chr(x)) def dump_asm(output, bytes): with open(output, "w") as o: bytes = ["%02Xh" % i for i in bytes] for x in range(0, len(bytes), 10): o.write("\tdb ") o.write(",".join(bytes[x:][:10])) o.write("\n") def dump_c(output, bytes): with open(output, "w") as o: bytes = ["0x%02X" % i for i in bytes] o.write("const unsigned char bytes[] = {\n") for x in range(0, len(bytes), 10): o.write("\t" + ",".join(bytes[x:][:10])) if x+10 <= len(bytes): o.write(",") o.write("\n") o.write("\n}") def dump_none(output, bytes): with open(output, "w") as o: bytes = ["%02X" % i for i in bytes] for x in range(0, len(bytes), 10): o.write(" ".join(bytes[x:][:10])) o.write("\n") def usage(): print("""bsave.py [options]) Options: -h --help Show help -s offset --start offset Start address of dump -z bytes --size bytes Number of bytes to dump -i filename --input filename Input filename -o filename --output filename Output filename -v --verbose Verbose -f type --format type Output type format Output type formats: bin Binary output c C format asm ASM format none Text output """) def main(argv): input = None output = None start_offset = None size = None verbose = False format = "none" try: opts, args = getopt.getopt(argv, "hs:z:i:o:vf:", ["help", "start", "size", "input", "output", "verbose", "format"]) except getopt.GetoptError: usage() sys.exit(2) for opt, arg in opts: if opt in ("-h", "--help"): usage() sys.exit() elif opt in ("-s", "--start"): if arg[:2] == '0x': start_offset = int(arg[2:], 16) else: start_offset = int(arg) elif opt in ("-z", "--size"): if arg[:2] == '0x': size = int(arg[2:], 16) else: size = int(arg) elif opt in ("-i", "--input"): input = arg elif opt in ("-o", "--output"): output = arg elif opt in ("-v", "--verbose"): verbose = True elif opt in ("-f", "--format"): format = arg if input == None: print("Missing input file") usage() sys.exit(3) if output == None: print("Missing output file") usage() sys.exit(4) # get bytes with open(input, "rb") as i: bytes = read(i, start_offset, size, verbose) # write bytes if format == "bin": dump_bin(output, bytes) else: if format == "none": dump_none(output, bytes) elif format == "c": dump_c(output, bytes) elif format == "asm": dump_asm(output, bytes) if __name__ == '__main__': main(sys.argv[1:]) |